Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / unit / atf-src / atf-c / detail / sanity_test.c
blob770113061db1b347c881a7832917b9e7041dd342
1 /* $NetBSD: sanity_test.c,v 1.3 2014/12/10 04:38:03 christos Exp $ */
3 /*
4 * Automated Testing Framework (atf)
6 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
19 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
29 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #if defined(HAVE_CONFIG_H)
33 #include "bconfig.h"
34 #endif
36 #include <sys/types.h>
37 #include <sys/wait.h>
39 #include <signal.h>
40 #include <stdbool.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
45 #include <atf-c.h>
47 #include "dynstr.h"
48 #include "process.h"
49 #include "sanity.h"
50 #include "test_helpers.h"
52 /* ---------------------------------------------------------------------
53 * Auxiliary functions.
54 * --------------------------------------------------------------------- */
56 enum type { inv, pre, post, unreachable };
58 struct test_data {
59 enum type m_type;
60 bool m_cond;
63 static void do_test_child(void *) ATF_DEFS_ATTRIBUTE_NORETURN;
65 static
66 void
67 do_test_child(void *v)
69 struct test_data *td = v;
71 switch (td->m_type) {
72 case inv:
73 INV(td->m_cond);
74 break;
76 case pre:
77 PRE(td->m_cond);
78 break;
80 case post:
81 POST(td->m_cond);
82 break;
84 case unreachable:
85 if (!td->m_cond)
86 UNREACHABLE;
87 break;
90 exit(EXIT_SUCCESS);
93 static
94 void
95 do_test(enum type t, bool cond)
97 atf_process_child_t child;
98 atf_process_status_t status;
99 int nlines;
100 char *lines[3];
103 atf_process_stream_t outsb, errsb;
104 struct test_data td = { t, cond };
106 RE(atf_process_stream_init_inherit(&outsb));
107 RE(atf_process_stream_init_capture(&errsb));
108 RE(atf_process_fork(&child, do_test_child, &outsb, &errsb, &td));
109 atf_process_stream_fini(&errsb);
110 atf_process_stream_fini(&outsb);
113 nlines = 0;
114 while (nlines < 3 && (lines[nlines] =
115 atf_utils_readline(atf_process_child_stderr(&child))) != NULL)
116 nlines++;
117 ATF_REQUIRE(nlines == 0 || nlines == 3);
119 RE(atf_process_child_wait(&child, &status));
120 if (!cond) {
121 ATF_REQUIRE(atf_process_status_signaled(&status));
122 ATF_REQUIRE(atf_process_status_termsig(&status) == SIGABRT);
123 } else {
124 ATF_REQUIRE(atf_process_status_exited(&status));
125 ATF_REQUIRE(atf_process_status_exitstatus(&status) == EXIT_SUCCESS);
127 atf_process_status_fini(&status);
129 if (!cond) {
130 switch (t) {
131 case inv:
132 ATF_REQUIRE(atf_utils_grep_string("Invariant", lines[0]));
133 break;
135 case pre:
136 ATF_REQUIRE(atf_utils_grep_string("Precondition", lines[0]));
137 break;
139 case post:
140 ATF_REQUIRE(atf_utils_grep_string("Postcondition", lines[0]));
141 break;
143 case unreachable:
144 ATF_REQUIRE(atf_utils_grep_string("Invariant", lines[0]));
145 break;
148 ATF_REQUIRE(atf_utils_grep_string(__FILE__, lines[0]));
149 ATF_REQUIRE(atf_utils_grep_string(PACKAGE_BUGREPORT, lines[2]));
152 while (nlines > 0) {
153 nlines--;
154 free(lines[nlines]);
158 static
159 void
160 require_ndebug(void)
162 #if defined(NDEBUG)
163 atf_tc_skip("Sanity checks not available; code built with -DNDEBUG");
164 #endif
167 /* ---------------------------------------------------------------------
168 * Test cases for the free functions.
169 * --------------------------------------------------------------------- */
171 ATF_TC(inv);
172 ATF_TC_HEAD(inv, tc)
174 atf_tc_set_md_var(tc, "descr", "Tests the INV macro");
176 ATF_TC_BODY(inv, tc)
178 require_ndebug();
180 do_test(inv, false);
181 do_test(inv, true);
184 ATF_TC(pre);
185 ATF_TC_HEAD(pre, tc)
187 atf_tc_set_md_var(tc, "descr", "Tests the PRE macro");
189 ATF_TC_BODY(pre, tc)
191 require_ndebug();
193 do_test(pre, false);
194 do_test(pre, true);
197 ATF_TC(post);
198 ATF_TC_HEAD(post, tc)
200 atf_tc_set_md_var(tc, "descr", "Tests the POST macro");
202 ATF_TC_BODY(post, tc)
204 require_ndebug();
206 do_test(post, false);
207 do_test(post, true);
210 ATF_TC(unreachable);
211 ATF_TC_HEAD(unreachable, tc)
213 atf_tc_set_md_var(tc, "descr", "Tests the UNREACHABLE macro");
215 ATF_TC_BODY(unreachable, tc)
217 require_ndebug();
219 do_test(unreachable, false);
220 do_test(unreachable, true);
223 /* ---------------------------------------------------------------------
224 * Main.
225 * --------------------------------------------------------------------- */
227 ATF_TP_ADD_TCS(tp)
229 ATF_TP_ADD_TC(tp, inv);
230 ATF_TP_ADD_TC(tp, pre);
231 ATF_TP_ADD_TC(tp, post);
232 ATF_TP_ADD_TC(tp, unreachable);
234 return atf_no_error();