2 // Automated Testing Framework (atf)
4 // Copyright (c) 2007 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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.
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.
44 #include <atf-c++.hpp>
48 #include "process.hpp"
50 // ------------------------------------------------------------------------
51 // Auxiliary functions.
52 // ------------------------------------------------------------------------
56 touch(const std::string
& path
)
58 std::ofstream
os(path
.c_str());
60 ATF_FAIL("Could not create file " + path
);
64 // ------------------------------------------------------------------------
65 // Helper tests for "atf-run_test".
66 // ------------------------------------------------------------------------
69 ATF_TEST_CASE_HEAD(pass
)
71 set_md_var("descr", "Helper test case for the t_integration test program");
73 ATF_TEST_CASE_BODY(pass
)
77 ATF_TEST_CASE(config
);
78 ATF_TEST_CASE_HEAD(config
)
80 set_md_var("descr", "Helper test case for the t_integration test program");
82 ATF_TEST_CASE_BODY(config
)
84 std::cout
<< "1st: " << get_config_var("1st") << "\n";
85 std::cout
<< "2nd: " << get_config_var("2nd") << "\n";
86 std::cout
<< "3rd: " << get_config_var("3rd") << "\n";
87 std::cout
<< "4th: " << get_config_var("4th") << "\n";
91 ATF_TEST_CASE_HEAD(fds
)
93 set_md_var("descr", "Helper test case for the t_integration test program");
95 ATF_TEST_CASE_BODY(fds
)
97 std::cout
<< "msg1 to stdout" << "\n";
98 std::cout
<< "msg2 to stdout" << "\n";
99 std::cerr
<< "msg1 to stderr" << "\n";
100 std::cerr
<< "msg2 to stderr" << "\n";
103 ATF_TEST_CASE_WITHOUT_HEAD(mux_streams
);
104 ATF_TEST_CASE_BODY(mux_streams
)
106 for (size_t i
= 0; i
< 10000; i
++) {
109 std::cout
<< "stdout " << i
<< "\n";
112 std::cerr
<< "stderr " << i
<< "\n";
115 std::cout
<< "stdout " << i
<< "\n";
116 std::cerr
<< "stderr " << i
<< "\n";
119 std::cout
<< "stdout " << i
<< "\n";
120 std::cout
<< "stdout " << i
<< "\n";
121 std::cerr
<< "stderr " << i
<< "\n";
124 std::cout
<< "stdout " << i
<< "\n";
125 std::cerr
<< "stderr " << i
<< "\n";
126 std::cerr
<< "stderr " << i
<< "\n";
134 ATF_TEST_CASE(testvar
);
135 ATF_TEST_CASE_HEAD(testvar
)
137 set_md_var("descr", "Helper test case for the t_integration test program");
139 ATF_TEST_CASE_BODY(testvar
)
141 if (!has_config_var("testvar"))
142 fail("testvar variable not defined");
143 std::cout
<< "testvar: " << get_config_var("testvar") << "\n";
146 ATF_TEST_CASE(env_list
);
147 ATF_TEST_CASE_HEAD(env_list
)
149 set_md_var("descr", "Helper test case for the t_integration test program");
151 ATF_TEST_CASE_BODY(env_list
)
153 const tools::process::status s
=
154 tools::process::exec(tools::fs::path("env"),
155 tools::process::argv_array("env", NULL
),
156 tools::process::stream_inherit(),
157 tools::process::stream_inherit());
158 ATF_REQUIRE(s
.exited());
159 ATF_REQUIRE(s
.exitstatus() == EXIT_SUCCESS
);
162 ATF_TEST_CASE(env_home
);
163 ATF_TEST_CASE_HEAD(env_home
)
165 set_md_var("descr", "Helper test case for the t_integration test program");
167 ATF_TEST_CASE_BODY(env_home
)
169 ATF_REQUIRE(tools::env::has("HOME"));
170 tools::fs::path
p(tools::env::get("HOME"));
171 tools::fs::file_info
fi1(p
);
172 tools::fs::file_info
fi2(tools::fs::path("."));
173 ATF_REQUIRE_EQ(fi1
.get_device(), fi2
.get_device());
174 ATF_REQUIRE_EQ(fi1
.get_inode(), fi2
.get_inode());
177 ATF_TEST_CASE(read_stdin
);
178 ATF_TEST_CASE_HEAD(read_stdin
)
180 set_md_var("descr", "Helper test case for the t_integration test program");
182 ATF_TEST_CASE_BODY(read_stdin
)
185 ssize_t len
= ::read(STDIN_FILENO
, buf
, sizeof(buf
) - 1);
186 ATF_REQUIRE(len
!= -1);
189 for (ssize_t i
= 0; i
< len
; i
++) {
190 if (buf
[i
] != '\0') {
191 fail("The stdin of the test case does not seem to be /dev/zero; "
192 "got '" + std::string(buf
) + "'");
197 ATF_TEST_CASE(umask
);
198 ATF_TEST_CASE_HEAD(umask
)
200 set_md_var("descr", "Helper test case for the t_integration test program");
202 ATF_TEST_CASE_BODY(umask
)
204 mode_t m
= ::umask(0);
205 std::cout
<< "umask: " << std::setw(4) << std::setfill('0')
206 << std::oct
<< m
<< "\n";
210 ATF_TEST_CASE_WITH_CLEANUP(cleanup_states
);
211 ATF_TEST_CASE_HEAD(cleanup_states
)
213 set_md_var("descr", "Helper test case for the t_integration test program");
215 ATF_TEST_CASE_BODY(cleanup_states
)
217 touch(get_config_var("statedir") + "/to-delete");
218 touch(get_config_var("statedir") + "/to-stay");
220 if (get_config_var("state") == "fail")
221 ATF_FAIL("On purpose");
222 else if (get_config_var("state") == "skip")
223 ATF_SKIP("On purpose");
225 ATF_TEST_CASE_CLEANUP(cleanup_states
)
227 tools::fs::remove(tools::fs::path(get_config_var("statedir") + "/to-delete"));
230 ATF_TEST_CASE_WITH_CLEANUP(cleanup_curdir
);
231 ATF_TEST_CASE_HEAD(cleanup_curdir
)
233 set_md_var("descr", "Helper test case for the t_integration test program");
235 ATF_TEST_CASE_BODY(cleanup_curdir
)
237 std::ofstream
os("oldvalue");
239 ATF_FAIL("Failed to create oldvalue file");
243 ATF_TEST_CASE_CLEANUP(cleanup_curdir
)
245 std::ifstream
is("oldvalue");
249 std::cout
<< "Old value: " << i
<< "\n";
254 ATF_TEST_CASE(require_arch
);
255 ATF_TEST_CASE_HEAD(require_arch
)
257 set_md_var("descr", "Helper test case for the t_integration test program");
258 set_md_var("require.arch", get_config_var("arch", "not-set"));
260 ATF_TEST_CASE_BODY(require_arch
)
264 ATF_TEST_CASE(require_config
);
265 ATF_TEST_CASE_HEAD(require_config
)
267 set_md_var("descr", "Helper test case for the t_integration test program");
268 set_md_var("require.config", "var1 var2");
270 ATF_TEST_CASE_BODY(require_config
)
272 std::cout
<< "var1: " << get_config_var("var1") << "\n";
273 std::cout
<< "var2: " << get_config_var("var2") << "\n";
276 ATF_TEST_CASE(require_files
);
277 ATF_TEST_CASE_HEAD(require_files
)
279 set_md_var("descr", "Helper test case for the t_integration test program");
280 set_md_var("require.files", get_config_var("files", "not-set"));
282 ATF_TEST_CASE_BODY(require_files
)
286 ATF_TEST_CASE(require_machine
);
287 ATF_TEST_CASE_HEAD(require_machine
)
289 set_md_var("descr", "Helper test case for the t_integration test program");
290 set_md_var("require.machine", get_config_var("machine", "not-set"));
292 ATF_TEST_CASE_BODY(require_machine
)
296 ATF_TEST_CASE(require_progs
);
297 ATF_TEST_CASE_HEAD(require_progs
)
299 set_md_var("descr", "Helper test case for the t_integration test program");
300 set_md_var("require.progs", get_config_var("progs", "not-set"));
302 ATF_TEST_CASE_BODY(require_progs
)
306 ATF_TEST_CASE(require_user
);
307 ATF_TEST_CASE_HEAD(require_user
)
309 set_md_var("descr", "Helper test case for the t_integration test program");
310 set_md_var("require.user", get_config_var("user", "not-set"));
312 ATF_TEST_CASE_BODY(require_user
)
316 ATF_TEST_CASE(timeout
);
317 ATF_TEST_CASE_HEAD(timeout
)
319 set_md_var("descr", "Helper test case for the t_integration test program");
320 set_md_var("timeout", "1");
322 ATF_TEST_CASE_BODY(timeout
)
325 touch(get_config_var("statedir") + "/finished");
328 ATF_TEST_CASE(timeout_forkexit
);
329 ATF_TEST_CASE_HEAD(timeout_forkexit
)
331 set_md_var("descr", "Helper test case for the t_integration test program");
333 ATF_TEST_CASE_BODY(timeout_forkexit
)
336 ATF_REQUIRE(pid
!= -1);
342 std::cout
<< "Waiting in subprocess\n";
346 touch(get_config_var("statedir") + "/child-finished");
347 std::cout
<< "Subprocess exiting\n";
351 // Don't wait for the child process and let atf-run deal with it.
352 touch(get_config_var("statedir") + "/parent-finished");
353 std::cout
<< "Parent process exiting\n";
358 ATF_TEST_CASE(use_fs
);
359 ATF_TEST_CASE_HEAD(use_fs
)
361 set_md_var("descr", "Helper test case for the t_integration test program");
362 set_md_var("use.fs", "this-is-deprecated");
364 ATF_TEST_CASE_BODY(use_fs
)
369 // ------------------------------------------------------------------------
370 // Helper tests for "atf-report_test".
371 // ------------------------------------------------------------------------
374 ATF_TEST_CASE_HEAD(diff
)
376 set_md_var("descr", "Helper test case for the t_integration program");
378 ATF_TEST_CASE_BODY(diff
)
380 std::cout
<< "--- a 2007-11-04 14:00:41.000000000 +0100\n";
381 std::cout
<< "+++ b 2007-11-04 14:00:48.000000000 +0100\n";
382 std::cout
<< "@@ -1,7 +1,7 @@\n";
383 std::cout
<< " This test is meant to simulate a diff.\n";
384 std::cout
<< " Blank space at beginning of context lines must be "
387 std::cout
<< "-First original line.\n";
388 std::cout
<< "-Second original line.\n";
389 std::cout
<< "+First modified line.\n";
390 std::cout
<< "+Second modified line.\n";
392 std::cout
<< " EOF\n";
395 // ------------------------------------------------------------------------
397 // ------------------------------------------------------------------------
399 ATF_INIT_TEST_CASES(tcs
)
401 std::string which
= tools::env::get("TESTCASE");
403 // Add helper tests for atf-run_test.
405 ATF_ADD_TEST_CASE(tcs
, pass
);
406 if (which
== "config")
407 ATF_ADD_TEST_CASE(tcs
, config
);
409 ATF_ADD_TEST_CASE(tcs
, fds
);
410 if (which
== "mux_streams")
411 ATF_ADD_TEST_CASE(tcs
, mux_streams
);
412 if (which
== "testvar")
413 ATF_ADD_TEST_CASE(tcs
, testvar
);
414 if (which
== "env_list")
415 ATF_ADD_TEST_CASE(tcs
, env_list
);
416 if (which
== "env_home")
417 ATF_ADD_TEST_CASE(tcs
, env_home
);
418 if (which
== "read_stdin")
419 ATF_ADD_TEST_CASE(tcs
, read_stdin
);
420 if (which
== "umask")
421 ATF_ADD_TEST_CASE(tcs
, umask
);
422 if (which
== "cleanup_states")
423 ATF_ADD_TEST_CASE(tcs
, cleanup_states
);
424 if (which
== "cleanup_curdir")
425 ATF_ADD_TEST_CASE(tcs
, cleanup_curdir
);
426 if (which
== "require_arch")
427 ATF_ADD_TEST_CASE(tcs
, require_arch
);
428 if (which
== "require_config")
429 ATF_ADD_TEST_CASE(tcs
, require_config
);
430 if (which
== "require_files")
431 ATF_ADD_TEST_CASE(tcs
, require_files
);
432 if (which
== "require_machine")
433 ATF_ADD_TEST_CASE(tcs
, require_machine
);
434 if (which
== "require_progs")
435 ATF_ADD_TEST_CASE(tcs
, require_progs
);
436 if (which
== "require_user")
437 ATF_ADD_TEST_CASE(tcs
, require_user
);
438 if (which
== "timeout")
439 ATF_ADD_TEST_CASE(tcs
, timeout
);
440 if (which
== "timeout_forkexit")
441 ATF_ADD_TEST_CASE(tcs
, timeout_forkexit
);
442 if (which
== "use_fs")
443 ATF_ADD_TEST_CASE(tcs
, use_fs
);
445 // Add helper tests for atf-report_test.
447 ATF_ADD_TEST_CASE(tcs
, diff
);