xref: /freebsd/contrib/atf/atf-c/tc_test.c (revision 40a8ac8f62b535d30349faf28cf47106b7041b83)
1 /*
2  * Automated Testing Framework (atf)
3  *
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <stdbool.h>
31 #include <string.h>
32 
33 #include <atf-c.h>
34 
35 #include "detail/test_helpers.h"
36 
37 /* ---------------------------------------------------------------------
38  * Auxiliary test cases.
39  * --------------------------------------------------------------------- */
40 
41 ATF_TC_HEAD(empty, tc)
42 {
43     if (tc != NULL) {}
44 }
45 ATF_TC_BODY(empty, tc)
46 {
47 }
48 
49 ATF_TC_HEAD(test_var, tc)
50 {
51     atf_tc_set_md_var(tc, "test-var", "Test text");
52 }
53 
54 /* ---------------------------------------------------------------------
55  * Test cases for the "atf_tc_t" type.
56  * --------------------------------------------------------------------- */
57 
58 ATF_TC(init);
59 ATF_TC_HEAD(init, tc)
60 {
61     atf_tc_set_md_var(tc, "descr", "Tests the atf_tc_init function");
62 }
63 ATF_TC_BODY(init, tcin)
64 {
65     atf_tc_t tc;
66 
67     RE(atf_tc_init(&tc, "test1", ATF_TC_HEAD_NAME(empty),
68                    ATF_TC_BODY_NAME(empty), NULL, NULL));
69     ATF_REQUIRE(strcmp(atf_tc_get_ident(&tc), "test1") == 0);
70     ATF_REQUIRE(!atf_tc_has_md_var(&tc, "test-var"));
71     atf_tc_fini(&tc);
72 
73     RE(atf_tc_init(&tc, "test2", ATF_TC_HEAD_NAME(test_var),
74                    ATF_TC_BODY_NAME(empty), NULL, NULL));
75     ATF_REQUIRE(strcmp(atf_tc_get_ident(&tc), "test2") == 0);
76     ATF_REQUIRE(atf_tc_has_md_var(&tc, "test-var"));
77     atf_tc_fini(&tc);
78 }
79 
80 ATF_TC(init_pack);
81 ATF_TC_HEAD(init_pack, tc)
82 {
83     atf_tc_set_md_var(tc, "descr", "Tests the atf_tc_init_pack function");
84 }
85 ATF_TC_BODY(init_pack, tcin)
86 {
87     atf_tc_t tc;
88     atf_tc_pack_t tcp1 = {
89         .m_ident = "test1",
90         .m_head = ATF_TC_HEAD_NAME(empty),
91         .m_body = ATF_TC_BODY_NAME(empty),
92         .m_cleanup = NULL,
93     };
94     atf_tc_pack_t tcp2 = {
95         .m_ident = "test2",
96         .m_head = ATF_TC_HEAD_NAME(test_var),
97         .m_body = ATF_TC_BODY_NAME(empty),
98         .m_cleanup = NULL,
99     };
100 
101     RE(atf_tc_init_pack(&tc, &tcp1, NULL));
102     ATF_REQUIRE(strcmp(atf_tc_get_ident(&tc), "test1") == 0);
103     ATF_REQUIRE(!atf_tc_has_md_var(&tc, "test-var"));
104     atf_tc_fini(&tc);
105 
106     RE(atf_tc_init_pack(&tc, &tcp2, NULL));
107     ATF_REQUIRE(strcmp(atf_tc_get_ident(&tc), "test2") == 0);
108     ATF_REQUIRE(atf_tc_has_md_var(&tc, "test-var"));
109     atf_tc_fini(&tc);
110 }
111 
112 ATF_TC(vars);
113 ATF_TC_HEAD(vars, tc)
114 {
115     atf_tc_set_md_var(tc, "descr", "Tests the atf_tc_get_md_var, "
116                       "atf_tc_has_md_var and atf_tc_set_md_var functions");
117 }
118 ATF_TC_BODY(vars, tcin)
119 {
120     atf_tc_t tc;
121 
122     RE(atf_tc_init(&tc, "test1", ATF_TC_HEAD_NAME(empty),
123                    ATF_TC_BODY_NAME(empty), NULL, NULL));
124     ATF_REQUIRE(!atf_tc_has_md_var(&tc, "test-var"));
125     RE(atf_tc_set_md_var(&tc, "test-var", "Test value"));
126     ATF_REQUIRE(atf_tc_has_md_var(&tc, "test-var"));
127     ATF_REQUIRE(strcmp(atf_tc_get_md_var(&tc, "test-var"), "Test value") == 0);
128     atf_tc_fini(&tc);
129 }
130 
131 ATF_TC(config);
132 ATF_TC_HEAD(config, tc)
133 {
134     atf_tc_set_md_var(tc, "descr", "Tests the atf_tc_get_config_var, "
135                       "atf_tc_get_config_var_wd and atf_tc_has_config_var "
136                       "functions");
137 }
138 ATF_TC_BODY(config, tcin)
139 {
140     atf_tc_t tc;
141     const char *const config[] = { "test-var", "test-value", NULL };
142 
143     RE(atf_tc_init(&tc, "test1", ATF_TC_HEAD_NAME(empty),
144                    ATF_TC_BODY_NAME(empty), NULL, NULL));
145     ATF_REQUIRE(!atf_tc_has_config_var(&tc, "test-var"));
146     ATF_REQUIRE(!atf_tc_has_md_var(&tc, "test-var"));
147     atf_tc_fini(&tc);
148 
149     RE(atf_tc_init(&tc, "test1", ATF_TC_HEAD_NAME(empty),
150                    ATF_TC_BODY_NAME(empty), NULL, config));
151     ATF_REQUIRE(atf_tc_has_config_var(&tc, "test-var"));
152     ATF_REQUIRE(strcmp(atf_tc_get_config_var(&tc, "test-var"),
153                      "test-value") == 0);
154     ATF_REQUIRE(!atf_tc_has_md_var(&tc, "test-var"));
155     ATF_REQUIRE(!atf_tc_has_config_var(&tc, "test-var2"));
156     ATF_REQUIRE(strcmp(atf_tc_get_config_var_wd(&tc, "test-var2", "def-value"),
157                      "def-value") == 0);
158     atf_tc_fini(&tc);
159 }
160 
161 /* ---------------------------------------------------------------------
162  * Test cases for the free functions.
163  * --------------------------------------------------------------------- */
164 
165 /* TODO: Add test cases for atf_tc_run.  This is going to be very tough,
166  * but good tests here could allow us to avoid much of the indirect
167  * testing done later on. */
168 
169 /* ---------------------------------------------------------------------
170  * Tests cases for the header file.
171  * --------------------------------------------------------------------- */
172 
173 HEADER_TC(include, "atf-c/tc.h");
174 
175 /* ---------------------------------------------------------------------
176  * Main.
177  * --------------------------------------------------------------------- */
178 
179 ATF_TP_ADD_TCS(tp)
180 {
181     /* Add the test cases for the "atf_tcr_t" type. */
182     ATF_TP_ADD_TC(tp, init);
183     ATF_TP_ADD_TC(tp, init_pack);
184     ATF_TP_ADD_TC(tp, vars);
185     ATF_TP_ADD_TC(tp, config);
186 
187     /* Add the test cases for the free functions. */
188     /* TODO */
189 
190     /* Add the test cases for the header file. */
191     ATF_TP_ADD_TC(tp, include);
192 
193     return atf_no_error();
194 }
195