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