1*b0d29bc4SBrooks Davis // Copyright 2010 The Kyua Authors.
2*b0d29bc4SBrooks Davis // All rights reserved.
3*b0d29bc4SBrooks Davis //
4*b0d29bc4SBrooks Davis // Redistribution and use in source and binary forms, with or without
5*b0d29bc4SBrooks Davis // modification, are permitted provided that the following conditions are
6*b0d29bc4SBrooks Davis // met:
7*b0d29bc4SBrooks Davis //
8*b0d29bc4SBrooks Davis // * Redistributions of source code must retain the above copyright
9*b0d29bc4SBrooks Davis // notice, this list of conditions and the following disclaimer.
10*b0d29bc4SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
11*b0d29bc4SBrooks Davis // notice, this list of conditions and the following disclaimer in the
12*b0d29bc4SBrooks Davis // documentation and/or other materials provided with the distribution.
13*b0d29bc4SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
14*b0d29bc4SBrooks Davis // may be used to endorse or promote products derived from this software
15*b0d29bc4SBrooks Davis // without specific prior written permission.
16*b0d29bc4SBrooks Davis //
17*b0d29bc4SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*b0d29bc4SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*b0d29bc4SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*b0d29bc4SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*b0d29bc4SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*b0d29bc4SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*b0d29bc4SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*b0d29bc4SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*b0d29bc4SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*b0d29bc4SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*b0d29bc4SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*b0d29bc4SBrooks Davis
29*b0d29bc4SBrooks Davis extern "C" {
30*b0d29bc4SBrooks Davis #include <sys/stat.h>
31*b0d29bc4SBrooks Davis
32*b0d29bc4SBrooks Davis #include <signal.h>
33*b0d29bc4SBrooks Davis #include <unistd.h>
34*b0d29bc4SBrooks Davis }
35*b0d29bc4SBrooks Davis
36*b0d29bc4SBrooks Davis #include <algorithm>
37*b0d29bc4SBrooks Davis #include <cstdlib>
38*b0d29bc4SBrooks Davis #include <fstream>
39*b0d29bc4SBrooks Davis #include <iostream>
40*b0d29bc4SBrooks Davis #include <set>
41*b0d29bc4SBrooks Davis #include <sstream>
42*b0d29bc4SBrooks Davis #include <string>
43*b0d29bc4SBrooks Davis #include <vector>
44*b0d29bc4SBrooks Davis
45*b0d29bc4SBrooks Davis #include <atf-c++.hpp>
46*b0d29bc4SBrooks Davis
47*b0d29bc4SBrooks Davis #include "utils/env.hpp"
48*b0d29bc4SBrooks Davis #include "utils/fs/operations.hpp"
49*b0d29bc4SBrooks Davis #include "utils/fs/path.hpp"
50*b0d29bc4SBrooks Davis #include "utils/logging/operations.hpp"
51*b0d29bc4SBrooks Davis #include "utils/optional.ipp"
52*b0d29bc4SBrooks Davis #include "utils/test_utils.ipp"
53*b0d29bc4SBrooks Davis #include "utils/text/operations.ipp"
54*b0d29bc4SBrooks Davis
55*b0d29bc4SBrooks Davis namespace fs = utils::fs;
56*b0d29bc4SBrooks Davis namespace logging = utils::logging;
57*b0d29bc4SBrooks Davis namespace text = utils::text;
58*b0d29bc4SBrooks Davis
59*b0d29bc4SBrooks Davis using utils::optional;
60*b0d29bc4SBrooks Davis
61*b0d29bc4SBrooks Davis
62*b0d29bc4SBrooks Davis namespace {
63*b0d29bc4SBrooks Davis
64*b0d29bc4SBrooks Davis
65*b0d29bc4SBrooks Davis /// Creates an empty file in the given directory.
66*b0d29bc4SBrooks Davis ///
67*b0d29bc4SBrooks Davis /// \param test_case The test case currently running.
68*b0d29bc4SBrooks Davis /// \param directory The name of the configuration variable that holds the path
69*b0d29bc4SBrooks Davis /// to the directory in which to create the cookie file.
70*b0d29bc4SBrooks Davis /// \param name The name of the cookie file to create.
71*b0d29bc4SBrooks Davis static void
create_cookie(const atf::tests::tc * test_case,const char * directory,const char * name)72*b0d29bc4SBrooks Davis create_cookie(const atf::tests::tc* test_case, const char* directory,
73*b0d29bc4SBrooks Davis const char* name)
74*b0d29bc4SBrooks Davis {
75*b0d29bc4SBrooks Davis if (!test_case->has_config_var(directory))
76*b0d29bc4SBrooks Davis test_case->fail(std::string(name) + " not provided");
77*b0d29bc4SBrooks Davis
78*b0d29bc4SBrooks Davis const fs::path control_dir(test_case->get_config_var(directory));
79*b0d29bc4SBrooks Davis std::ofstream file((control_dir / name).c_str());
80*b0d29bc4SBrooks Davis if (!file)
81*b0d29bc4SBrooks Davis test_case->fail("Failed to create the control cookie");
82*b0d29bc4SBrooks Davis file.close();
83*b0d29bc4SBrooks Davis }
84*b0d29bc4SBrooks Davis
85*b0d29bc4SBrooks Davis
86*b0d29bc4SBrooks Davis } // anonymous namespace
87*b0d29bc4SBrooks Davis
88*b0d29bc4SBrooks Davis
89*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITH_CLEANUP(check_cleanup_workdir);
ATF_TEST_CASE_HEAD(check_cleanup_workdir)90*b0d29bc4SBrooks Davis ATF_TEST_CASE_HEAD(check_cleanup_workdir)
91*b0d29bc4SBrooks Davis {
92*b0d29bc4SBrooks Davis set_md_var("require.config", "control_dir");
93*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_BODY(check_cleanup_workdir)94*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(check_cleanup_workdir)
95*b0d29bc4SBrooks Davis {
96*b0d29bc4SBrooks Davis std::ofstream cookie("workdir_cookie");
97*b0d29bc4SBrooks Davis cookie << "1234\n";
98*b0d29bc4SBrooks Davis cookie.close();
99*b0d29bc4SBrooks Davis skip("cookie created");
100*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_CLEANUP(check_cleanup_workdir)101*b0d29bc4SBrooks Davis ATF_TEST_CASE_CLEANUP(check_cleanup_workdir)
102*b0d29bc4SBrooks Davis {
103*b0d29bc4SBrooks Davis const fs::path control_dir(get_config_var("control_dir"));
104*b0d29bc4SBrooks Davis
105*b0d29bc4SBrooks Davis std::ifstream cookie("workdir_cookie");
106*b0d29bc4SBrooks Davis if (!cookie) {
107*b0d29bc4SBrooks Davis std::ofstream((control_dir / "missing_cookie").c_str()).close();
108*b0d29bc4SBrooks Davis std::exit(EXIT_FAILURE);
109*b0d29bc4SBrooks Davis }
110*b0d29bc4SBrooks Davis
111*b0d29bc4SBrooks Davis std::string value;
112*b0d29bc4SBrooks Davis cookie >> value;
113*b0d29bc4SBrooks Davis if (value != "1234") {
114*b0d29bc4SBrooks Davis std::ofstream((control_dir / "invalid_cookie").c_str()).close();
115*b0d29bc4SBrooks Davis std::exit(EXIT_FAILURE);
116*b0d29bc4SBrooks Davis }
117*b0d29bc4SBrooks Davis
118*b0d29bc4SBrooks Davis std::ofstream((control_dir / "cookie_ok").c_str()).close();
119*b0d29bc4SBrooks Davis std::exit(EXIT_SUCCESS);
120*b0d29bc4SBrooks Davis }
121*b0d29bc4SBrooks Davis
122*b0d29bc4SBrooks Davis
123*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(check_configuration_variables);
ATF_TEST_CASE_BODY(check_configuration_variables)124*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(check_configuration_variables)
125*b0d29bc4SBrooks Davis {
126*b0d29bc4SBrooks Davis ATF_REQUIRE(has_config_var("first"));
127*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ("some value", get_config_var("first"));
128*b0d29bc4SBrooks Davis
129*b0d29bc4SBrooks Davis ATF_REQUIRE(has_config_var("second"));
130*b0d29bc4SBrooks Davis ATF_REQUIRE_EQ("some other value", get_config_var("second"));
131*b0d29bc4SBrooks Davis }
132*b0d29bc4SBrooks Davis
133*b0d29bc4SBrooks Davis
134*b0d29bc4SBrooks Davis ATF_TEST_CASE(check_list_config);
ATF_TEST_CASE_HEAD(check_list_config)135*b0d29bc4SBrooks Davis ATF_TEST_CASE_HEAD(check_list_config)
136*b0d29bc4SBrooks Davis {
137*b0d29bc4SBrooks Davis std::string description = "Found:";
138*b0d29bc4SBrooks Davis
139*b0d29bc4SBrooks Davis if (has_config_var("var1"))
140*b0d29bc4SBrooks Davis description += " var1=" + get_config_var("var1");
141*b0d29bc4SBrooks Davis if (has_config_var("var2"))
142*b0d29bc4SBrooks Davis description += " var2=" + get_config_var("var2");
143*b0d29bc4SBrooks Davis
144*b0d29bc4SBrooks Davis set_md_var("descr", description);
145*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_BODY(check_list_config)146*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(check_list_config)
147*b0d29bc4SBrooks Davis {
148*b0d29bc4SBrooks Davis }
149*b0d29bc4SBrooks Davis
150*b0d29bc4SBrooks Davis
151*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(check_unprivileged);
ATF_TEST_CASE_BODY(check_unprivileged)152*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(check_unprivileged)
153*b0d29bc4SBrooks Davis {
154*b0d29bc4SBrooks Davis if (::getuid() == 0)
155*b0d29bc4SBrooks Davis fail("Running as root, but I shouldn't be");
156*b0d29bc4SBrooks Davis
157*b0d29bc4SBrooks Davis std::ofstream file("cookie");
158*b0d29bc4SBrooks Davis if (!file)
159*b0d29bc4SBrooks Davis fail("Failed to create the cookie; work directory probably owned by "
160*b0d29bc4SBrooks Davis "root");
161*b0d29bc4SBrooks Davis file.close();
162*b0d29bc4SBrooks Davis }
163*b0d29bc4SBrooks Davis
164*b0d29bc4SBrooks Davis
165*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(crash);
ATF_TEST_CASE_BODY(crash)166*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(crash)
167*b0d29bc4SBrooks Davis {
168*b0d29bc4SBrooks Davis std::abort();
169*b0d29bc4SBrooks Davis }
170*b0d29bc4SBrooks Davis
171*b0d29bc4SBrooks Davis
172*b0d29bc4SBrooks Davis ATF_TEST_CASE(crash_head);
ATF_TEST_CASE_HEAD(crash_head)173*b0d29bc4SBrooks Davis ATF_TEST_CASE_HEAD(crash_head)
174*b0d29bc4SBrooks Davis {
175*b0d29bc4SBrooks Davis utils::abort_without_coredump();
176*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_BODY(crash_head)177*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(crash_head)
178*b0d29bc4SBrooks Davis {
179*b0d29bc4SBrooks Davis }
180*b0d29bc4SBrooks Davis
181*b0d29bc4SBrooks Davis
182*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITH_CLEANUP(crash_cleanup);
ATF_TEST_CASE_HEAD(crash_cleanup)183*b0d29bc4SBrooks Davis ATF_TEST_CASE_HEAD(crash_cleanup)
184*b0d29bc4SBrooks Davis {
185*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_BODY(crash_cleanup)186*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(crash_cleanup)
187*b0d29bc4SBrooks Davis {
188*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_CLEANUP(crash_cleanup)189*b0d29bc4SBrooks Davis ATF_TEST_CASE_CLEANUP(crash_cleanup)
190*b0d29bc4SBrooks Davis {
191*b0d29bc4SBrooks Davis utils::abort_without_coredump();
192*b0d29bc4SBrooks Davis }
193*b0d29bc4SBrooks Davis
194*b0d29bc4SBrooks Davis
195*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(create_cookie_in_control_dir);
ATF_TEST_CASE_BODY(create_cookie_in_control_dir)196*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(create_cookie_in_control_dir)
197*b0d29bc4SBrooks Davis {
198*b0d29bc4SBrooks Davis create_cookie(this, "control_dir", "cookie");
199*b0d29bc4SBrooks Davis }
200*b0d29bc4SBrooks Davis
201*b0d29bc4SBrooks Davis
202*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(create_cookie_in_workdir);
ATF_TEST_CASE_BODY(create_cookie_in_workdir)203*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(create_cookie_in_workdir)
204*b0d29bc4SBrooks Davis {
205*b0d29bc4SBrooks Davis std::ofstream file("cookie");
206*b0d29bc4SBrooks Davis if (!file)
207*b0d29bc4SBrooks Davis fail("Failed to create the cookie");
208*b0d29bc4SBrooks Davis file.close();
209*b0d29bc4SBrooks Davis }
210*b0d29bc4SBrooks Davis
211*b0d29bc4SBrooks Davis
212*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITH_CLEANUP(create_cookie_from_cleanup);
ATF_TEST_CASE_HEAD(create_cookie_from_cleanup)213*b0d29bc4SBrooks Davis ATF_TEST_CASE_HEAD(create_cookie_from_cleanup)
214*b0d29bc4SBrooks Davis {
215*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_BODY(create_cookie_from_cleanup)216*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(create_cookie_from_cleanup)
217*b0d29bc4SBrooks Davis {
218*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_CLEANUP(create_cookie_from_cleanup)219*b0d29bc4SBrooks Davis ATF_TEST_CASE_CLEANUP(create_cookie_from_cleanup)
220*b0d29bc4SBrooks Davis {
221*b0d29bc4SBrooks Davis create_cookie(this, "control_dir", "cookie");
222*b0d29bc4SBrooks Davis }
223*b0d29bc4SBrooks Davis
224*b0d29bc4SBrooks Davis
225*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITH_CLEANUP(expect_timeout);
ATF_TEST_CASE_HEAD(expect_timeout)226*b0d29bc4SBrooks Davis ATF_TEST_CASE_HEAD(expect_timeout)
227*b0d29bc4SBrooks Davis {
228*b0d29bc4SBrooks Davis if (has_config_var("timeout"))
229*b0d29bc4SBrooks Davis set_md_var("timeout", get_config_var("timeout"));
230*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_BODY(expect_timeout)231*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(expect_timeout)
232*b0d29bc4SBrooks Davis {
233*b0d29bc4SBrooks Davis expect_timeout("Times out on purpose");
234*b0d29bc4SBrooks Davis ::sleep(10);
235*b0d29bc4SBrooks Davis create_cookie(this, "control_dir", "cookie");
236*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_CLEANUP(expect_timeout)237*b0d29bc4SBrooks Davis ATF_TEST_CASE_CLEANUP(expect_timeout)
238*b0d29bc4SBrooks Davis {
239*b0d29bc4SBrooks Davis create_cookie(this, "control_dir", "cookie.cleanup");
240*b0d29bc4SBrooks Davis }
241*b0d29bc4SBrooks Davis
242*b0d29bc4SBrooks Davis
243*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITH_CLEANUP(output);
ATF_TEST_CASE_HEAD(output)244*b0d29bc4SBrooks Davis ATF_TEST_CASE_HEAD(output)
245*b0d29bc4SBrooks Davis {
246*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_BODY(output)247*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(output)
248*b0d29bc4SBrooks Davis {
249*b0d29bc4SBrooks Davis std::cout << "Body message to stdout\n";
250*b0d29bc4SBrooks Davis std::cerr << "Body message to stderr\n";
251*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_CLEANUP(output)252*b0d29bc4SBrooks Davis ATF_TEST_CASE_CLEANUP(output)
253*b0d29bc4SBrooks Davis {
254*b0d29bc4SBrooks Davis std::cout << "Cleanup message to stdout\n";
255*b0d29bc4SBrooks Davis std::cerr << "Cleanup message to stderr\n";
256*b0d29bc4SBrooks Davis }
257*b0d29bc4SBrooks Davis
258*b0d29bc4SBrooks Davis
259*b0d29bc4SBrooks Davis ATF_TEST_CASE(output_in_list);
ATF_TEST_CASE_HEAD(output_in_list)260*b0d29bc4SBrooks Davis ATF_TEST_CASE_HEAD(output_in_list)
261*b0d29bc4SBrooks Davis {
262*b0d29bc4SBrooks Davis std::cerr << "Should not write anything!\n";
263*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_BODY(output_in_list)264*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(output_in_list)
265*b0d29bc4SBrooks Davis {
266*b0d29bc4SBrooks Davis }
267*b0d29bc4SBrooks Davis
268*b0d29bc4SBrooks Davis
269*b0d29bc4SBrooks Davis ATF_TEST_CASE(pass);
ATF_TEST_CASE_HEAD(pass)270*b0d29bc4SBrooks Davis ATF_TEST_CASE_HEAD(pass)
271*b0d29bc4SBrooks Davis {
272*b0d29bc4SBrooks Davis set_md_var("descr", "Always-passing test case");
273*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_BODY(pass)274*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(pass)
275*b0d29bc4SBrooks Davis {
276*b0d29bc4SBrooks Davis }
277*b0d29bc4SBrooks Davis
278*b0d29bc4SBrooks Davis
279*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITH_CLEANUP(shared_workdir);
ATF_TEST_CASE_HEAD(shared_workdir)280*b0d29bc4SBrooks Davis ATF_TEST_CASE_HEAD(shared_workdir)
281*b0d29bc4SBrooks Davis {
282*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_BODY(shared_workdir)283*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(shared_workdir)
284*b0d29bc4SBrooks Davis {
285*b0d29bc4SBrooks Davis atf::utils::create_file("shared_cookie", "");
286*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_CLEANUP(shared_workdir)287*b0d29bc4SBrooks Davis ATF_TEST_CASE_CLEANUP(shared_workdir)
288*b0d29bc4SBrooks Davis {
289*b0d29bc4SBrooks Davis if (!atf::utils::file_exists("shared_cookie"))
290*b0d29bc4SBrooks Davis utils::abort_without_coredump();
291*b0d29bc4SBrooks Davis }
292*b0d29bc4SBrooks Davis
293*b0d29bc4SBrooks Davis
294*b0d29bc4SBrooks Davis ATF_TEST_CASE(spawn_blocking_child);
ATF_TEST_CASE_HEAD(spawn_blocking_child)295*b0d29bc4SBrooks Davis ATF_TEST_CASE_HEAD(spawn_blocking_child)
296*b0d29bc4SBrooks Davis {
297*b0d29bc4SBrooks Davis set_md_var("require.config", "control_dir");
298*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_BODY(spawn_blocking_child)299*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(spawn_blocking_child)
300*b0d29bc4SBrooks Davis {
301*b0d29bc4SBrooks Davis pid_t pid = ::fork();
302*b0d29bc4SBrooks Davis if (pid == -1)
303*b0d29bc4SBrooks Davis fail("Cannot fork subprocess");
304*b0d29bc4SBrooks Davis else if (pid == 0) {
305*b0d29bc4SBrooks Davis for (;;)
306*b0d29bc4SBrooks Davis ::pause();
307*b0d29bc4SBrooks Davis } else {
308*b0d29bc4SBrooks Davis const fs::path name = fs::path(get_config_var("control_dir")) / "pid";
309*b0d29bc4SBrooks Davis std::ofstream pidfile(name.c_str());
310*b0d29bc4SBrooks Davis ATF_REQUIRE(pidfile);
311*b0d29bc4SBrooks Davis pidfile << pid;
312*b0d29bc4SBrooks Davis pidfile.close();
313*b0d29bc4SBrooks Davis }
314*b0d29bc4SBrooks Davis }
315*b0d29bc4SBrooks Davis
316*b0d29bc4SBrooks Davis
317*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITH_CLEANUP(timeout_body);
ATF_TEST_CASE_HEAD(timeout_body)318*b0d29bc4SBrooks Davis ATF_TEST_CASE_HEAD(timeout_body)
319*b0d29bc4SBrooks Davis {
320*b0d29bc4SBrooks Davis if (has_config_var("timeout"))
321*b0d29bc4SBrooks Davis set_md_var("timeout", get_config_var("timeout"));
322*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_BODY(timeout_body)323*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(timeout_body)
324*b0d29bc4SBrooks Davis {
325*b0d29bc4SBrooks Davis ::sleep(10);
326*b0d29bc4SBrooks Davis create_cookie(this, "control_dir", "cookie");
327*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_CLEANUP(timeout_body)328*b0d29bc4SBrooks Davis ATF_TEST_CASE_CLEANUP(timeout_body)
329*b0d29bc4SBrooks Davis {
330*b0d29bc4SBrooks Davis create_cookie(this, "control_dir", "cookie.cleanup");
331*b0d29bc4SBrooks Davis }
332*b0d29bc4SBrooks Davis
333*b0d29bc4SBrooks Davis
334*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITH_CLEANUP(timeout_cleanup);
ATF_TEST_CASE_HEAD(timeout_cleanup)335*b0d29bc4SBrooks Davis ATF_TEST_CASE_HEAD(timeout_cleanup)
336*b0d29bc4SBrooks Davis {
337*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_BODY(timeout_cleanup)338*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(timeout_cleanup)
339*b0d29bc4SBrooks Davis {
340*b0d29bc4SBrooks Davis }
ATF_TEST_CASE_CLEANUP(timeout_cleanup)341*b0d29bc4SBrooks Davis ATF_TEST_CASE_CLEANUP(timeout_cleanup)
342*b0d29bc4SBrooks Davis {
343*b0d29bc4SBrooks Davis ::sleep(10);
344*b0d29bc4SBrooks Davis create_cookie(this, "control_dir", "cookie");
345*b0d29bc4SBrooks Davis }
346*b0d29bc4SBrooks Davis
347*b0d29bc4SBrooks Davis
348*b0d29bc4SBrooks Davis ATF_TEST_CASE_WITHOUT_HEAD(validate_isolation);
ATF_TEST_CASE_BODY(validate_isolation)349*b0d29bc4SBrooks Davis ATF_TEST_CASE_BODY(validate_isolation)
350*b0d29bc4SBrooks Davis {
351*b0d29bc4SBrooks Davis ATF_REQUIRE(utils::getenv("HOME").get() != "fake-value");
352*b0d29bc4SBrooks Davis ATF_REQUIRE(!utils::getenv("LANG"));
353*b0d29bc4SBrooks Davis }
354*b0d29bc4SBrooks Davis
355*b0d29bc4SBrooks Davis
356*b0d29bc4SBrooks Davis /// Wrapper around ATF_ADD_TEST_CASE to only add a test when requested.
357*b0d29bc4SBrooks Davis ///
358*b0d29bc4SBrooks Davis /// The caller can set the TEST_CASES environment variable to a
359*b0d29bc4SBrooks Davis /// whitespace-separated list of test case names to enable. If not empty, the
360*b0d29bc4SBrooks Davis /// list acts as a filter for the tests to add.
361*b0d29bc4SBrooks Davis ///
362*b0d29bc4SBrooks Davis /// \param tcs List of test cases into which to register the test.
363*b0d29bc4SBrooks Davis /// \param filters List of filters to determine whether the test applies or not.
364*b0d29bc4SBrooks Davis /// \param name Name of the test case being added.
365*b0d29bc4SBrooks Davis #define ADD_TEST_CASE(tcs, filters, name) \
366*b0d29bc4SBrooks Davis do { \
367*b0d29bc4SBrooks Davis if (filters.empty() || filters.find(#name) != filters.end()) \
368*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, name); \
369*b0d29bc4SBrooks Davis } while (false)
370*b0d29bc4SBrooks Davis
371*b0d29bc4SBrooks Davis
ATF_INIT_TEST_CASES(tcs)372*b0d29bc4SBrooks Davis ATF_INIT_TEST_CASES(tcs)
373*b0d29bc4SBrooks Davis {
374*b0d29bc4SBrooks Davis logging::set_inmemory();
375*b0d29bc4SBrooks Davis
376*b0d29bc4SBrooks Davis // TODO(jmmv): Instead of using "filters", we should make TEST_CASES
377*b0d29bc4SBrooks Davis // explicitly list all the test cases to enable. This would let us get rid
378*b0d29bc4SBrooks Davis // of some of the hacks below...
379*b0d29bc4SBrooks Davis std::set< std::string > filters;
380*b0d29bc4SBrooks Davis
381*b0d29bc4SBrooks Davis const optional< std::string > names_raw = utils::getenv("TEST_CASES");
382*b0d29bc4SBrooks Davis if (names_raw) {
383*b0d29bc4SBrooks Davis if (names_raw.get().empty())
384*b0d29bc4SBrooks Davis return; // See TODO above.
385*b0d29bc4SBrooks Davis
386*b0d29bc4SBrooks Davis const std::vector< std::string > names = text::split(
387*b0d29bc4SBrooks Davis names_raw.get(), ' ');
388*b0d29bc4SBrooks Davis std::copy(names.begin(), names.end(),
389*b0d29bc4SBrooks Davis std::inserter(filters, filters.begin()));
390*b0d29bc4SBrooks Davis }
391*b0d29bc4SBrooks Davis
392*b0d29bc4SBrooks Davis if (filters.find("crash_head") != filters.end()) // See TODO above.
393*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, crash_head);
394*b0d29bc4SBrooks Davis if (filters.find("output_in_list") != filters.end()) // See TODO above.
395*b0d29bc4SBrooks Davis ATF_ADD_TEST_CASE(tcs, output_in_list);
396*b0d29bc4SBrooks Davis
397*b0d29bc4SBrooks Davis ADD_TEST_CASE(tcs, filters, check_cleanup_workdir);
398*b0d29bc4SBrooks Davis ADD_TEST_CASE(tcs, filters, check_configuration_variables);
399*b0d29bc4SBrooks Davis ADD_TEST_CASE(tcs, filters, check_list_config);
400*b0d29bc4SBrooks Davis ADD_TEST_CASE(tcs, filters, check_unprivileged);
401*b0d29bc4SBrooks Davis ADD_TEST_CASE(tcs, filters, crash);
402*b0d29bc4SBrooks Davis ADD_TEST_CASE(tcs, filters, crash_cleanup);
403*b0d29bc4SBrooks Davis ADD_TEST_CASE(tcs, filters, create_cookie_in_control_dir);
404*b0d29bc4SBrooks Davis ADD_TEST_CASE(tcs, filters, create_cookie_in_workdir);
405*b0d29bc4SBrooks Davis ADD_TEST_CASE(tcs, filters, create_cookie_from_cleanup);
406*b0d29bc4SBrooks Davis ADD_TEST_CASE(tcs, filters, expect_timeout);
407*b0d29bc4SBrooks Davis ADD_TEST_CASE(tcs, filters, output);
408*b0d29bc4SBrooks Davis ADD_TEST_CASE(tcs, filters, pass);
409*b0d29bc4SBrooks Davis ADD_TEST_CASE(tcs, filters, shared_workdir);
410*b0d29bc4SBrooks Davis ADD_TEST_CASE(tcs, filters, spawn_blocking_child);
411*b0d29bc4SBrooks Davis ADD_TEST_CASE(tcs, filters, timeout_body);
412*b0d29bc4SBrooks Davis ADD_TEST_CASE(tcs, filters, timeout_cleanup);
413*b0d29bc4SBrooks Davis ADD_TEST_CASE(tcs, filters, validate_isolation);
414*b0d29bc4SBrooks Davis }
415