xref: /freebsd/contrib/atf/test-programs/cpp_helpers.cpp (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 // Copyright (c) 2007 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 extern "C" {
27 #include <signal.h>
28 #include <unistd.h>
29 }
30 
31 #include <cstdlib>
32 #include <fstream>
33 #include <iostream>
34 
35 #include <atf-c++.hpp>
36 
37 #include "atf-c++/detail/fs.hpp"
38 
39 // ------------------------------------------------------------------------
40 // Helper tests for "t_config".
41 // ------------------------------------------------------------------------
42 
43 ATF_TEST_CASE(config_unset);
44 ATF_TEST_CASE_HEAD(config_unset)
45 {
46     set_md_var("descr", "Helper test case for the t_config test program");
47 }
48 ATF_TEST_CASE_BODY(config_unset)
49 {
50     ATF_REQUIRE(!has_config_var("test"));
51 }
52 
53 ATF_TEST_CASE(config_empty);
54 ATF_TEST_CASE_HEAD(config_empty)
55 {
56     set_md_var("descr", "Helper test case for the t_config test program");
57 }
58 ATF_TEST_CASE_BODY(config_empty)
59 {
60     ATF_REQUIRE_EQ(get_config_var("test"), "");
61 }
62 
63 ATF_TEST_CASE(config_value);
64 ATF_TEST_CASE_HEAD(config_value)
65 {
66     set_md_var("descr", "Helper test case for the t_config test program");
67 }
68 ATF_TEST_CASE_BODY(config_value)
69 {
70     ATF_REQUIRE_EQ(get_config_var("test"), "foo");
71 }
72 
73 ATF_TEST_CASE(config_multi_value);
74 ATF_TEST_CASE_HEAD(config_multi_value)
75 {
76     set_md_var("descr", "Helper test case for the t_config test program");
77 }
78 ATF_TEST_CASE_BODY(config_multi_value)
79 {
80     ATF_REQUIRE_EQ(get_config_var("test"), "foo bar");
81 }
82 
83 // ------------------------------------------------------------------------
84 // Helper tests for "t_expect".
85 // ------------------------------------------------------------------------
86 
87 ATF_TEST_CASE_WITHOUT_HEAD(expect_pass_and_pass);
88 ATF_TEST_CASE_BODY(expect_pass_and_pass)
89 {
90     expect_pass();
91 }
92 
93 ATF_TEST_CASE_WITHOUT_HEAD(expect_pass_but_fail_requirement);
94 ATF_TEST_CASE_BODY(expect_pass_but_fail_requirement)
95 {
96     expect_pass();
97     fail("Some reason");
98 }
99 
100 ATF_TEST_CASE_WITHOUT_HEAD(expect_pass_but_fail_check);
101 ATF_TEST_CASE_BODY(expect_pass_but_fail_check)
102 {
103     expect_pass();
104     fail_nonfatal("Some reason");
105 }
106 
107 ATF_TEST_CASE_WITHOUT_HEAD(expect_fail_and_fail_requirement);
108 ATF_TEST_CASE_BODY(expect_fail_and_fail_requirement)
109 {
110     expect_fail("Fail reason");
111     fail("The failure");
112     expect_pass();
113 }
114 
115 ATF_TEST_CASE_WITHOUT_HEAD(expect_fail_and_fail_check);
116 ATF_TEST_CASE_BODY(expect_fail_and_fail_check)
117 {
118     expect_fail("Fail first");
119     fail_nonfatal("abc");
120     expect_pass();
121 
122     expect_fail("And fail again");
123     fail_nonfatal("def");
124     expect_pass();
125 }
126 
127 ATF_TEST_CASE_WITHOUT_HEAD(expect_fail_but_pass);
128 ATF_TEST_CASE_BODY(expect_fail_but_pass)
129 {
130     expect_fail("Fail first");
131     fail_nonfatal("abc");
132     expect_pass();
133 
134     expect_fail("Will not fail");
135     expect_pass();
136 
137     expect_fail("And fail again");
138     fail_nonfatal("def");
139     expect_pass();
140 }
141 
142 ATF_TEST_CASE_WITHOUT_HEAD(expect_exit_any_and_exit);
143 ATF_TEST_CASE_BODY(expect_exit_any_and_exit)
144 {
145     expect_exit(-1, "Call will exit");
146     std::exit(EXIT_SUCCESS);
147 }
148 
149 ATF_TEST_CASE_WITHOUT_HEAD(expect_exit_code_and_exit);
150 ATF_TEST_CASE_BODY(expect_exit_code_and_exit)
151 {
152     expect_exit(123, "Call will exit");
153     std::exit(123);
154 }
155 
156 ATF_TEST_CASE_WITHOUT_HEAD(expect_exit_but_pass);
157 ATF_TEST_CASE_BODY(expect_exit_but_pass)
158 {
159     expect_exit(-1, "Call won't exit");
160 }
161 
162 ATF_TEST_CASE_WITHOUT_HEAD(expect_signal_any_and_signal);
163 ATF_TEST_CASE_BODY(expect_signal_any_and_signal)
164 {
165     expect_signal(-1, "Call will signal");
166     ::kill(getpid(), SIGKILL);
167 }
168 
169 ATF_TEST_CASE_WITHOUT_HEAD(expect_signal_no_and_signal);
170 ATF_TEST_CASE_BODY(expect_signal_no_and_signal)
171 {
172     expect_signal(SIGHUP, "Call will signal");
173     ::kill(getpid(), SIGHUP);
174 }
175 
176 ATF_TEST_CASE_WITHOUT_HEAD(expect_signal_but_pass);
177 ATF_TEST_CASE_BODY(expect_signal_but_pass)
178 {
179     expect_signal(-1, "Call won't signal");
180 }
181 
182 ATF_TEST_CASE_WITHOUT_HEAD(expect_death_and_exit);
183 ATF_TEST_CASE_BODY(expect_death_and_exit)
184 {
185     expect_death("Exit case");
186     std::exit(123);
187 }
188 
189 ATF_TEST_CASE_WITHOUT_HEAD(expect_death_and_signal);
190 ATF_TEST_CASE_BODY(expect_death_and_signal)
191 {
192     expect_death("Signal case");
193     kill(getpid(), SIGKILL);
194 }
195 
196 ATF_TEST_CASE_WITHOUT_HEAD(expect_death_but_pass);
197 ATF_TEST_CASE_BODY(expect_death_but_pass)
198 {
199     expect_death("Call won't die");
200 }
201 
202 ATF_TEST_CASE(expect_timeout_and_hang);
203 ATF_TEST_CASE_HEAD(expect_timeout_and_hang)
204 {
205     set_md_var("timeout", "1");
206 }
207 ATF_TEST_CASE_BODY(expect_timeout_and_hang)
208 {
209     expect_timeout("Will overrun");
210     ::sleep(5);
211 }
212 
213 ATF_TEST_CASE(expect_timeout_but_pass);
214 ATF_TEST_CASE_HEAD(expect_timeout_but_pass)
215 {
216     set_md_var("timeout", "1");
217 }
218 ATF_TEST_CASE_BODY(expect_timeout_but_pass)
219 {
220     expect_timeout("Will just exit");
221 }
222 
223 // ------------------------------------------------------------------------
224 // Helper tests for "t_meta_data".
225 // ------------------------------------------------------------------------
226 
227 ATF_TEST_CASE(metadata_no_descr);
228 ATF_TEST_CASE_HEAD(metadata_no_descr)
229 {
230 }
231 ATF_TEST_CASE_BODY(metadata_no_descr)
232 {
233 }
234 
235 ATF_TEST_CASE_WITHOUT_HEAD(metadata_no_head);
236 ATF_TEST_CASE_BODY(metadata_no_head)
237 {
238 }
239 
240 // ------------------------------------------------------------------------
241 // Helper tests for "t_srcdir".
242 // ------------------------------------------------------------------------
243 
244 ATF_TEST_CASE(srcdir_exists);
245 ATF_TEST_CASE_HEAD(srcdir_exists)
246 {
247     set_md_var("descr", "Helper test case for the t_srcdir test program");
248 }
249 ATF_TEST_CASE_BODY(srcdir_exists)
250 {
251     if (!atf::fs::exists(atf::fs::path(get_config_var("srcdir")) /
252         "datafile"))
253         ATF_FAIL("Cannot find datafile");
254 }
255 
256 // ------------------------------------------------------------------------
257 // Helper tests for "t_result".
258 // ------------------------------------------------------------------------
259 
260 ATF_TEST_CASE(result_pass);
261 ATF_TEST_CASE_HEAD(result_pass) { }
262 ATF_TEST_CASE_BODY(result_pass)
263 {
264     std::cout << "msg\n";
265 }
266 
267 ATF_TEST_CASE(result_fail);
268 ATF_TEST_CASE_HEAD(result_fail) { }
269 ATF_TEST_CASE_BODY(result_fail)
270 {
271     std::cout << "msg\n";
272     ATF_FAIL("Failure reason");
273 }
274 
275 ATF_TEST_CASE(result_skip);
276 ATF_TEST_CASE_HEAD(result_skip) { }
277 ATF_TEST_CASE_BODY(result_skip)
278 {
279     std::cout << "msg\n";
280     ATF_SKIP("Skipped reason");
281 }
282 
283 ATF_TEST_CASE(result_newlines_fail);
284 ATF_TEST_CASE_HEAD(result_newlines_fail)
285 {
286     set_md_var("descr", "Helper test case for the t_result test program");
287 }
288 ATF_TEST_CASE_BODY(result_newlines_fail)
289 {
290     ATF_FAIL("First line\nSecond line");
291 }
292 
293 ATF_TEST_CASE(result_newlines_skip);
294 ATF_TEST_CASE_HEAD(result_newlines_skip)
295 {
296     set_md_var("descr", "Helper test case for the t_result test program");
297 }
298 ATF_TEST_CASE_BODY(result_newlines_skip)
299 {
300     ATF_SKIP("First line\nSecond line");
301 }
302 
303 ATF_TEST_CASE(result_exception);
304 ATF_TEST_CASE_HEAD(result_exception) { }
305 ATF_TEST_CASE_BODY(result_exception)
306 {
307     throw std::runtime_error("This is unhandled");
308 }
309 
310 // ------------------------------------------------------------------------
311 // Main.
312 // ------------------------------------------------------------------------
313 
314 ATF_INIT_TEST_CASES(tcs)
315 {
316     // Add helper tests for t_config.
317     ATF_ADD_TEST_CASE(tcs, config_unset);
318     ATF_ADD_TEST_CASE(tcs, config_empty);
319     ATF_ADD_TEST_CASE(tcs, config_value);
320     ATF_ADD_TEST_CASE(tcs, config_multi_value);
321 
322     // Add helper tests for t_expect.
323     ATF_ADD_TEST_CASE(tcs, expect_pass_and_pass);
324     ATF_ADD_TEST_CASE(tcs, expect_pass_but_fail_requirement);
325     ATF_ADD_TEST_CASE(tcs, expect_pass_but_fail_check);
326     ATF_ADD_TEST_CASE(tcs, expect_fail_and_fail_requirement);
327     ATF_ADD_TEST_CASE(tcs, expect_fail_and_fail_check);
328     ATF_ADD_TEST_CASE(tcs, expect_fail_but_pass);
329     ATF_ADD_TEST_CASE(tcs, expect_exit_any_and_exit);
330     ATF_ADD_TEST_CASE(tcs, expect_exit_code_and_exit);
331     ATF_ADD_TEST_CASE(tcs, expect_exit_but_pass);
332     ATF_ADD_TEST_CASE(tcs, expect_signal_any_and_signal);
333     ATF_ADD_TEST_CASE(tcs, expect_signal_no_and_signal);
334     ATF_ADD_TEST_CASE(tcs, expect_signal_but_pass);
335     ATF_ADD_TEST_CASE(tcs, expect_death_and_exit);
336     ATF_ADD_TEST_CASE(tcs, expect_death_and_signal);
337     ATF_ADD_TEST_CASE(tcs, expect_death_but_pass);
338     ATF_ADD_TEST_CASE(tcs, expect_timeout_and_hang);
339     ATF_ADD_TEST_CASE(tcs, expect_timeout_but_pass);
340 
341     // Add helper tests for t_meta_data.
342     ATF_ADD_TEST_CASE(tcs, metadata_no_descr);
343     ATF_ADD_TEST_CASE(tcs, metadata_no_head);
344 
345     // Add helper tests for t_srcdir.
346     ATF_ADD_TEST_CASE(tcs, srcdir_exists);
347 
348     // Add helper tests for t_result.
349     ATF_ADD_TEST_CASE(tcs, result_pass);
350     ATF_ADD_TEST_CASE(tcs, result_fail);
351     ATF_ADD_TEST_CASE(tcs, result_skip);
352     ATF_ADD_TEST_CASE(tcs, result_newlines_fail);
353     ATF_ADD_TEST_CASE(tcs, result_newlines_skip);
354     ATF_ADD_TEST_CASE(tcs, result_exception);
355 }
356