2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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)
34 #include <sys/types.h>
48 #include "test_helpers.h"
50 /* ---------------------------------------------------------------------
51 * Auxiliary functions.
52 * --------------------------------------------------------------------- */
54 enum type { inv, pre, post, unreachable };
58 grep(const atf_dynstr_t *line, const char *text)
60 const char *l = atf_dynstr_cstring(line);
65 if (strstr(l, text) != NULL)
76 static void do_test_child(void *) ATF_DEFS_ATTRIBUTE_NORETURN;
80 do_test_child(void *v)
82 struct test_data *td = v;
108 do_test(enum type t, bool cond)
110 atf_process_child_t child;
111 atf_process_status_t status;
114 atf_dynstr_t lines[3];
117 atf_process_stream_t outsb, errsb;
118 struct test_data td = { t, cond };
120 RE(atf_process_stream_init_inherit(&outsb));
121 RE(atf_process_stream_init_capture(&errsb));
122 RE(atf_process_fork(&child, do_test_child, &outsb, &errsb, &td));
123 atf_process_stream_fini(&errsb);
124 atf_process_stream_fini(&outsb);
130 RE(atf_dynstr_init(&lines[nlines]));
132 eof = read_line(atf_process_child_stderr(&child), &lines[nlines]);
134 } while (nlines < 3);
135 ATF_REQUIRE(nlines == 0 || nlines == 3);
137 RE(atf_process_child_wait(&child, &status));
139 ATF_REQUIRE(atf_process_status_signaled(&status));
140 ATF_REQUIRE(atf_process_status_termsig(&status) == SIGABRT);
142 ATF_REQUIRE(atf_process_status_exited(&status));
143 ATF_REQUIRE(atf_process_status_exitstatus(&status) == EXIT_SUCCESS);
145 atf_process_status_fini(&status);
150 ATF_REQUIRE(grep(&lines[0], "Invariant"));
154 ATF_REQUIRE(grep(&lines[0], "Precondition"));
158 ATF_REQUIRE(grep(&lines[0], "Postcondition"));
162 ATF_REQUIRE(grep(&lines[0], "Invariant"));
166 ATF_REQUIRE(grep(&lines[0], __FILE__));
167 ATF_REQUIRE(grep(&lines[2], PACKAGE_BUGREPORT));
172 atf_dynstr_fini(&lines[nlines]);
181 atf_tc_skip("Sanity checks not available; code built with -DNDEBUG");
185 /* ---------------------------------------------------------------------
186 * Test cases for the free functions.
187 * --------------------------------------------------------------------- */
192 atf_tc_set_md_var(tc, "descr", "Tests the INV macro");
205 atf_tc_set_md_var(tc, "descr", "Tests the PRE macro");
216 ATF_TC_HEAD(post, tc)
218 atf_tc_set_md_var(tc, "descr", "Tests the POST macro");
220 ATF_TC_BODY(post, tc)
224 do_test(post, false);
229 ATF_TC_HEAD(unreachable, tc)
231 atf_tc_set_md_var(tc, "descr", "Tests the UNREACHABLE macro");
233 ATF_TC_BODY(unreachable, tc)
237 do_test(unreachable, false);
238 do_test(unreachable, true);
241 /* ---------------------------------------------------------------------
243 * --------------------------------------------------------------------- */
247 ATF_TP_ADD_TC(tp, inv);
248 ATF_TP_ADD_TC(tp, pre);
249 ATF_TP_ADD_TC(tp, post);
250 ATF_TP_ADD_TC(tp, unreachable);
252 return atf_no_error();