xref: /minix3/external/bsd/atf/dist/test-programs/c_helpers.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*
2*11be35a1SLionel Sambuc  * Automated Testing Framework (atf)
3*11be35a1SLionel Sambuc  *
4*11be35a1SLionel Sambuc  * Copyright (c) 2007 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 #include <sys/types.h>
31*11be35a1SLionel Sambuc #include <sys/wait.h>
32*11be35a1SLionel Sambuc #include <fcntl.h>
33*11be35a1SLionel Sambuc #include <unistd.h>
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc #include <signal.h>
36*11be35a1SLionel Sambuc #include <stdio.h>
37*11be35a1SLionel Sambuc #include <stdlib.h>
38*11be35a1SLionel Sambuc #include <string.h>
39*11be35a1SLionel Sambuc 
40*11be35a1SLionel Sambuc #include <atf-c.h>
41*11be35a1SLionel Sambuc 
42*11be35a1SLionel Sambuc #include "atf-c/error.h"
43*11be35a1SLionel Sambuc 
44*11be35a1SLionel Sambuc #include "atf-c/detail/env.h"
45*11be35a1SLionel Sambuc #include "atf-c/detail/fs.h"
46*11be35a1SLionel Sambuc #include "atf-c/detail/test_helpers.h"
47*11be35a1SLionel Sambuc #include "atf-c/detail/text.h"
48*11be35a1SLionel Sambuc 
49*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
50*11be35a1SLionel Sambuc  * Auxiliary functions.
51*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
52*11be35a1SLionel Sambuc 
53*11be35a1SLionel Sambuc static
54*11be35a1SLionel Sambuc void
safe_remove(const char * path)55*11be35a1SLionel Sambuc safe_remove(const char* path)
56*11be35a1SLionel Sambuc {
57*11be35a1SLionel Sambuc     if (unlink(path) == -1)
58*11be35a1SLionel Sambuc         atf_tc_fail("unlink(2) of %s failed", path);
59*11be35a1SLionel Sambuc }
60*11be35a1SLionel Sambuc 
61*11be35a1SLionel Sambuc static
62*11be35a1SLionel Sambuc void
touch(const char * path)63*11be35a1SLionel Sambuc touch(const char *path)
64*11be35a1SLionel Sambuc {
65*11be35a1SLionel Sambuc     int fd;
66*11be35a1SLionel Sambuc     fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644);
67*11be35a1SLionel Sambuc     if (fd == -1)
68*11be35a1SLionel Sambuc         atf_tc_fail("Could not create file %s", path);
69*11be35a1SLionel Sambuc     close(fd);
70*11be35a1SLionel Sambuc }
71*11be35a1SLionel Sambuc 
72*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
73*11be35a1SLionel Sambuc  * Helper tests for "t_cleanup".
74*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
75*11be35a1SLionel Sambuc 
76*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(cleanup_pass);
ATF_TC_HEAD(cleanup_pass,tc)77*11be35a1SLionel Sambuc ATF_TC_HEAD(cleanup_pass, tc)
78*11be35a1SLionel Sambuc {
79*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Helper test case for the t_cleanup test "
80*11be35a1SLionel Sambuc                "program");
81*11be35a1SLionel Sambuc }
ATF_TC_BODY(cleanup_pass,tc)82*11be35a1SLionel Sambuc ATF_TC_BODY(cleanup_pass, tc)
83*11be35a1SLionel Sambuc {
84*11be35a1SLionel Sambuc     touch(atf_tc_get_config_var(tc, "tmpfile"));
85*11be35a1SLionel Sambuc }
ATF_TC_CLEANUP(cleanup_pass,tc)86*11be35a1SLionel Sambuc ATF_TC_CLEANUP(cleanup_pass, tc)
87*11be35a1SLionel Sambuc {
88*11be35a1SLionel Sambuc     if (atf_tc_get_config_var_as_bool(tc, "cleanup"))
89*11be35a1SLionel Sambuc         safe_remove(atf_tc_get_config_var(tc, "tmpfile"));
90*11be35a1SLionel Sambuc }
91*11be35a1SLionel Sambuc 
92*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(cleanup_fail);
ATF_TC_HEAD(cleanup_fail,tc)93*11be35a1SLionel Sambuc ATF_TC_HEAD(cleanup_fail, tc)
94*11be35a1SLionel Sambuc {
95*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Helper test case for the t_cleanup test "
96*11be35a1SLionel Sambuc                       "program");
97*11be35a1SLionel Sambuc }
ATF_TC_BODY(cleanup_fail,tc)98*11be35a1SLionel Sambuc ATF_TC_BODY(cleanup_fail, tc)
99*11be35a1SLionel Sambuc {
100*11be35a1SLionel Sambuc     touch(atf_tc_get_config_var(tc, "tmpfile"));
101*11be35a1SLionel Sambuc     atf_tc_fail("On purpose");
102*11be35a1SLionel Sambuc }
ATF_TC_CLEANUP(cleanup_fail,tc)103*11be35a1SLionel Sambuc ATF_TC_CLEANUP(cleanup_fail, tc)
104*11be35a1SLionel Sambuc {
105*11be35a1SLionel Sambuc     if (atf_tc_get_config_var_as_bool(tc, "cleanup"))
106*11be35a1SLionel Sambuc         safe_remove(atf_tc_get_config_var(tc, "tmpfile"));
107*11be35a1SLionel Sambuc }
108*11be35a1SLionel Sambuc 
109*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(cleanup_skip);
ATF_TC_HEAD(cleanup_skip,tc)110*11be35a1SLionel Sambuc ATF_TC_HEAD(cleanup_skip, tc)
111*11be35a1SLionel Sambuc {
112*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Helper test case for the t_cleanup test "
113*11be35a1SLionel Sambuc                       "program");
114*11be35a1SLionel Sambuc }
ATF_TC_BODY(cleanup_skip,tc)115*11be35a1SLionel Sambuc ATF_TC_BODY(cleanup_skip, tc)
116*11be35a1SLionel Sambuc {
117*11be35a1SLionel Sambuc     touch(atf_tc_get_config_var(tc, "tmpfile"));
118*11be35a1SLionel Sambuc     atf_tc_skip("On purpose");
119*11be35a1SLionel Sambuc }
ATF_TC_CLEANUP(cleanup_skip,tc)120*11be35a1SLionel Sambuc ATF_TC_CLEANUP(cleanup_skip, tc)
121*11be35a1SLionel Sambuc {
122*11be35a1SLionel Sambuc     if (atf_tc_get_config_var_as_bool(tc, "cleanup"))
123*11be35a1SLionel Sambuc         safe_remove(atf_tc_get_config_var(tc, "tmpfile"));
124*11be35a1SLionel Sambuc }
125*11be35a1SLionel Sambuc 
126*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(cleanup_curdir);
ATF_TC_HEAD(cleanup_curdir,tc)127*11be35a1SLionel Sambuc ATF_TC_HEAD(cleanup_curdir, tc)
128*11be35a1SLionel Sambuc {
129*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Helper test case for the t_cleanup test "
130*11be35a1SLionel Sambuc                       "program");
131*11be35a1SLionel Sambuc }
ATF_TC_BODY(cleanup_curdir,tc)132*11be35a1SLionel Sambuc ATF_TC_BODY(cleanup_curdir, tc)
133*11be35a1SLionel Sambuc {
134*11be35a1SLionel Sambuc     FILE *f;
135*11be35a1SLionel Sambuc 
136*11be35a1SLionel Sambuc     f = fopen("oldvalue", "w");
137*11be35a1SLionel Sambuc     if (f == NULL)
138*11be35a1SLionel Sambuc         atf_tc_fail("Failed to create oldvalue file");
139*11be35a1SLionel Sambuc     fprintf(f, "1234");
140*11be35a1SLionel Sambuc     fclose(f);
141*11be35a1SLionel Sambuc }
ATF_TC_CLEANUP(cleanup_curdir,tc)142*11be35a1SLionel Sambuc ATF_TC_CLEANUP(cleanup_curdir, tc)
143*11be35a1SLionel Sambuc {
144*11be35a1SLionel Sambuc     FILE *f;
145*11be35a1SLionel Sambuc 
146*11be35a1SLionel Sambuc     f = fopen("oldvalue", "r");
147*11be35a1SLionel Sambuc     if (f != NULL) {
148*11be35a1SLionel Sambuc         int i;
149*11be35a1SLionel Sambuc         if (fscanf(f, "%d", &i) != 1)
150*11be35a1SLionel Sambuc             fprintf(stderr, "Failed to read old value\n");
151*11be35a1SLionel Sambuc         else
152*11be35a1SLionel Sambuc             printf("Old value: %d", i);
153*11be35a1SLionel Sambuc         fclose(f);
154*11be35a1SLionel Sambuc     }
155*11be35a1SLionel Sambuc }
156*11be35a1SLionel Sambuc 
157*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(cleanup_sigterm);
ATF_TC_HEAD(cleanup_sigterm,tc)158*11be35a1SLionel Sambuc ATF_TC_HEAD(cleanup_sigterm, tc)
159*11be35a1SLionel Sambuc {
160*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Helper test case for the t_cleanup test "
161*11be35a1SLionel Sambuc                       "program");
162*11be35a1SLionel Sambuc }
ATF_TC_BODY(cleanup_sigterm,tc)163*11be35a1SLionel Sambuc ATF_TC_BODY(cleanup_sigterm, tc)
164*11be35a1SLionel Sambuc {
165*11be35a1SLionel Sambuc     char *nofile;
166*11be35a1SLionel Sambuc 
167*11be35a1SLionel Sambuc     touch(atf_tc_get_config_var(tc, "tmpfile"));
168*11be35a1SLionel Sambuc     kill(getpid(), SIGTERM);
169*11be35a1SLionel Sambuc 
170*11be35a1SLionel Sambuc     RE(atf_text_format(&nofile, "%s.no",
171*11be35a1SLionel Sambuc                        atf_tc_get_config_var(tc, "tmpfile")));
172*11be35a1SLionel Sambuc     touch(nofile);
173*11be35a1SLionel Sambuc     free(nofile);
174*11be35a1SLionel Sambuc }
ATF_TC_CLEANUP(cleanup_sigterm,tc)175*11be35a1SLionel Sambuc ATF_TC_CLEANUP(cleanup_sigterm, tc)
176*11be35a1SLionel Sambuc {
177*11be35a1SLionel Sambuc     safe_remove(atf_tc_get_config_var(tc, "tmpfile"));
178*11be35a1SLionel Sambuc }
179*11be35a1SLionel Sambuc 
180*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
181*11be35a1SLionel Sambuc  * Helper tests for "t_config".
182*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
183*11be35a1SLionel Sambuc 
184*11be35a1SLionel Sambuc ATF_TC(config_unset);
ATF_TC_HEAD(config_unset,tc)185*11be35a1SLionel Sambuc ATF_TC_HEAD(config_unset, tc)
186*11be35a1SLionel Sambuc {
187*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Helper test case for the t_config test "
188*11be35a1SLionel Sambuc                       "program");
189*11be35a1SLionel Sambuc }
ATF_TC_BODY(config_unset,tc)190*11be35a1SLionel Sambuc ATF_TC_BODY(config_unset, tc)
191*11be35a1SLionel Sambuc {
192*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf_tc_has_config_var(tc, "test"));
193*11be35a1SLionel Sambuc }
194*11be35a1SLionel Sambuc 
195*11be35a1SLionel Sambuc ATF_TC(config_empty);
ATF_TC_HEAD(config_empty,tc)196*11be35a1SLionel Sambuc ATF_TC_HEAD(config_empty, tc)
197*11be35a1SLionel Sambuc {
198*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Helper test case for the t_config test "
199*11be35a1SLionel Sambuc                       "program");
200*11be35a1SLionel Sambuc }
ATF_TC_BODY(config_empty,tc)201*11be35a1SLionel Sambuc ATF_TC_BODY(config_empty, tc)
202*11be35a1SLionel Sambuc {
203*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_tc_has_config_var(tc, "test"));
204*11be35a1SLionel Sambuc     ATF_REQUIRE(strlen(atf_tc_get_config_var(tc, "test")) == 0);
205*11be35a1SLionel Sambuc }
206*11be35a1SLionel Sambuc 
207*11be35a1SLionel Sambuc ATF_TC(config_value);
ATF_TC_HEAD(config_value,tc)208*11be35a1SLionel Sambuc ATF_TC_HEAD(config_value, tc)
209*11be35a1SLionel Sambuc {
210*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Helper test case for the t_config test "
211*11be35a1SLionel Sambuc                       "program");
212*11be35a1SLionel Sambuc }
ATF_TC_BODY(config_value,tc)213*11be35a1SLionel Sambuc ATF_TC_BODY(config_value, tc)
214*11be35a1SLionel Sambuc {
215*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_tc_has_config_var(tc, "test"));
216*11be35a1SLionel Sambuc     ATF_REQUIRE(strcmp(atf_tc_get_config_var(tc, "test"), "foo") == 0);
217*11be35a1SLionel Sambuc }
218*11be35a1SLionel Sambuc 
219*11be35a1SLionel Sambuc ATF_TC(config_multi_value);
ATF_TC_HEAD(config_multi_value,tc)220*11be35a1SLionel Sambuc ATF_TC_HEAD(config_multi_value, tc)
221*11be35a1SLionel Sambuc {
222*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Helper test case for the t_config test "
223*11be35a1SLionel Sambuc                       "program");
224*11be35a1SLionel Sambuc }
ATF_TC_BODY(config_multi_value,tc)225*11be35a1SLionel Sambuc ATF_TC_BODY(config_multi_value, tc)
226*11be35a1SLionel Sambuc {
227*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_tc_has_config_var(tc, "test"));
228*11be35a1SLionel Sambuc     ATF_REQUIRE(strcmp(atf_tc_get_config_var(tc, "test"), "foo bar") == 0);
229*11be35a1SLionel Sambuc }
230*11be35a1SLionel Sambuc 
231*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
232*11be35a1SLionel Sambuc  * Helper tests for "t_expect".
233*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
234*11be35a1SLionel Sambuc 
235*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(expect_pass_and_pass);
ATF_TC_BODY(expect_pass_and_pass,tc)236*11be35a1SLionel Sambuc ATF_TC_BODY(expect_pass_and_pass, tc)
237*11be35a1SLionel Sambuc {
238*11be35a1SLionel Sambuc     atf_tc_expect_pass();
239*11be35a1SLionel Sambuc 
240*11be35a1SLionel Sambuc }
241*11be35a1SLionel Sambuc 
242*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(expect_pass_but_fail_requirement);
ATF_TC_BODY(expect_pass_but_fail_requirement,tc)243*11be35a1SLionel Sambuc ATF_TC_BODY(expect_pass_but_fail_requirement, tc)
244*11be35a1SLionel Sambuc {
245*11be35a1SLionel Sambuc     atf_tc_expect_pass();
246*11be35a1SLionel Sambuc     atf_tc_fail("Some reason");
247*11be35a1SLionel Sambuc }
248*11be35a1SLionel Sambuc 
249*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(expect_pass_but_fail_check);
ATF_TC_BODY(expect_pass_but_fail_check,tc)250*11be35a1SLionel Sambuc ATF_TC_BODY(expect_pass_but_fail_check, tc)
251*11be35a1SLionel Sambuc {
252*11be35a1SLionel Sambuc     atf_tc_expect_pass();
253*11be35a1SLionel Sambuc     atf_tc_fail_nonfatal("Some reason");
254*11be35a1SLionel Sambuc }
255*11be35a1SLionel Sambuc 
256*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(expect_fail_and_fail_requirement);
ATF_TC_BODY(expect_fail_and_fail_requirement,tc)257*11be35a1SLionel Sambuc ATF_TC_BODY(expect_fail_and_fail_requirement, tc)
258*11be35a1SLionel Sambuc {
259*11be35a1SLionel Sambuc     atf_tc_expect_fail("Fail %s", "reason");
260*11be35a1SLionel Sambuc     atf_tc_fail("The failure");
261*11be35a1SLionel Sambuc     atf_tc_expect_pass();
262*11be35a1SLionel Sambuc }
263*11be35a1SLionel Sambuc 
264*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(expect_fail_and_fail_check);
ATF_TC_BODY(expect_fail_and_fail_check,tc)265*11be35a1SLionel Sambuc ATF_TC_BODY(expect_fail_and_fail_check, tc)
266*11be35a1SLionel Sambuc {
267*11be35a1SLionel Sambuc     atf_tc_expect_fail("Fail first");
268*11be35a1SLionel Sambuc     atf_tc_fail_nonfatal("abc");
269*11be35a1SLionel Sambuc     atf_tc_expect_pass();
270*11be35a1SLionel Sambuc 
271*11be35a1SLionel Sambuc     atf_tc_expect_fail("And fail again");
272*11be35a1SLionel Sambuc     atf_tc_fail_nonfatal("def");
273*11be35a1SLionel Sambuc     atf_tc_expect_pass();
274*11be35a1SLionel Sambuc }
275*11be35a1SLionel Sambuc 
276*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(expect_fail_but_pass);
ATF_TC_BODY(expect_fail_but_pass,tc)277*11be35a1SLionel Sambuc ATF_TC_BODY(expect_fail_but_pass, tc)
278*11be35a1SLionel Sambuc {
279*11be35a1SLionel Sambuc     atf_tc_expect_fail("Fail first");
280*11be35a1SLionel Sambuc     atf_tc_fail_nonfatal("abc");
281*11be35a1SLionel Sambuc     atf_tc_expect_pass();
282*11be35a1SLionel Sambuc 
283*11be35a1SLionel Sambuc     atf_tc_expect_fail("Will not fail");
284*11be35a1SLionel Sambuc     atf_tc_expect_pass();
285*11be35a1SLionel Sambuc 
286*11be35a1SLionel Sambuc     atf_tc_expect_fail("And fail again");
287*11be35a1SLionel Sambuc     atf_tc_fail_nonfatal("def");
288*11be35a1SLionel Sambuc     atf_tc_expect_pass();
289*11be35a1SLionel Sambuc }
290*11be35a1SLionel Sambuc 
291*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(expect_exit_any_and_exit);
ATF_TC_BODY(expect_exit_any_and_exit,tc)292*11be35a1SLionel Sambuc ATF_TC_BODY(expect_exit_any_and_exit, tc)
293*11be35a1SLionel Sambuc {
294*11be35a1SLionel Sambuc     atf_tc_expect_exit(-1, "Call will exit");
295*11be35a1SLionel Sambuc     exit(EXIT_SUCCESS);
296*11be35a1SLionel Sambuc }
297*11be35a1SLionel Sambuc 
298*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(expect_exit_code_and_exit);
ATF_TC_BODY(expect_exit_code_and_exit,tc)299*11be35a1SLionel Sambuc ATF_TC_BODY(expect_exit_code_and_exit, tc)
300*11be35a1SLionel Sambuc {
301*11be35a1SLionel Sambuc     atf_tc_expect_exit(123, "Call will exit");
302*11be35a1SLionel Sambuc     exit(123);
303*11be35a1SLionel Sambuc }
304*11be35a1SLionel Sambuc 
305*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(expect_exit_but_pass);
ATF_TC_BODY(expect_exit_but_pass,tc)306*11be35a1SLionel Sambuc ATF_TC_BODY(expect_exit_but_pass, tc)
307*11be35a1SLionel Sambuc {
308*11be35a1SLionel Sambuc     atf_tc_expect_exit(-1, "Call won't exit");
309*11be35a1SLionel Sambuc }
310*11be35a1SLionel Sambuc 
311*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(expect_signal_any_and_signal);
ATF_TC_BODY(expect_signal_any_and_signal,tc)312*11be35a1SLionel Sambuc ATF_TC_BODY(expect_signal_any_and_signal, tc)
313*11be35a1SLionel Sambuc {
314*11be35a1SLionel Sambuc     atf_tc_expect_signal(-1, "Call will signal");
315*11be35a1SLionel Sambuc     kill(getpid(), SIGKILL);
316*11be35a1SLionel Sambuc }
317*11be35a1SLionel Sambuc 
318*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(expect_signal_no_and_signal);
ATF_TC_BODY(expect_signal_no_and_signal,tc)319*11be35a1SLionel Sambuc ATF_TC_BODY(expect_signal_no_and_signal, tc)
320*11be35a1SLionel Sambuc {
321*11be35a1SLionel Sambuc     atf_tc_expect_signal(SIGHUP, "Call will signal");
322*11be35a1SLionel Sambuc     kill(getpid(), SIGHUP);
323*11be35a1SLionel Sambuc }
324*11be35a1SLionel Sambuc 
325*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(expect_signal_but_pass);
ATF_TC_BODY(expect_signal_but_pass,tc)326*11be35a1SLionel Sambuc ATF_TC_BODY(expect_signal_but_pass, tc)
327*11be35a1SLionel Sambuc {
328*11be35a1SLionel Sambuc     atf_tc_expect_signal(-1, "Call won't signal");
329*11be35a1SLionel Sambuc }
330*11be35a1SLionel Sambuc 
331*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(expect_death_and_exit);
ATF_TC_BODY(expect_death_and_exit,tc)332*11be35a1SLionel Sambuc ATF_TC_BODY(expect_death_and_exit, tc)
333*11be35a1SLionel Sambuc {
334*11be35a1SLionel Sambuc     atf_tc_expect_death("Exit case");
335*11be35a1SLionel Sambuc     exit(123);
336*11be35a1SLionel Sambuc }
337*11be35a1SLionel Sambuc 
338*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(expect_death_and_signal);
ATF_TC_BODY(expect_death_and_signal,tc)339*11be35a1SLionel Sambuc ATF_TC_BODY(expect_death_and_signal, tc)
340*11be35a1SLionel Sambuc {
341*11be35a1SLionel Sambuc     atf_tc_expect_death("Signal case");
342*11be35a1SLionel Sambuc     kill(getpid(), SIGKILL);
343*11be35a1SLionel Sambuc }
344*11be35a1SLionel Sambuc 
345*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(expect_death_but_pass);
ATF_TC_BODY(expect_death_but_pass,tc)346*11be35a1SLionel Sambuc ATF_TC_BODY(expect_death_but_pass, tc)
347*11be35a1SLionel Sambuc {
348*11be35a1SLionel Sambuc     atf_tc_expect_death("Call won't die");
349*11be35a1SLionel Sambuc }
350*11be35a1SLionel Sambuc 
351*11be35a1SLionel Sambuc ATF_TC(expect_timeout_and_hang);
ATF_TC_HEAD(expect_timeout_and_hang,tc)352*11be35a1SLionel Sambuc ATF_TC_HEAD(expect_timeout_and_hang, tc)
353*11be35a1SLionel Sambuc {
354*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "timeout", "1");
355*11be35a1SLionel Sambuc }
ATF_TC_BODY(expect_timeout_and_hang,tc)356*11be35a1SLionel Sambuc ATF_TC_BODY(expect_timeout_and_hang, tc)
357*11be35a1SLionel Sambuc {
358*11be35a1SLionel Sambuc     atf_tc_expect_timeout("Will overrun");
359*11be35a1SLionel Sambuc     sleep(5);
360*11be35a1SLionel Sambuc }
361*11be35a1SLionel Sambuc 
362*11be35a1SLionel Sambuc ATF_TC(expect_timeout_but_pass);
ATF_TC_HEAD(expect_timeout_but_pass,tc)363*11be35a1SLionel Sambuc ATF_TC_HEAD(expect_timeout_but_pass, tc)
364*11be35a1SLionel Sambuc {
365*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "timeout", "1");
366*11be35a1SLionel Sambuc }
ATF_TC_BODY(expect_timeout_but_pass,tc)367*11be35a1SLionel Sambuc ATF_TC_BODY(expect_timeout_but_pass, tc)
368*11be35a1SLionel Sambuc {
369*11be35a1SLionel Sambuc     atf_tc_expect_timeout("Will just exit");
370*11be35a1SLionel Sambuc }
371*11be35a1SLionel Sambuc 
372*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
373*11be35a1SLionel Sambuc  * Helper tests for "t_meta_data".
374*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
375*11be35a1SLionel Sambuc 
376*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(metadata_no_descr);
ATF_TC_BODY(metadata_no_descr,tc)377*11be35a1SLionel Sambuc ATF_TC_BODY(metadata_no_descr, tc)
378*11be35a1SLionel Sambuc {
379*11be35a1SLionel Sambuc }
380*11be35a1SLionel Sambuc 
381*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(metadata_no_head);
ATF_TC_BODY(metadata_no_head,tc)382*11be35a1SLionel Sambuc ATF_TC_BODY(metadata_no_head, tc)
383*11be35a1SLionel Sambuc {
384*11be35a1SLionel Sambuc }
385*11be35a1SLionel Sambuc 
386*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
387*11be35a1SLionel Sambuc  * Helper tests for "t_srcdir".
388*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
389*11be35a1SLionel Sambuc 
390*11be35a1SLionel Sambuc ATF_TC(srcdir_exists);
ATF_TC_HEAD(srcdir_exists,tc)391*11be35a1SLionel Sambuc ATF_TC_HEAD(srcdir_exists, tc)
392*11be35a1SLionel Sambuc {
393*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Helper test case for the t_srcdir test "
394*11be35a1SLionel Sambuc                       "program");
395*11be35a1SLionel Sambuc }
ATF_TC_BODY(srcdir_exists,tc)396*11be35a1SLionel Sambuc ATF_TC_BODY(srcdir_exists, tc)
397*11be35a1SLionel Sambuc {
398*11be35a1SLionel Sambuc     atf_fs_path_t p;
399*11be35a1SLionel Sambuc     bool b;
400*11be35a1SLionel Sambuc 
401*11be35a1SLionel Sambuc     RE(atf_fs_path_init_fmt(&p, "%s/datafile",
402*11be35a1SLionel Sambuc                             atf_tc_get_config_var(tc, "srcdir")));
403*11be35a1SLionel Sambuc     RE(atf_fs_exists(&p, &b));
404*11be35a1SLionel Sambuc     atf_fs_path_fini(&p);
405*11be35a1SLionel Sambuc     if (!b)
406*11be35a1SLionel Sambuc         atf_tc_fail("Cannot find datafile");
407*11be35a1SLionel Sambuc }
408*11be35a1SLionel Sambuc 
409*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
410*11be35a1SLionel Sambuc  * Helper tests for "t_result".
411*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
412*11be35a1SLionel Sambuc 
413*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(result_pass);
ATF_TC_BODY(result_pass,tc)414*11be35a1SLionel Sambuc ATF_TC_BODY(result_pass, tc)
415*11be35a1SLionel Sambuc {
416*11be35a1SLionel Sambuc     printf("msg\n");
417*11be35a1SLionel Sambuc }
418*11be35a1SLionel Sambuc 
419*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(result_fail);
ATF_TC_BODY(result_fail,tc)420*11be35a1SLionel Sambuc ATF_TC_BODY(result_fail, tc)
421*11be35a1SLionel Sambuc {
422*11be35a1SLionel Sambuc     printf("msg\n");
423*11be35a1SLionel Sambuc     atf_tc_fail("Failure reason");
424*11be35a1SLionel Sambuc }
425*11be35a1SLionel Sambuc 
426*11be35a1SLionel Sambuc ATF_TC_WITHOUT_HEAD(result_skip);
ATF_TC_BODY(result_skip,tc)427*11be35a1SLionel Sambuc ATF_TC_BODY(result_skip, tc)
428*11be35a1SLionel Sambuc {
429*11be35a1SLionel Sambuc     printf("msg\n");
430*11be35a1SLionel Sambuc     atf_tc_skip("Skipped reason");
431*11be35a1SLionel Sambuc }
432*11be35a1SLionel Sambuc 
433*11be35a1SLionel Sambuc ATF_TC(result_newlines_fail);
ATF_TC_HEAD(result_newlines_fail,tc)434*11be35a1SLionel Sambuc ATF_TC_HEAD(result_newlines_fail, tc)
435*11be35a1SLionel Sambuc {
436*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Helper test case for the t_result test "
437*11be35a1SLionel Sambuc                       "program");
438*11be35a1SLionel Sambuc }
ATF_TC_BODY(result_newlines_fail,tc)439*11be35a1SLionel Sambuc ATF_TC_BODY(result_newlines_fail, tc)
440*11be35a1SLionel Sambuc {
441*11be35a1SLionel Sambuc     atf_tc_fail("First line\nSecond line");
442*11be35a1SLionel Sambuc }
443*11be35a1SLionel Sambuc 
444*11be35a1SLionel Sambuc ATF_TC(result_newlines_skip);
ATF_TC_HEAD(result_newlines_skip,tc)445*11be35a1SLionel Sambuc ATF_TC_HEAD(result_newlines_skip, tc)
446*11be35a1SLionel Sambuc {
447*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Helper test case for the t_result test "
448*11be35a1SLionel Sambuc                       "program");
449*11be35a1SLionel Sambuc }
ATF_TC_BODY(result_newlines_skip,tc)450*11be35a1SLionel Sambuc ATF_TC_BODY(result_newlines_skip, tc)
451*11be35a1SLionel Sambuc {
452*11be35a1SLionel Sambuc     atf_tc_skip("First line\nSecond line");
453*11be35a1SLionel Sambuc }
454*11be35a1SLionel Sambuc 
455*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
456*11be35a1SLionel Sambuc  * Main.
457*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
458*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)459*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
460*11be35a1SLionel Sambuc {
461*11be35a1SLionel Sambuc     /* Add helper tests for t_cleanup. */
462*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, cleanup_pass);
463*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, cleanup_fail);
464*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, cleanup_skip);
465*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, cleanup_curdir);
466*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, cleanup_sigterm);
467*11be35a1SLionel Sambuc 
468*11be35a1SLionel Sambuc     /* Add helper tests for t_config. */
469*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, config_unset);
470*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, config_empty);
471*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, config_value);
472*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, config_multi_value);
473*11be35a1SLionel Sambuc 
474*11be35a1SLionel Sambuc     /* Add helper tests for t_expect. */
475*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, expect_pass_and_pass);
476*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, expect_pass_but_fail_requirement);
477*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, expect_pass_but_fail_check);
478*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, expect_fail_and_fail_requirement);
479*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, expect_fail_and_fail_check);
480*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, expect_fail_but_pass);
481*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, expect_exit_any_and_exit);
482*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, expect_exit_code_and_exit);
483*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, expect_exit_but_pass);
484*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, expect_signal_any_and_signal);
485*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, expect_signal_no_and_signal);
486*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, expect_signal_but_pass);
487*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, expect_death_and_exit);
488*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, expect_death_and_signal);
489*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, expect_death_but_pass);
490*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, expect_timeout_and_hang);
491*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, expect_timeout_but_pass);
492*11be35a1SLionel Sambuc 
493*11be35a1SLionel Sambuc     /* Add helper tests for t_meta_data. */
494*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, metadata_no_descr);
495*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, metadata_no_head);
496*11be35a1SLionel Sambuc 
497*11be35a1SLionel Sambuc     /* Add helper tests for t_srcdir. */
498*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, srcdir_exists);
499*11be35a1SLionel Sambuc 
500*11be35a1SLionel Sambuc     /* Add helper tests for t_result. */
501*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, result_pass);
502*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, result_fail);
503*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, result_skip);
504*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, result_newlines_fail);
505*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, result_newlines_skip);
506*11be35a1SLionel Sambuc 
507*11be35a1SLionel Sambuc     return atf_no_error();
508*11be35a1SLionel Sambuc }
509