1 // Copyright 2011 The Kyua Authors.
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.
34 extern char** environ;
43 #include "utils/env.hpp"
44 #include "utils/format/containers.ipp"
45 #include "utils/format/macros.hpp"
46 #include "utils/fs/operations.hpp"
47 #include "utils/fs/path.hpp"
48 #include "utils/optional.ipp"
49 #include "utils/test_utils.ipp"
51 namespace fs = utils::fs;
53 using utils::optional;
59 /// Gets the name of the test case to run.
61 /// We use the value of the TEST_CASE environment variable if present, or
62 /// else the basename of the test program.
64 /// \param arg0 Value of argv[0] as passed to main().
66 /// \return A test case name. The name may not be valid.
68 guess_test_case_name(const char* arg0)
70 const optional< std::string > test_case_env = utils::getenv("TEST_CASE");
72 return test_case_env.get();
74 return fs::path(arg0).leaf_name();
79 /// Logs an error message and exits the test with an error code.
81 /// \param str The error message to log.
83 fail(const std::string& str)
85 std::cerr << str << '\n';
86 std::exit(EXIT_FAILURE);
90 /// A test case that validates the TEST_ENV_* variables.
92 test_check_configuration_variables(void)
94 std::set< std::string > vars;
96 for (iter = environ; *iter != NULL; ++iter) {
97 if (std::strstr(*iter, "TEST_ENV_") == *iter) {
102 std::set< std::string > exp_vars;
103 exp_vars.insert("TEST_ENV_first=some value");
104 exp_vars.insert("TEST_ENV_second=some other value");
105 if (vars != exp_vars) {
106 fail(F("Expected: %s\nFound: %s\n") % exp_vars % vars);
111 /// A test case that crashes.
115 utils::abort_without_coredump();
119 /// A test case that exits with a non-zero exit code, and not 1.
127 /// A test case that passes.
134 /// A test case that spawns a subchild that gets stuck.
136 /// This test case is used by the caller to validate that the whole process tree
137 /// is terminated when the test case is killed.
139 test_spawn_blocking_child(void)
141 pid_t pid = ::fork();
143 fail("Cannot fork subprocess");
148 const fs::path name = fs::path(utils::getenv("CONTROL_DIR").get()) /
150 std::ofstream pidfile(name.c_str());
152 fail("Failed to create the pidfile");
159 /// A test case that times out.
161 /// Note that the timeout is defined in the Kyuafile, as the plain interface has
162 /// no means for test programs to specify this by themselves.
167 const fs::path control_dir = fs::path(utils::getenv("CONTROL_DIR").get());
168 std::ofstream file((control_dir / "cookie").c_str());
170 fail("Failed to create the control cookie");
175 /// A test case that performs basic checks on the runtime environment.
177 /// If the runtime environment does not look clean (according to the rules in
178 /// the Kyua runtime properties), the test fails.
180 test_validate_isolation(void)
182 if (utils::getenv("HOME").get() == "fake-value")
183 fail("HOME not reset");
184 if (utils::getenv("LANG"))
185 fail("LANG not unset");
189 } // anonymous namespace
192 /// Entry point to the test program.
194 /// The caller can select which test case to run by defining the TEST_CASE
195 /// environment variable. This is not "standard", in the sense this is not a
196 /// generic property of the plain test case interface.
198 /// \todo It may be worth to split this binary into separate, smaller binaries,
199 /// one for every "test case". We use this program as a dispatcher for
200 /// different "main"s, the only reason being to keep the amount of helper test
201 /// programs to a minimum. However, putting this each function in its own
202 /// binary could simplify many other things.
204 /// \param argc The number of CLI arguments.
205 /// \param argv The CLI arguments themselves. These are not used because
206 /// Kyua will not pass any arguments to the plain test program.
208 main(int argc, char** argv)
211 std::cerr << "No arguments allowed; select the test case with the "
212 "TEST_CASE variable";
216 const std::string& test_case = guess_test_case_name(argv[0]);
218 if (test_case == "check_configuration_variables")
219 test_check_configuration_variables();
220 else if (test_case == "crash")
222 else if (test_case == "fail")
224 else if (test_case == "pass")
226 else if (test_case == "spawn_blocking_child")
227 test_spawn_blocking_child();
228 else if (test_case == "timeout")
230 else if (test_case == "validate_isolation")
231 test_validate_isolation();
233 std::cerr << "Unknown test case";