xref: /minix3/external/bsd/atf/dist/tools/misc_helpers.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //
2*0a6a1f1dSLionel Sambuc // Automated Testing Framework (atf)
3*0a6a1f1dSLionel Sambuc //
4*0a6a1f1dSLionel Sambuc // Copyright (c) 2007 The NetBSD Foundation, Inc.
5*0a6a1f1dSLionel Sambuc // All rights reserved.
6*0a6a1f1dSLionel Sambuc //
7*0a6a1f1dSLionel Sambuc // Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc // modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc // are met:
10*0a6a1f1dSLionel Sambuc // 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc //    notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc //    notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc //    documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc //
16*0a6a1f1dSLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*0a6a1f1dSLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*0a6a1f1dSLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*0a6a1f1dSLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*0a6a1f1dSLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*0a6a1f1dSLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*0a6a1f1dSLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*0a6a1f1dSLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*0a6a1f1dSLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*0a6a1f1dSLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*0a6a1f1dSLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*0a6a1f1dSLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*0a6a1f1dSLionel Sambuc //
29*0a6a1f1dSLionel Sambuc 
30*0a6a1f1dSLionel Sambuc extern "C" {
31*0a6a1f1dSLionel Sambuc #include <sys/stat.h>
32*0a6a1f1dSLionel Sambuc 
33*0a6a1f1dSLionel Sambuc #include <signal.h>
34*0a6a1f1dSLionel Sambuc #include <unistd.h>
35*0a6a1f1dSLionel Sambuc }
36*0a6a1f1dSLionel Sambuc 
37*0a6a1f1dSLionel Sambuc #include <cstdlib>
38*0a6a1f1dSLionel Sambuc #include <fstream>
39*0a6a1f1dSLionel Sambuc #include <iomanip>
40*0a6a1f1dSLionel Sambuc #include <ios>
41*0a6a1f1dSLionel Sambuc #include <iostream>
42*0a6a1f1dSLionel Sambuc #include <string>
43*0a6a1f1dSLionel Sambuc 
44*0a6a1f1dSLionel Sambuc #include <atf-c++.hpp>
45*0a6a1f1dSLionel Sambuc 
46*0a6a1f1dSLionel Sambuc #include "env.hpp"
47*0a6a1f1dSLionel Sambuc #include "fs.hpp"
48*0a6a1f1dSLionel Sambuc #include "process.hpp"
49*0a6a1f1dSLionel Sambuc 
50*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
51*0a6a1f1dSLionel Sambuc // Auxiliary functions.
52*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
53*0a6a1f1dSLionel Sambuc 
54*0a6a1f1dSLionel Sambuc static
55*0a6a1f1dSLionel Sambuc void
touch(const std::string & path)56*0a6a1f1dSLionel Sambuc touch(const std::string& path)
57*0a6a1f1dSLionel Sambuc {
58*0a6a1f1dSLionel Sambuc     std::ofstream os(path.c_str());
59*0a6a1f1dSLionel Sambuc     if (!os)
60*0a6a1f1dSLionel Sambuc         ATF_FAIL("Could not create file " + path);
61*0a6a1f1dSLionel Sambuc     os.close();
62*0a6a1f1dSLionel Sambuc }
63*0a6a1f1dSLionel Sambuc 
64*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
65*0a6a1f1dSLionel Sambuc // Helper tests for "atf-run_test".
66*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
67*0a6a1f1dSLionel Sambuc 
68*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(pass);
ATF_TEST_CASE_HEAD(pass)69*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(pass)
70*0a6a1f1dSLionel Sambuc {
71*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
72*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(pass)73*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(pass)
74*0a6a1f1dSLionel Sambuc {
75*0a6a1f1dSLionel Sambuc }
76*0a6a1f1dSLionel Sambuc 
77*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(config);
ATF_TEST_CASE_HEAD(config)78*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(config)
79*0a6a1f1dSLionel Sambuc {
80*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
81*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(config)82*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(config)
83*0a6a1f1dSLionel Sambuc {
84*0a6a1f1dSLionel Sambuc     std::cout << "1st: " << get_config_var("1st") << "\n";
85*0a6a1f1dSLionel Sambuc     std::cout << "2nd: " << get_config_var("2nd") << "\n";
86*0a6a1f1dSLionel Sambuc     std::cout << "3rd: " << get_config_var("3rd") << "\n";
87*0a6a1f1dSLionel Sambuc     std::cout << "4th: " << get_config_var("4th") << "\n";
88*0a6a1f1dSLionel Sambuc }
89*0a6a1f1dSLionel Sambuc 
90*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(fds);
ATF_TEST_CASE_HEAD(fds)91*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(fds)
92*0a6a1f1dSLionel Sambuc {
93*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
94*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(fds)95*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(fds)
96*0a6a1f1dSLionel Sambuc {
97*0a6a1f1dSLionel Sambuc     std::cout << "msg1 to stdout" << "\n";
98*0a6a1f1dSLionel Sambuc     std::cout << "msg2 to stdout" << "\n";
99*0a6a1f1dSLionel Sambuc     std::cerr << "msg1 to stderr" << "\n";
100*0a6a1f1dSLionel Sambuc     std::cerr << "msg2 to stderr" << "\n";
101*0a6a1f1dSLionel Sambuc }
102*0a6a1f1dSLionel Sambuc 
103*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(mux_streams);
ATF_TEST_CASE_BODY(mux_streams)104*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(mux_streams)
105*0a6a1f1dSLionel Sambuc {
106*0a6a1f1dSLionel Sambuc     for (size_t i = 0; i < 10000; i++) {
107*0a6a1f1dSLionel Sambuc         switch (i % 5) {
108*0a6a1f1dSLionel Sambuc         case 0:
109*0a6a1f1dSLionel Sambuc             std::cout << "stdout " << i << "\n";
110*0a6a1f1dSLionel Sambuc             break;
111*0a6a1f1dSLionel Sambuc         case 1:
112*0a6a1f1dSLionel Sambuc             std::cerr << "stderr " << i << "\n";
113*0a6a1f1dSLionel Sambuc             break;
114*0a6a1f1dSLionel Sambuc         case 2:
115*0a6a1f1dSLionel Sambuc             std::cout << "stdout " << i << "\n";
116*0a6a1f1dSLionel Sambuc             std::cerr << "stderr " << i << "\n";
117*0a6a1f1dSLionel Sambuc             break;
118*0a6a1f1dSLionel Sambuc         case 3:
119*0a6a1f1dSLionel Sambuc             std::cout << "stdout " << i << "\n";
120*0a6a1f1dSLionel Sambuc             std::cout << "stdout " << i << "\n";
121*0a6a1f1dSLionel Sambuc             std::cerr << "stderr " << i << "\n";
122*0a6a1f1dSLionel Sambuc             break;
123*0a6a1f1dSLionel Sambuc         case 4:
124*0a6a1f1dSLionel Sambuc             std::cout << "stdout " << i << "\n";
125*0a6a1f1dSLionel Sambuc             std::cerr << "stderr " << i << "\n";
126*0a6a1f1dSLionel Sambuc             std::cerr << "stderr " << i << "\n";
127*0a6a1f1dSLionel Sambuc             break;
128*0a6a1f1dSLionel Sambuc         default:
129*0a6a1f1dSLionel Sambuc             std::abort();
130*0a6a1f1dSLionel Sambuc         }
131*0a6a1f1dSLionel Sambuc     }
132*0a6a1f1dSLionel Sambuc }
133*0a6a1f1dSLionel Sambuc 
134*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(testvar);
ATF_TEST_CASE_HEAD(testvar)135*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(testvar)
136*0a6a1f1dSLionel Sambuc {
137*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
138*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(testvar)139*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(testvar)
140*0a6a1f1dSLionel Sambuc {
141*0a6a1f1dSLionel Sambuc     if (!has_config_var("testvar"))
142*0a6a1f1dSLionel Sambuc         fail("testvar variable not defined");
143*0a6a1f1dSLionel Sambuc     std::cout << "testvar: " << get_config_var("testvar") << "\n";
144*0a6a1f1dSLionel Sambuc }
145*0a6a1f1dSLionel Sambuc 
146*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(env_list);
ATF_TEST_CASE_HEAD(env_list)147*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(env_list)
148*0a6a1f1dSLionel Sambuc {
149*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
150*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(env_list)151*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(env_list)
152*0a6a1f1dSLionel Sambuc {
153*0a6a1f1dSLionel Sambuc     const tools::process::status s =
154*0a6a1f1dSLionel Sambuc         tools::process::exec(tools::fs::path("env"),
155*0a6a1f1dSLionel Sambuc                            tools::process::argv_array("env", NULL),
156*0a6a1f1dSLionel Sambuc                            tools::process::stream_inherit(),
157*0a6a1f1dSLionel Sambuc                            tools::process::stream_inherit());
158*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(s.exited());
159*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(s.exitstatus() == EXIT_SUCCESS);
160*0a6a1f1dSLionel Sambuc }
161*0a6a1f1dSLionel Sambuc 
162*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(env_home);
ATF_TEST_CASE_HEAD(env_home)163*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(env_home)
164*0a6a1f1dSLionel Sambuc {
165*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
166*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(env_home)167*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(env_home)
168*0a6a1f1dSLionel Sambuc {
169*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(tools::env::has("HOME"));
170*0a6a1f1dSLionel Sambuc     tools::fs::path p(tools::env::get("HOME"));
171*0a6a1f1dSLionel Sambuc     tools::fs::file_info fi1(p);
172*0a6a1f1dSLionel Sambuc     tools::fs::file_info fi2(tools::fs::path("."));
173*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(fi1.get_device(), fi2.get_device());
174*0a6a1f1dSLionel Sambuc     ATF_REQUIRE_EQ(fi1.get_inode(), fi2.get_inode());
175*0a6a1f1dSLionel Sambuc }
176*0a6a1f1dSLionel Sambuc 
177*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(read_stdin);
ATF_TEST_CASE_HEAD(read_stdin)178*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(read_stdin)
179*0a6a1f1dSLionel Sambuc {
180*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
181*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(read_stdin)182*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(read_stdin)
183*0a6a1f1dSLionel Sambuc {
184*0a6a1f1dSLionel Sambuc     char buf[100];
185*0a6a1f1dSLionel Sambuc     ssize_t len = ::read(STDIN_FILENO, buf, sizeof(buf) - 1);
186*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(len != -1);
187*0a6a1f1dSLionel Sambuc 
188*0a6a1f1dSLionel Sambuc     buf[len + 1] = '\0';
189*0a6a1f1dSLionel Sambuc     for (ssize_t i = 0; i < len; i++) {
190*0a6a1f1dSLionel Sambuc         if (buf[i] != '\0') {
191*0a6a1f1dSLionel Sambuc             fail("The stdin of the test case does not seem to be /dev/zero; "
192*0a6a1f1dSLionel Sambuc                  "got '" + std::string(buf) + "'");
193*0a6a1f1dSLionel Sambuc         }
194*0a6a1f1dSLionel Sambuc     }
195*0a6a1f1dSLionel Sambuc }
196*0a6a1f1dSLionel Sambuc 
197*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(umask);
ATF_TEST_CASE_HEAD(umask)198*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(umask)
199*0a6a1f1dSLionel Sambuc {
200*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
201*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(umask)202*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(umask)
203*0a6a1f1dSLionel Sambuc {
204*0a6a1f1dSLionel Sambuc     mode_t m = ::umask(0);
205*0a6a1f1dSLionel Sambuc     std::cout << "umask: " << std::setw(4) << std::setfill('0')
206*0a6a1f1dSLionel Sambuc               << std::oct << m << "\n";
207*0a6a1f1dSLionel Sambuc     (void)::umask(m);
208*0a6a1f1dSLionel Sambuc }
209*0a6a1f1dSLionel Sambuc 
210*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_WITH_CLEANUP(cleanup_states);
ATF_TEST_CASE_HEAD(cleanup_states)211*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(cleanup_states)
212*0a6a1f1dSLionel Sambuc {
213*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
214*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(cleanup_states)215*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(cleanup_states)
216*0a6a1f1dSLionel Sambuc {
217*0a6a1f1dSLionel Sambuc     touch(get_config_var("statedir") + "/to-delete");
218*0a6a1f1dSLionel Sambuc     touch(get_config_var("statedir") + "/to-stay");
219*0a6a1f1dSLionel Sambuc 
220*0a6a1f1dSLionel Sambuc     if (get_config_var("state") == "fail")
221*0a6a1f1dSLionel Sambuc         ATF_FAIL("On purpose");
222*0a6a1f1dSLionel Sambuc     else if (get_config_var("state") == "skip")
223*0a6a1f1dSLionel Sambuc         ATF_SKIP("On purpose");
224*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_CLEANUP(cleanup_states)225*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_CLEANUP(cleanup_states)
226*0a6a1f1dSLionel Sambuc {
227*0a6a1f1dSLionel Sambuc     tools::fs::remove(tools::fs::path(get_config_var("statedir") + "/to-delete"));
228*0a6a1f1dSLionel Sambuc }
229*0a6a1f1dSLionel Sambuc 
230*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_WITH_CLEANUP(cleanup_curdir);
ATF_TEST_CASE_HEAD(cleanup_curdir)231*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(cleanup_curdir)
232*0a6a1f1dSLionel Sambuc {
233*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
234*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(cleanup_curdir)235*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(cleanup_curdir)
236*0a6a1f1dSLionel Sambuc {
237*0a6a1f1dSLionel Sambuc     std::ofstream os("oldvalue");
238*0a6a1f1dSLionel Sambuc     if (!os)
239*0a6a1f1dSLionel Sambuc         ATF_FAIL("Failed to create oldvalue file");
240*0a6a1f1dSLionel Sambuc     os << 1234;
241*0a6a1f1dSLionel Sambuc     os.close();
242*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_CLEANUP(cleanup_curdir)243*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_CLEANUP(cleanup_curdir)
244*0a6a1f1dSLionel Sambuc {
245*0a6a1f1dSLionel Sambuc     std::ifstream is("oldvalue");
246*0a6a1f1dSLionel Sambuc     if (is) {
247*0a6a1f1dSLionel Sambuc         int i;
248*0a6a1f1dSLionel Sambuc         is >> i;
249*0a6a1f1dSLionel Sambuc         std::cout << "Old value: " << i << "\n";
250*0a6a1f1dSLionel Sambuc         is.close();
251*0a6a1f1dSLionel Sambuc     }
252*0a6a1f1dSLionel Sambuc }
253*0a6a1f1dSLionel Sambuc 
254*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(require_arch);
ATF_TEST_CASE_HEAD(require_arch)255*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(require_arch)
256*0a6a1f1dSLionel Sambuc {
257*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
258*0a6a1f1dSLionel Sambuc     set_md_var("require.arch", get_config_var("arch", "not-set"));
259*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(require_arch)260*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(require_arch)
261*0a6a1f1dSLionel Sambuc {
262*0a6a1f1dSLionel Sambuc }
263*0a6a1f1dSLionel Sambuc 
264*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(require_config);
ATF_TEST_CASE_HEAD(require_config)265*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(require_config)
266*0a6a1f1dSLionel Sambuc {
267*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
268*0a6a1f1dSLionel Sambuc     set_md_var("require.config", "var1 var2");
269*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(require_config)270*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(require_config)
271*0a6a1f1dSLionel Sambuc {
272*0a6a1f1dSLionel Sambuc     std::cout << "var1: " << get_config_var("var1") << "\n";
273*0a6a1f1dSLionel Sambuc     std::cout << "var2: " << get_config_var("var2") << "\n";
274*0a6a1f1dSLionel Sambuc }
275*0a6a1f1dSLionel Sambuc 
276*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(require_files);
ATF_TEST_CASE_HEAD(require_files)277*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(require_files)
278*0a6a1f1dSLionel Sambuc {
279*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
280*0a6a1f1dSLionel Sambuc     set_md_var("require.files", get_config_var("files", "not-set"));
281*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(require_files)282*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(require_files)
283*0a6a1f1dSLionel Sambuc {
284*0a6a1f1dSLionel Sambuc }
285*0a6a1f1dSLionel Sambuc 
286*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(require_machine);
ATF_TEST_CASE_HEAD(require_machine)287*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(require_machine)
288*0a6a1f1dSLionel Sambuc {
289*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
290*0a6a1f1dSLionel Sambuc     set_md_var("require.machine", get_config_var("machine", "not-set"));
291*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(require_machine)292*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(require_machine)
293*0a6a1f1dSLionel Sambuc {
294*0a6a1f1dSLionel Sambuc }
295*0a6a1f1dSLionel Sambuc 
296*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(require_progs);
ATF_TEST_CASE_HEAD(require_progs)297*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(require_progs)
298*0a6a1f1dSLionel Sambuc {
299*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
300*0a6a1f1dSLionel Sambuc     set_md_var("require.progs", get_config_var("progs", "not-set"));
301*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(require_progs)302*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(require_progs)
303*0a6a1f1dSLionel Sambuc {
304*0a6a1f1dSLionel Sambuc }
305*0a6a1f1dSLionel Sambuc 
306*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(require_user);
ATF_TEST_CASE_HEAD(require_user)307*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(require_user)
308*0a6a1f1dSLionel Sambuc {
309*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
310*0a6a1f1dSLionel Sambuc     set_md_var("require.user", get_config_var("user", "not-set"));
311*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(require_user)312*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(require_user)
313*0a6a1f1dSLionel Sambuc {
314*0a6a1f1dSLionel Sambuc }
315*0a6a1f1dSLionel Sambuc 
316*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(timeout);
ATF_TEST_CASE_HEAD(timeout)317*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(timeout)
318*0a6a1f1dSLionel Sambuc {
319*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
320*0a6a1f1dSLionel Sambuc     set_md_var("timeout", "1");
321*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(timeout)322*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(timeout)
323*0a6a1f1dSLionel Sambuc {
324*0a6a1f1dSLionel Sambuc     sleep(10);
325*0a6a1f1dSLionel Sambuc     touch(get_config_var("statedir") + "/finished");
326*0a6a1f1dSLionel Sambuc }
327*0a6a1f1dSLionel Sambuc 
328*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(timeout_forkexit);
ATF_TEST_CASE_HEAD(timeout_forkexit)329*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(timeout_forkexit)
330*0a6a1f1dSLionel Sambuc {
331*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
332*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(timeout_forkexit)333*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(timeout_forkexit)
334*0a6a1f1dSLionel Sambuc {
335*0a6a1f1dSLionel Sambuc     pid_t pid = fork();
336*0a6a1f1dSLionel Sambuc     ATF_REQUIRE(pid != -1);
337*0a6a1f1dSLionel Sambuc 
338*0a6a1f1dSLionel Sambuc     if (pid == 0) {
339*0a6a1f1dSLionel Sambuc         sigset_t mask;
340*0a6a1f1dSLionel Sambuc         sigemptyset(&mask);
341*0a6a1f1dSLionel Sambuc 
342*0a6a1f1dSLionel Sambuc         std::cout << "Waiting in subprocess\n";
343*0a6a1f1dSLionel Sambuc         std::cout.flush();
344*0a6a1f1dSLionel Sambuc         ::sigsuspend(&mask);
345*0a6a1f1dSLionel Sambuc 
346*0a6a1f1dSLionel Sambuc         touch(get_config_var("statedir") + "/child-finished");
347*0a6a1f1dSLionel Sambuc         std::cout << "Subprocess exiting\n";
348*0a6a1f1dSLionel Sambuc         std::cout.flush();
349*0a6a1f1dSLionel Sambuc         exit(EXIT_SUCCESS);
350*0a6a1f1dSLionel Sambuc     } else {
351*0a6a1f1dSLionel Sambuc         // Don't wait for the child process and let atf-run deal with it.
352*0a6a1f1dSLionel Sambuc         touch(get_config_var("statedir") + "/parent-finished");
353*0a6a1f1dSLionel Sambuc         std::cout << "Parent process exiting\n";
354*0a6a1f1dSLionel Sambuc         ATF_PASS();
355*0a6a1f1dSLionel Sambuc     }
356*0a6a1f1dSLionel Sambuc }
357*0a6a1f1dSLionel Sambuc 
358*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(use_fs);
ATF_TEST_CASE_HEAD(use_fs)359*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(use_fs)
360*0a6a1f1dSLionel Sambuc {
361*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration test program");
362*0a6a1f1dSLionel Sambuc     set_md_var("use.fs", "this-is-deprecated");
363*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(use_fs)364*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(use_fs)
365*0a6a1f1dSLionel Sambuc {
366*0a6a1f1dSLionel Sambuc     touch("test-file");
367*0a6a1f1dSLionel Sambuc }
368*0a6a1f1dSLionel Sambuc 
369*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
370*0a6a1f1dSLionel Sambuc // Helper tests for "atf-report_test".
371*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
372*0a6a1f1dSLionel Sambuc 
373*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(diff);
ATF_TEST_CASE_HEAD(diff)374*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(diff)
375*0a6a1f1dSLionel Sambuc {
376*0a6a1f1dSLionel Sambuc     set_md_var("descr", "Helper test case for the t_integration program");
377*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(diff)378*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(diff)
379*0a6a1f1dSLionel Sambuc {
380*0a6a1f1dSLionel Sambuc     std::cout << "--- a	2007-11-04 14:00:41.000000000 +0100\n";
381*0a6a1f1dSLionel Sambuc     std::cout << "+++ b	2007-11-04 14:00:48.000000000 +0100\n";
382*0a6a1f1dSLionel Sambuc     std::cout << "@@ -1,7 +1,7 @@\n";
383*0a6a1f1dSLionel Sambuc     std::cout << " This test is meant to simulate a diff.\n";
384*0a6a1f1dSLionel Sambuc     std::cout << " Blank space at beginning of context lines must be "
385*0a6a1f1dSLionel Sambuc                  "preserved.\n";
386*0a6a1f1dSLionel Sambuc     std::cout << " \n";
387*0a6a1f1dSLionel Sambuc     std::cout << "-First original line.\n";
388*0a6a1f1dSLionel Sambuc     std::cout << "-Second original line.\n";
389*0a6a1f1dSLionel Sambuc     std::cout << "+First modified line.\n";
390*0a6a1f1dSLionel Sambuc     std::cout << "+Second modified line.\n";
391*0a6a1f1dSLionel Sambuc     std::cout << " \n";
392*0a6a1f1dSLionel Sambuc     std::cout << " EOF\n";
393*0a6a1f1dSLionel Sambuc }
394*0a6a1f1dSLionel Sambuc 
395*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
396*0a6a1f1dSLionel Sambuc // Main.
397*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
398*0a6a1f1dSLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)399*0a6a1f1dSLionel Sambuc ATF_INIT_TEST_CASES(tcs)
400*0a6a1f1dSLionel Sambuc {
401*0a6a1f1dSLionel Sambuc     std::string which = tools::env::get("TESTCASE");
402*0a6a1f1dSLionel Sambuc 
403*0a6a1f1dSLionel Sambuc     // Add helper tests for atf-run_test.
404*0a6a1f1dSLionel Sambuc     if (which == "pass")
405*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, pass);
406*0a6a1f1dSLionel Sambuc     if (which == "config")
407*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, config);
408*0a6a1f1dSLionel Sambuc     if (which == "fds")
409*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, fds);
410*0a6a1f1dSLionel Sambuc     if (which == "mux_streams")
411*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, mux_streams);
412*0a6a1f1dSLionel Sambuc     if (which == "testvar")
413*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, testvar);
414*0a6a1f1dSLionel Sambuc     if (which == "env_list")
415*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, env_list);
416*0a6a1f1dSLionel Sambuc     if (which == "env_home")
417*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, env_home);
418*0a6a1f1dSLionel Sambuc     if (which == "read_stdin")
419*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, read_stdin);
420*0a6a1f1dSLionel Sambuc     if (which == "umask")
421*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, umask);
422*0a6a1f1dSLionel Sambuc     if (which == "cleanup_states")
423*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, cleanup_states);
424*0a6a1f1dSLionel Sambuc     if (which == "cleanup_curdir")
425*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, cleanup_curdir);
426*0a6a1f1dSLionel Sambuc     if (which == "require_arch")
427*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, require_arch);
428*0a6a1f1dSLionel Sambuc     if (which == "require_config")
429*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, require_config);
430*0a6a1f1dSLionel Sambuc     if (which == "require_files")
431*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, require_files);
432*0a6a1f1dSLionel Sambuc     if (which == "require_machine")
433*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, require_machine);
434*0a6a1f1dSLionel Sambuc     if (which == "require_progs")
435*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, require_progs);
436*0a6a1f1dSLionel Sambuc     if (which == "require_user")
437*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, require_user);
438*0a6a1f1dSLionel Sambuc     if (which == "timeout")
439*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, timeout);
440*0a6a1f1dSLionel Sambuc     if (which == "timeout_forkexit")
441*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, timeout_forkexit);
442*0a6a1f1dSLionel Sambuc     if (which == "use_fs")
443*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, use_fs);
444*0a6a1f1dSLionel Sambuc 
445*0a6a1f1dSLionel Sambuc     // Add helper tests for atf-report_test.
446*0a6a1f1dSLionel Sambuc     if (which == "diff")
447*0a6a1f1dSLionel Sambuc         ATF_ADD_TEST_CASE(tcs, diff);
448*0a6a1f1dSLionel Sambuc }
449