2 // Automated Testing Framework (atf)
4 // Copyright (c) 2008 The NetBSD Foundation, Inc.
5 // All rights reserved.
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.
43 #include "detail/fs.hpp"
44 #include "detail/process.hpp"
45 #include "detail/sanity.hpp"
46 #include "detail/test_helpers.hpp"
47 #include "detail/text.hpp"
49 // ------------------------------------------------------------------------
50 // Auxiliary functions.
51 // ------------------------------------------------------------------------
55 create_ctl_file(const char *name)
57 ATF_REQUIRE(open(name, O_CREAT | O_WRONLY | O_TRUNC, 0644) != -1);
60 // ------------------------------------------------------------------------
61 // Auxiliary test cases.
62 // ------------------------------------------------------------------------
64 ATF_TEST_CASE(h_pass);
65 ATF_TEST_CASE_HEAD(h_pass)
67 set_md_var("descr", "Helper test case");
69 ATF_TEST_CASE_BODY(h_pass)
71 create_ctl_file("before");
73 create_ctl_file("after");
76 ATF_TEST_CASE(h_fail);
77 ATF_TEST_CASE_HEAD(h_fail)
79 set_md_var("descr", "Helper test case");
81 ATF_TEST_CASE_BODY(h_fail)
83 create_ctl_file("before");
84 ATF_FAIL("Failed on purpose");
85 create_ctl_file("after");
88 ATF_TEST_CASE(h_skip);
89 ATF_TEST_CASE_HEAD(h_skip)
91 set_md_var("descr", "Helper test case");
93 ATF_TEST_CASE_BODY(h_skip)
95 create_ctl_file("before");
96 ATF_SKIP("Skipped on purpose");
97 create_ctl_file("after");
100 ATF_TEST_CASE(h_require);
101 ATF_TEST_CASE_HEAD(h_require)
103 set_md_var("descr", "Helper test case");
105 ATF_TEST_CASE_BODY(h_require)
107 bool condition = atf::text::to_bool(get_config_var("condition"));
109 create_ctl_file("before");
110 ATF_REQUIRE(condition);
111 create_ctl_file("after");
114 ATF_TEST_CASE(h_require_eq);
115 ATF_TEST_CASE_HEAD(h_require_eq)
117 set_md_var("descr", "Helper test case");
119 ATF_TEST_CASE_BODY(h_require_eq)
121 long v1 = atf::text::to_type< long >(get_config_var("v1"));
122 long v2 = atf::text::to_type< long >(get_config_var("v2"));
124 create_ctl_file("before");
125 ATF_REQUIRE_EQ(v1, v2);
126 create_ctl_file("after");
129 ATF_TEST_CASE(h_require_in);
130 ATF_TEST_CASE_HEAD(h_require_in)
132 set_md_var("descr", "Helper test case");
134 ATF_TEST_CASE_BODY(h_require_in)
136 const std::string element = get_config_var("value");
138 std::set< std::string > collection;
139 collection.insert("foo");
140 collection.insert("bar");
141 collection.insert("baz");
143 create_ctl_file("before");
144 ATF_REQUIRE_IN(element, collection);
145 create_ctl_file("after");
148 ATF_TEST_CASE(h_require_match);
149 ATF_TEST_CASE_HEAD(h_require_match)
151 set_md_var("descr", "Helper test case");
153 ATF_TEST_CASE_BODY(h_require_match)
155 const std::string regexp = get_config_var("regexp");
156 const std::string string = get_config_var("string");
158 create_ctl_file("before");
159 ATF_REQUIRE_MATCH(regexp, string);
160 create_ctl_file("after");
163 ATF_TEST_CASE(h_require_not_in);
164 ATF_TEST_CASE_HEAD(h_require_not_in)
166 set_md_var("descr", "Helper test case");
168 ATF_TEST_CASE_BODY(h_require_not_in)
170 const std::string element = get_config_var("value");
172 std::set< std::string > collection;
173 collection.insert("foo");
174 collection.insert("bar");
175 collection.insert("baz");
177 create_ctl_file("before");
178 ATF_REQUIRE_NOT_IN(element, collection);
179 create_ctl_file("after");
182 ATF_TEST_CASE(h_require_throw);
183 ATF_TEST_CASE_HEAD(h_require_throw)
185 set_md_var("descr", "Helper test case");
187 ATF_TEST_CASE_BODY(h_require_throw)
189 create_ctl_file("before");
191 if (get_config_var("what") == "throw_int")
192 ATF_REQUIRE_THROW(std::runtime_error, if (1) throw int(5));
193 else if (get_config_var("what") == "throw_rt")
194 ATF_REQUIRE_THROW(std::runtime_error,
195 if (1) throw std::runtime_error("e"));
196 else if (get_config_var("what") == "no_throw_rt")
197 ATF_REQUIRE_THROW(std::runtime_error,
198 if (0) throw std::runtime_error("e"));
200 create_ctl_file("after");
203 ATF_TEST_CASE(h_require_throw_re);
204 ATF_TEST_CASE_HEAD(h_require_throw_re)
206 set_md_var("descr", "Helper test case");
208 ATF_TEST_CASE_BODY(h_require_throw_re)
210 create_ctl_file("before");
212 if (get_config_var("what") == "throw_int")
213 ATF_REQUIRE_THROW_RE(std::runtime_error, "5", if (1) throw int(5));
214 else if (get_config_var("what") == "throw_rt_match")
215 ATF_REQUIRE_THROW_RE(std::runtime_error, "foo.*baz",
216 if (1) throw std::runtime_error("a foo bar baz"));
217 else if (get_config_var("what") == "throw_rt_no_match")
218 ATF_REQUIRE_THROW_RE(std::runtime_error, "foo.*baz",
219 if (1) throw std::runtime_error("baz foo bar a"));
220 else if (get_config_var("what") == "no_throw_rt")
221 ATF_REQUIRE_THROW_RE(std::runtime_error, "e",
222 if (0) throw std::runtime_error("e"));
224 create_ctl_file("after");
228 errno_fail_stub(const int raised_errno)
230 errno = raised_errno;
240 ATF_TEST_CASE(h_check_errno);
241 ATF_TEST_CASE_HEAD(h_check_errno)
243 set_md_var("descr", "Helper test case");
245 ATF_TEST_CASE_BODY(h_check_errno)
247 create_ctl_file("before");
249 if (get_config_var("what") == "no_error")
250 ATF_CHECK_ERRNO(-1, errno_ok_stub() == -1);
251 else if (get_config_var("what") == "errno_ok")
252 ATF_CHECK_ERRNO(2, errno_fail_stub(2) == -1);
253 else if (get_config_var("what") == "errno_fail")
254 ATF_CHECK_ERRNO(3, errno_fail_stub(4) == -1);
258 create_ctl_file("after");
261 ATF_TEST_CASE(h_require_errno);
262 ATF_TEST_CASE_HEAD(h_require_errno)
264 set_md_var("descr", "Helper test case");
266 ATF_TEST_CASE_BODY(h_require_errno)
268 create_ctl_file("before");
270 if (get_config_var("what") == "no_error")
271 ATF_REQUIRE_ERRNO(-1, errno_ok_stub() == -1);
272 else if (get_config_var("what") == "errno_ok")
273 ATF_REQUIRE_ERRNO(2, errno_fail_stub(2) == -1);
274 else if (get_config_var("what") == "errno_fail")
275 ATF_REQUIRE_ERRNO(3, errno_fail_stub(4) == -1);
279 create_ctl_file("after");
282 // ------------------------------------------------------------------------
283 // Test cases for the macros.
284 // ------------------------------------------------------------------------
287 ATF_TEST_CASE_HEAD(pass)
289 set_md_var("descr", "Tests the ATF_PASS macro");
291 ATF_TEST_CASE_BODY(pass)
293 ATF_TEST_CASE_USE(h_pass);
294 run_h_tc< ATF_TEST_CASE_NAME(h_pass) >();
295 ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
296 ATF_REQUIRE(atf::fs::exists(atf::fs::path("before")));
297 ATF_REQUIRE(!atf::fs::exists(atf::fs::path("after")));
301 ATF_TEST_CASE_HEAD(fail)
303 set_md_var("descr", "Tests the ATF_FAIL macro");
305 ATF_TEST_CASE_BODY(fail)
307 ATF_TEST_CASE_USE(h_fail);
308 run_h_tc< ATF_TEST_CASE_NAME(h_fail) >();
309 ATF_REQUIRE(atf::utils::grep_file("^failed: Failed on purpose", "result"));
310 ATF_REQUIRE(atf::fs::exists(atf::fs::path("before")));
311 ATF_REQUIRE(!atf::fs::exists(atf::fs::path("after")));
315 ATF_TEST_CASE_HEAD(skip)
317 set_md_var("descr", "Tests the ATF_SKIP macro");
319 ATF_TEST_CASE_BODY(skip)
321 ATF_TEST_CASE_USE(h_skip);
322 run_h_tc< ATF_TEST_CASE_NAME(h_skip) >();
323 ATF_REQUIRE(atf::utils::grep_file("^skipped: Skipped on purpose",
325 ATF_REQUIRE(atf::fs::exists(atf::fs::path("before")));
326 ATF_REQUIRE(!atf::fs::exists(atf::fs::path("after")));
329 ATF_TEST_CASE(require);
330 ATF_TEST_CASE_HEAD(require)
332 set_md_var("descr", "Tests the ATF_REQUIRE macro");
334 ATF_TEST_CASE_BODY(require)
345 const atf::fs::path before("before");
346 const atf::fs::path after("after");
348 for (t = &tests[0]; t->cond != NULL; t++) {
349 atf::tests::vars_map config;
350 config["condition"] = t->cond;
352 std::cout << "Checking with a " << t->cond << " value\n";
354 ATF_TEST_CASE_USE(h_require);
355 run_h_tc< ATF_TEST_CASE_NAME(h_require) >(config);
357 ATF_REQUIRE(atf::fs::exists(before));
359 ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
360 ATF_REQUIRE(atf::fs::exists(after));
362 ATF_REQUIRE(atf::utils::grep_file(
363 "^failed: .*condition not met", "result"));
364 ATF_REQUIRE(!atf::fs::exists(after));
367 atf::fs::remove(before);
369 atf::fs::remove(after);
373 ATF_TEST_CASE(require_eq);
374 ATF_TEST_CASE_HEAD(require_eq)
376 set_md_var("descr", "Tests the ATF_REQUIRE_EQ macro");
378 ATF_TEST_CASE_BODY(require_eq)
389 { NULL, NULL, false }
392 const atf::fs::path before("before");
393 const atf::fs::path after("after");
395 for (t = &tests[0]; t->v1 != NULL; t++) {
396 atf::tests::vars_map config;
397 config["v1"] = t->v1;
398 config["v2"] = t->v2;
400 std::cout << "Checking with " << t->v1 << ", " << t->v2
401 << " and expecting " << (t->ok ? "true" : "false")
404 ATF_TEST_CASE_USE(h_require_eq);
405 run_h_tc< ATF_TEST_CASE_NAME(h_require_eq) >(config);
407 ATF_REQUIRE(atf::fs::exists(before));
409 ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
410 ATF_REQUIRE(atf::fs::exists(after));
412 ATF_REQUIRE(atf::utils::grep_file("^failed: .*v1 != v2", "result"));
413 ATF_REQUIRE(!atf::fs::exists(after));
416 atf::fs::remove(before);
418 atf::fs::remove(after);
422 ATF_TEST_CASE(require_in);
423 ATF_TEST_CASE_HEAD(require_in)
425 set_md_var("descr", "Tests the ATF_REQUIRE_IN macro");
427 ATF_TEST_CASE_BODY(require_in)
442 const atf::fs::path before("before");
443 const atf::fs::path after("after");
445 for (t = &tests[0]; t->value != NULL; t++) {
446 atf::tests::vars_map config;
447 config["value"] = t->value;
449 ATF_TEST_CASE_USE(h_require_in);
450 run_h_tc< ATF_TEST_CASE_NAME(h_require_in) >(config);
452 ATF_REQUIRE(atf::fs::exists(before));
454 ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
455 ATF_REQUIRE(atf::fs::exists(after));
457 ATF_REQUIRE(atf::utils::grep_file("^failed: ", "result"));
458 ATF_REQUIRE(!atf::fs::exists(after));
461 atf::fs::remove(before);
463 atf::fs::remove(after);
467 ATF_TEST_CASE(require_match);
468 ATF_TEST_CASE_HEAD(require_match)
470 set_md_var("descr", "Tests the ATF_REQUIRE_MATCH macro");
472 ATF_TEST_CASE_BODY(require_match)
479 { "foo.*bar", "this is a foo, bar, baz", true },
480 { "bar.*baz", "this is a baz, bar, foo", false },
481 { NULL, NULL, false }
484 const atf::fs::path before("before");
485 const atf::fs::path after("after");
487 for (t = &tests[0]; t->regexp != NULL; t++) {
488 atf::tests::vars_map config;
489 config["regexp"] = t->regexp;
490 config["string"] = t->string;
492 std::cout << "Checking with " << t->regexp << ", " << t->string
493 << " and expecting " << (t->ok ? "true" : "false")
496 ATF_TEST_CASE_USE(h_require_match);
497 run_h_tc< ATF_TEST_CASE_NAME(h_require_match) >(config);
499 ATF_REQUIRE(atf::fs::exists(before));
501 ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
502 ATF_REQUIRE(atf::fs::exists(after));
504 ATF_REQUIRE(atf::utils::grep_file("^failed: ", "result"));
505 ATF_REQUIRE(!atf::fs::exists(after));
508 atf::fs::remove(before);
510 atf::fs::remove(after);
514 ATF_TEST_CASE(require_not_in);
515 ATF_TEST_CASE_HEAD(require_not_in)
517 set_md_var("descr", "Tests the ATF_REQUIRE_NOT_IN macro");
519 ATF_TEST_CASE_BODY(require_not_in)
534 const atf::fs::path before("before");
535 const atf::fs::path after("after");
537 for (t = &tests[0]; t->value != NULL; t++) {
538 atf::tests::vars_map config;
539 config["value"] = t->value;
541 ATF_TEST_CASE_USE(h_require_not_in);
542 run_h_tc< ATF_TEST_CASE_NAME(h_require_not_in) >(config);
544 ATF_REQUIRE(atf::fs::exists(before));
546 ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
547 ATF_REQUIRE(atf::fs::exists(after));
549 ATF_REQUIRE(atf::utils::grep_file("^failed: ", "result"));
550 ATF_REQUIRE(!atf::fs::exists(after));
553 atf::fs::remove(before);
555 atf::fs::remove(after);
559 ATF_TEST_CASE(require_throw);
560 ATF_TEST_CASE_HEAD(require_throw)
562 set_md_var("descr", "Tests the ATF_REQUIRE_THROW macro");
564 ATF_TEST_CASE_BODY(require_throw)
571 { "throw_int", false, "unexpected error" },
572 { "throw_rt", true, NULL },
573 { "no_throw_rt", false, "did not throw" },
574 { NULL, false, NULL }
577 const atf::fs::path before("before");
578 const atf::fs::path after("after");
580 for (t = &tests[0]; t->what != NULL; t++) {
581 atf::tests::vars_map config;
582 config["what"] = t->what;
584 std::cout << "Checking with " << t->what << " and expecting "
585 << (t->ok ? "true" : "false") << "\n";
587 ATF_TEST_CASE_USE(h_require_throw);
588 run_h_tc< ATF_TEST_CASE_NAME(h_require_throw) >(config);
590 ATF_REQUIRE(atf::fs::exists(before));
592 ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
593 ATF_REQUIRE(atf::fs::exists(after));
595 std::cout << "Checking that message contains '" << t->msg
597 std::string exp_result = std::string("^failed: .*") + t->msg;
598 ATF_REQUIRE(atf::utils::grep_file(exp_result.c_str(), "result"));
599 ATF_REQUIRE(!atf::fs::exists(after));
602 atf::fs::remove(before);
604 atf::fs::remove(after);
608 ATF_TEST_CASE(require_throw_re);
609 ATF_TEST_CASE_HEAD(require_throw_re)
611 set_md_var("descr", "Tests the ATF_REQUIRE_THROW_RE macro");
613 ATF_TEST_CASE_BODY(require_throw_re)
620 { "throw_int", false, "unexpected error" },
621 { "throw_rt_match", true, NULL },
622 { "throw_rt_no_match", false,
623 "threw.*runtime_error\\(baz foo bar a\\).*"
624 "does not match 'foo\\.\\*baz'" },
625 { "no_throw_rt", false, "did not throw" },
626 { NULL, false, NULL }
629 const atf::fs::path before("before");
630 const atf::fs::path after("after");
632 for (t = &tests[0]; t->what != NULL; t++) {
633 atf::tests::vars_map config;
634 config["what"] = t->what;
636 std::cout << "Checking with " << t->what << " and expecting "
637 << (t->ok ? "true" : "false") << "\n";
639 ATF_TEST_CASE_USE(h_require_throw_re);
640 run_h_tc< ATF_TEST_CASE_NAME(h_require_throw_re) >(config);
642 ATF_REQUIRE(atf::fs::exists(before));
644 ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
645 ATF_REQUIRE(atf::fs::exists(after));
647 std::cout << "Checking that message contains '" << t->msg
649 std::string exp_result = std::string("^failed: .*") + t->msg;
650 ATF_REQUIRE(atf::utils::grep_file(exp_result.c_str(), "result"));
651 ATF_REQUIRE(!atf::fs::exists(after));
654 atf::fs::remove(before);
656 atf::fs::remove(after);
660 ATF_TEST_CASE(check_errno);
661 ATF_TEST_CASE_HEAD(check_errno)
663 set_md_var("descr", "Tests the ATF_CHECK_ERRNO macro");
665 ATF_TEST_CASE_BODY(check_errno)
673 "Expected true value in errno_ok_stub\\(\\) == -1" },
674 { "errno_ok", true, NULL },
675 { "errno_fail", false,
676 "Expected errno 3, got 4, in errno_fail_stub\\(4\\) == -1" },
677 { NULL, false, NULL }
680 const atf::fs::path before("before");
681 const atf::fs::path after("after");
683 for (t = &tests[0]; t->what != NULL; t++) {
684 atf::tests::vars_map config;
685 config["what"] = t->what;
687 ATF_TEST_CASE_USE(h_check_errno);
688 run_h_tc< ATF_TEST_CASE_NAME(h_check_errno) >(config);
690 ATF_REQUIRE(atf::fs::exists(before));
691 ATF_REQUIRE(atf::fs::exists(after));
694 ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
696 ATF_REQUIRE(atf::utils::grep_file("^failed", "result"));
698 std::string exp_result = "macros_test.cpp:[0-9]+: " +
699 std::string(t->msg) + "$";
700 ATF_REQUIRE(atf::utils::grep_file(exp_result.c_str(), "stderr"));
703 atf::fs::remove(before);
704 atf::fs::remove(after);
708 ATF_TEST_CASE(require_errno);
709 ATF_TEST_CASE_HEAD(require_errno)
711 set_md_var("descr", "Tests the ATF_REQUIRE_ERRNO macro");
713 ATF_TEST_CASE_BODY(require_errno)
721 "Expected true value in errno_ok_stub\\(\\) == -1" },
722 { "errno_ok", true, NULL },
723 { "errno_fail", false,
724 "Expected errno 3, got 4, in errno_fail_stub\\(4\\) == -1" },
725 { NULL, false, NULL }
728 const atf::fs::path before("before");
729 const atf::fs::path after("after");
731 for (t = &tests[0]; t->what != NULL; t++) {
732 atf::tests::vars_map config;
733 config["what"] = t->what;
735 ATF_TEST_CASE_USE(h_require_errno);
736 run_h_tc< ATF_TEST_CASE_NAME(h_require_errno) >(config);
738 ATF_REQUIRE(atf::fs::exists(before));
740 ATF_REQUIRE(atf::utils::grep_file("^passed", "result"));
741 ATF_REQUIRE(atf::fs::exists(after));
743 std::string exp_result = "^failed: .*macros_test.cpp:[0-9]+: " +
744 std::string(t->msg) + "$";
745 ATF_REQUIRE(atf::utils::grep_file(exp_result.c_str(), "result"));
747 ATF_REQUIRE(!atf::fs::exists(after));
750 atf::fs::remove(before);
752 atf::fs::remove(after);
756 // ------------------------------------------------------------------------
757 // Tests cases for the header file.
758 // ------------------------------------------------------------------------
760 HEADER_TC(include, "atf-c++/macros.hpp");
761 BUILD_TC(use, "macros_hpp_test.cpp",
762 "Tests that the macros provided by the atf-c++/macros.hpp file "
763 "do not cause syntax errors when used",
764 "Build of macros_hpp_test.cpp failed; some macros in "
765 "atf-c++/macros.hpp are broken");
766 BUILD_TC_FAIL(detect_unused_tests, "unused_test.cpp",
767 "Tests that defining an unused test case raises a warning (and thus "
769 "Build of unused_test.cpp passed; unused test cases are not properly "
772 // ------------------------------------------------------------------------
774 // ------------------------------------------------------------------------
776 ATF_INIT_TEST_CASES(tcs)
778 // Add the test cases for the macros.
779 ATF_ADD_TEST_CASE(tcs, pass);
780 ATF_ADD_TEST_CASE(tcs, fail);
781 ATF_ADD_TEST_CASE(tcs, skip);
782 ATF_ADD_TEST_CASE(tcs, check_errno);
783 ATF_ADD_TEST_CASE(tcs, require);
784 ATF_ADD_TEST_CASE(tcs, require_eq);
785 ATF_ADD_TEST_CASE(tcs, require_in);
786 ATF_ADD_TEST_CASE(tcs, require_match);
787 ATF_ADD_TEST_CASE(tcs, require_not_in);
788 ATF_ADD_TEST_CASE(tcs, require_throw);
789 ATF_ADD_TEST_CASE(tcs, require_throw_re);
790 ATF_ADD_TEST_CASE(tcs, require_errno);
792 // Add the test cases for the header file.
793 ATF_ADD_TEST_CASE(tcs, include);
794 ATF_ADD_TEST_CASE(tcs, use);
795 ATF_ADD_TEST_CASE(tcs, detect_unused_tests);