1 /* Copyright (c) 2008 The NetBSD Foundation, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
26 #include "atf-c/macros.h"
28 #include <sys/types.h>
42 #include "atf-c/detail/fs.h"
43 #include "atf-c/detail/process.h"
44 #include "atf-c/detail/test_helpers.h"
45 #include "atf-c/detail/text.h"
47 /* ---------------------------------------------------------------------
48 * Auxiliary functions.
49 * --------------------------------------------------------------------- */
53 create_ctl_file(const char *name)
57 RE(atf_fs_path_init_fmt(&p, "%s", name));
58 ATF_REQUIRE(open(atf_fs_path_cstring(&p),
59 O_CREAT | O_WRONLY | O_TRUNC, 0644) != -1);
70 RE(atf_fs_path_init_fmt(&pp, "%s", p));
71 RE(atf_fs_exists(&pp, &b));
72 atf_fs_path_fini(&pp);
79 init_and_run_h_tc(const char *name, void (*head)(atf_tc_t *),
80 void (*body)(const atf_tc_t *))
83 const char *const config[] = { NULL };
85 RE(atf_tc_init(&tc, name, head, body, NULL, config));
86 run_h_tc(&tc, "output", "error", "result");
90 /* ---------------------------------------------------------------------
92 * --------------------------------------------------------------------- */
94 #define H_DEF(id, macro) \
95 ATF_TC_HEAD(h_ ## id, tc) \
97 atf_tc_set_md_var(tc, "descr", "Helper test case"); \
99 ATF_TC_BODY(h_ ## id, tc) \
101 create_ctl_file("before"); \
103 create_ctl_file("after"); \
106 #define H_CHECK_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_check_ ## id)
107 #define H_CHECK_BODY_NAME(id) ATF_TC_BODY_NAME(h_check_ ## id)
108 #define H_CHECK(id, condition) \
109 H_DEF(check_ ## id, ATF_CHECK(condition))
111 #define H_CHECK_MSG_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_check_msg_ ## id)
112 #define H_CHECK_MSG_BODY_NAME(id) ATF_TC_BODY_NAME(h_check_msg_ ## id)
113 #define H_CHECK_MSG(id, condition, msg) \
114 H_DEF(check_msg_ ## id, ATF_CHECK_MSG(condition, msg))
116 #define H_CHECK_EQ_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_check_eq_ ## id)
117 #define H_CHECK_EQ_BODY_NAME(id) ATF_TC_BODY_NAME(h_check_eq_ ## id)
118 #define H_CHECK_EQ(id, v1, v2) \
119 H_DEF(check_eq_ ## id, ATF_CHECK_EQ(v1, v2))
121 #define H_CHECK_STREQ_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_check_streq_ ## id)
122 #define H_CHECK_STREQ_BODY_NAME(id) ATF_TC_BODY_NAME(h_check_streq_ ## id)
123 #define H_CHECK_STREQ(id, v1, v2) \
124 H_DEF(check_streq_ ## id, ATF_CHECK_STREQ(v1, v2))
126 #define H_CHECK_MATCH_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_check_match_ ## id)
127 #define H_CHECK_MATCH_BODY_NAME(id) ATF_TC_BODY_NAME(h_check_match_ ## id)
128 #define H_CHECK_MATCH(id, v1, v2) \
129 H_DEF(check_match_ ## id, ATF_CHECK_MATCH(v1, v2))
131 #define H_CHECK_EQ_MSG_HEAD_NAME(id) \
132 ATF_TC_HEAD_NAME(h_check_eq_msg_ ## id)
133 #define H_CHECK_EQ_MSG_BODY_NAME(id) \
134 ATF_TC_BODY_NAME(h_check_eq_msg_ ## id)
135 #define H_CHECK_EQ_MSG(id, v1, v2, msg) \
136 H_DEF(check_eq_msg_ ## id, ATF_CHECK_EQ_MSG(v1, v2, msg))
138 #define H_CHECK_STREQ_MSG_HEAD_NAME(id) \
139 ATF_TC_HEAD_NAME(h_check_streq_msg_ ## id)
140 #define H_CHECK_STREQ_MSG_BODY_NAME(id) \
141 ATF_TC_BODY_NAME(h_check_streq_msg_ ## id)
142 #define H_CHECK_STREQ_MSG(id, v1, v2, msg) \
143 H_DEF(check_streq_msg_ ## id, ATF_CHECK_STREQ_MSG(v1, v2, msg))
145 #define H_CHECK_MATCH_MSG_HEAD_NAME(id) \
146 ATF_TC_HEAD_NAME(h_check_match_msg_ ## id)
147 #define H_CHECK_MATCH_MSG_BODY_NAME(id) \
148 ATF_TC_BODY_NAME(h_check_match_msg_ ## id)
149 #define H_CHECK_MATCH_MSG(id, v1, v2, msg) \
150 H_DEF(check_match_msg_ ## id, ATF_CHECK_MATCH_MSG(v1, v2, msg))
152 #define H_CHECK_ERRNO_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_check_errno_ ## id)
153 #define H_CHECK_ERRNO_BODY_NAME(id) ATF_TC_BODY_NAME(h_check_errno_ ## id)
154 #define H_CHECK_ERRNO(id, exp_errno, bool_expr) \
155 H_DEF(check_errno_ ## id, ATF_CHECK_ERRNO(exp_errno, bool_expr))
157 #define H_REQUIRE_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_require_ ## id)
158 #define H_REQUIRE_BODY_NAME(id) ATF_TC_BODY_NAME(h_require_ ## id)
159 #define H_REQUIRE(id, condition) \
160 H_DEF(require_ ## id, ATF_REQUIRE(condition))
162 #define H_REQUIRE_MSG_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_require_msg_ ## id)
163 #define H_REQUIRE_MSG_BODY_NAME(id) ATF_TC_BODY_NAME(h_require_msg_ ## id)
164 #define H_REQUIRE_MSG(id, condition, msg) \
165 H_DEF(require_msg_ ## id, ATF_REQUIRE_MSG(condition, msg))
167 #define H_REQUIRE_EQ_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_require_eq_ ## id)
168 #define H_REQUIRE_EQ_BODY_NAME(id) ATF_TC_BODY_NAME(h_require_eq_ ## id)
169 #define H_REQUIRE_EQ(id, v1, v2) \
170 H_DEF(require_eq_ ## id, ATF_REQUIRE_EQ(v1, v2))
172 #define H_REQUIRE_STREQ_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_require_streq_ ## id)
173 #define H_REQUIRE_STREQ_BODY_NAME(id) ATF_TC_BODY_NAME(h_require_streq_ ## id)
174 #define H_REQUIRE_STREQ(id, v1, v2) \
175 H_DEF(require_streq_ ## id, ATF_REQUIRE_STREQ(v1, v2))
177 #define H_REQUIRE_MATCH_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_require_match_ ## id)
178 #define H_REQUIRE_MATCH_BODY_NAME(id) ATF_TC_BODY_NAME(h_require_match_ ## id)
179 #define H_REQUIRE_MATCH(id, v1, v2) \
180 H_DEF(require_match_ ## id, ATF_REQUIRE_MATCH(v1, v2))
182 #define H_REQUIRE_EQ_MSG_HEAD_NAME(id) \
183 ATF_TC_HEAD_NAME(h_require_eq_msg_ ## id)
184 #define H_REQUIRE_EQ_MSG_BODY_NAME(id) \
185 ATF_TC_BODY_NAME(h_require_eq_msg_ ## id)
186 #define H_REQUIRE_EQ_MSG(id, v1, v2, msg) \
187 H_DEF(require_eq_msg_ ## id, ATF_REQUIRE_EQ_MSG(v1, v2, msg))
189 #define H_REQUIRE_STREQ_MSG_HEAD_NAME(id) \
190 ATF_TC_HEAD_NAME(h_require_streq_msg_ ## id)
191 #define H_REQUIRE_STREQ_MSG_BODY_NAME(id) \
192 ATF_TC_BODY_NAME(h_require_streq_msg_ ## id)
193 #define H_REQUIRE_STREQ_MSG(id, v1, v2, msg) \
194 H_DEF(require_streq_msg_ ## id, ATF_REQUIRE_STREQ_MSG(v1, v2, msg))
196 #define H_REQUIRE_MATCH_MSG_HEAD_NAME(id) \
197 ATF_TC_HEAD_NAME(h_require_match_msg_ ## id)
198 #define H_REQUIRE_MATCH_MSG_BODY_NAME(id) \
199 ATF_TC_BODY_NAME(h_require_match_msg_ ## id)
200 #define H_REQUIRE_MATCH_MSG(id, v1, v2, msg) \
201 H_DEF(require_match_msg_ ## id, ATF_REQUIRE_MATCH_MSG(v1, v2, msg))
203 #define H_REQUIRE_ERRNO_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_require_errno_ ## id)
204 #define H_REQUIRE_ERRNO_BODY_NAME(id) ATF_TC_BODY_NAME(h_require_errno_ ## id)
205 #define H_REQUIRE_ERRNO(id, exp_errno, bool_expr) \
206 H_DEF(require_errno_ ## id, ATF_REQUIRE_ERRNO(exp_errno, bool_expr))
208 /* ---------------------------------------------------------------------
209 * Test cases for the ATF_{CHECK,REQUIRE}_ERRNO macros.
210 * --------------------------------------------------------------------- */
213 errno_fail_stub(const int raised_errno)
215 errno = raised_errno;
225 H_CHECK_ERRNO(no_error, -1, errno_ok_stub() == -1);
226 H_CHECK_ERRNO(errno_ok, 2, errno_fail_stub(2) == -1);
227 H_CHECK_ERRNO(errno_fail, 3, errno_fail_stub(4) == -1);
229 H_REQUIRE_ERRNO(no_error, -1, errno_ok_stub() == -1);
230 H_REQUIRE_ERRNO(errno_ok, 2, errno_fail_stub(2) == -1);
231 H_REQUIRE_ERRNO(errno_fail, 3, errno_fail_stub(4) == -1);
234 ATF_TC_HEAD(check_errno, tc)
236 atf_tc_set_md_var(tc, "descr", "Tests the ATF_CHECK_ERRNO macro");
238 ATF_TC_BODY(check_errno, tc)
241 void (*head)(atf_tc_t *);
242 void (*body)(const atf_tc_t *);
244 const char *exp_regex;
246 { H_CHECK_ERRNO_HEAD_NAME(no_error),
247 H_CHECK_ERRNO_BODY_NAME(no_error),
248 false, "Expected true value in errno_ok_stub\\(\\) == -1" },
249 { H_CHECK_ERRNO_HEAD_NAME(errno_ok),
250 H_CHECK_ERRNO_BODY_NAME(errno_ok),
252 { H_CHECK_ERRNO_HEAD_NAME(errno_fail),
253 H_CHECK_ERRNO_BODY_NAME(errno_fail),
254 false, "Expected errno 3, got 4, in errno_fail_stub\\(4\\) == -1" },
255 { NULL, NULL, false, NULL }
258 for (t = &tests[0]; t->head != NULL; t++) {
259 init_and_run_h_tc("h_check_errno", t->head, t->body);
261 ATF_REQUIRE(exists("before"));
262 ATF_REQUIRE(exists("after"));
265 ATF_REQUIRE(atf_utils_grep_file("^passed", "result"));
267 ATF_REQUIRE(atf_utils_grep_file("^failed", "result"));
268 ATF_REQUIRE(atf_utils_grep_file(
269 "macros_test.c:[0-9]+: %s$", "error", t->exp_regex));
272 ATF_REQUIRE(unlink("before") != -1);
273 ATF_REQUIRE(unlink("after") != -1);
277 ATF_TC(require_errno);
278 ATF_TC_HEAD(require_errno, tc)
280 atf_tc_set_md_var(tc, "descr", "Tests the ATF_REQUIRE_ERRNO macro");
282 ATF_TC_BODY(require_errno, tc)
285 void (*head)(atf_tc_t *);
286 void (*body)(const atf_tc_t *);
288 const char *exp_regex;
290 { H_REQUIRE_ERRNO_HEAD_NAME(no_error),
291 H_REQUIRE_ERRNO_BODY_NAME(no_error),
292 false, "Expected true value in errno_ok_stub\\(\\) == -1" },
293 { H_REQUIRE_ERRNO_HEAD_NAME(errno_ok),
294 H_REQUIRE_ERRNO_BODY_NAME(errno_ok),
296 { H_REQUIRE_ERRNO_HEAD_NAME(errno_fail),
297 H_REQUIRE_ERRNO_BODY_NAME(errno_fail),
298 false, "Expected errno 3, got 4, in errno_fail_stub\\(4\\) == -1" },
299 { NULL, NULL, false, NULL }
302 for (t = &tests[0]; t->head != NULL; t++) {
303 init_and_run_h_tc("h_require_errno", t->head, t->body);
305 ATF_REQUIRE(exists("before"));
307 ATF_REQUIRE(atf_utils_grep_file("^passed", "result"));
308 ATF_REQUIRE(exists("after"));
310 ATF_REQUIRE(atf_utils_grep_file(
311 "^failed: .*macros_test.c:[0-9]+: %s$", "result",
313 ATF_REQUIRE(!exists("after"));
316 ATF_REQUIRE(unlink("before") != -1);
318 ATF_REQUIRE(unlink("after") != -1);
322 /* ---------------------------------------------------------------------
323 * Test cases for the ATF_CHECK and ATF_CHECK_MSG macros.
324 * --------------------------------------------------------------------- */
328 H_CHECK_MSG(0, 0, "expected a false value");
329 H_CHECK_MSG(1, 1, "expected a true value");
332 ATF_TC_HEAD(check, tc)
334 atf_tc_set_md_var(tc, "descr", "Tests the ATF_CHECK and "
335 "ATF_CHECK_MSG macros");
337 ATF_TC_BODY(check, tc)
340 void (*head)(atf_tc_t *);
341 void (*body)(const atf_tc_t *);
346 { H_CHECK_HEAD_NAME(0), H_CHECK_BODY_NAME(0), 0,
347 "0 not met", false },
348 { H_CHECK_HEAD_NAME(1), H_CHECK_BODY_NAME(1), 1,
350 { H_CHECK_MSG_HEAD_NAME(0), H_CHECK_MSG_BODY_NAME(0), 0,
351 "expected a false value", false },
352 { H_CHECK_MSG_HEAD_NAME(1), H_CHECK_MSG_BODY_NAME(1), 1,
353 "expected a true value", true },
354 { NULL, NULL, false, NULL, false }
357 for (t = &tests[0]; t->head != NULL; t++) {
358 printf("Checking with a %d value\n", t->value);
360 init_and_run_h_tc("h_check", t->head, t->body);
362 ATF_REQUIRE(exists("before"));
363 ATF_REQUIRE(exists("after"));
366 ATF_REQUIRE(atf_utils_grep_file("^passed", "result"));
368 ATF_REQUIRE(atf_utils_grep_file("^failed", "result"));
369 ATF_REQUIRE(atf_utils_grep_file("Check failed: .*"
370 "macros_test.c:[0-9]+: %s$", "error", t->msg));
373 ATF_REQUIRE(unlink("before") != -1);
374 ATF_REQUIRE(unlink("after") != -1);
378 /* ---------------------------------------------------------------------
379 * Test cases for the ATF_CHECK_*EQ_ macros.
380 * --------------------------------------------------------------------- */
382 struct check_eq_test {
383 void (*head)(atf_tc_t *);
384 void (*body)(const atf_tc_t *);
393 do_check_eq_tests(const struct check_eq_test *tests)
395 const struct check_eq_test *t;
397 for (t = &tests[0]; t->head != NULL; t++) {
398 printf("Checking with %s, %s and expecting %s\n", t->v1, t->v2,
399 t->ok ? "true" : "false");
401 init_and_run_h_tc("h_check", t->head, t->body);
403 ATF_CHECK(exists("before"));
404 ATF_CHECK(exists("after"));
407 ATF_REQUIRE(atf_utils_grep_file("^passed", "result"));
409 ATF_REQUIRE(atf_utils_grep_file("^failed", "result"));
410 ATF_CHECK(atf_utils_grep_file("Check failed: .*"
411 "macros_test.c:[0-9]+: %s$", "error", t->msg));
414 ATF_CHECK(unlink("before") != -1);
415 ATF_CHECK(unlink("after") != -1);
419 H_CHECK_EQ(1_1, 1, 1);
420 H_CHECK_EQ(1_2, 1, 2);
421 H_CHECK_EQ(2_1, 2, 1);
422 H_CHECK_EQ(2_2, 2, 2);
423 H_CHECK_EQ_MSG(1_1, 1, 1, "1 does not match 1");
424 H_CHECK_EQ_MSG(1_2, 1, 2, "1 does not match 2");
425 H_CHECK_EQ_MSG(2_1, 2, 1, "2 does not match 1");
426 H_CHECK_EQ_MSG(2_2, 2, 2, "2 does not match 2");
429 ATF_TC_HEAD(check_eq, tc)
431 atf_tc_set_md_var(tc, "descr", "Tests the ATF_CHECK_EQ and "
432 "ATF_CHECK_EQ_MSG macros");
434 ATF_TC_BODY(check_eq, tc)
436 struct check_eq_test tests[] = {
437 { H_CHECK_EQ_HEAD_NAME(1_1), H_CHECK_EQ_BODY_NAME(1_1),
438 "1", "1", "1 != 1", true },
439 { H_CHECK_EQ_HEAD_NAME(1_2), H_CHECK_EQ_BODY_NAME(1_2),
440 "1", "2", "1 != 2", false },
441 { H_CHECK_EQ_HEAD_NAME(2_1), H_CHECK_EQ_BODY_NAME(2_1),
442 "2", "1", "2 != 1", false },
443 { H_CHECK_EQ_HEAD_NAME(2_2), H_CHECK_EQ_BODY_NAME(2_2),
444 "2", "2", "2 != 2", true },
445 { H_CHECK_EQ_MSG_HEAD_NAME(1_1), H_CHECK_EQ_MSG_BODY_NAME(1_1),
446 "1", "1", "1 != 1: 1 does not match 1", true },
447 { H_CHECK_EQ_MSG_HEAD_NAME(1_2), H_CHECK_EQ_MSG_BODY_NAME(1_2),
448 "1", "2", "1 != 2: 1 does not match 2", false },
449 { H_CHECK_EQ_MSG_HEAD_NAME(2_1), H_CHECK_EQ_MSG_BODY_NAME(2_1),
450 "2", "1", "2 != 1: 2 does not match 1", false },
451 { H_CHECK_EQ_MSG_HEAD_NAME(2_2), H_CHECK_EQ_MSG_BODY_NAME(2_2),
452 "2", "2", "2 != 2: 2 does not match 2", true },
453 { NULL, NULL, 0, 0, "", false }
455 do_check_eq_tests(tests);
458 H_CHECK_STREQ(1_1, "1", "1");
459 H_CHECK_STREQ(1_2, "1", "2");
460 H_CHECK_STREQ(2_1, "2", "1");
461 H_CHECK_STREQ(2_2, "2", "2");
462 H_CHECK_STREQ_MSG(1_1, "1", "1", "1 does not match 1");
463 H_CHECK_STREQ_MSG(1_2, "1", "2", "1 does not match 2");
464 H_CHECK_STREQ_MSG(2_1, "2", "1", "2 does not match 1");
465 H_CHECK_STREQ_MSG(2_2, "2", "2", "2 does not match 2");
466 #define CHECK_STREQ_VAR1 "5"
467 #define CHECK_STREQ_VAR2 "9"
468 const char *check_streq_var1 = CHECK_STREQ_VAR1;
469 const char *check_streq_var2 = CHECK_STREQ_VAR2;
470 H_CHECK_STREQ(vars, check_streq_var1, check_streq_var2);
473 ATF_TC_HEAD(check_streq, tc)
475 atf_tc_set_md_var(tc, "descr", "Tests the ATF_CHECK_STREQ and "
476 "ATF_CHECK_STREQ_MSG macros");
478 ATF_TC_BODY(check_streq, tc)
480 struct check_eq_test tests[] = {
481 { H_CHECK_STREQ_HEAD_NAME(1_1), H_CHECK_STREQ_BODY_NAME(1_1),
482 "1", "1", "\"1\" != \"1\" \\(1 != 1\\)", true },
483 { H_CHECK_STREQ_HEAD_NAME(1_2), H_CHECK_STREQ_BODY_NAME(1_2),
484 "1", "2", "\"1\" != \"2\" \\(1 != 2\\)", false },
485 { H_CHECK_STREQ_HEAD_NAME(2_1), H_CHECK_STREQ_BODY_NAME(2_1),
486 "2", "1", "\"2\" != \"1\" \\(2 != 1\\)", false },
487 { H_CHECK_STREQ_HEAD_NAME(2_2), H_CHECK_STREQ_BODY_NAME(2_2),
488 "2", "2", "\"2\" != \"2\" \\(2 != 2\\)", true },
489 { H_CHECK_STREQ_MSG_HEAD_NAME(1_1),
490 H_CHECK_STREQ_MSG_BODY_NAME(1_1),
491 "1", "1", "\"1\" != \"1\" \\(1 != 1\\): 1 does not match 1", true },
492 { H_CHECK_STREQ_MSG_HEAD_NAME(1_2),
493 H_CHECK_STREQ_MSG_BODY_NAME(1_2),
494 "1", "2", "\"1\" != \"2\" \\(1 != 2\\): 1 does not match 2", false },
495 { H_CHECK_STREQ_MSG_HEAD_NAME(2_1),
496 H_CHECK_STREQ_MSG_BODY_NAME(2_1),
497 "2", "1", "\"2\" != \"1\" \\(2 != 1\\): 2 does not match 1", false },
498 { H_CHECK_STREQ_MSG_HEAD_NAME(2_2),
499 H_CHECK_STREQ_MSG_BODY_NAME(2_2),
500 "2", "2", "\"2\" != \"2\" \\(2 != 2\\): 2 does not match 2", true },
501 { H_CHECK_STREQ_HEAD_NAME(vars), H_CHECK_STREQ_BODY_NAME(vars),
502 check_streq_var1, check_streq_var2,
503 "check_streq_var1 != check_streq_var2 \\("
504 CHECK_STREQ_VAR1 " != " CHECK_STREQ_VAR2 "\\)", false },
505 { NULL, NULL, 0, 0, "", false }
507 do_check_eq_tests(tests);
510 /* ---------------------------------------------------------------------
511 * Test cases for the ATF_CHECK_MATCH and ATF_CHECK_MATCH_MSG macros.
512 * --------------------------------------------------------------------- */
514 H_CHECK_MATCH(yes, "hello [a-z]+", "abc hello world");
515 H_CHECK_MATCH(no, "hello [a-z]+", "abc hello WORLD");
516 H_CHECK_MATCH_MSG(yes, "hello [a-z]+", "abc hello world", "lowercase");
517 H_CHECK_MATCH_MSG(no, "hello [a-z]+", "abc hello WORLD", "uppercase");
520 ATF_TC_HEAD(check_match, tc)
522 atf_tc_set_md_var(tc, "descr", "Tests the ATF_CHECK_MATCH and "
523 "ATF_CHECK_MATCH_MSG macros");
525 ATF_TC_BODY(check_match, tc)
527 struct check_eq_test tests[] = {
528 { H_CHECK_MATCH_HEAD_NAME(yes), H_CHECK_MATCH_BODY_NAME(yes),
529 "hello [a-z]+", "abc hello world", "", true },
530 { H_CHECK_MATCH_HEAD_NAME(no), H_CHECK_MATCH_BODY_NAME(no),
531 "hello [a-z]+", "abc hello WORLD",
532 "'hello \\[a-z\\]\\+' not matched in 'abc hello WORLD'", false },
533 { H_CHECK_MATCH_MSG_HEAD_NAME(yes), H_CHECK_MATCH_MSG_BODY_NAME(yes),
534 "hello [a-z]+", "abc hello world", "", true },
535 { H_CHECK_MATCH_MSG_HEAD_NAME(no), H_CHECK_MATCH_MSG_BODY_NAME(no),
536 "hello [a-z]+", "abc hello WORLD",
537 "'hello \\[a-z\\]\\+' not matched in 'abc hello WORLD': uppercase",
539 { NULL, NULL, 0, 0, "", false }
541 do_check_eq_tests(tests);
544 /* ---------------------------------------------------------------------
545 * Test cases for the ATF_REQUIRE and ATF_REQUIRE_MSG macros.
546 * --------------------------------------------------------------------- */
550 H_REQUIRE_MSG(0, 0, "expected a false value");
551 H_REQUIRE_MSG(1, 1, "expected a true value");
554 ATF_TC_HEAD(require, tc)
556 atf_tc_set_md_var(tc, "descr", "Tests the ATF_REQUIRE and "
557 "ATF_REQUIRE_MSG macros");
559 ATF_TC_BODY(require, tc)
562 void (*head)(atf_tc_t *);
563 void (*body)(const atf_tc_t *);
568 { H_REQUIRE_HEAD_NAME(0), H_REQUIRE_BODY_NAME(0), 0,
569 "0 not met", false },
570 { H_REQUIRE_HEAD_NAME(1), H_REQUIRE_BODY_NAME(1), 1,
572 { H_REQUIRE_MSG_HEAD_NAME(0), H_REQUIRE_MSG_BODY_NAME(0), 0,
573 "expected a false value", false },
574 { H_REQUIRE_MSG_HEAD_NAME(1), H_REQUIRE_MSG_BODY_NAME(1), 1,
575 "expected a true value", true },
576 { NULL, NULL, false, NULL, false }
579 for (t = &tests[0]; t->head != NULL; t++) {
580 printf("Checking with a %d value\n", t->value);
582 init_and_run_h_tc("h_require", t->head, t->body);
584 ATF_REQUIRE(exists("before"));
586 ATF_REQUIRE(atf_utils_grep_file("^passed", "result"));
587 ATF_REQUIRE(exists("after"));
589 ATF_REQUIRE(atf_utils_grep_file(
590 "^failed: .*macros_test.c:[0-9]+: %s$", "result", t->msg));
591 ATF_REQUIRE(!exists("after"));
594 ATF_REQUIRE(unlink("before") != -1);
596 ATF_REQUIRE(unlink("after") != -1);
600 /* ---------------------------------------------------------------------
601 * Test cases for the ATF_REQUIRE_*EQ_ macros.
602 * --------------------------------------------------------------------- */
604 struct require_eq_test {
605 void (*head)(atf_tc_t *);
606 void (*body)(const atf_tc_t *);
615 do_require_eq_tests(const struct require_eq_test *tests)
617 const struct require_eq_test *t;
619 for (t = &tests[0]; t->head != NULL; t++) {
620 printf("Checking with %s, %s and expecting %s\n", t->v1, t->v2,
621 t->ok ? "true" : "false");
623 init_and_run_h_tc("h_require", t->head, t->body);
625 ATF_REQUIRE(exists("before"));
627 ATF_REQUIRE(atf_utils_grep_file("^passed", "result"));
628 ATF_REQUIRE(exists("after"));
630 ATF_REQUIRE(atf_utils_grep_file("^failed: .*macros_test.c"
631 ":[0-9]+: %s$", "result", t->msg));
632 ATF_REQUIRE(!exists("after"));
635 ATF_REQUIRE(unlink("before") != -1);
637 ATF_REQUIRE(unlink("after") != -1);
641 H_REQUIRE_EQ(1_1, 1, 1);
642 H_REQUIRE_EQ(1_2, 1, 2);
643 H_REQUIRE_EQ(2_1, 2, 1);
644 H_REQUIRE_EQ(2_2, 2, 2);
645 H_REQUIRE_EQ_MSG(1_1, 1, 1, "1 does not match 1");
646 H_REQUIRE_EQ_MSG(1_2, 1, 2, "1 does not match 2");
647 H_REQUIRE_EQ_MSG(2_1, 2, 1, "2 does not match 1");
648 H_REQUIRE_EQ_MSG(2_2, 2, 2, "2 does not match 2");
651 ATF_TC_HEAD(require_eq, tc)
653 atf_tc_set_md_var(tc, "descr", "Tests the ATF_REQUIRE_EQ and "
654 "ATF_REQUIRE_EQ_MSG macros");
656 ATF_TC_BODY(require_eq, tc)
658 struct require_eq_test tests[] = {
659 { H_REQUIRE_EQ_HEAD_NAME(1_1), H_REQUIRE_EQ_BODY_NAME(1_1),
660 "1", "1", "1 != 1", true },
661 { H_REQUIRE_EQ_HEAD_NAME(1_2), H_REQUIRE_EQ_BODY_NAME(1_2),
662 "1", "2", "1 != 2", false },
663 { H_REQUIRE_EQ_HEAD_NAME(2_1), H_REQUIRE_EQ_BODY_NAME(2_1),
664 "2", "1", "2 != 1", false },
665 { H_REQUIRE_EQ_HEAD_NAME(2_2), H_REQUIRE_EQ_BODY_NAME(2_2),
666 "2", "2", "2 != 2", true },
667 { H_REQUIRE_EQ_MSG_HEAD_NAME(1_1), H_REQUIRE_EQ_MSG_BODY_NAME(1_1),
668 "1", "1", "1 != 1: 1 does not match 1", true },
669 { H_REQUIRE_EQ_MSG_HEAD_NAME(1_2), H_REQUIRE_EQ_MSG_BODY_NAME(1_2),
670 "1", "2", "1 != 2: 1 does not match 2", false },
671 { H_REQUIRE_EQ_MSG_HEAD_NAME(2_1), H_REQUIRE_EQ_MSG_BODY_NAME(2_1),
672 "2", "1", "2 != 1: 2 does not match 1", false },
673 { H_REQUIRE_EQ_MSG_HEAD_NAME(2_2), H_REQUIRE_EQ_MSG_BODY_NAME(2_2),
674 "2", "2", "2 != 2: 2 does not match 2", true },
675 { NULL, NULL, 0, 0, "", false }
677 do_require_eq_tests(tests);
680 H_REQUIRE_STREQ(1_1, "1", "1");
681 H_REQUIRE_STREQ(1_2, "1", "2");
682 H_REQUIRE_STREQ(2_1, "2", "1");
683 H_REQUIRE_STREQ(2_2, "2", "2");
684 H_REQUIRE_STREQ_MSG(1_1, "1", "1", "1 does not match 1");
685 H_REQUIRE_STREQ_MSG(1_2, "1", "2", "1 does not match 2");
686 H_REQUIRE_STREQ_MSG(2_1, "2", "1", "2 does not match 1");
687 H_REQUIRE_STREQ_MSG(2_2, "2", "2", "2 does not match 2");
688 #define REQUIRE_STREQ_VAR1 "5"
689 #define REQUIRE_STREQ_VAR2 "9"
690 const char *require_streq_var1 = REQUIRE_STREQ_VAR1;
691 const char *require_streq_var2 = REQUIRE_STREQ_VAR2;
692 H_REQUIRE_STREQ(vars, require_streq_var1, require_streq_var2);
694 ATF_TC(require_streq);
695 ATF_TC_HEAD(require_streq, tc)
697 atf_tc_set_md_var(tc, "descr", "Tests the ATF_REQUIRE_STREQ and "
698 "ATF_REQUIRE_STREQ_MSG macros");
700 ATF_TC_BODY(require_streq, tc)
702 struct require_eq_test tests[] = {
703 { H_REQUIRE_STREQ_HEAD_NAME(1_1), H_REQUIRE_STREQ_BODY_NAME(1_1),
704 "1", "1", "\"1\" != \"1\" \\(1 != 1\\)", true },
705 { H_REQUIRE_STREQ_HEAD_NAME(1_2), H_REQUIRE_STREQ_BODY_NAME(1_2),
706 "1", "2", "\"1\" != \"2\" \\(1 != 2\\)", false },
707 { H_REQUIRE_STREQ_HEAD_NAME(2_1), H_REQUIRE_STREQ_BODY_NAME(2_1),
708 "2", "1", "\"2\" != \"1\" \\(2 != 1\\)", false },
709 { H_REQUIRE_STREQ_HEAD_NAME(2_2), H_REQUIRE_STREQ_BODY_NAME(2_2),
710 "2", "2", "\"2\" != \"2\" \\(2 != 2\\)", true },
711 { H_REQUIRE_STREQ_MSG_HEAD_NAME(1_1),
712 H_REQUIRE_STREQ_MSG_BODY_NAME(1_1),
713 "1", "1", "\"1\" != \"1\" \\(1 != 1\\): 1 does not match 1", true },
714 { H_REQUIRE_STREQ_MSG_HEAD_NAME(1_2),
715 H_REQUIRE_STREQ_MSG_BODY_NAME(1_2),
716 "1", "2", "\"1\" != \"2\" \\(1 != 2\\): 1 does not match 2", false },
717 { H_REQUIRE_STREQ_MSG_HEAD_NAME(2_1),
718 H_REQUIRE_STREQ_MSG_BODY_NAME(2_1),
719 "2", "1", "\"2\" != \"1\" \\(2 != 1\\): 2 does not match 1", false },
720 { H_REQUIRE_STREQ_MSG_HEAD_NAME(2_2),
721 H_REQUIRE_STREQ_MSG_BODY_NAME(2_2),
722 "2", "2", "\"2\" != \"2\" \\(2 != 2\\): 2 does not match 2", true },
723 { H_REQUIRE_STREQ_HEAD_NAME(vars), H_REQUIRE_STREQ_BODY_NAME(vars),
724 require_streq_var1, require_streq_var2,
725 "require_streq_var1 != require_streq_var2 \\("
726 REQUIRE_STREQ_VAR1 " != " REQUIRE_STREQ_VAR2 "\\)", false },
727 { NULL, NULL, 0, 0, "", false }
729 do_require_eq_tests(tests);
732 /* ---------------------------------------------------------------------
733 * Test cases for the ATF_REQUIRE_MATCH and ATF_REQUIRE_MATCH_MSG macros.
734 * --------------------------------------------------------------------- */
736 H_REQUIRE_MATCH(yes, "hello [a-z]+", "abc hello world");
737 H_REQUIRE_MATCH(no, "hello [a-z]+", "abc hello WORLD");
738 H_REQUIRE_MATCH_MSG(yes, "hello [a-z]+", "abc hello world", "lowercase");
739 H_REQUIRE_MATCH_MSG(no, "hello [a-z]+", "abc hello WORLD", "uppercase");
741 ATF_TC(require_match);
742 ATF_TC_HEAD(require_match, tc)
744 atf_tc_set_md_var(tc, "descr", "Tests the ATF_REQUIRE_MATCH and "
745 "ATF_REQUIRE_MATCH_MSG macros");
747 ATF_TC_BODY(require_match, tc)
749 struct require_eq_test tests[] = {
750 { H_REQUIRE_MATCH_HEAD_NAME(yes), H_REQUIRE_MATCH_BODY_NAME(yes),
751 "hello [a-z]+", "abc hello world", "", true },
752 { H_REQUIRE_MATCH_HEAD_NAME(no), H_REQUIRE_MATCH_BODY_NAME(no),
753 "hello [a-z]+", "abc hello WORLD",
754 "'hello \\[a-z\\]\\+' not matched in 'abc hello WORLD'", false },
755 { H_REQUIRE_MATCH_MSG_HEAD_NAME(yes),
756 H_REQUIRE_MATCH_MSG_BODY_NAME(yes),
757 "hello [a-z]+", "abc hello world", "", true },
758 { H_REQUIRE_MATCH_MSG_HEAD_NAME(no), H_REQUIRE_MATCH_MSG_BODY_NAME(no),
759 "hello [a-z]+", "abc hello WORLD",
760 "'hello \\[a-z\\]\\+' not matched in 'abc hello WORLD': uppercase",
762 { NULL, NULL, 0, 0, "", false }
764 do_require_eq_tests(tests);
767 /* ---------------------------------------------------------------------
768 * Miscellaneous test cases covering several macros.
769 * --------------------------------------------------------------------- */
773 aux_bool(const char *fmt ATF_DEFS_ATTRIBUTE_UNUSED)
780 aux_str(const char *fmt ATF_DEFS_ATTRIBUTE_UNUSED)
785 H_CHECK(msg, aux_bool("%d"));
786 H_REQUIRE(msg, aux_bool("%d"));
787 H_CHECK_STREQ(msg, aux_str("%d"), "");
788 H_REQUIRE_STREQ(msg, aux_str("%d"), "");
790 ATF_TC(msg_embedded_fmt);
791 ATF_TC_HEAD(msg_embedded_fmt, tc)
793 atf_tc_set_md_var(tc, "descr", "Tests that format strings passed "
794 "as part of the automatically-generated messages "
795 "do not get expanded");
797 ATF_TC_BODY(msg_embedded_fmt, tc)
800 void (*head)(atf_tc_t *);
801 void (*body)(const atf_tc_t *);
805 { H_CHECK_HEAD_NAME(msg), H_CHECK_BODY_NAME(msg), false,
806 "aux_bool\\(\"%d\"\\) not met" },
807 { H_REQUIRE_HEAD_NAME(msg), H_REQUIRE_BODY_NAME(msg), true,
808 "aux_bool\\(\"%d\"\\) not met" },
809 { H_CHECK_STREQ_HEAD_NAME(msg), H_CHECK_STREQ_BODY_NAME(msg), false,
810 "aux_str\\(\"%d\"\\) != \"\" \\(foo != \\)" },
811 { H_REQUIRE_STREQ_HEAD_NAME(msg), H_REQUIRE_STREQ_BODY_NAME(msg), true,
812 "aux_str\\(\"%d\"\\) != \"\" \\(foo != \\)" },
813 { NULL, NULL, false, NULL }
816 for (t = &tests[0]; t->head != NULL; t++) {
817 printf("Checking with an expected '%s' message\n", t->msg);
819 init_and_run_h_tc("h_check", t->head, t->body);
824 "^failed: .*macros_test.c:[0-9]+: %s$", "result", t->msg);
825 ATF_CHECK_MSG(matched, "couldn't find error string in result");
827 bool matched = atf_utils_grep_file("Check failed: .*"
828 "macros_test.c:[0-9]+: %s$", "error", t->msg);
829 ATF_CHECK_MSG(matched, "couldn't find error string in output");
834 /* ---------------------------------------------------------------------
835 * Tests cases for the header file.
836 * --------------------------------------------------------------------- */
838 BUILD_TC(use, "macros_h_test.c",
839 "Tests that the macros provided by the atf-c/macros.h file "
840 "do not cause syntax errors when used",
841 "Build of macros_h_test.c failed; some macros in atf-c/macros.h "
844 ATF_TC(detect_unused_tests);
845 ATF_TC_HEAD(detect_unused_tests, tc)
847 atf_tc_set_md_var(tc, "descr",
848 "Tests that defining an unused test case raises a "
849 "warning (and thus an error)");
851 ATF_TC_BODY(detect_unused_tests, tc)
853 const char* validate_compiler =
854 "struct test_struct { int dummy; };\n"
855 "#define define_unused static struct test_struct unused\n"
858 atf_utils_create_file("compiler_test.c", "%s", validate_compiler);
859 if (build_check_c_o("compiler_test.c"))
860 atf_tc_expect_fail("Compiler does not raise a warning on an unused "
861 "static global variable declared by a macro");
863 if (build_check_c_o_srcdir(tc, "unused_test.c"))
864 atf_tc_fail("Build of unused_test.c passed; unused test cases are "
865 "not properly detected");
868 /* ---------------------------------------------------------------------
870 * --------------------------------------------------------------------- */
874 ATF_TP_ADD_TC(tp, check);
875 ATF_TP_ADD_TC(tp, check_eq);
876 ATF_TP_ADD_TC(tp, check_streq);
877 ATF_TP_ADD_TC(tp, check_errno);
878 ATF_TP_ADD_TC(tp, check_match);
880 ATF_TP_ADD_TC(tp, require);
881 ATF_TP_ADD_TC(tp, require_eq);
882 ATF_TP_ADD_TC(tp, require_streq);
883 ATF_TP_ADD_TC(tp, require_errno);
884 ATF_TP_ADD_TC(tp, require_match);
886 ATF_TP_ADD_TC(tp, msg_embedded_fmt);
888 /* Add the test cases for the header file. */
889 ATF_TP_ADD_TC(tp, use);
890 ATF_TP_ADD_TC(tp, detect_unused_tests);
892 return atf_no_error();