1 /*	$NetBSD: sanity_test.c,v 1.3 2014/12/10 04:38:03 christos Exp $	*/
2 
3 /*
4  * Automated Testing Framework (atf)
5  *
6  * Copyright (c) 2008 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
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.
17  *
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.
30  */
31 
32 #if defined(HAVE_CONFIG_H)
33 #include "bconfig.h"
34 #endif
35 
36 #include <sys/types.h>
37 #include <sys/wait.h>
38 
39 #include <signal.h>
40 #include <stdbool.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #include <atf-c.h>
46 
47 #include "dynstr.h"
48 #include "process.h"
49 #include "sanity.h"
50 #include "test_helpers.h"
51 
52 /* ---------------------------------------------------------------------
53  * Auxiliary functions.
54  * --------------------------------------------------------------------- */
55 
56 enum type { inv, pre, post, unreachable };
57 
58 struct test_data {
59     enum type m_type;
60     bool m_cond;
61 };
62 
63 static void do_test_child(void *) ATF_DEFS_ATTRIBUTE_NORETURN;
64 
65 static
66 void
do_test_child(void * v)67 do_test_child(void *v)
68 {
69     struct test_data *td = v;
70 
71     switch (td->m_type) {
72     case inv:
73         INV(td->m_cond);
74         break;
75 
76     case pre:
77         PRE(td->m_cond);
78         break;
79 
80     case post:
81         POST(td->m_cond);
82         break;
83 
84     case unreachable:
85         if (!td->m_cond)
86             UNREACHABLE;
87         break;
88     }
89 
90     exit(EXIT_SUCCESS);
91 }
92 
93 static
94 void
do_test(enum type t,bool cond)95 do_test(enum type t, bool cond)
96 {
97     atf_process_child_t child;
98     atf_process_status_t status;
99     int nlines;
100     char *lines[3];
101 
102     {
103         atf_process_stream_t outsb, errsb;
104         struct test_data td = { t, cond };
105 
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);
111     }
112 
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);
118 
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);
126     }
127     atf_process_status_fini(&status);
128 
129     if (!cond) {
130         switch (t) {
131         case inv:
132             ATF_REQUIRE(atf_utils_grep_string("Invariant", lines[0]));
133             break;
134 
135         case pre:
136             ATF_REQUIRE(atf_utils_grep_string("Precondition", lines[0]));
137             break;
138 
139         case post:
140             ATF_REQUIRE(atf_utils_grep_string("Postcondition", lines[0]));
141             break;
142 
143         case unreachable:
144             ATF_REQUIRE(atf_utils_grep_string("Invariant", lines[0]));
145             break;
146         }
147 
148         ATF_REQUIRE(atf_utils_grep_string(__FILE__, lines[0]));
149         ATF_REQUIRE(atf_utils_grep_string(PACKAGE_BUGREPORT, lines[2]));
150     }
151 
152     while (nlines > 0) {
153         nlines--;
154         free(lines[nlines]);
155     }
156 }
157 
158 static
159 void
require_ndebug(void)160 require_ndebug(void)
161 {
162 #if defined(NDEBUG)
163     atf_tc_skip("Sanity checks not available; code built with -DNDEBUG");
164 #endif
165 }
166 
167 /* ---------------------------------------------------------------------
168  * Test cases for the free functions.
169  * --------------------------------------------------------------------- */
170 
171 ATF_TC(inv);
ATF_TC_HEAD(inv,tc)172 ATF_TC_HEAD(inv, tc)
173 {
174     atf_tc_set_md_var(tc, "descr", "Tests the INV macro");
175 }
ATF_TC_BODY(inv,tc)176 ATF_TC_BODY(inv, tc)
177 {
178     require_ndebug();
179 
180     do_test(inv, false);
181     do_test(inv, true);
182 }
183 
184 ATF_TC(pre);
ATF_TC_HEAD(pre,tc)185 ATF_TC_HEAD(pre, tc)
186 {
187     atf_tc_set_md_var(tc, "descr", "Tests the PRE macro");
188 }
ATF_TC_BODY(pre,tc)189 ATF_TC_BODY(pre, tc)
190 {
191     require_ndebug();
192 
193     do_test(pre, false);
194     do_test(pre, true);
195 }
196 
197 ATF_TC(post);
ATF_TC_HEAD(post,tc)198 ATF_TC_HEAD(post, tc)
199 {
200     atf_tc_set_md_var(tc, "descr", "Tests the POST macro");
201 }
ATF_TC_BODY(post,tc)202 ATF_TC_BODY(post, tc)
203 {
204     require_ndebug();
205 
206     do_test(post, false);
207     do_test(post, true);
208 }
209 
210 ATF_TC(unreachable);
ATF_TC_HEAD(unreachable,tc)211 ATF_TC_HEAD(unreachable, tc)
212 {
213     atf_tc_set_md_var(tc, "descr", "Tests the UNREACHABLE macro");
214 }
ATF_TC_BODY(unreachable,tc)215 ATF_TC_BODY(unreachable, tc)
216 {
217     require_ndebug();
218 
219     do_test(unreachable, false);
220     do_test(unreachable, true);
221 }
222 
223 /* ---------------------------------------------------------------------
224  * Main.
225  * --------------------------------------------------------------------- */
226 
ATF_TP_ADD_TCS(tp)227 ATF_TP_ADD_TCS(tp)
228 {
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);
233 
234     return atf_no_error();
235 }
236