t_assert.c revision 1.2.2.2 1 1.2.2.2 cherry /* $NetBSD: t_assert.c,v 1.2.2.2 2011/06/23 14:20:39 cherry Exp $ */
2 1.2.2.2 cherry
3 1.2.2.2 cherry /*-
4 1.2.2.2 cherry * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.2.2.2 cherry * All rights reserved.
6 1.2.2.2 cherry *
7 1.2.2.2 cherry * This code is derived from software contributed to The NetBSD Foundation
8 1.2.2.2 cherry * by Jukka Ruohonen.
9 1.2.2.2 cherry *
10 1.2.2.2 cherry * Redistribution and use in source and binary forms, with or without
11 1.2.2.2 cherry * modification, are permitted provided that the following conditions
12 1.2.2.2 cherry * are met:
13 1.2.2.2 cherry * 1. Redistributions of source code must retain the above copyright
14 1.2.2.2 cherry * notice, this list of conditions and the following disclaimer.
15 1.2.2.2 cherry * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.2.2 cherry * notice, this list of conditions and the following disclaimer in the
17 1.2.2.2 cherry * documentation and/or other materials provided with the distribution.
18 1.2.2.2 cherry *
19 1.2.2.2 cherry * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.2.2 cherry * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.2.2 cherry * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.2.2 cherry * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.2.2 cherry * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.2.2 cherry * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.2.2 cherry * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.2.2 cherry * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.2.2 cherry * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.2.2 cherry * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.2.2 cherry * POSSIBILITY OF SUCH DAMAGE.
30 1.2.2.2 cherry */
31 1.2.2.2 cherry #include <sys/cdefs.h>
32 1.2.2.2 cherry __RCSID("$NetBSD: t_assert.c,v 1.2.2.2 2011/06/23 14:20:39 cherry Exp $");
33 1.2.2.2 cherry
34 1.2.2.2 cherry #include <sys/wait.h>
35 1.2.2.2 cherry
36 1.2.2.2 cherry #include <assert.h>
37 1.2.2.2 cherry #include <atf-c.h>
38 1.2.2.2 cherry #include <signal.h>
39 1.2.2.2 cherry #include <stdlib.h>
40 1.2.2.2 cherry #include <string.h>
41 1.2.2.2 cherry #include <unistd.h>
42 1.2.2.2 cherry
43 1.2.2.2 cherry static void handler(int);
44 1.2.2.2 cherry
45 1.2.2.2 cherry static void
46 1.2.2.2 cherry handler(int signo)
47 1.2.2.2 cherry {
48 1.2.2.2 cherry /* Nothing. */
49 1.2.2.2 cherry }
50 1.2.2.2 cherry
51 1.2.2.2 cherry ATF_TC(assert_false);
52 1.2.2.2 cherry ATF_TC_HEAD(assert_false, tc)
53 1.2.2.2 cherry {
54 1.2.2.2 cherry atf_tc_set_md_var(tc, "descr", "Test that assert(3) works, #1");
55 1.2.2.2 cherry }
56 1.2.2.2 cherry
57 1.2.2.2 cherry ATF_TC_BODY(assert_false, tc)
58 1.2.2.2 cherry {
59 1.2.2.2 cherry struct sigaction sa;
60 1.2.2.2 cherry pid_t pid;
61 1.2.2.2 cherry int sta;
62 1.2.2.2 cherry
63 1.2.2.2 cherry pid = fork();
64 1.2.2.2 cherry ATF_REQUIRE(pid >= 0);
65 1.2.2.2 cherry
66 1.2.2.2 cherry if (pid == 0) {
67 1.2.2.2 cherry
68 1.2.2.2 cherry (void)closefrom(0);
69 1.2.2.2 cherry (void)memset(&sa, 0, sizeof(struct sigaction));
70 1.2.2.2 cherry
71 1.2.2.2 cherry sa.sa_flags = 0;
72 1.2.2.2 cherry sa.sa_handler = handler;
73 1.2.2.2 cherry
74 1.2.2.2 cherry (void)sigemptyset(&sa.sa_mask);
75 1.2.2.2 cherry (void)sigaction(SIGABRT, &sa, 0);
76 1.2.2.2 cherry
77 1.2.2.2 cherry assert(1 == 1);
78 1.2.2.2 cherry
79 1.2.2.2 cherry _exit(EXIT_SUCCESS);
80 1.2.2.2 cherry }
81 1.2.2.2 cherry
82 1.2.2.2 cherry (void)wait(&sta);
83 1.2.2.2 cherry
84 1.2.2.2 cherry if (WIFSIGNALED(sta) != 0 || WIFEXITED(sta) == 0)
85 1.2.2.2 cherry atf_tc_fail("assert(3) fired haphazardly");
86 1.2.2.2 cherry }
87 1.2.2.2 cherry
88 1.2.2.2 cherry ATF_TC(assert_true);
89 1.2.2.2 cherry ATF_TC_HEAD(assert_true, tc)
90 1.2.2.2 cherry {
91 1.2.2.2 cherry atf_tc_set_md_var(tc, "descr", "Test that assert(3) works, #2");
92 1.2.2.2 cherry }
93 1.2.2.2 cherry
94 1.2.2.2 cherry ATF_TC_BODY(assert_true, tc)
95 1.2.2.2 cherry {
96 1.2.2.2 cherry struct sigaction sa;
97 1.2.2.2 cherry pid_t pid;
98 1.2.2.2 cherry int sta;
99 1.2.2.2 cherry
100 1.2.2.2 cherry pid = fork();
101 1.2.2.2 cherry ATF_REQUIRE(pid >= 0);
102 1.2.2.2 cherry
103 1.2.2.2 cherry if (pid == 0) {
104 1.2.2.2 cherry
105 1.2.2.2 cherry (void)closefrom(0);
106 1.2.2.2 cherry (void)memset(&sa, 0, sizeof(struct sigaction));
107 1.2.2.2 cherry
108 1.2.2.2 cherry sa.sa_flags = 0;
109 1.2.2.2 cherry sa.sa_handler = handler;
110 1.2.2.2 cherry
111 1.2.2.2 cherry (void)sigemptyset(&sa.sa_mask);
112 1.2.2.2 cherry (void)sigaction(SIGABRT, &sa, 0);
113 1.2.2.2 cherry
114 1.2.2.2 cherry assert(1 == 2);
115 1.2.2.2 cherry
116 1.2.2.2 cherry _exit(EXIT_SUCCESS);
117 1.2.2.2 cherry }
118 1.2.2.2 cherry
119 1.2.2.2 cherry (void)wait(&sta);
120 1.2.2.2 cherry
121 1.2.2.2 cherry if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != SIGABRT)
122 1.2.2.2 cherry atf_tc_fail("assert(3) did not fire");
123 1.2.2.2 cherry }
124 1.2.2.2 cherry
125 1.2.2.2 cherry ATF_TP_ADD_TCS(tp)
126 1.2.2.2 cherry {
127 1.2.2.2 cherry
128 1.2.2.2 cherry ATF_TP_ADD_TC(tp, assert_false);
129 1.2.2.2 cherry ATF_TP_ADD_TC(tp, assert_true);
130 1.2.2.2 cherry
131 1.2.2.2 cherry return atf_no_error();
132 1.2.2.2 cherry }
133