xref: /freebsd/contrib/atf/atf-c/tc_test.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /* Copyright (c) 2008 The NetBSD Foundation, Inc.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.  */
25 
26 #include "atf-c/tc.h"
27 
28 #include <stdbool.h>
29 #include <string.h>
30 
31 #include <atf-c.h>
32 
33 #include "atf-c/detail/test_helpers.h"
34 
35 /* ---------------------------------------------------------------------
36  * Auxiliary test cases.
37  * --------------------------------------------------------------------- */
38 
39 ATF_TC_HEAD(empty, tc)
40 {
41     if (tc != NULL) {}
42 }
43 ATF_TC_BODY(empty, tc)
44 {
45 }
46 
47 ATF_TC_HEAD(test_var, tc)
48 {
49     atf_tc_set_md_var(tc, "test-var", "Test text");
50 }
51 
52 /* ---------------------------------------------------------------------
53  * Test cases for the "atf_tc_t" type.
54  * --------------------------------------------------------------------- */
55 
56 ATF_TC(init);
57 ATF_TC_HEAD(init, tc)
58 {
59     atf_tc_set_md_var(tc, "descr", "Tests the atf_tc_init function");
60 }
61 ATF_TC_BODY(init, tcin)
62 {
63     atf_tc_t tc;
64 
65     RE(atf_tc_init(&tc, "test1", ATF_TC_HEAD_NAME(empty),
66                    ATF_TC_BODY_NAME(empty), NULL, NULL));
67     ATF_REQUIRE(strcmp(atf_tc_get_ident(&tc), "test1") == 0);
68     ATF_REQUIRE(!atf_tc_has_md_var(&tc, "test-var"));
69     atf_tc_fini(&tc);
70 
71     RE(atf_tc_init(&tc, "test2", ATF_TC_HEAD_NAME(test_var),
72                    ATF_TC_BODY_NAME(empty), NULL, NULL));
73     ATF_REQUIRE(strcmp(atf_tc_get_ident(&tc), "test2") == 0);
74     ATF_REQUIRE(atf_tc_has_md_var(&tc, "test-var"));
75     atf_tc_fini(&tc);
76 }
77 
78 ATF_TC(init_pack);
79 ATF_TC_HEAD(init_pack, tc)
80 {
81     atf_tc_set_md_var(tc, "descr", "Tests the atf_tc_init_pack function");
82 }
83 ATF_TC_BODY(init_pack, tcin)
84 {
85     atf_tc_t tc;
86     atf_tc_pack_t tcp1 = {
87         .m_ident = "test1",
88         .m_head = ATF_TC_HEAD_NAME(empty),
89         .m_body = ATF_TC_BODY_NAME(empty),
90         .m_cleanup = NULL,
91     };
92     atf_tc_pack_t tcp2 = {
93         .m_ident = "test2",
94         .m_head = ATF_TC_HEAD_NAME(test_var),
95         .m_body = ATF_TC_BODY_NAME(empty),
96         .m_cleanup = NULL,
97     };
98 
99     RE(atf_tc_init_pack(&tc, &tcp1, NULL));
100     ATF_REQUIRE(strcmp(atf_tc_get_ident(&tc), "test1") == 0);
101     ATF_REQUIRE(!atf_tc_has_md_var(&tc, "test-var"));
102     atf_tc_fini(&tc);
103 
104     RE(atf_tc_init_pack(&tc, &tcp2, NULL));
105     ATF_REQUIRE(strcmp(atf_tc_get_ident(&tc), "test2") == 0);
106     ATF_REQUIRE(atf_tc_has_md_var(&tc, "test-var"));
107     atf_tc_fini(&tc);
108 }
109 
110 ATF_TC(vars);
111 ATF_TC_HEAD(vars, tc)
112 {
113     atf_tc_set_md_var(tc, "descr", "Tests the atf_tc_get_md_var, "
114                       "atf_tc_has_md_var and atf_tc_set_md_var functions");
115 }
116 ATF_TC_BODY(vars, tcin)
117 {
118     atf_tc_t tc;
119 
120     RE(atf_tc_init(&tc, "test1", ATF_TC_HEAD_NAME(empty),
121                    ATF_TC_BODY_NAME(empty), NULL, NULL));
122     ATF_REQUIRE(!atf_tc_has_md_var(&tc, "test-var"));
123     RE(atf_tc_set_md_var(&tc, "test-var", "Test value"));
124     ATF_REQUIRE(atf_tc_has_md_var(&tc, "test-var"));
125     ATF_REQUIRE(strcmp(atf_tc_get_md_var(&tc, "test-var"), "Test value") == 0);
126     atf_tc_fini(&tc);
127 }
128 
129 ATF_TC(config);
130 ATF_TC_HEAD(config, tc)
131 {
132     atf_tc_set_md_var(tc, "descr", "Tests the atf_tc_get_config_var, "
133                       "atf_tc_get_config_var_wd and atf_tc_has_config_var "
134                       "functions");
135 }
136 ATF_TC_BODY(config, tcin)
137 {
138     atf_tc_t tc;
139     const char *const config[] = { "test-var", "test-value", NULL };
140 
141     RE(atf_tc_init(&tc, "test1", ATF_TC_HEAD_NAME(empty),
142                    ATF_TC_BODY_NAME(empty), NULL, NULL));
143     ATF_REQUIRE(!atf_tc_has_config_var(&tc, "test-var"));
144     ATF_REQUIRE(!atf_tc_has_md_var(&tc, "test-var"));
145     atf_tc_fini(&tc);
146 
147     RE(atf_tc_init(&tc, "test1", ATF_TC_HEAD_NAME(empty),
148                    ATF_TC_BODY_NAME(empty), NULL, config));
149     ATF_REQUIRE(atf_tc_has_config_var(&tc, "test-var"));
150     ATF_REQUIRE(strcmp(atf_tc_get_config_var(&tc, "test-var"),
151                      "test-value") == 0);
152     ATF_REQUIRE(!atf_tc_has_md_var(&tc, "test-var"));
153     ATF_REQUIRE(!atf_tc_has_config_var(&tc, "test-var2"));
154     ATF_REQUIRE(strcmp(atf_tc_get_config_var_wd(&tc, "test-var2", "def-value"),
155                      "def-value") == 0);
156     atf_tc_fini(&tc);
157 }
158 
159 /* ---------------------------------------------------------------------
160  * Test cases for the free functions.
161  * --------------------------------------------------------------------- */
162 
163 /* TODO: Add test cases for atf_tc_run.  This is going to be very tough,
164  * but good tests here could allow us to avoid much of the indirect
165  * testing done later on. */
166 
167 /* ---------------------------------------------------------------------
168  * Main.
169  * --------------------------------------------------------------------- */
170 
171 ATF_TP_ADD_TCS(tp)
172 {
173     /* Add the test cases for the "atf_tcr_t" type. */
174     ATF_TP_ADD_TC(tp, init);
175     ATF_TP_ADD_TC(tp, init_pack);
176     ATF_TP_ADD_TC(tp, vars);
177     ATF_TP_ADD_TC(tp, config);
178 
179     /* Add the test cases for the free functions. */
180     /* TODO */
181 
182     return atf_no_error();
183 }
184