1 // Copyright 2011 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include "utils/defs.hpp"
41 #include "utils/env.hpp"
42 #include "utils/fs/operations.hpp"
43 #include "utils/fs/path.hpp"
44 #include "utils/optional.ipp"
46 namespace fs
= utils::fs
;
48 using utils::optional
;
54 /// Logs an error message and exits the test with an error code.
56 /// \param str The error message to log.
60 std::cerr
<< str
<< '\n';
61 std::exit(EXIT_FAILURE
);
65 /// A test case that crashes.
73 /// A test case that exits with a non-zero exit code, and not 1.
81 /// A test case that passes.
88 /// A test case that spawns a subchild that gets stuck.
90 /// This test case is used by the caller to validate that the whole process tree
91 /// is terminated when the test case is killed.
93 test_spawn_blocking_child(void)
97 fail("Cannot fork subprocess");
102 const fs::path name
= fs::path(utils::getenv("CONTROL_DIR").get()) /
104 std::ofstream
pidfile(name
.c_str());
106 fail("Failed to create the pidfile");
113 /// A test case that times out.
115 /// Note that the timeout is defined in the Kyuafile, as the plain interface has
116 /// no means for test programs to specify this by themselves.
121 const fs::path control_dir
= fs::path(utils::getenv("CONTROL_DIR").get());
122 std::ofstream
file((control_dir
/ "cookie").c_str());
124 fail("Failed to create the control cookie");
129 /// A test case that performs basic checks on the runtime environment.
131 /// If the runtime environment does not look clean (according to the rules in
132 /// the Kyua runtime properties), the test fails.
134 test_validate_isolation(void)
136 if (utils::getenv("HOME").get() == "fake-value")
137 fail("HOME not reset");
138 if (utils::getenv("LANG"))
139 fail("LANG not unset");
143 } // anonymous namespace
146 /// Entry point to the test program.
148 /// The caller can select which test case to run by defining the TEST_CASE
149 /// environment variable. This is not "standard", in the sense this is not a
150 /// generic property of the plain test case interface.
152 /// \todo It may be worth to split this binary into separate, smaller binaries,
153 /// one for every "test case". We use this program as a dispatcher for
154 /// different "main"s, the only reason being to keep the amount of helper test
155 /// programs to a minimum. However, putting this each function in its own
156 /// binary could simplify many other things.
158 /// \param argc The number of CLI arguments.
159 /// \param unused_argv The CLI arguments themselves. These are not used because
160 /// Kyua will not pass any arguments to the plain test program.
162 main(int argc
, char** UTILS_UNUSED_PARAM(argv
))
165 std::cerr
<< "No arguments allowed; select the test case with the "
166 "TEST_CASE variable";
170 const optional
< std::string
> test_case_env
= utils::getenv("TEST_CASE");
171 if (!test_case_env
) {
172 std::cerr
<< "TEST_CASE not defined";
175 const std::string
& test_case
= test_case_env
.get();
177 if (test_case
== "crash")
179 else if (test_case
== "fail")
181 else if (test_case
== "pass")
183 else if (test_case
== "spawn_blocking_child")
184 test_spawn_blocking_child();
185 else if (test_case
== "timeout")
187 else if (test_case
== "validate_isolation")
188 test_validate_isolation();
190 std::cerr
<< "Unknown test case";