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