xref: /minix3/external/bsd/atf/dist/atf-c/macros_test.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
111be35a1SLionel Sambuc /*
211be35a1SLionel Sambuc  * Automated Testing Framework (atf)
311be35a1SLionel Sambuc  *
411be35a1SLionel Sambuc  * Copyright (c) 2008 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc  * are met:
1011be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc  *
1611be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1711be35a1SLionel Sambuc  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1811be35a1SLionel Sambuc  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1911be35a1SLionel Sambuc  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2011be35a1SLionel Sambuc  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
2111be35a1SLionel Sambuc  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2211be35a1SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2311be35a1SLionel Sambuc  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2411be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2511be35a1SLionel Sambuc  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2611be35a1SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2711be35a1SLionel Sambuc  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc  */
2911be35a1SLionel Sambuc 
3011be35a1SLionel Sambuc #include <sys/types.h>
3111be35a1SLionel Sambuc #include <sys/wait.h>
3211be35a1SLionel Sambuc 
3311be35a1SLionel Sambuc #include <errno.h>
3411be35a1SLionel Sambuc #include <fcntl.h>
3511be35a1SLionel Sambuc #include <stdarg.h>
3611be35a1SLionel Sambuc #include <stdbool.h>
3711be35a1SLionel Sambuc #include <stdio.h>
3811be35a1SLionel Sambuc #include <stdlib.h>
3911be35a1SLionel Sambuc #include <string.h>
4011be35a1SLionel Sambuc #include <unistd.h>
4111be35a1SLionel Sambuc 
4211be35a1SLionel Sambuc #include <atf-c.h>
4311be35a1SLionel Sambuc 
4411be35a1SLionel Sambuc #include "detail/fs.h"
4511be35a1SLionel Sambuc #include "detail/process.h"
4611be35a1SLionel Sambuc #include "detail/test_helpers.h"
4711be35a1SLionel Sambuc #include "detail/text.h"
4811be35a1SLionel Sambuc 
4911be35a1SLionel Sambuc /* ---------------------------------------------------------------------
5011be35a1SLionel Sambuc  * Auxiliary functions.
5111be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
5211be35a1SLionel Sambuc 
5311be35a1SLionel Sambuc static
5411be35a1SLionel Sambuc void
create_ctl_file(const char * name)5511be35a1SLionel Sambuc create_ctl_file(const char *name)
5611be35a1SLionel Sambuc {
5711be35a1SLionel Sambuc     atf_fs_path_t p;
5811be35a1SLionel Sambuc 
5911be35a1SLionel Sambuc     RE(atf_fs_path_init_fmt(&p, "%s", name));
6011be35a1SLionel Sambuc     ATF_REQUIRE(open(atf_fs_path_cstring(&p),
6111be35a1SLionel Sambuc                    O_CREAT | O_WRONLY | O_TRUNC, 0644) != -1);
6211be35a1SLionel Sambuc     atf_fs_path_fini(&p);
6311be35a1SLionel Sambuc }
6411be35a1SLionel Sambuc 
6511be35a1SLionel Sambuc static
6611be35a1SLionel Sambuc bool
exists(const char * p)6711be35a1SLionel Sambuc exists(const char *p)
6811be35a1SLionel Sambuc {
6911be35a1SLionel Sambuc     bool b;
7011be35a1SLionel Sambuc     atf_fs_path_t pp;
7111be35a1SLionel Sambuc 
7211be35a1SLionel Sambuc     RE(atf_fs_path_init_fmt(&pp, "%s", p));
7311be35a1SLionel Sambuc     RE(atf_fs_exists(&pp, &b));
7411be35a1SLionel Sambuc     atf_fs_path_fini(&pp);
7511be35a1SLionel Sambuc 
7611be35a1SLionel Sambuc     return b;
7711be35a1SLionel Sambuc }
7811be35a1SLionel Sambuc 
7911be35a1SLionel Sambuc static
8011be35a1SLionel Sambuc void
init_and_run_h_tc(const char * name,void (* head)(atf_tc_t *),void (* body)(const atf_tc_t *))8111be35a1SLionel Sambuc init_and_run_h_tc(const char *name, void (*head)(atf_tc_t *),
8211be35a1SLionel Sambuc                   void (*body)(const atf_tc_t *))
8311be35a1SLionel Sambuc {
8411be35a1SLionel Sambuc     atf_tc_t tc;
8511be35a1SLionel Sambuc     const char *const config[] = { NULL };
8611be35a1SLionel Sambuc 
8711be35a1SLionel Sambuc     RE(atf_tc_init(&tc, name, head, body, NULL, config));
8811be35a1SLionel Sambuc     run_h_tc(&tc, "output", "error", "result");
8911be35a1SLionel Sambuc     atf_tc_fini(&tc);
9011be35a1SLionel Sambuc }
9111be35a1SLionel Sambuc 
9211be35a1SLionel Sambuc /* ---------------------------------------------------------------------
9311be35a1SLionel Sambuc  * Helper test cases.
9411be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
9511be35a1SLionel Sambuc 
9611be35a1SLionel Sambuc #define H_DEF(id, macro) \
9711be35a1SLionel Sambuc     ATF_TC_HEAD(h_ ## id, tc) \
9811be35a1SLionel Sambuc     { \
9911be35a1SLionel Sambuc         atf_tc_set_md_var(tc, "descr", "Helper test case"); \
10011be35a1SLionel Sambuc     } \
10111be35a1SLionel Sambuc     ATF_TC_BODY(h_ ## id, tc) \
10211be35a1SLionel Sambuc     { \
10311be35a1SLionel Sambuc         create_ctl_file("before"); \
10411be35a1SLionel Sambuc         macro; \
10511be35a1SLionel Sambuc         create_ctl_file("after"); \
10611be35a1SLionel Sambuc     }
10711be35a1SLionel Sambuc 
10811be35a1SLionel Sambuc #define H_CHECK_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_check_ ## id)
10911be35a1SLionel Sambuc #define H_CHECK_BODY_NAME(id) ATF_TC_BODY_NAME(h_check_ ## id)
11011be35a1SLionel Sambuc #define H_CHECK(id, condition) \
11111be35a1SLionel Sambuc     H_DEF(check_ ## id, ATF_CHECK(condition))
11211be35a1SLionel Sambuc 
11311be35a1SLionel Sambuc #define H_CHECK_MSG_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_check_msg_ ## id)
11411be35a1SLionel Sambuc #define H_CHECK_MSG_BODY_NAME(id) ATF_TC_BODY_NAME(h_check_msg_ ## id)
11511be35a1SLionel Sambuc #define H_CHECK_MSG(id, condition, msg) \
11611be35a1SLionel Sambuc     H_DEF(check_msg_ ## id, ATF_CHECK_MSG(condition, msg))
11711be35a1SLionel Sambuc 
11811be35a1SLionel Sambuc #define H_CHECK_EQ_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_check_eq_ ## id)
11911be35a1SLionel Sambuc #define H_CHECK_EQ_BODY_NAME(id) ATF_TC_BODY_NAME(h_check_eq_ ## id)
12011be35a1SLionel Sambuc #define H_CHECK_EQ(id, v1, v2) \
12111be35a1SLionel Sambuc     H_DEF(check_eq_ ## id, ATF_CHECK_EQ(v1, v2))
12211be35a1SLionel Sambuc 
12311be35a1SLionel Sambuc #define H_CHECK_STREQ_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_check_streq_ ## id)
12411be35a1SLionel Sambuc #define H_CHECK_STREQ_BODY_NAME(id) ATF_TC_BODY_NAME(h_check_streq_ ## id)
12511be35a1SLionel Sambuc #define H_CHECK_STREQ(id, v1, v2) \
12611be35a1SLionel Sambuc     H_DEF(check_streq_ ## id, ATF_CHECK_STREQ(v1, v2))
12711be35a1SLionel Sambuc 
12811be35a1SLionel Sambuc #define H_CHECK_MATCH_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_check_match_ ## id)
12911be35a1SLionel Sambuc #define H_CHECK_MATCH_BODY_NAME(id) ATF_TC_BODY_NAME(h_check_match_ ## id)
13011be35a1SLionel Sambuc #define H_CHECK_MATCH(id, v1, v2) \
13111be35a1SLionel Sambuc     H_DEF(check_match_ ## id, ATF_CHECK_MATCH(v1, v2))
13211be35a1SLionel Sambuc 
13311be35a1SLionel Sambuc #define H_CHECK_EQ_MSG_HEAD_NAME(id) \
13411be35a1SLionel Sambuc     ATF_TC_HEAD_NAME(h_check_eq_msg_ ## id)
13511be35a1SLionel Sambuc #define H_CHECK_EQ_MSG_BODY_NAME(id) \
13611be35a1SLionel Sambuc     ATF_TC_BODY_NAME(h_check_eq_msg_ ## id)
13711be35a1SLionel Sambuc #define H_CHECK_EQ_MSG(id, v1, v2, msg) \
13811be35a1SLionel Sambuc     H_DEF(check_eq_msg_ ## id, ATF_CHECK_EQ_MSG(v1, v2, msg))
13911be35a1SLionel Sambuc 
14011be35a1SLionel Sambuc #define H_CHECK_STREQ_MSG_HEAD_NAME(id) \
14111be35a1SLionel Sambuc     ATF_TC_HEAD_NAME(h_check_streq_msg_ ## id)
14211be35a1SLionel Sambuc #define H_CHECK_STREQ_MSG_BODY_NAME(id) \
14311be35a1SLionel Sambuc     ATF_TC_BODY_NAME(h_check_streq_msg_ ## id)
14411be35a1SLionel Sambuc #define H_CHECK_STREQ_MSG(id, v1, v2, msg) \
14511be35a1SLionel Sambuc     H_DEF(check_streq_msg_ ## id, ATF_CHECK_STREQ_MSG(v1, v2, msg))
14611be35a1SLionel Sambuc 
14711be35a1SLionel Sambuc #define H_CHECK_MATCH_MSG_HEAD_NAME(id) \
14811be35a1SLionel Sambuc     ATF_TC_HEAD_NAME(h_check_match_msg_ ## id)
14911be35a1SLionel Sambuc #define H_CHECK_MATCH_MSG_BODY_NAME(id) \
15011be35a1SLionel Sambuc     ATF_TC_BODY_NAME(h_check_match_msg_ ## id)
15111be35a1SLionel Sambuc #define H_CHECK_MATCH_MSG(id, v1, v2, msg) \
15211be35a1SLionel Sambuc     H_DEF(check_match_msg_ ## id, ATF_CHECK_MATCH_MSG(v1, v2, msg))
15311be35a1SLionel Sambuc 
15411be35a1SLionel Sambuc #define H_CHECK_ERRNO_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_check_errno_ ## id)
15511be35a1SLionel Sambuc #define H_CHECK_ERRNO_BODY_NAME(id) ATF_TC_BODY_NAME(h_check_errno_ ## id)
15611be35a1SLionel Sambuc #define H_CHECK_ERRNO(id, exp_errno, bool_expr) \
15711be35a1SLionel Sambuc     H_DEF(check_errno_ ## id, ATF_CHECK_ERRNO(exp_errno, bool_expr))
15811be35a1SLionel Sambuc 
15911be35a1SLionel Sambuc #define H_REQUIRE_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_require_ ## id)
16011be35a1SLionel Sambuc #define H_REQUIRE_BODY_NAME(id) ATF_TC_BODY_NAME(h_require_ ## id)
16111be35a1SLionel Sambuc #define H_REQUIRE(id, condition) \
16211be35a1SLionel Sambuc     H_DEF(require_ ## id, ATF_REQUIRE(condition))
16311be35a1SLionel Sambuc 
16411be35a1SLionel Sambuc #define H_REQUIRE_MSG_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_require_msg_ ## id)
16511be35a1SLionel Sambuc #define H_REQUIRE_MSG_BODY_NAME(id) ATF_TC_BODY_NAME(h_require_msg_ ## id)
16611be35a1SLionel Sambuc #define H_REQUIRE_MSG(id, condition, msg) \
16711be35a1SLionel Sambuc     H_DEF(require_msg_ ## id, ATF_REQUIRE_MSG(condition, msg))
16811be35a1SLionel Sambuc 
16911be35a1SLionel Sambuc #define H_REQUIRE_EQ_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_require_eq_ ## id)
17011be35a1SLionel Sambuc #define H_REQUIRE_EQ_BODY_NAME(id) ATF_TC_BODY_NAME(h_require_eq_ ## id)
17111be35a1SLionel Sambuc #define H_REQUIRE_EQ(id, v1, v2) \
17211be35a1SLionel Sambuc     H_DEF(require_eq_ ## id, ATF_REQUIRE_EQ(v1, v2))
17311be35a1SLionel Sambuc 
17411be35a1SLionel Sambuc #define H_REQUIRE_STREQ_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_require_streq_ ## id)
17511be35a1SLionel Sambuc #define H_REQUIRE_STREQ_BODY_NAME(id) ATF_TC_BODY_NAME(h_require_streq_ ## id)
17611be35a1SLionel Sambuc #define H_REQUIRE_STREQ(id, v1, v2) \
17711be35a1SLionel Sambuc     H_DEF(require_streq_ ## id, ATF_REQUIRE_STREQ(v1, v2))
17811be35a1SLionel Sambuc 
17911be35a1SLionel Sambuc #define H_REQUIRE_MATCH_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_require_match_ ## id)
18011be35a1SLionel Sambuc #define H_REQUIRE_MATCH_BODY_NAME(id) ATF_TC_BODY_NAME(h_require_match_ ## id)
18111be35a1SLionel Sambuc #define H_REQUIRE_MATCH(id, v1, v2) \
18211be35a1SLionel Sambuc     H_DEF(require_match_ ## id, ATF_REQUIRE_MATCH(v1, v2))
18311be35a1SLionel Sambuc 
18411be35a1SLionel Sambuc #define H_REQUIRE_EQ_MSG_HEAD_NAME(id) \
18511be35a1SLionel Sambuc     ATF_TC_HEAD_NAME(h_require_eq_msg_ ## id)
18611be35a1SLionel Sambuc #define H_REQUIRE_EQ_MSG_BODY_NAME(id) \
18711be35a1SLionel Sambuc     ATF_TC_BODY_NAME(h_require_eq_msg_ ## id)
18811be35a1SLionel Sambuc #define H_REQUIRE_EQ_MSG(id, v1, v2, msg) \
18911be35a1SLionel Sambuc     H_DEF(require_eq_msg_ ## id, ATF_REQUIRE_EQ_MSG(v1, v2, msg))
19011be35a1SLionel Sambuc 
19111be35a1SLionel Sambuc #define H_REQUIRE_STREQ_MSG_HEAD_NAME(id) \
19211be35a1SLionel Sambuc     ATF_TC_HEAD_NAME(h_require_streq_msg_ ## id)
19311be35a1SLionel Sambuc #define H_REQUIRE_STREQ_MSG_BODY_NAME(id) \
19411be35a1SLionel Sambuc     ATF_TC_BODY_NAME(h_require_streq_msg_ ## id)
19511be35a1SLionel Sambuc #define H_REQUIRE_STREQ_MSG(id, v1, v2, msg) \
19611be35a1SLionel Sambuc     H_DEF(require_streq_msg_ ## id, ATF_REQUIRE_STREQ_MSG(v1, v2, msg))
19711be35a1SLionel Sambuc 
19811be35a1SLionel Sambuc #define H_REQUIRE_MATCH_MSG_HEAD_NAME(id) \
19911be35a1SLionel Sambuc     ATF_TC_HEAD_NAME(h_require_match_msg_ ## id)
20011be35a1SLionel Sambuc #define H_REQUIRE_MATCH_MSG_BODY_NAME(id) \
20111be35a1SLionel Sambuc     ATF_TC_BODY_NAME(h_require_match_msg_ ## id)
20211be35a1SLionel Sambuc #define H_REQUIRE_MATCH_MSG(id, v1, v2, msg) \
20311be35a1SLionel Sambuc     H_DEF(require_match_msg_ ## id, ATF_REQUIRE_MATCH_MSG(v1, v2, msg))
20411be35a1SLionel Sambuc 
20511be35a1SLionel Sambuc #define H_REQUIRE_ERRNO_HEAD_NAME(id) ATF_TC_HEAD_NAME(h_require_errno_ ## id)
20611be35a1SLionel Sambuc #define H_REQUIRE_ERRNO_BODY_NAME(id) ATF_TC_BODY_NAME(h_require_errno_ ## id)
20711be35a1SLionel Sambuc #define H_REQUIRE_ERRNO(id, exp_errno, bool_expr) \
20811be35a1SLionel Sambuc     H_DEF(require_errno_ ## id, ATF_REQUIRE_ERRNO(exp_errno, bool_expr))
20911be35a1SLionel Sambuc 
21011be35a1SLionel Sambuc /* ---------------------------------------------------------------------
21111be35a1SLionel Sambuc  * Test cases for the ATF_{CHECK,REQUIRE}_ERRNO macros.
21211be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
21311be35a1SLionel Sambuc 
21411be35a1SLionel Sambuc static int
errno_fail_stub(const int raised_errno)21511be35a1SLionel Sambuc errno_fail_stub(const int raised_errno)
21611be35a1SLionel Sambuc {
21711be35a1SLionel Sambuc     errno = raised_errno;
21811be35a1SLionel Sambuc     return -1;
21911be35a1SLionel Sambuc }
22011be35a1SLionel Sambuc 
22111be35a1SLionel Sambuc static int
errno_ok_stub(void)22211be35a1SLionel Sambuc errno_ok_stub(void)
22311be35a1SLionel Sambuc {
22411be35a1SLionel Sambuc     return 0;
22511be35a1SLionel Sambuc }
22611be35a1SLionel Sambuc 
22711be35a1SLionel Sambuc H_CHECK_ERRNO(no_error, -1, errno_ok_stub() == -1);
22811be35a1SLionel Sambuc H_CHECK_ERRNO(errno_ok, 2, errno_fail_stub(2) == -1);
22911be35a1SLionel Sambuc H_CHECK_ERRNO(errno_fail, 3, errno_fail_stub(4) == -1);
23011be35a1SLionel Sambuc 
23111be35a1SLionel Sambuc H_REQUIRE_ERRNO(no_error, -1, errno_ok_stub() == -1);
23211be35a1SLionel Sambuc H_REQUIRE_ERRNO(errno_ok, 2, errno_fail_stub(2) == -1);
23311be35a1SLionel Sambuc H_REQUIRE_ERRNO(errno_fail, 3, errno_fail_stub(4) == -1);
23411be35a1SLionel Sambuc 
23511be35a1SLionel Sambuc ATF_TC(check_errno);
ATF_TC_HEAD(check_errno,tc)23611be35a1SLionel Sambuc ATF_TC_HEAD(check_errno, tc)
23711be35a1SLionel Sambuc {
23811be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the ATF_CHECK_ERRNO macro");
23911be35a1SLionel Sambuc }
ATF_TC_BODY(check_errno,tc)24011be35a1SLionel Sambuc ATF_TC_BODY(check_errno, tc)
24111be35a1SLionel Sambuc {
24211be35a1SLionel Sambuc     struct test {
24311be35a1SLionel Sambuc         void (*head)(atf_tc_t *);
24411be35a1SLionel Sambuc         void (*body)(const atf_tc_t *);
24511be35a1SLionel Sambuc         bool ok;
24611be35a1SLionel Sambuc         const char *exp_regex;
24711be35a1SLionel Sambuc     } *t, tests[] = {
24811be35a1SLionel Sambuc         { H_CHECK_ERRNO_HEAD_NAME(no_error),
24911be35a1SLionel Sambuc           H_CHECK_ERRNO_BODY_NAME(no_error),
25011be35a1SLionel Sambuc           false, "Expected true value in errno_ok_stub\\(\\) == -1" },
25111be35a1SLionel Sambuc         { H_CHECK_ERRNO_HEAD_NAME(errno_ok),
25211be35a1SLionel Sambuc           H_CHECK_ERRNO_BODY_NAME(errno_ok),
25311be35a1SLionel Sambuc           true, NULL },
25411be35a1SLionel Sambuc         { H_CHECK_ERRNO_HEAD_NAME(errno_fail),
25511be35a1SLionel Sambuc           H_CHECK_ERRNO_BODY_NAME(errno_fail),
25611be35a1SLionel Sambuc           false, "Expected errno 3, got 4, in errno_fail_stub\\(4\\) == -1" },
25711be35a1SLionel Sambuc         { NULL, NULL, false, NULL }
25811be35a1SLionel Sambuc     };
25911be35a1SLionel Sambuc 
26011be35a1SLionel Sambuc     for (t = &tests[0]; t->head != NULL; t++) {
26111be35a1SLionel Sambuc         init_and_run_h_tc("h_check_errno", t->head, t->body);
26211be35a1SLionel Sambuc 
26311be35a1SLionel Sambuc         ATF_REQUIRE(exists("before"));
26411be35a1SLionel Sambuc         ATF_REQUIRE(exists("after"));
26511be35a1SLionel Sambuc 
26611be35a1SLionel Sambuc         if (t->ok) {
26711be35a1SLionel Sambuc             ATF_REQUIRE(atf_utils_grep_file("^passed", "result"));
26811be35a1SLionel Sambuc         } else {
26911be35a1SLionel Sambuc             ATF_REQUIRE(atf_utils_grep_file("^failed", "result"));
27011be35a1SLionel Sambuc             ATF_REQUIRE(atf_utils_grep_file(
27111be35a1SLionel Sambuc                 "macros_test.c:[0-9]+: %s$", "error", t->exp_regex));
27211be35a1SLionel Sambuc         }
27311be35a1SLionel Sambuc 
27411be35a1SLionel Sambuc         ATF_REQUIRE(unlink("before") != -1);
27511be35a1SLionel Sambuc         ATF_REQUIRE(unlink("after") != -1);
27611be35a1SLionel Sambuc     }
27711be35a1SLionel Sambuc }
27811be35a1SLionel Sambuc 
27911be35a1SLionel Sambuc ATF_TC(require_errno);
ATF_TC_HEAD(require_errno,tc)28011be35a1SLionel Sambuc ATF_TC_HEAD(require_errno, tc)
28111be35a1SLionel Sambuc {
28211be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the ATF_REQUIRE_ERRNO macro");
28311be35a1SLionel Sambuc }
ATF_TC_BODY(require_errno,tc)28411be35a1SLionel Sambuc ATF_TC_BODY(require_errno, tc)
28511be35a1SLionel Sambuc {
28611be35a1SLionel Sambuc     struct test {
28711be35a1SLionel Sambuc         void (*head)(atf_tc_t *);
28811be35a1SLionel Sambuc         void (*body)(const atf_tc_t *);
28911be35a1SLionel Sambuc         bool ok;
29011be35a1SLionel Sambuc         const char *exp_regex;
29111be35a1SLionel Sambuc     } *t, tests[] = {
29211be35a1SLionel Sambuc         { H_REQUIRE_ERRNO_HEAD_NAME(no_error),
29311be35a1SLionel Sambuc           H_REQUIRE_ERRNO_BODY_NAME(no_error),
29411be35a1SLionel Sambuc           false, "Expected true value in errno_ok_stub\\(\\) == -1" },
29511be35a1SLionel Sambuc         { H_REQUIRE_ERRNO_HEAD_NAME(errno_ok),
29611be35a1SLionel Sambuc           H_REQUIRE_ERRNO_BODY_NAME(errno_ok),
29711be35a1SLionel Sambuc           true, NULL },
29811be35a1SLionel Sambuc         { H_REQUIRE_ERRNO_HEAD_NAME(errno_fail),
29911be35a1SLionel Sambuc           H_REQUIRE_ERRNO_BODY_NAME(errno_fail),
30011be35a1SLionel Sambuc           false, "Expected errno 3, got 4, in errno_fail_stub\\(4\\) == -1" },
30111be35a1SLionel Sambuc         { NULL, NULL, false, NULL }
30211be35a1SLionel Sambuc     };
30311be35a1SLionel Sambuc 
30411be35a1SLionel Sambuc     for (t = &tests[0]; t->head != NULL; t++) {
30511be35a1SLionel Sambuc         init_and_run_h_tc("h_require_errno", t->head, t->body);
30611be35a1SLionel Sambuc 
30711be35a1SLionel Sambuc         ATF_REQUIRE(exists("before"));
30811be35a1SLionel Sambuc         if (t->ok) {
30911be35a1SLionel Sambuc             ATF_REQUIRE(atf_utils_grep_file("^passed", "result"));
31011be35a1SLionel Sambuc             ATF_REQUIRE(exists("after"));
31111be35a1SLionel Sambuc         } else {
31211be35a1SLionel Sambuc             ATF_REQUIRE(atf_utils_grep_file(
31311be35a1SLionel Sambuc                 "^failed: .*macros_test.c:[0-9]+: %s$", "result",
31411be35a1SLionel Sambuc                 t->exp_regex));
31511be35a1SLionel Sambuc             ATF_REQUIRE(!exists("after"));
31611be35a1SLionel Sambuc         }
31711be35a1SLionel Sambuc 
31811be35a1SLionel Sambuc         ATF_REQUIRE(unlink("before") != -1);
31911be35a1SLionel Sambuc         if (t->ok)
32011be35a1SLionel Sambuc             ATF_REQUIRE(unlink("after") != -1);
32111be35a1SLionel Sambuc     }
32211be35a1SLionel Sambuc }
32311be35a1SLionel Sambuc 
32411be35a1SLionel Sambuc /* ---------------------------------------------------------------------
32511be35a1SLionel Sambuc  * Test cases for the ATF_CHECK and ATF_CHECK_MSG macros.
32611be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
32711be35a1SLionel Sambuc 
32811be35a1SLionel Sambuc H_CHECK(0, 0);
32911be35a1SLionel Sambuc H_CHECK(1, 1);
33011be35a1SLionel Sambuc H_CHECK_MSG(0, 0, "expected a false value");
33111be35a1SLionel Sambuc H_CHECK_MSG(1, 1, "expected a true value");
33211be35a1SLionel Sambuc 
33311be35a1SLionel Sambuc ATF_TC(check);
ATF_TC_HEAD(check,tc)33411be35a1SLionel Sambuc ATF_TC_HEAD(check, tc)
33511be35a1SLionel Sambuc {
33611be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the ATF_CHECK and "
33711be35a1SLionel Sambuc                       "ATF_CHECK_MSG macros");
33811be35a1SLionel Sambuc }
ATF_TC_BODY(check,tc)33911be35a1SLionel Sambuc ATF_TC_BODY(check, tc)
34011be35a1SLionel Sambuc {
34111be35a1SLionel Sambuc     struct test {
34211be35a1SLionel Sambuc         void (*head)(atf_tc_t *);
34311be35a1SLionel Sambuc         void (*body)(const atf_tc_t *);
34411be35a1SLionel Sambuc         bool value;
34511be35a1SLionel Sambuc         const char *msg;
34611be35a1SLionel Sambuc         bool ok;
34711be35a1SLionel Sambuc     } *t, tests[] = {
34811be35a1SLionel Sambuc         { H_CHECK_HEAD_NAME(0), H_CHECK_BODY_NAME(0), 0,
34911be35a1SLionel Sambuc           "0 not met", false },
35011be35a1SLionel Sambuc         { H_CHECK_HEAD_NAME(1), H_CHECK_BODY_NAME(1), 1,
35111be35a1SLionel Sambuc           "1 not met", true },
35211be35a1SLionel Sambuc         { H_CHECK_MSG_HEAD_NAME(0), H_CHECK_MSG_BODY_NAME(0), 0,
35311be35a1SLionel Sambuc           "expected a false value", false },
35411be35a1SLionel Sambuc         { H_CHECK_MSG_HEAD_NAME(1), H_CHECK_MSG_BODY_NAME(1), 1,
35511be35a1SLionel Sambuc           "expected a true value", true },
35611be35a1SLionel Sambuc         { NULL, NULL, false, NULL, false }
35711be35a1SLionel Sambuc     };
35811be35a1SLionel Sambuc 
35911be35a1SLionel Sambuc     for (t = &tests[0]; t->head != NULL; t++) {
36011be35a1SLionel Sambuc         printf("Checking with a %d value\n", t->value);
36111be35a1SLionel Sambuc 
36211be35a1SLionel Sambuc         init_and_run_h_tc("h_check", t->head, t->body);
36311be35a1SLionel Sambuc 
36411be35a1SLionel Sambuc         ATF_REQUIRE(exists("before"));
36511be35a1SLionel Sambuc         ATF_REQUIRE(exists("after"));
36611be35a1SLionel Sambuc 
36711be35a1SLionel Sambuc         if (t->ok) {
36811be35a1SLionel Sambuc             ATF_REQUIRE(atf_utils_grep_file("^passed", "result"));
36911be35a1SLionel Sambuc         } else {
37011be35a1SLionel Sambuc             ATF_REQUIRE(atf_utils_grep_file("^failed", "result"));
37111be35a1SLionel Sambuc             ATF_REQUIRE(atf_utils_grep_file("Check failed: .*"
37211be35a1SLionel Sambuc                 "macros_test.c:[0-9]+: %s$", "error", t->msg));
37311be35a1SLionel Sambuc         }
37411be35a1SLionel Sambuc 
37511be35a1SLionel Sambuc         ATF_REQUIRE(unlink("before") != -1);
37611be35a1SLionel Sambuc         ATF_REQUIRE(unlink("after") != -1);
37711be35a1SLionel Sambuc     }
37811be35a1SLionel Sambuc }
37911be35a1SLionel Sambuc 
38011be35a1SLionel Sambuc /* ---------------------------------------------------------------------
38111be35a1SLionel Sambuc  * Test cases for the ATF_CHECK_*EQ_ macros.
38211be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
38311be35a1SLionel Sambuc 
38411be35a1SLionel Sambuc struct check_eq_test {
38511be35a1SLionel Sambuc     void (*head)(atf_tc_t *);
38611be35a1SLionel Sambuc     void (*body)(const atf_tc_t *);
38711be35a1SLionel Sambuc     const char *v1;
38811be35a1SLionel Sambuc     const char *v2;
38911be35a1SLionel Sambuc     const char *msg;
39011be35a1SLionel Sambuc     bool ok;
39111be35a1SLionel Sambuc };
39211be35a1SLionel Sambuc 
39311be35a1SLionel Sambuc static
39411be35a1SLionel Sambuc void
do_check_eq_tests(const struct check_eq_test * tests)39511be35a1SLionel Sambuc do_check_eq_tests(const struct check_eq_test *tests)
39611be35a1SLionel Sambuc {
39711be35a1SLionel Sambuc     const struct check_eq_test *t;
39811be35a1SLionel Sambuc 
39911be35a1SLionel Sambuc     for (t = &tests[0]; t->head != NULL; t++) {
40011be35a1SLionel Sambuc         printf("Checking with %s, %s and expecting %s\n", t->v1, t->v2,
40111be35a1SLionel Sambuc                t->ok ? "true" : "false");
40211be35a1SLionel Sambuc 
40311be35a1SLionel Sambuc         init_and_run_h_tc("h_check", t->head, t->body);
40411be35a1SLionel Sambuc 
40511be35a1SLionel Sambuc         ATF_CHECK(exists("before"));
40611be35a1SLionel Sambuc         ATF_CHECK(exists("after"));
40711be35a1SLionel Sambuc 
40811be35a1SLionel Sambuc         if (t->ok) {
40911be35a1SLionel Sambuc             ATF_REQUIRE(atf_utils_grep_file("^passed", "result"));
41011be35a1SLionel Sambuc         } else {
41111be35a1SLionel Sambuc             ATF_REQUIRE(atf_utils_grep_file("^failed", "result"));
41211be35a1SLionel Sambuc             ATF_CHECK(atf_utils_grep_file("Check failed: .*"
41311be35a1SLionel Sambuc                 "macros_test.c:[0-9]+: %s$", "error", t->msg));
41411be35a1SLionel Sambuc         }
41511be35a1SLionel Sambuc 
41611be35a1SLionel Sambuc         ATF_CHECK(unlink("before") != -1);
41711be35a1SLionel Sambuc         ATF_CHECK(unlink("after") != -1);
41811be35a1SLionel Sambuc     }
41911be35a1SLionel Sambuc }
42011be35a1SLionel Sambuc 
42111be35a1SLionel Sambuc H_CHECK_EQ(1_1, 1, 1);
42211be35a1SLionel Sambuc H_CHECK_EQ(1_2, 1, 2);
42311be35a1SLionel Sambuc H_CHECK_EQ(2_1, 2, 1);
42411be35a1SLionel Sambuc H_CHECK_EQ(2_2, 2, 2);
42511be35a1SLionel Sambuc H_CHECK_EQ_MSG(1_1, 1, 1, "1 does not match 1");
42611be35a1SLionel Sambuc H_CHECK_EQ_MSG(1_2, 1, 2, "1 does not match 2");
42711be35a1SLionel Sambuc H_CHECK_EQ_MSG(2_1, 2, 1, "2 does not match 1");
42811be35a1SLionel Sambuc H_CHECK_EQ_MSG(2_2, 2, 2, "2 does not match 2");
42911be35a1SLionel Sambuc 
43011be35a1SLionel Sambuc ATF_TC(check_eq);
ATF_TC_HEAD(check_eq,tc)43111be35a1SLionel Sambuc ATF_TC_HEAD(check_eq, tc)
43211be35a1SLionel Sambuc {
43311be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the ATF_CHECK_EQ and "
43411be35a1SLionel Sambuc                       "ATF_CHECK_EQ_MSG macros");
43511be35a1SLionel Sambuc }
ATF_TC_BODY(check_eq,tc)43611be35a1SLionel Sambuc ATF_TC_BODY(check_eq, tc)
43711be35a1SLionel Sambuc {
43811be35a1SLionel Sambuc     struct check_eq_test tests[] = {
43911be35a1SLionel Sambuc         { H_CHECK_EQ_HEAD_NAME(1_1), H_CHECK_EQ_BODY_NAME(1_1),
44011be35a1SLionel Sambuc           "1", "1", "1 != 1", true },
44111be35a1SLionel Sambuc         { H_CHECK_EQ_HEAD_NAME(1_2), H_CHECK_EQ_BODY_NAME(1_2),
44211be35a1SLionel Sambuc           "1", "2", "1 != 2", false },
44311be35a1SLionel Sambuc         { H_CHECK_EQ_HEAD_NAME(2_1), H_CHECK_EQ_BODY_NAME(2_1),
44411be35a1SLionel Sambuc           "2", "1", "2 != 1", false },
44511be35a1SLionel Sambuc         { H_CHECK_EQ_HEAD_NAME(2_2), H_CHECK_EQ_BODY_NAME(2_2),
44611be35a1SLionel Sambuc           "2", "2", "2 != 2", true },
44711be35a1SLionel Sambuc         { H_CHECK_EQ_MSG_HEAD_NAME(1_1), H_CHECK_EQ_MSG_BODY_NAME(1_1),
44811be35a1SLionel Sambuc           "1", "1", "1 != 1: 1 does not match 1", true },
44911be35a1SLionel Sambuc         { H_CHECK_EQ_MSG_HEAD_NAME(1_2), H_CHECK_EQ_MSG_BODY_NAME(1_2),
45011be35a1SLionel Sambuc           "1", "2", "1 != 2: 1 does not match 2", false },
45111be35a1SLionel Sambuc         { H_CHECK_EQ_MSG_HEAD_NAME(2_1), H_CHECK_EQ_MSG_BODY_NAME(2_1),
45211be35a1SLionel Sambuc           "2", "1", "2 != 1: 2 does not match 1", false },
45311be35a1SLionel Sambuc         { H_CHECK_EQ_MSG_HEAD_NAME(2_2), H_CHECK_EQ_MSG_BODY_NAME(2_2),
45411be35a1SLionel Sambuc           "2", "2", "2 != 2: 2 does not match 2", true },
45511be35a1SLionel Sambuc         { NULL, NULL, 0, 0, "", false }
45611be35a1SLionel Sambuc     };
45711be35a1SLionel Sambuc     do_check_eq_tests(tests);
45811be35a1SLionel Sambuc }
45911be35a1SLionel Sambuc 
46011be35a1SLionel Sambuc H_CHECK_STREQ(1_1, "1", "1");
46111be35a1SLionel Sambuc H_CHECK_STREQ(1_2, "1", "2");
46211be35a1SLionel Sambuc H_CHECK_STREQ(2_1, "2", "1");
46311be35a1SLionel Sambuc H_CHECK_STREQ(2_2, "2", "2");
46411be35a1SLionel Sambuc H_CHECK_STREQ_MSG(1_1, "1", "1", "1 does not match 1");
46511be35a1SLionel Sambuc H_CHECK_STREQ_MSG(1_2, "1", "2", "1 does not match 2");
46611be35a1SLionel Sambuc H_CHECK_STREQ_MSG(2_1, "2", "1", "2 does not match 1");
46711be35a1SLionel Sambuc H_CHECK_STREQ_MSG(2_2, "2", "2", "2 does not match 2");
46811be35a1SLionel Sambuc #define CHECK_STREQ_VAR1 "5"
46911be35a1SLionel Sambuc #define CHECK_STREQ_VAR2 "9"
47011be35a1SLionel Sambuc const char *check_streq_var1 = CHECK_STREQ_VAR1;
47111be35a1SLionel Sambuc const char *check_streq_var2 = CHECK_STREQ_VAR2;
47211be35a1SLionel Sambuc H_CHECK_STREQ(vars, check_streq_var1, check_streq_var2);
47311be35a1SLionel Sambuc 
47411be35a1SLionel Sambuc ATF_TC(check_streq);
ATF_TC_HEAD(check_streq,tc)47511be35a1SLionel Sambuc ATF_TC_HEAD(check_streq, tc)
47611be35a1SLionel Sambuc {
47711be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the ATF_CHECK_STREQ and "
47811be35a1SLionel Sambuc                       "ATF_CHECK_STREQ_MSG macros");
47911be35a1SLionel Sambuc }
ATF_TC_BODY(check_streq,tc)48011be35a1SLionel Sambuc ATF_TC_BODY(check_streq, tc)
48111be35a1SLionel Sambuc {
48211be35a1SLionel Sambuc     struct check_eq_test tests[] = {
48311be35a1SLionel Sambuc         { H_CHECK_STREQ_HEAD_NAME(1_1), H_CHECK_STREQ_BODY_NAME(1_1),
48411be35a1SLionel Sambuc           "1", "1", "\"1\" != \"1\" \\(1 != 1\\)", true },
48511be35a1SLionel Sambuc         { H_CHECK_STREQ_HEAD_NAME(1_2), H_CHECK_STREQ_BODY_NAME(1_2),
48611be35a1SLionel Sambuc           "1", "2", "\"1\" != \"2\" \\(1 != 2\\)", false },
48711be35a1SLionel Sambuc         { H_CHECK_STREQ_HEAD_NAME(2_1), H_CHECK_STREQ_BODY_NAME(2_1),
48811be35a1SLionel Sambuc           "2", "1", "\"2\" != \"1\" \\(2 != 1\\)", false },
48911be35a1SLionel Sambuc         { H_CHECK_STREQ_HEAD_NAME(2_2), H_CHECK_STREQ_BODY_NAME(2_2),
49011be35a1SLionel Sambuc           "2", "2", "\"2\" != \"2\" \\(2 != 2\\)", true },
49111be35a1SLionel Sambuc         { H_CHECK_STREQ_MSG_HEAD_NAME(1_1),
49211be35a1SLionel Sambuc           H_CHECK_STREQ_MSG_BODY_NAME(1_1),
49311be35a1SLionel Sambuc           "1", "1", "\"1\" != \"1\" \\(1 != 1\\): 1 does not match 1", true },
49411be35a1SLionel Sambuc         { H_CHECK_STREQ_MSG_HEAD_NAME(1_2),
49511be35a1SLionel Sambuc           H_CHECK_STREQ_MSG_BODY_NAME(1_2),
49611be35a1SLionel Sambuc           "1", "2", "\"1\" != \"2\" \\(1 != 2\\): 1 does not match 2", false },
49711be35a1SLionel Sambuc         { H_CHECK_STREQ_MSG_HEAD_NAME(2_1),
49811be35a1SLionel Sambuc           H_CHECK_STREQ_MSG_BODY_NAME(2_1),
49911be35a1SLionel Sambuc           "2", "1", "\"2\" != \"1\" \\(2 != 1\\): 2 does not match 1", false },
50011be35a1SLionel Sambuc         { H_CHECK_STREQ_MSG_HEAD_NAME(2_2),
50111be35a1SLionel Sambuc           H_CHECK_STREQ_MSG_BODY_NAME(2_2),
50211be35a1SLionel Sambuc           "2", "2", "\"2\" != \"2\" \\(2 != 2\\): 2 does not match 2", true },
50311be35a1SLionel Sambuc         { H_CHECK_STREQ_HEAD_NAME(vars), H_CHECK_STREQ_BODY_NAME(vars),
50411be35a1SLionel Sambuc           check_streq_var1, check_streq_var2,
50511be35a1SLionel Sambuc           "check_streq_var1 != check_streq_var2 \\("
50611be35a1SLionel Sambuc           CHECK_STREQ_VAR1 " != " CHECK_STREQ_VAR2 "\\)", false },
50711be35a1SLionel Sambuc         { NULL, NULL, 0, 0, "", false }
50811be35a1SLionel Sambuc     };
50911be35a1SLionel Sambuc     do_check_eq_tests(tests);
51011be35a1SLionel Sambuc }
51111be35a1SLionel Sambuc 
51211be35a1SLionel Sambuc /* ---------------------------------------------------------------------
51311be35a1SLionel Sambuc  * Test cases for the ATF_CHECK_MATCH and ATF_CHECK_MATCH_MSG macros.
51411be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
51511be35a1SLionel Sambuc 
51611be35a1SLionel Sambuc H_CHECK_MATCH(yes, "hello [a-z]+", "abc hello world");
51711be35a1SLionel Sambuc H_CHECK_MATCH(no, "hello [a-z]+", "abc hello WORLD");
51811be35a1SLionel Sambuc H_CHECK_MATCH_MSG(yes, "hello [a-z]+", "abc hello world", "lowercase");
51911be35a1SLionel Sambuc H_CHECK_MATCH_MSG(no, "hello [a-z]+", "abc hello WORLD", "uppercase");
52011be35a1SLionel Sambuc 
52111be35a1SLionel Sambuc ATF_TC(check_match);
ATF_TC_HEAD(check_match,tc)52211be35a1SLionel Sambuc ATF_TC_HEAD(check_match, tc)
52311be35a1SLionel Sambuc {
52411be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the ATF_CHECK_MATCH and "
52511be35a1SLionel Sambuc                       "ATF_CHECK_MATCH_MSG macros");
52611be35a1SLionel Sambuc }
ATF_TC_BODY(check_match,tc)52711be35a1SLionel Sambuc ATF_TC_BODY(check_match, tc)
52811be35a1SLionel Sambuc {
52911be35a1SLionel Sambuc     struct check_eq_test tests[] = {
53011be35a1SLionel Sambuc         { H_CHECK_MATCH_HEAD_NAME(yes), H_CHECK_MATCH_BODY_NAME(yes),
53111be35a1SLionel Sambuc           "hello [a-z]+", "abc hello world", "", true },
53211be35a1SLionel Sambuc         { H_CHECK_MATCH_HEAD_NAME(no), H_CHECK_MATCH_BODY_NAME(no),
53311be35a1SLionel Sambuc           "hello [a-z]+", "abc hello WORLD",
53411be35a1SLionel Sambuc           "'hello \\[a-z\\]\\+' not matched in 'abc hello WORLD'", false },
53511be35a1SLionel Sambuc         { H_CHECK_MATCH_MSG_HEAD_NAME(yes), H_CHECK_MATCH_MSG_BODY_NAME(yes),
53611be35a1SLionel Sambuc           "hello [a-z]+", "abc hello world", "", true },
53711be35a1SLionel Sambuc         { H_CHECK_MATCH_MSG_HEAD_NAME(no), H_CHECK_MATCH_MSG_BODY_NAME(no),
53811be35a1SLionel Sambuc           "hello [a-z]+", "abc hello WORLD",
53911be35a1SLionel Sambuc           "'hello \\[a-z\\]\\+' not matched in 'abc hello WORLD': uppercase",
54011be35a1SLionel Sambuc           false },
54111be35a1SLionel Sambuc         { NULL, NULL, 0, 0, "", false }
54211be35a1SLionel Sambuc     };
54311be35a1SLionel Sambuc     do_check_eq_tests(tests);
54411be35a1SLionel Sambuc }
54511be35a1SLionel Sambuc 
54611be35a1SLionel Sambuc /* ---------------------------------------------------------------------
54711be35a1SLionel Sambuc  * Test cases for the ATF_REQUIRE and ATF_REQUIRE_MSG macros.
54811be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
54911be35a1SLionel Sambuc 
55011be35a1SLionel Sambuc H_REQUIRE(0, 0);
55111be35a1SLionel Sambuc H_REQUIRE(1, 1);
55211be35a1SLionel Sambuc H_REQUIRE_MSG(0, 0, "expected a false value");
55311be35a1SLionel Sambuc H_REQUIRE_MSG(1, 1, "expected a true value");
55411be35a1SLionel Sambuc 
55511be35a1SLionel Sambuc ATF_TC(require);
ATF_TC_HEAD(require,tc)55611be35a1SLionel Sambuc ATF_TC_HEAD(require, tc)
55711be35a1SLionel Sambuc {
55811be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the ATF_REQUIRE and "
55911be35a1SLionel Sambuc                       "ATF_REQUIRE_MSG macros");
56011be35a1SLionel Sambuc }
ATF_TC_BODY(require,tc)56111be35a1SLionel Sambuc ATF_TC_BODY(require, tc)
56211be35a1SLionel Sambuc {
56311be35a1SLionel Sambuc     struct test {
56411be35a1SLionel Sambuc         void (*head)(atf_tc_t *);
56511be35a1SLionel Sambuc         void (*body)(const atf_tc_t *);
56611be35a1SLionel Sambuc         bool value;
56711be35a1SLionel Sambuc         const char *msg;
56811be35a1SLionel Sambuc         bool ok;
56911be35a1SLionel Sambuc     } *t, tests[] = {
57011be35a1SLionel Sambuc         { H_REQUIRE_HEAD_NAME(0), H_REQUIRE_BODY_NAME(0), 0,
57111be35a1SLionel Sambuc           "0 not met", false },
57211be35a1SLionel Sambuc         { H_REQUIRE_HEAD_NAME(1), H_REQUIRE_BODY_NAME(1), 1,
57311be35a1SLionel Sambuc           "1 not met", true },
57411be35a1SLionel Sambuc         { H_REQUIRE_MSG_HEAD_NAME(0), H_REQUIRE_MSG_BODY_NAME(0), 0,
57511be35a1SLionel Sambuc           "expected a false value", false },
57611be35a1SLionel Sambuc         { H_REQUIRE_MSG_HEAD_NAME(1), H_REQUIRE_MSG_BODY_NAME(1), 1,
57711be35a1SLionel Sambuc           "expected a true value", true },
57811be35a1SLionel Sambuc         { NULL, NULL, false, NULL, false }
57911be35a1SLionel Sambuc     };
58011be35a1SLionel Sambuc 
58111be35a1SLionel Sambuc     for (t = &tests[0]; t->head != NULL; t++) {
58211be35a1SLionel Sambuc         printf("Checking with a %d value\n", t->value);
58311be35a1SLionel Sambuc 
58411be35a1SLionel Sambuc         init_and_run_h_tc("h_require", t->head, t->body);
58511be35a1SLionel Sambuc 
58611be35a1SLionel Sambuc         ATF_REQUIRE(exists("before"));
58711be35a1SLionel Sambuc         if (t->ok) {
58811be35a1SLionel Sambuc             ATF_REQUIRE(atf_utils_grep_file("^passed", "result"));
58911be35a1SLionel Sambuc             ATF_REQUIRE(exists("after"));
59011be35a1SLionel Sambuc         } else {
59111be35a1SLionel Sambuc             ATF_REQUIRE(atf_utils_grep_file(
59211be35a1SLionel Sambuc                 "^failed: .*macros_test.c:[0-9]+: %s$", "result", t->msg));
59311be35a1SLionel Sambuc             ATF_REQUIRE(!exists("after"));
59411be35a1SLionel Sambuc         }
59511be35a1SLionel Sambuc 
59611be35a1SLionel Sambuc         ATF_REQUIRE(unlink("before") != -1);
59711be35a1SLionel Sambuc         if (t->ok)
59811be35a1SLionel Sambuc             ATF_REQUIRE(unlink("after") != -1);
59911be35a1SLionel Sambuc     }
60011be35a1SLionel Sambuc }
60111be35a1SLionel Sambuc 
60211be35a1SLionel Sambuc /* ---------------------------------------------------------------------
60311be35a1SLionel Sambuc  * Test cases for the ATF_REQUIRE_*EQ_ macros.
60411be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
60511be35a1SLionel Sambuc 
60611be35a1SLionel Sambuc struct require_eq_test {
60711be35a1SLionel Sambuc     void (*head)(atf_tc_t *);
60811be35a1SLionel Sambuc     void (*body)(const atf_tc_t *);
60911be35a1SLionel Sambuc     const char *v1;
61011be35a1SLionel Sambuc     const char *v2;
61111be35a1SLionel Sambuc     const char *msg;
61211be35a1SLionel Sambuc     bool ok;
61311be35a1SLionel Sambuc };
61411be35a1SLionel Sambuc 
61511be35a1SLionel Sambuc static
61611be35a1SLionel Sambuc void
do_require_eq_tests(const struct require_eq_test * tests)61711be35a1SLionel Sambuc do_require_eq_tests(const struct require_eq_test *tests)
61811be35a1SLionel Sambuc {
61911be35a1SLionel Sambuc     const struct require_eq_test *t;
62011be35a1SLionel Sambuc 
62111be35a1SLionel Sambuc     for (t = &tests[0]; t->head != NULL; t++) {
62211be35a1SLionel Sambuc         printf("Checking with %s, %s and expecting %s\n", t->v1, t->v2,
62311be35a1SLionel Sambuc                t->ok ? "true" : "false");
62411be35a1SLionel Sambuc 
62511be35a1SLionel Sambuc         init_and_run_h_tc("h_require", t->head, t->body);
62611be35a1SLionel Sambuc 
62711be35a1SLionel Sambuc         ATF_REQUIRE(exists("before"));
62811be35a1SLionel Sambuc         if (t->ok) {
62911be35a1SLionel Sambuc             ATF_REQUIRE(atf_utils_grep_file("^passed", "result"));
63011be35a1SLionel Sambuc             ATF_REQUIRE(exists("after"));
63111be35a1SLionel Sambuc         } else {
63211be35a1SLionel Sambuc             ATF_REQUIRE(atf_utils_grep_file("^failed: .*macros_test.c"
63311be35a1SLionel Sambuc                 ":[0-9]+: %s$", "result", t->msg));
63411be35a1SLionel Sambuc             ATF_REQUIRE(!exists("after"));
63511be35a1SLionel Sambuc         }
63611be35a1SLionel Sambuc 
63711be35a1SLionel Sambuc         ATF_REQUIRE(unlink("before") != -1);
63811be35a1SLionel Sambuc         if (t->ok)
63911be35a1SLionel Sambuc             ATF_REQUIRE(unlink("after") != -1);
64011be35a1SLionel Sambuc     }
64111be35a1SLionel Sambuc }
64211be35a1SLionel Sambuc 
64311be35a1SLionel Sambuc H_REQUIRE_EQ(1_1, 1, 1);
64411be35a1SLionel Sambuc H_REQUIRE_EQ(1_2, 1, 2);
64511be35a1SLionel Sambuc H_REQUIRE_EQ(2_1, 2, 1);
64611be35a1SLionel Sambuc H_REQUIRE_EQ(2_2, 2, 2);
64711be35a1SLionel Sambuc H_REQUIRE_EQ_MSG(1_1, 1, 1, "1 does not match 1");
64811be35a1SLionel Sambuc H_REQUIRE_EQ_MSG(1_2, 1, 2, "1 does not match 2");
64911be35a1SLionel Sambuc H_REQUIRE_EQ_MSG(2_1, 2, 1, "2 does not match 1");
65011be35a1SLionel Sambuc H_REQUIRE_EQ_MSG(2_2, 2, 2, "2 does not match 2");
65111be35a1SLionel Sambuc 
65211be35a1SLionel Sambuc ATF_TC(require_eq);
ATF_TC_HEAD(require_eq,tc)65311be35a1SLionel Sambuc ATF_TC_HEAD(require_eq, tc)
65411be35a1SLionel Sambuc {
65511be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the ATF_REQUIRE_EQ and "
65611be35a1SLionel Sambuc                       "ATF_REQUIRE_EQ_MSG macros");
65711be35a1SLionel Sambuc }
ATF_TC_BODY(require_eq,tc)65811be35a1SLionel Sambuc ATF_TC_BODY(require_eq, tc)
65911be35a1SLionel Sambuc {
66011be35a1SLionel Sambuc     struct require_eq_test tests[] = {
66111be35a1SLionel Sambuc         { H_REQUIRE_EQ_HEAD_NAME(1_1), H_REQUIRE_EQ_BODY_NAME(1_1),
66211be35a1SLionel Sambuc           "1", "1", "1 != 1", true },
66311be35a1SLionel Sambuc         { H_REQUIRE_EQ_HEAD_NAME(1_2), H_REQUIRE_EQ_BODY_NAME(1_2),
66411be35a1SLionel Sambuc           "1", "2", "1 != 2", false },
66511be35a1SLionel Sambuc         { H_REQUIRE_EQ_HEAD_NAME(2_1), H_REQUIRE_EQ_BODY_NAME(2_1),
66611be35a1SLionel Sambuc           "2", "1", "2 != 1", false },
66711be35a1SLionel Sambuc         { H_REQUIRE_EQ_HEAD_NAME(2_2), H_REQUIRE_EQ_BODY_NAME(2_2),
66811be35a1SLionel Sambuc           "2", "2", "2 != 2", true },
66911be35a1SLionel Sambuc         { H_REQUIRE_EQ_MSG_HEAD_NAME(1_1), H_REQUIRE_EQ_MSG_BODY_NAME(1_1),
67011be35a1SLionel Sambuc           "1", "1", "1 != 1: 1 does not match 1", true },
67111be35a1SLionel Sambuc         { H_REQUIRE_EQ_MSG_HEAD_NAME(1_2), H_REQUIRE_EQ_MSG_BODY_NAME(1_2),
67211be35a1SLionel Sambuc           "1", "2", "1 != 2: 1 does not match 2", false },
67311be35a1SLionel Sambuc         { H_REQUIRE_EQ_MSG_HEAD_NAME(2_1), H_REQUIRE_EQ_MSG_BODY_NAME(2_1),
67411be35a1SLionel Sambuc           "2", "1", "2 != 1: 2 does not match 1", false },
67511be35a1SLionel Sambuc         { H_REQUIRE_EQ_MSG_HEAD_NAME(2_2), H_REQUIRE_EQ_MSG_BODY_NAME(2_2),
67611be35a1SLionel Sambuc           "2", "2", "2 != 2: 2 does not match 2", true },
67711be35a1SLionel Sambuc         { NULL, NULL, 0, 0, "", false }
67811be35a1SLionel Sambuc     };
67911be35a1SLionel Sambuc     do_require_eq_tests(tests);
68011be35a1SLionel Sambuc }
68111be35a1SLionel Sambuc 
68211be35a1SLionel Sambuc H_REQUIRE_STREQ(1_1, "1", "1");
68311be35a1SLionel Sambuc H_REQUIRE_STREQ(1_2, "1", "2");
68411be35a1SLionel Sambuc H_REQUIRE_STREQ(2_1, "2", "1");
68511be35a1SLionel Sambuc H_REQUIRE_STREQ(2_2, "2", "2");
68611be35a1SLionel Sambuc H_REQUIRE_STREQ_MSG(1_1, "1", "1", "1 does not match 1");
68711be35a1SLionel Sambuc H_REQUIRE_STREQ_MSG(1_2, "1", "2", "1 does not match 2");
68811be35a1SLionel Sambuc H_REQUIRE_STREQ_MSG(2_1, "2", "1", "2 does not match 1");
68911be35a1SLionel Sambuc H_REQUIRE_STREQ_MSG(2_2, "2", "2", "2 does not match 2");
69011be35a1SLionel Sambuc #define REQUIRE_STREQ_VAR1 "5"
69111be35a1SLionel Sambuc #define REQUIRE_STREQ_VAR2 "9"
69211be35a1SLionel Sambuc const char *require_streq_var1 = REQUIRE_STREQ_VAR1;
69311be35a1SLionel Sambuc const char *require_streq_var2 = REQUIRE_STREQ_VAR2;
69411be35a1SLionel Sambuc H_REQUIRE_STREQ(vars, require_streq_var1, require_streq_var2);
69511be35a1SLionel Sambuc 
69611be35a1SLionel Sambuc ATF_TC(require_streq);
ATF_TC_HEAD(require_streq,tc)69711be35a1SLionel Sambuc ATF_TC_HEAD(require_streq, tc)
69811be35a1SLionel Sambuc {
69911be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the ATF_REQUIRE_STREQ and "
70011be35a1SLionel Sambuc                       "ATF_REQUIRE_STREQ_MSG macros");
70111be35a1SLionel Sambuc }
ATF_TC_BODY(require_streq,tc)70211be35a1SLionel Sambuc ATF_TC_BODY(require_streq, tc)
70311be35a1SLionel Sambuc {
70411be35a1SLionel Sambuc     struct require_eq_test tests[] = {
70511be35a1SLionel Sambuc         { H_REQUIRE_STREQ_HEAD_NAME(1_1), H_REQUIRE_STREQ_BODY_NAME(1_1),
70611be35a1SLionel Sambuc           "1", "1", "\"1\" != \"1\" \\(1 != 1\\)", true },
70711be35a1SLionel Sambuc         { H_REQUIRE_STREQ_HEAD_NAME(1_2), H_REQUIRE_STREQ_BODY_NAME(1_2),
70811be35a1SLionel Sambuc           "1", "2", "\"1\" != \"2\" \\(1 != 2\\)", false },
70911be35a1SLionel Sambuc         { H_REQUIRE_STREQ_HEAD_NAME(2_1), H_REQUIRE_STREQ_BODY_NAME(2_1),
71011be35a1SLionel Sambuc           "2", "1", "\"2\" != \"1\" \\(2 != 1\\)", false },
71111be35a1SLionel Sambuc         { H_REQUIRE_STREQ_HEAD_NAME(2_2), H_REQUIRE_STREQ_BODY_NAME(2_2),
71211be35a1SLionel Sambuc           "2", "2", "\"2\" != \"2\" \\(2 != 2\\)", true },
71311be35a1SLionel Sambuc         { H_REQUIRE_STREQ_MSG_HEAD_NAME(1_1),
71411be35a1SLionel Sambuc           H_REQUIRE_STREQ_MSG_BODY_NAME(1_1),
71511be35a1SLionel Sambuc           "1", "1", "\"1\" != \"1\" \\(1 != 1\\): 1 does not match 1", true },
71611be35a1SLionel Sambuc         { H_REQUIRE_STREQ_MSG_HEAD_NAME(1_2),
71711be35a1SLionel Sambuc           H_REQUIRE_STREQ_MSG_BODY_NAME(1_2),
71811be35a1SLionel Sambuc           "1", "2", "\"1\" != \"2\" \\(1 != 2\\): 1 does not match 2", false },
71911be35a1SLionel Sambuc         { H_REQUIRE_STREQ_MSG_HEAD_NAME(2_1),
72011be35a1SLionel Sambuc           H_REQUIRE_STREQ_MSG_BODY_NAME(2_1),
72111be35a1SLionel Sambuc           "2", "1", "\"2\" != \"1\" \\(2 != 1\\): 2 does not match 1", false },
72211be35a1SLionel Sambuc         { H_REQUIRE_STREQ_MSG_HEAD_NAME(2_2),
72311be35a1SLionel Sambuc           H_REQUIRE_STREQ_MSG_BODY_NAME(2_2),
72411be35a1SLionel Sambuc           "2", "2", "\"2\" != \"2\" \\(2 != 2\\): 2 does not match 2", true },
72511be35a1SLionel Sambuc         { H_REQUIRE_STREQ_HEAD_NAME(vars), H_REQUIRE_STREQ_BODY_NAME(vars),
72611be35a1SLionel Sambuc           require_streq_var1, require_streq_var2,
72711be35a1SLionel Sambuc           "require_streq_var1 != require_streq_var2 \\("
72811be35a1SLionel Sambuc           REQUIRE_STREQ_VAR1 " != " REQUIRE_STREQ_VAR2 "\\)", false },
72911be35a1SLionel Sambuc         { NULL, NULL, 0, 0, "", false }
73011be35a1SLionel Sambuc     };
73111be35a1SLionel Sambuc     do_require_eq_tests(tests);
73211be35a1SLionel Sambuc }
73311be35a1SLionel Sambuc 
73411be35a1SLionel Sambuc /* ---------------------------------------------------------------------
73511be35a1SLionel Sambuc  * Test cases for the ATF_REQUIRE_MATCH and ATF_REQUIRE_MATCH_MSG macros.
73611be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
73711be35a1SLionel Sambuc 
73811be35a1SLionel Sambuc H_REQUIRE_MATCH(yes, "hello [a-z]+", "abc hello world");
73911be35a1SLionel Sambuc H_REQUIRE_MATCH(no, "hello [a-z]+", "abc hello WORLD");
74011be35a1SLionel Sambuc H_REQUIRE_MATCH_MSG(yes, "hello [a-z]+", "abc hello world", "lowercase");
74111be35a1SLionel Sambuc H_REQUIRE_MATCH_MSG(no, "hello [a-z]+", "abc hello WORLD", "uppercase");
74211be35a1SLionel Sambuc 
74311be35a1SLionel Sambuc ATF_TC(require_match);
ATF_TC_HEAD(require_match,tc)74411be35a1SLionel Sambuc ATF_TC_HEAD(require_match, tc)
74511be35a1SLionel Sambuc {
74611be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the ATF_REQUIRE_MATCH and "
74711be35a1SLionel Sambuc                       "ATF_REQUIRE_MATCH_MSG macros");
74811be35a1SLionel Sambuc }
ATF_TC_BODY(require_match,tc)74911be35a1SLionel Sambuc ATF_TC_BODY(require_match, tc)
75011be35a1SLionel Sambuc {
75111be35a1SLionel Sambuc     struct require_eq_test tests[] = {
75211be35a1SLionel Sambuc         { H_REQUIRE_MATCH_HEAD_NAME(yes), H_REQUIRE_MATCH_BODY_NAME(yes),
75311be35a1SLionel Sambuc           "hello [a-z]+", "abc hello world", "", true },
75411be35a1SLionel Sambuc         { H_REQUIRE_MATCH_HEAD_NAME(no), H_REQUIRE_MATCH_BODY_NAME(no),
75511be35a1SLionel Sambuc           "hello [a-z]+", "abc hello WORLD",
75611be35a1SLionel Sambuc           "'hello \\[a-z\\]\\+' not matched in 'abc hello WORLD'", false },
75711be35a1SLionel Sambuc         { H_REQUIRE_MATCH_MSG_HEAD_NAME(yes),
75811be35a1SLionel Sambuc           H_REQUIRE_MATCH_MSG_BODY_NAME(yes),
75911be35a1SLionel Sambuc           "hello [a-z]+", "abc hello world", "", true },
76011be35a1SLionel Sambuc         { H_REQUIRE_MATCH_MSG_HEAD_NAME(no), H_REQUIRE_MATCH_MSG_BODY_NAME(no),
76111be35a1SLionel Sambuc           "hello [a-z]+", "abc hello WORLD",
76211be35a1SLionel Sambuc           "'hello \\[a-z\\]\\+' not matched in 'abc hello WORLD': uppercase",
76311be35a1SLionel Sambuc           false },
76411be35a1SLionel Sambuc         { NULL, NULL, 0, 0, "", false }
76511be35a1SLionel Sambuc     };
76611be35a1SLionel Sambuc     do_require_eq_tests(tests);
76711be35a1SLionel Sambuc }
76811be35a1SLionel Sambuc 
76911be35a1SLionel Sambuc /* ---------------------------------------------------------------------
77011be35a1SLionel Sambuc  * Miscellaneous test cases covering several macros.
77111be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
77211be35a1SLionel Sambuc 
77311be35a1SLionel Sambuc static
77411be35a1SLionel Sambuc bool
aux_bool(const char * fmt ATF_DEFS_ATTRIBUTE_UNUSED)77511be35a1SLionel Sambuc aux_bool(const char *fmt ATF_DEFS_ATTRIBUTE_UNUSED)
77611be35a1SLionel Sambuc {
77711be35a1SLionel Sambuc     return false;
77811be35a1SLionel Sambuc }
77911be35a1SLionel Sambuc 
78011be35a1SLionel Sambuc static
78111be35a1SLionel Sambuc const char *
aux_str(const char * fmt ATF_DEFS_ATTRIBUTE_UNUSED)78211be35a1SLionel Sambuc aux_str(const char *fmt ATF_DEFS_ATTRIBUTE_UNUSED)
78311be35a1SLionel Sambuc {
78411be35a1SLionel Sambuc     return "foo";
78511be35a1SLionel Sambuc }
78611be35a1SLionel Sambuc 
78711be35a1SLionel Sambuc H_CHECK(msg, aux_bool("%d"));
78811be35a1SLionel Sambuc H_REQUIRE(msg, aux_bool("%d"));
78911be35a1SLionel Sambuc H_CHECK_STREQ(msg, aux_str("%d"), "");
79011be35a1SLionel Sambuc H_REQUIRE_STREQ(msg, aux_str("%d"), "");
79111be35a1SLionel Sambuc 
79211be35a1SLionel Sambuc ATF_TC(msg_embedded_fmt);
ATF_TC_HEAD(msg_embedded_fmt,tc)79311be35a1SLionel Sambuc ATF_TC_HEAD(msg_embedded_fmt, tc)
79411be35a1SLionel Sambuc {
79511be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests that format strings passed "
79611be35a1SLionel Sambuc                       "as part of the automatically-generated messages "
79711be35a1SLionel Sambuc                       "do not get expanded");
79811be35a1SLionel Sambuc }
ATF_TC_BODY(msg_embedded_fmt,tc)79911be35a1SLionel Sambuc ATF_TC_BODY(msg_embedded_fmt, tc)
80011be35a1SLionel Sambuc {
80111be35a1SLionel Sambuc     struct test {
80211be35a1SLionel Sambuc         void (*head)(atf_tc_t *);
80311be35a1SLionel Sambuc         void (*body)(const atf_tc_t *);
80411be35a1SLionel Sambuc         bool fatal;
80511be35a1SLionel Sambuc         const char *msg;
80611be35a1SLionel Sambuc     } *t, tests[] = {
80711be35a1SLionel Sambuc        {  H_CHECK_HEAD_NAME(msg), H_CHECK_BODY_NAME(msg), false,
80811be35a1SLionel Sambuc           "aux_bool\\(\"%d\"\\) not met" },
80911be35a1SLionel Sambuc        {  H_REQUIRE_HEAD_NAME(msg), H_REQUIRE_BODY_NAME(msg), true,
81011be35a1SLionel Sambuc           "aux_bool\\(\"%d\"\\) not met" },
81111be35a1SLionel Sambuc        {  H_CHECK_STREQ_HEAD_NAME(msg), H_CHECK_STREQ_BODY_NAME(msg), false,
81211be35a1SLionel Sambuc           "aux_str\\(\"%d\"\\) != \"\" \\(foo != \\)" },
81311be35a1SLionel Sambuc        {  H_REQUIRE_STREQ_HEAD_NAME(msg), H_REQUIRE_STREQ_BODY_NAME(msg), true,
81411be35a1SLionel Sambuc           "aux_str\\(\"%d\"\\) != \"\" \\(foo != \\)" },
81511be35a1SLionel Sambuc        { NULL, NULL, false, NULL }
81611be35a1SLionel Sambuc     };
81711be35a1SLionel Sambuc 
81811be35a1SLionel Sambuc     for (t = &tests[0]; t->head != NULL; t++) {
81911be35a1SLionel Sambuc         printf("Checking with an expected '%s' message\n", t->msg);
82011be35a1SLionel Sambuc 
82111be35a1SLionel Sambuc         init_and_run_h_tc("h_check", t->head, t->body);
82211be35a1SLionel Sambuc 
82311be35a1SLionel Sambuc         if (t->fatal) {
82411be35a1SLionel Sambuc             bool matched =
82511be35a1SLionel Sambuc                 atf_utils_grep_file(
82611be35a1SLionel Sambuc                     "^failed: .*macros_test.c:[0-9]+: %s$", "result", t->msg);
82711be35a1SLionel Sambuc             ATF_CHECK_MSG(matched, "couldn't find error string in result");
82811be35a1SLionel Sambuc         } else {
82911be35a1SLionel Sambuc             bool matched = atf_utils_grep_file("Check failed: .*"
83011be35a1SLionel Sambuc                 "macros_test.c:[0-9]+: %s$", "error", t->msg);
83111be35a1SLionel Sambuc             ATF_CHECK_MSG(matched, "couldn't find error string in output");
83211be35a1SLionel Sambuc         }
83311be35a1SLionel Sambuc     }
83411be35a1SLionel Sambuc }
83511be35a1SLionel Sambuc 
83611be35a1SLionel Sambuc /* ---------------------------------------------------------------------
83711be35a1SLionel Sambuc  * Tests cases for the header file.
83811be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
83911be35a1SLionel Sambuc 
84011be35a1SLionel Sambuc HEADER_TC(include, "atf-c/macros.h");
84111be35a1SLionel Sambuc BUILD_TC(use, "macros_h_test.c",
84211be35a1SLionel Sambuc          "Tests that the macros provided by the atf-c/macros.h file "
84311be35a1SLionel Sambuc          "do not cause syntax errors when used",
84411be35a1SLionel Sambuc          "Build of macros_h_test.c failed; some macros in atf-c/macros.h "
84511be35a1SLionel Sambuc          "are broken");
846*0a6a1f1dSLionel Sambuc 
847*0a6a1f1dSLionel Sambuc ATF_TC(detect_unused_tests);
ATF_TC_HEAD(detect_unused_tests,tc)848*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(detect_unused_tests, tc)
849*0a6a1f1dSLionel Sambuc {
850*0a6a1f1dSLionel Sambuc     atf_tc_set_md_var(tc, "descr",
851*0a6a1f1dSLionel Sambuc                       "Tests that defining an unused test case raises a "
852*0a6a1f1dSLionel Sambuc                       "warning (and thus an error)");
853*0a6a1f1dSLionel Sambuc }
ATF_TC_BODY(detect_unused_tests,tc)854*0a6a1f1dSLionel Sambuc ATF_TC_BODY(detect_unused_tests, tc)
855*0a6a1f1dSLionel Sambuc {
856*0a6a1f1dSLionel Sambuc     const char* validate_compiler =
857*0a6a1f1dSLionel Sambuc         "struct test_struct { int dummy; };\n"
858*0a6a1f1dSLionel Sambuc         "#define define_unused static struct test_struct unused\n"
859*0a6a1f1dSLionel Sambuc         "define_unused;\n";
860*0a6a1f1dSLionel Sambuc 
861*0a6a1f1dSLionel Sambuc     atf_utils_create_file("compiler_test.c", "%s", validate_compiler);
862*0a6a1f1dSLionel Sambuc     if (build_check_c_o("compiler_test.c"))
863*0a6a1f1dSLionel Sambuc         atf_tc_expect_fail("Compiler does not raise a warning on an unused "
864*0a6a1f1dSLionel Sambuc                            "static global variable declared by a macro");
865*0a6a1f1dSLionel Sambuc 
866*0a6a1f1dSLionel Sambuc #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
867*0a6a1f1dSLionel Sambuc         atf_tc_expect_fail("PR 49187");
868*0a6a1f1dSLionel Sambuc #endif
869*0a6a1f1dSLionel Sambuc 
870*0a6a1f1dSLionel Sambuc     if (build_check_c_o_srcdir(tc, "unused_test.c"))
871*0a6a1f1dSLionel Sambuc         atf_tc_fail("Build of unused_test.c passed; unused test cases are "
872*0a6a1f1dSLionel Sambuc                     "not properly detected");
873*0a6a1f1dSLionel Sambuc }
87411be35a1SLionel Sambuc 
87511be35a1SLionel Sambuc /* ---------------------------------------------------------------------
87611be35a1SLionel Sambuc  * Main.
87711be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
87811be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)87911be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
88011be35a1SLionel Sambuc {
88111be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, check);
88211be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, check_eq);
88311be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, check_streq);
88411be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, check_errno);
88511be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, check_match);
88611be35a1SLionel Sambuc 
88711be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, require);
88811be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, require_eq);
88911be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, require_streq);
89011be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, require_errno);
89111be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, require_match);
89211be35a1SLionel Sambuc 
89311be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, msg_embedded_fmt);
89411be35a1SLionel Sambuc 
89511be35a1SLionel Sambuc     /* Add the test cases for the header file. */
89611be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, include);
89711be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, use);
89811be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, detect_unused_tests);
89911be35a1SLionel Sambuc 
90011be35a1SLionel Sambuc     return atf_no_error();
90111be35a1SLionel Sambuc }
902