1 // Copyright 2015 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;
42 #include "utils/env.hpp"
43 #include "utils/format/containers.ipp"
44 #include "utils/format/macros.hpp"
45 #include "utils/fs/path.hpp"
46 #include "utils/optional.ipp"
47 #include "utils/test_utils.ipp"
49 namespace fs = utils::fs;
55 /// Logs an error message and exits the test with an error code.
57 /// \param str The error message to log.
59 fail(const std::string& str)
61 std::cerr << str << '\n';
62 std::exit(EXIT_FAILURE);
66 /// A test scenario that validates the TEST_ENV_* variables.
68 test_check_configuration_variables(void)
70 std::set< std::string > vars;
72 for (iter = environ; *iter != NULL; ++iter) {
73 if (std::strstr(*iter, "TEST_ENV_") == *iter) {
78 std::set< std::string > exp_vars;
79 exp_vars.insert("TEST_ENV_first=some value");
80 exp_vars.insert("TEST_ENV_second=some other value");
81 if (vars == exp_vars) {
87 << F(" Expected: %s\nFound: %s\n") % exp_vars % vars;
92 /// A test scenario that crashes.
96 utils::abort_without_coredump();
100 /// A test scenario that reports some tests as failed.
104 std::cout << "1..5\n"
105 << "ok 1 - This is good!\n"
107 << "ok 3 - TODO Consider this as passed\n"
113 /// A test scenario that passes.
117 std::cout << "1..4\n"
118 << "ok 1 - This is good!\n"
119 << "non-result data\n"
120 << "ok 2 - SKIP Consider this as passed\n"
121 << "ok 3 - TODO Consider this as passed\n"
126 /// A test scenario that passes but then exits with non-zero.
128 test_pass_but_exit_failure(void)
130 std::cout << "1..2\n"
137 /// A test scenario that times out.
139 /// Note that the timeout is defined in the Kyuafile, as the TAP interface has
140 /// no means for test programs to specify this by themselves.
144 std::cout << "1..2\n"
148 const fs::path control_dir = fs::path(utils::getenv("CONTROL_DIR").get());
149 std::ofstream file((control_dir / "cookie").c_str());
151 fail("Failed to create the control cookie");
156 } // anonymous namespace
159 /// Entry point to the test program.
161 /// The caller can select which test scenario to run by modifying the program's
162 /// basename on disk (either by a copy or by a hard link).
164 /// \todo It may be worth to split this binary into separate, smaller binaries,
165 /// one for every "test scenario". We use this program as a dispatcher for
166 /// different "main"s, the only reason being to keep the amount of helper test
167 /// programs to a minimum. However, putting this each function in its own
168 /// binary could simplify many other things.
170 /// \param argc The number of CLI arguments.
171 /// \param argv The CLI arguments themselves. These are not used because
172 /// Kyua will not pass any arguments to the plain test program.
174 main(int argc, char** argv)
177 std::cerr << "No arguments allowed; select the test scenario with the "
178 "program's basename\n";
182 const std::string& test_scenario = fs::path(argv[0]).leaf_name();
184 if (test_scenario == "check_configuration_variables")
185 test_check_configuration_variables();
186 else if (test_scenario == "crash")
188 else if (test_scenario == "fail")
190 else if (test_scenario == "pass")
192 else if (test_scenario == "pass_but_exit_failure")
193 test_pass_but_exit_failure();
194 else if (test_scenario == "timeout")
197 std::cerr << "Unknown test scenario\n";