Remove building with NOCRYPTO option
[minix3.git] / external / bsd / atf / dist / atf-c / detail / sanity_test.c
blob7c8285b5193c1d73826740875cbb092da3bde880
1 /*
2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #if defined(HAVE_CONFIG_H)
31 #include "bconfig.h"
32 #endif
34 #include <sys/types.h>
35 #include <sys/wait.h>
37 #include <signal.h>
38 #include <stdbool.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
43 #include <atf-c.h>
45 #include "dynstr.h"
46 #include "process.h"
47 #include "sanity.h"
48 #include "test_helpers.h"
50 /* ---------------------------------------------------------------------
51 * Auxiliary functions.
52 * --------------------------------------------------------------------- */
54 enum type { inv, pre, post, unreachable };
56 struct test_data {
57 enum type m_type;
58 bool m_cond;
61 static void do_test_child(void *) ATF_DEFS_ATTRIBUTE_NORETURN;
63 static
64 void
65 do_test_child(void *v)
67 struct test_data *td = v;
69 switch (td->m_type) {
70 case inv:
71 INV(td->m_cond);
72 break;
74 case pre:
75 PRE(td->m_cond);
76 break;
78 case post:
79 POST(td->m_cond);
80 break;
82 case unreachable:
83 if (!td->m_cond)
84 UNREACHABLE;
85 break;
88 exit(EXIT_SUCCESS);
91 static
92 void
93 do_test(enum type t, bool cond)
95 atf_process_child_t child;
96 atf_process_status_t status;
97 int nlines;
98 char *lines[3];
101 atf_process_stream_t outsb, errsb;
102 struct test_data td = { t, cond };
104 RE(atf_process_stream_init_inherit(&outsb));
105 RE(atf_process_stream_init_capture(&errsb));
106 RE(atf_process_fork(&child, do_test_child, &outsb, &errsb, &td));
107 atf_process_stream_fini(&errsb);
108 atf_process_stream_fini(&outsb);
111 nlines = 0;
112 while (nlines < 3 && (lines[nlines] =
113 atf_utils_readline(atf_process_child_stderr(&child))) != NULL)
114 nlines++;
115 ATF_REQUIRE(nlines == 0 || nlines == 3);
117 RE(atf_process_child_wait(&child, &status));
118 if (!cond) {
119 ATF_REQUIRE(atf_process_status_signaled(&status));
120 ATF_REQUIRE(atf_process_status_termsig(&status) == SIGABRT);
121 } else {
122 ATF_REQUIRE(atf_process_status_exited(&status));
123 ATF_REQUIRE(atf_process_status_exitstatus(&status) == EXIT_SUCCESS);
125 atf_process_status_fini(&status);
127 if (!cond) {
128 switch (t) {
129 case inv:
130 ATF_REQUIRE(atf_utils_grep_string("Invariant", lines[0]));
131 break;
133 case pre:
134 ATF_REQUIRE(atf_utils_grep_string("Precondition", lines[0]));
135 break;
137 case post:
138 ATF_REQUIRE(atf_utils_grep_string("Postcondition", lines[0]));
139 break;
141 case unreachable:
142 ATF_REQUIRE(atf_utils_grep_string("Invariant", lines[0]));
143 break;
146 ATF_REQUIRE(atf_utils_grep_string(__FILE__, lines[0]));
147 ATF_REQUIRE(atf_utils_grep_string(PACKAGE_BUGREPORT, lines[2]));
150 while (nlines > 0) {
151 nlines--;
152 free(lines[nlines]);
156 static
157 void
158 require_ndebug(void)
160 #if defined(NDEBUG)
161 atf_tc_skip("Sanity checks not available; code built with -DNDEBUG");
162 #endif
165 /* ---------------------------------------------------------------------
166 * Test cases for the free functions.
167 * --------------------------------------------------------------------- */
169 ATF_TC(inv);
170 ATF_TC_HEAD(inv, tc)
172 atf_tc_set_md_var(tc, "descr", "Tests the INV macro");
174 ATF_TC_BODY(inv, tc)
176 require_ndebug();
178 do_test(inv, false);
179 do_test(inv, true);
182 ATF_TC(pre);
183 ATF_TC_HEAD(pre, tc)
185 atf_tc_set_md_var(tc, "descr", "Tests the PRE macro");
187 ATF_TC_BODY(pre, tc)
189 require_ndebug();
191 do_test(pre, false);
192 do_test(pre, true);
195 ATF_TC(post);
196 ATF_TC_HEAD(post, tc)
198 atf_tc_set_md_var(tc, "descr", "Tests the POST macro");
200 ATF_TC_BODY(post, tc)
202 require_ndebug();
204 do_test(post, false);
205 do_test(post, true);
208 ATF_TC(unreachable);
209 ATF_TC_HEAD(unreachable, tc)
211 atf_tc_set_md_var(tc, "descr", "Tests the UNREACHABLE macro");
213 ATF_TC_BODY(unreachable, tc)
215 require_ndebug();
217 do_test(unreachable, false);
218 do_test(unreachable, true);
221 /* ---------------------------------------------------------------------
222 * Main.
223 * --------------------------------------------------------------------- */
225 ATF_TP_ADD_TCS(tp)
227 ATF_TP_ADD_TC(tp, inv);
228 ATF_TP_ADD_TC(tp, pre);
229 ATF_TP_ADD_TC(tp, post);
230 ATF_TP_ADD_TC(tp, unreachable);
232 return atf_no_error();