1 /* $NetBSD: c_helpers.c,v 1.3 2014/12/10 04:38:04 christos Exp $ */
4 * Automated Testing Framework (atf)
6 * Copyright (c) 2007 The NetBSD Foundation, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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 #include <sys/types.h>
44 #include "atf-c/error.h"
46 #include "atf-c/detail/env.h"
47 #include "atf-c/detail/fs.h"
48 #include "atf-c/detail/test_helpers.h"
49 #include "atf-c/detail/text.h"
51 /* ---------------------------------------------------------------------
52 * Auxiliary functions.
53 * --------------------------------------------------------------------- */
57 safe_remove(const char* path
)
59 if (unlink(path
) == -1)
60 atf_tc_fail("unlink(2) of %s failed", path
);
65 touch(const char *path
)
68 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0644);
70 atf_tc_fail("Could not create file %s", path
);
74 /* ---------------------------------------------------------------------
75 * Helper tests for "t_cleanup".
76 * --------------------------------------------------------------------- */
78 ATF_TC_WITH_CLEANUP(cleanup_pass
);
79 ATF_TC_HEAD(cleanup_pass
, tc
)
81 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_cleanup test "
84 ATF_TC_BODY(cleanup_pass
, tc
)
86 touch(atf_tc_get_config_var(tc
, "tmpfile"));
88 ATF_TC_CLEANUP(cleanup_pass
, tc
)
90 if (atf_tc_get_config_var_as_bool(tc
, "cleanup"))
91 safe_remove(atf_tc_get_config_var(tc
, "tmpfile"));
94 ATF_TC_WITH_CLEANUP(cleanup_fail
);
95 ATF_TC_HEAD(cleanup_fail
, tc
)
97 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_cleanup test "
100 ATF_TC_BODY(cleanup_fail
, tc
)
102 touch(atf_tc_get_config_var(tc
, "tmpfile"));
103 atf_tc_fail("On purpose");
105 ATF_TC_CLEANUP(cleanup_fail
, tc
)
107 if (atf_tc_get_config_var_as_bool(tc
, "cleanup"))
108 safe_remove(atf_tc_get_config_var(tc
, "tmpfile"));
111 ATF_TC_WITH_CLEANUP(cleanup_skip
);
112 ATF_TC_HEAD(cleanup_skip
, tc
)
114 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_cleanup test "
117 ATF_TC_BODY(cleanup_skip
, tc
)
119 touch(atf_tc_get_config_var(tc
, "tmpfile"));
120 atf_tc_skip("On purpose");
122 ATF_TC_CLEANUP(cleanup_skip
, tc
)
124 if (atf_tc_get_config_var_as_bool(tc
, "cleanup"))
125 safe_remove(atf_tc_get_config_var(tc
, "tmpfile"));
128 ATF_TC_WITH_CLEANUP(cleanup_curdir
);
129 ATF_TC_HEAD(cleanup_curdir
, tc
)
131 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_cleanup test "
134 ATF_TC_BODY(cleanup_curdir
, tc
)
138 f
= fopen("oldvalue", "w");
140 atf_tc_fail("Failed to create oldvalue file");
144 ATF_TC_CLEANUP(cleanup_curdir
, tc
)
148 f
= fopen("oldvalue", "r");
151 if (fscanf(f
, "%d", &i
) != 1)
152 fprintf(stderr
, "Failed to read old value\n");
154 printf("Old value: %d", i
);
159 ATF_TC_WITH_CLEANUP(cleanup_sigterm
);
160 ATF_TC_HEAD(cleanup_sigterm
, tc
)
162 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_cleanup test "
165 ATF_TC_BODY(cleanup_sigterm
, tc
)
169 touch(atf_tc_get_config_var(tc
, "tmpfile"));
170 kill(getpid(), SIGTERM
);
172 RE(atf_text_format(&nofile
, "%s.no",
173 atf_tc_get_config_var(tc
, "tmpfile")));
177 ATF_TC_CLEANUP(cleanup_sigterm
, tc
)
179 safe_remove(atf_tc_get_config_var(tc
, "tmpfile"));
182 /* ---------------------------------------------------------------------
183 * Helper tests for "t_config".
184 * --------------------------------------------------------------------- */
186 ATF_TC(config_unset
);
187 ATF_TC_HEAD(config_unset
, tc
)
189 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_config test "
192 ATF_TC_BODY(config_unset
, tc
)
194 ATF_REQUIRE(!atf_tc_has_config_var(tc
, "test"));
197 ATF_TC(config_empty
);
198 ATF_TC_HEAD(config_empty
, tc
)
200 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_config test "
203 ATF_TC_BODY(config_empty
, tc
)
205 ATF_REQUIRE(atf_tc_has_config_var(tc
, "test"));
206 ATF_REQUIRE(strlen(atf_tc_get_config_var(tc
, "test")) == 0);
209 ATF_TC(config_value
);
210 ATF_TC_HEAD(config_value
, tc
)
212 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_config test "
215 ATF_TC_BODY(config_value
, tc
)
217 ATF_REQUIRE(atf_tc_has_config_var(tc
, "test"));
218 ATF_REQUIRE(strcmp(atf_tc_get_config_var(tc
, "test"), "foo") == 0);
221 ATF_TC(config_multi_value
);
222 ATF_TC_HEAD(config_multi_value
, tc
)
224 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_config test "
227 ATF_TC_BODY(config_multi_value
, tc
)
229 ATF_REQUIRE(atf_tc_has_config_var(tc
, "test"));
230 ATF_REQUIRE(strcmp(atf_tc_get_config_var(tc
, "test"), "foo bar") == 0);
233 /* ---------------------------------------------------------------------
234 * Helper tests for "t_expect".
235 * --------------------------------------------------------------------- */
237 ATF_TC_WITHOUT_HEAD(expect_pass_and_pass
);
238 ATF_TC_BODY(expect_pass_and_pass
, tc
)
240 atf_tc_expect_pass();
244 ATF_TC_WITHOUT_HEAD(expect_pass_but_fail_requirement
);
245 ATF_TC_BODY(expect_pass_but_fail_requirement
, tc
)
247 atf_tc_expect_pass();
248 atf_tc_fail("Some reason");
251 ATF_TC_WITHOUT_HEAD(expect_pass_but_fail_check
);
252 ATF_TC_BODY(expect_pass_but_fail_check
, tc
)
254 atf_tc_expect_pass();
255 atf_tc_fail_nonfatal("Some reason");
258 ATF_TC_WITHOUT_HEAD(expect_fail_and_fail_requirement
);
259 ATF_TC_BODY(expect_fail_and_fail_requirement
, tc
)
261 atf_tc_expect_fail("Fail %s", "reason");
262 atf_tc_fail("The failure");
263 atf_tc_expect_pass();
266 ATF_TC_WITHOUT_HEAD(expect_fail_and_fail_check
);
267 ATF_TC_BODY(expect_fail_and_fail_check
, tc
)
269 atf_tc_expect_fail("Fail first");
270 atf_tc_fail_nonfatal("abc");
271 atf_tc_expect_pass();
273 atf_tc_expect_fail("And fail again");
274 atf_tc_fail_nonfatal("def");
275 atf_tc_expect_pass();
278 ATF_TC_WITHOUT_HEAD(expect_fail_but_pass
);
279 ATF_TC_BODY(expect_fail_but_pass
, tc
)
281 atf_tc_expect_fail("Fail first");
282 atf_tc_fail_nonfatal("abc");
283 atf_tc_expect_pass();
285 atf_tc_expect_fail("Will not fail");
286 atf_tc_expect_pass();
288 atf_tc_expect_fail("And fail again");
289 atf_tc_fail_nonfatal("def");
290 atf_tc_expect_pass();
293 ATF_TC_WITHOUT_HEAD(expect_exit_any_and_exit
);
294 ATF_TC_BODY(expect_exit_any_and_exit
, tc
)
296 atf_tc_expect_exit(-1, "Call will exit");
300 ATF_TC_WITHOUT_HEAD(expect_exit_code_and_exit
);
301 ATF_TC_BODY(expect_exit_code_and_exit
, tc
)
303 atf_tc_expect_exit(123, "Call will exit");
307 ATF_TC_WITHOUT_HEAD(expect_exit_but_pass
);
308 ATF_TC_BODY(expect_exit_but_pass
, tc
)
310 atf_tc_expect_exit(-1, "Call won't exit");
313 ATF_TC_WITHOUT_HEAD(expect_signal_any_and_signal
);
314 ATF_TC_BODY(expect_signal_any_and_signal
, tc
)
316 atf_tc_expect_signal(-1, "Call will signal");
317 kill(getpid(), SIGKILL
);
320 ATF_TC_WITHOUT_HEAD(expect_signal_no_and_signal
);
321 ATF_TC_BODY(expect_signal_no_and_signal
, tc
)
323 atf_tc_expect_signal(SIGHUP
, "Call will signal");
324 kill(getpid(), SIGHUP
);
327 ATF_TC_WITHOUT_HEAD(expect_signal_but_pass
);
328 ATF_TC_BODY(expect_signal_but_pass
, tc
)
330 atf_tc_expect_signal(-1, "Call won't signal");
333 ATF_TC_WITHOUT_HEAD(expect_death_and_exit
);
334 ATF_TC_BODY(expect_death_and_exit
, tc
)
336 atf_tc_expect_death("Exit case");
340 ATF_TC_WITHOUT_HEAD(expect_death_and_signal
);
341 ATF_TC_BODY(expect_death_and_signal
, tc
)
343 atf_tc_expect_death("Signal case");
344 kill(getpid(), SIGKILL
);
347 ATF_TC_WITHOUT_HEAD(expect_death_but_pass
);
348 ATF_TC_BODY(expect_death_but_pass
, tc
)
350 atf_tc_expect_death("Call won't die");
353 ATF_TC(expect_timeout_and_hang
);
354 ATF_TC_HEAD(expect_timeout_and_hang
, tc
)
356 atf_tc_set_md_var(tc
, "timeout", "1");
358 ATF_TC_BODY(expect_timeout_and_hang
, tc
)
360 atf_tc_expect_timeout("Will overrun");
364 ATF_TC(expect_timeout_but_pass
);
365 ATF_TC_HEAD(expect_timeout_but_pass
, tc
)
367 atf_tc_set_md_var(tc
, "timeout", "1");
369 ATF_TC_BODY(expect_timeout_but_pass
, tc
)
371 atf_tc_expect_timeout("Will just exit");
374 /* ---------------------------------------------------------------------
375 * Helper tests for "t_meta_data".
376 * --------------------------------------------------------------------- */
378 ATF_TC_WITHOUT_HEAD(metadata_no_descr
);
379 ATF_TC_BODY(metadata_no_descr
, tc
)
383 ATF_TC_WITHOUT_HEAD(metadata_no_head
);
384 ATF_TC_BODY(metadata_no_head
, tc
)
388 /* ---------------------------------------------------------------------
389 * Helper tests for "t_srcdir".
390 * --------------------------------------------------------------------- */
392 ATF_TC(srcdir_exists
);
393 ATF_TC_HEAD(srcdir_exists
, tc
)
395 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_srcdir test "
398 ATF_TC_BODY(srcdir_exists
, tc
)
403 RE(atf_fs_path_init_fmt(&p
, "%s/datafile",
404 atf_tc_get_config_var(tc
, "srcdir")));
405 RE(atf_fs_exists(&p
, &b
));
406 atf_fs_path_fini(&p
);
408 atf_tc_fail("Cannot find datafile");
411 /* ---------------------------------------------------------------------
412 * Helper tests for "t_result".
413 * --------------------------------------------------------------------- */
415 ATF_TC_WITHOUT_HEAD(result_pass
);
416 ATF_TC_BODY(result_pass
, tc
)
421 ATF_TC_WITHOUT_HEAD(result_fail
);
422 ATF_TC_BODY(result_fail
, tc
)
425 atf_tc_fail("Failure reason");
428 ATF_TC_WITHOUT_HEAD(result_skip
);
429 ATF_TC_BODY(result_skip
, tc
)
432 atf_tc_skip("Skipped reason");
435 ATF_TC(result_newlines_fail
);
436 ATF_TC_HEAD(result_newlines_fail
, tc
)
438 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_result test "
441 ATF_TC_BODY(result_newlines_fail
, tc
)
443 atf_tc_fail("First line\nSecond line");
446 ATF_TC(result_newlines_skip
);
447 ATF_TC_HEAD(result_newlines_skip
, tc
)
449 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_result test "
452 ATF_TC_BODY(result_newlines_skip
, tc
)
454 atf_tc_skip("First line\nSecond line");
457 /* ---------------------------------------------------------------------
459 * --------------------------------------------------------------------- */
463 /* Add helper tests for t_cleanup. */
464 ATF_TP_ADD_TC(tp
, cleanup_pass
);
465 ATF_TP_ADD_TC(tp
, cleanup_fail
);
466 ATF_TP_ADD_TC(tp
, cleanup_skip
);
467 ATF_TP_ADD_TC(tp
, cleanup_curdir
);
468 ATF_TP_ADD_TC(tp
, cleanup_sigterm
);
470 /* Add helper tests for t_config. */
471 ATF_TP_ADD_TC(tp
, config_unset
);
472 ATF_TP_ADD_TC(tp
, config_empty
);
473 ATF_TP_ADD_TC(tp
, config_value
);
474 ATF_TP_ADD_TC(tp
, config_multi_value
);
476 /* Add helper tests for t_expect. */
477 ATF_TP_ADD_TC(tp
, expect_pass_and_pass
);
478 ATF_TP_ADD_TC(tp
, expect_pass_but_fail_requirement
);
479 ATF_TP_ADD_TC(tp
, expect_pass_but_fail_check
);
480 ATF_TP_ADD_TC(tp
, expect_fail_and_fail_requirement
);
481 ATF_TP_ADD_TC(tp
, expect_fail_and_fail_check
);
482 ATF_TP_ADD_TC(tp
, expect_fail_but_pass
);
483 ATF_TP_ADD_TC(tp
, expect_exit_any_and_exit
);
484 ATF_TP_ADD_TC(tp
, expect_exit_code_and_exit
);
485 ATF_TP_ADD_TC(tp
, expect_exit_but_pass
);
486 ATF_TP_ADD_TC(tp
, expect_signal_any_and_signal
);
487 ATF_TP_ADD_TC(tp
, expect_signal_no_and_signal
);
488 ATF_TP_ADD_TC(tp
, expect_signal_but_pass
);
489 ATF_TP_ADD_TC(tp
, expect_death_and_exit
);
490 ATF_TP_ADD_TC(tp
, expect_death_and_signal
);
491 ATF_TP_ADD_TC(tp
, expect_death_but_pass
);
492 ATF_TP_ADD_TC(tp
, expect_timeout_and_hang
);
493 ATF_TP_ADD_TC(tp
, expect_timeout_but_pass
);
495 /* Add helper tests for t_meta_data. */
496 ATF_TP_ADD_TC(tp
, metadata_no_descr
);
497 ATF_TP_ADD_TC(tp
, metadata_no_head
);
499 /* Add helper tests for t_srcdir. */
500 ATF_TP_ADD_TC(tp
, srcdir_exists
);
502 /* Add helper tests for t_result. */
503 ATF_TP_ADD_TC(tp
, result_pass
);
504 ATF_TP_ADD_TC(tp
, result_fail
);
505 ATF_TP_ADD_TC(tp
, result_skip
);
506 ATF_TP_ADD_TC(tp
, result_newlines_fail
);
507 ATF_TP_ADD_TC(tp
, result_newlines_skip
);
509 return atf_no_error();