2 * Automated Testing Framework (atf)
4 * Copyright (c) 2007 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 #include <sys/types.h>
42 #include "atf-c/error.h"
44 #include "atf-c/detail/env.h"
45 #include "atf-c/detail/fs.h"
46 #include "atf-c/detail/test_helpers.h"
47 #include "atf-c/detail/text.h"
49 /* ---------------------------------------------------------------------
50 * Auxiliary functions.
51 * --------------------------------------------------------------------- */
55 safe_remove(const char* path
)
57 if (unlink(path
) == -1)
58 atf_tc_fail("unlink(2) of %s failed", path
);
63 touch(const char *path
)
66 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0644);
68 atf_tc_fail("Could not create file %s", path
);
72 /* ---------------------------------------------------------------------
73 * Helper tests for "t_cleanup".
74 * --------------------------------------------------------------------- */
76 ATF_TC_WITH_CLEANUP(cleanup_pass
);
77 ATF_TC_HEAD(cleanup_pass
, tc
)
79 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_cleanup test "
82 ATF_TC_BODY(cleanup_pass
, tc
)
84 touch(atf_tc_get_config_var(tc
, "tmpfile"));
86 ATF_TC_CLEANUP(cleanup_pass
, tc
)
88 if (atf_tc_get_config_var_as_bool(tc
, "cleanup"))
89 safe_remove(atf_tc_get_config_var(tc
, "tmpfile"));
92 ATF_TC_WITH_CLEANUP(cleanup_fail
);
93 ATF_TC_HEAD(cleanup_fail
, tc
)
95 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_cleanup test "
98 ATF_TC_BODY(cleanup_fail
, tc
)
100 touch(atf_tc_get_config_var(tc
, "tmpfile"));
101 atf_tc_fail("On purpose");
103 ATF_TC_CLEANUP(cleanup_fail
, tc
)
105 if (atf_tc_get_config_var_as_bool(tc
, "cleanup"))
106 safe_remove(atf_tc_get_config_var(tc
, "tmpfile"));
109 ATF_TC_WITH_CLEANUP(cleanup_skip
);
110 ATF_TC_HEAD(cleanup_skip
, tc
)
112 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_cleanup test "
115 ATF_TC_BODY(cleanup_skip
, tc
)
117 touch(atf_tc_get_config_var(tc
, "tmpfile"));
118 atf_tc_skip("On purpose");
120 ATF_TC_CLEANUP(cleanup_skip
, tc
)
122 if (atf_tc_get_config_var_as_bool(tc
, "cleanup"))
123 safe_remove(atf_tc_get_config_var(tc
, "tmpfile"));
126 ATF_TC_WITH_CLEANUP(cleanup_curdir
);
127 ATF_TC_HEAD(cleanup_curdir
, tc
)
129 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_cleanup test "
132 ATF_TC_BODY(cleanup_curdir
, tc
)
136 f
= fopen("oldvalue", "w");
138 atf_tc_fail("Failed to create oldvalue file");
142 ATF_TC_CLEANUP(cleanup_curdir
, tc
)
146 f
= fopen("oldvalue", "r");
149 if (fscanf(f
, "%d", &i
) != 1)
150 fprintf(stderr
, "Failed to read old value\n");
152 printf("Old value: %d", i
);
157 ATF_TC_WITH_CLEANUP(cleanup_sigterm
);
158 ATF_TC_HEAD(cleanup_sigterm
, tc
)
160 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_cleanup test "
163 ATF_TC_BODY(cleanup_sigterm
, tc
)
167 touch(atf_tc_get_config_var(tc
, "tmpfile"));
168 kill(getpid(), SIGTERM
);
170 RE(atf_text_format(&nofile
, "%s.no",
171 atf_tc_get_config_var(tc
, "tmpfile")));
175 ATF_TC_CLEANUP(cleanup_sigterm
, tc
)
177 safe_remove(atf_tc_get_config_var(tc
, "tmpfile"));
180 /* ---------------------------------------------------------------------
181 * Helper tests for "t_config".
182 * --------------------------------------------------------------------- */
184 ATF_TC(config_unset
);
185 ATF_TC_HEAD(config_unset
, tc
)
187 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_config test "
190 ATF_TC_BODY(config_unset
, tc
)
192 ATF_REQUIRE(!atf_tc_has_config_var(tc
, "test"));
195 ATF_TC(config_empty
);
196 ATF_TC_HEAD(config_empty
, tc
)
198 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_config test "
201 ATF_TC_BODY(config_empty
, tc
)
203 ATF_REQUIRE(atf_tc_has_config_var(tc
, "test"));
204 ATF_REQUIRE(strlen(atf_tc_get_config_var(tc
, "test")) == 0);
207 ATF_TC(config_value
);
208 ATF_TC_HEAD(config_value
, tc
)
210 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_config test "
213 ATF_TC_BODY(config_value
, tc
)
215 ATF_REQUIRE(atf_tc_has_config_var(tc
, "test"));
216 ATF_REQUIRE(strcmp(atf_tc_get_config_var(tc
, "test"), "foo") == 0);
219 ATF_TC(config_multi_value
);
220 ATF_TC_HEAD(config_multi_value
, tc
)
222 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_config test "
225 ATF_TC_BODY(config_multi_value
, tc
)
227 ATF_REQUIRE(atf_tc_has_config_var(tc
, "test"));
228 ATF_REQUIRE(strcmp(atf_tc_get_config_var(tc
, "test"), "foo bar") == 0);
231 /* ---------------------------------------------------------------------
232 * Helper tests for "t_expect".
233 * --------------------------------------------------------------------- */
235 ATF_TC_WITHOUT_HEAD(expect_pass_and_pass
);
236 ATF_TC_BODY(expect_pass_and_pass
, tc
)
238 atf_tc_expect_pass();
242 ATF_TC_WITHOUT_HEAD(expect_pass_but_fail_requirement
);
243 ATF_TC_BODY(expect_pass_but_fail_requirement
, tc
)
245 atf_tc_expect_pass();
246 atf_tc_fail("Some reason");
249 ATF_TC_WITHOUT_HEAD(expect_pass_but_fail_check
);
250 ATF_TC_BODY(expect_pass_but_fail_check
, tc
)
252 atf_tc_expect_pass();
253 atf_tc_fail_nonfatal("Some reason");
256 ATF_TC_WITHOUT_HEAD(expect_fail_and_fail_requirement
);
257 ATF_TC_BODY(expect_fail_and_fail_requirement
, tc
)
259 atf_tc_expect_fail("Fail %s", "reason");
260 atf_tc_fail("The failure");
261 atf_tc_expect_pass();
264 ATF_TC_WITHOUT_HEAD(expect_fail_and_fail_check
);
265 ATF_TC_BODY(expect_fail_and_fail_check
, tc
)
267 atf_tc_expect_fail("Fail first");
268 atf_tc_fail_nonfatal("abc");
269 atf_tc_expect_pass();
271 atf_tc_expect_fail("And fail again");
272 atf_tc_fail_nonfatal("def");
273 atf_tc_expect_pass();
276 ATF_TC_WITHOUT_HEAD(expect_fail_but_pass
);
277 ATF_TC_BODY(expect_fail_but_pass
, tc
)
279 atf_tc_expect_fail("Fail first");
280 atf_tc_fail_nonfatal("abc");
281 atf_tc_expect_pass();
283 atf_tc_expect_fail("Will not fail");
284 atf_tc_expect_pass();
286 atf_tc_expect_fail("And fail again");
287 atf_tc_fail_nonfatal("def");
288 atf_tc_expect_pass();
291 ATF_TC_WITHOUT_HEAD(expect_exit_any_and_exit
);
292 ATF_TC_BODY(expect_exit_any_and_exit
, tc
)
294 atf_tc_expect_exit(-1, "Call will exit");
298 ATF_TC_WITHOUT_HEAD(expect_exit_code_and_exit
);
299 ATF_TC_BODY(expect_exit_code_and_exit
, tc
)
301 atf_tc_expect_exit(123, "Call will exit");
305 ATF_TC_WITHOUT_HEAD(expect_exit_but_pass
);
306 ATF_TC_BODY(expect_exit_but_pass
, tc
)
308 atf_tc_expect_exit(-1, "Call won't exit");
311 ATF_TC_WITHOUT_HEAD(expect_signal_any_and_signal
);
312 ATF_TC_BODY(expect_signal_any_and_signal
, tc
)
314 atf_tc_expect_signal(-1, "Call will signal");
315 kill(getpid(), SIGKILL
);
318 ATF_TC_WITHOUT_HEAD(expect_signal_no_and_signal
);
319 ATF_TC_BODY(expect_signal_no_and_signal
, tc
)
321 atf_tc_expect_signal(SIGHUP
, "Call will signal");
322 kill(getpid(), SIGHUP
);
325 ATF_TC_WITHOUT_HEAD(expect_signal_but_pass
);
326 ATF_TC_BODY(expect_signal_but_pass
, tc
)
328 atf_tc_expect_signal(-1, "Call won't signal");
331 ATF_TC_WITHOUT_HEAD(expect_death_and_exit
);
332 ATF_TC_BODY(expect_death_and_exit
, tc
)
334 atf_tc_expect_death("Exit case");
338 ATF_TC_WITHOUT_HEAD(expect_death_and_signal
);
339 ATF_TC_BODY(expect_death_and_signal
, tc
)
341 atf_tc_expect_death("Signal case");
342 kill(getpid(), SIGKILL
);
345 ATF_TC_WITHOUT_HEAD(expect_death_but_pass
);
346 ATF_TC_BODY(expect_death_but_pass
, tc
)
348 atf_tc_expect_death("Call won't die");
351 ATF_TC(expect_timeout_and_hang
);
352 ATF_TC_HEAD(expect_timeout_and_hang
, tc
)
354 atf_tc_set_md_var(tc
, "timeout", "1");
356 ATF_TC_BODY(expect_timeout_and_hang
, tc
)
358 atf_tc_expect_timeout("Will overrun");
362 ATF_TC(expect_timeout_but_pass
);
363 ATF_TC_HEAD(expect_timeout_but_pass
, tc
)
365 atf_tc_set_md_var(tc
, "timeout", "1");
367 ATF_TC_BODY(expect_timeout_but_pass
, tc
)
369 atf_tc_expect_timeout("Will just exit");
372 /* ---------------------------------------------------------------------
373 * Helper tests for "t_meta_data".
374 * --------------------------------------------------------------------- */
376 ATF_TC_WITHOUT_HEAD(metadata_no_descr
);
377 ATF_TC_BODY(metadata_no_descr
, tc
)
381 ATF_TC_WITHOUT_HEAD(metadata_no_head
);
382 ATF_TC_BODY(metadata_no_head
, tc
)
386 /* ---------------------------------------------------------------------
387 * Helper tests for "t_srcdir".
388 * --------------------------------------------------------------------- */
390 ATF_TC(srcdir_exists
);
391 ATF_TC_HEAD(srcdir_exists
, tc
)
393 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_srcdir test "
396 ATF_TC_BODY(srcdir_exists
, tc
)
401 RE(atf_fs_path_init_fmt(&p
, "%s/datafile",
402 atf_tc_get_config_var(tc
, "srcdir")));
403 RE(atf_fs_exists(&p
, &b
));
404 atf_fs_path_fini(&p
);
406 atf_tc_fail("Cannot find datafile");
409 /* ---------------------------------------------------------------------
410 * Helper tests for "t_result".
411 * --------------------------------------------------------------------- */
413 ATF_TC_WITHOUT_HEAD(result_pass
);
414 ATF_TC_BODY(result_pass
, tc
)
419 ATF_TC_WITHOUT_HEAD(result_fail
);
420 ATF_TC_BODY(result_fail
, tc
)
423 atf_tc_fail("Failure reason");
426 ATF_TC_WITHOUT_HEAD(result_skip
);
427 ATF_TC_BODY(result_skip
, tc
)
430 atf_tc_skip("Skipped reason");
433 ATF_TC(result_newlines_fail
);
434 ATF_TC_HEAD(result_newlines_fail
, tc
)
436 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_result test "
439 ATF_TC_BODY(result_newlines_fail
, tc
)
441 atf_tc_fail("First line\nSecond line");
444 ATF_TC(result_newlines_skip
);
445 ATF_TC_HEAD(result_newlines_skip
, tc
)
447 atf_tc_set_md_var(tc
, "descr", "Helper test case for the t_result test "
450 ATF_TC_BODY(result_newlines_skip
, tc
)
452 atf_tc_skip("First line\nSecond line");
455 /* ---------------------------------------------------------------------
457 * --------------------------------------------------------------------- */
461 /* Add helper tests for t_cleanup. */
462 ATF_TP_ADD_TC(tp
, cleanup_pass
);
463 ATF_TP_ADD_TC(tp
, cleanup_fail
);
464 ATF_TP_ADD_TC(tp
, cleanup_skip
);
465 ATF_TP_ADD_TC(tp
, cleanup_curdir
);
466 ATF_TP_ADD_TC(tp
, cleanup_sigterm
);
468 /* Add helper tests for t_config. */
469 ATF_TP_ADD_TC(tp
, config_unset
);
470 ATF_TP_ADD_TC(tp
, config_empty
);
471 ATF_TP_ADD_TC(tp
, config_value
);
472 ATF_TP_ADD_TC(tp
, config_multi_value
);
474 /* Add helper tests for t_expect. */
475 ATF_TP_ADD_TC(tp
, expect_pass_and_pass
);
476 ATF_TP_ADD_TC(tp
, expect_pass_but_fail_requirement
);
477 ATF_TP_ADD_TC(tp
, expect_pass_but_fail_check
);
478 ATF_TP_ADD_TC(tp
, expect_fail_and_fail_requirement
);
479 ATF_TP_ADD_TC(tp
, expect_fail_and_fail_check
);
480 ATF_TP_ADD_TC(tp
, expect_fail_but_pass
);
481 ATF_TP_ADD_TC(tp
, expect_exit_any_and_exit
);
482 ATF_TP_ADD_TC(tp
, expect_exit_code_and_exit
);
483 ATF_TP_ADD_TC(tp
, expect_exit_but_pass
);
484 ATF_TP_ADD_TC(tp
, expect_signal_any_and_signal
);
485 ATF_TP_ADD_TC(tp
, expect_signal_no_and_signal
);
486 ATF_TP_ADD_TC(tp
, expect_signal_but_pass
);
487 ATF_TP_ADD_TC(tp
, expect_death_and_exit
);
488 ATF_TP_ADD_TC(tp
, expect_death_and_signal
);
489 ATF_TP_ADD_TC(tp
, expect_death_but_pass
);
490 ATF_TP_ADD_TC(tp
, expect_timeout_and_hang
);
491 ATF_TP_ADD_TC(tp
, expect_timeout_but_pass
);
493 /* Add helper tests for t_meta_data. */
494 ATF_TP_ADD_TC(tp
, metadata_no_descr
);
495 ATF_TP_ADD_TC(tp
, metadata_no_head
);
497 /* Add helper tests for t_srcdir. */
498 ATF_TP_ADD_TC(tp
, srcdir_exists
);
500 /* Add helper tests for t_result. */
501 ATF_TP_ADD_TC(tp
, result_pass
);
502 ATF_TP_ADD_TC(tp
, result_fail
);
503 ATF_TP_ADD_TC(tp
, result_skip
);
504 ATF_TP_ADD_TC(tp
, result_newlines_fail
);
505 ATF_TP_ADD_TC(tp
, result_newlines_skip
);
507 return atf_no_error();