1 // Copyright 2010 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.
45 #include <atf-c++.hpp>
47 #include "utils/env.hpp"
48 #include "utils/fs/operations.hpp"
49 #include "utils/fs/path.hpp"
50 #include "utils/logging/operations.hpp"
51 #include "utils/optional.ipp"
52 #include "utils/test_utils.ipp"
53 #include "utils/text/operations.ipp"
55 namespace fs = utils::fs;
56 namespace logging = utils::logging;
57 namespace text = utils::text;
59 using utils::optional;
65 /// Creates an empty file in the given directory.
67 /// \param test_case The test case currently running.
68 /// \param directory The name of the configuration variable that holds the path
69 /// to the directory in which to create the cookie file.
70 /// \param name The name of the cookie file to create.
72 create_cookie(const atf::tests::tc* test_case, const char* directory,
75 if (!test_case->has_config_var(directory))
76 test_case->fail(std::string(name) + " not provided");
78 const fs::path control_dir(test_case->get_config_var(directory));
79 std::ofstream file((control_dir / name).c_str());
81 test_case->fail("Failed to create the control cookie");
86 } // anonymous namespace
89 ATF_TEST_CASE_WITH_CLEANUP(check_cleanup_workdir);
90 ATF_TEST_CASE_HEAD(check_cleanup_workdir)
92 set_md_var("require.config", "control_dir");
94 ATF_TEST_CASE_BODY(check_cleanup_workdir)
96 std::ofstream cookie("workdir_cookie");
99 skip("cookie created");
101 ATF_TEST_CASE_CLEANUP(check_cleanup_workdir)
103 const fs::path control_dir(get_config_var("control_dir"));
105 std::ifstream cookie("workdir_cookie");
107 std::ofstream((control_dir / "missing_cookie").c_str()).close();
108 std::exit(EXIT_FAILURE);
113 if (value != "1234") {
114 std::ofstream((control_dir / "invalid_cookie").c_str()).close();
115 std::exit(EXIT_FAILURE);
118 std::ofstream((control_dir / "cookie_ok").c_str()).close();
119 std::exit(EXIT_SUCCESS);
123 ATF_TEST_CASE_WITHOUT_HEAD(check_configuration_variables);
124 ATF_TEST_CASE_BODY(check_configuration_variables)
126 ATF_REQUIRE(has_config_var("first"));
127 ATF_REQUIRE_EQ("some value", get_config_var("first"));
129 ATF_REQUIRE(has_config_var("second"));
130 ATF_REQUIRE_EQ("some other value", get_config_var("second"));
134 ATF_TEST_CASE(check_list_config);
135 ATF_TEST_CASE_HEAD(check_list_config)
137 std::string description = "Found:";
139 if (has_config_var("var1"))
140 description += " var1=" + get_config_var("var1");
141 if (has_config_var("var2"))
142 description += " var2=" + get_config_var("var2");
144 set_md_var("descr", description);
146 ATF_TEST_CASE_BODY(check_list_config)
151 ATF_TEST_CASE_WITHOUT_HEAD(check_unprivileged);
152 ATF_TEST_CASE_BODY(check_unprivileged)
155 fail("Running as root, but I shouldn't be");
157 std::ofstream file("cookie");
159 fail("Failed to create the cookie; work directory probably owned by "
165 ATF_TEST_CASE_WITHOUT_HEAD(crash);
166 ATF_TEST_CASE_BODY(crash)
172 ATF_TEST_CASE(crash_head);
173 ATF_TEST_CASE_HEAD(crash_head)
175 utils::abort_without_coredump();
177 ATF_TEST_CASE_BODY(crash_head)
182 ATF_TEST_CASE_WITH_CLEANUP(crash_cleanup);
183 ATF_TEST_CASE_HEAD(crash_cleanup)
186 ATF_TEST_CASE_BODY(crash_cleanup)
189 ATF_TEST_CASE_CLEANUP(crash_cleanup)
191 utils::abort_without_coredump();
195 ATF_TEST_CASE_WITHOUT_HEAD(create_cookie_in_control_dir);
196 ATF_TEST_CASE_BODY(create_cookie_in_control_dir)
198 create_cookie(this, "control_dir", "cookie");
202 ATF_TEST_CASE_WITHOUT_HEAD(create_cookie_in_workdir);
203 ATF_TEST_CASE_BODY(create_cookie_in_workdir)
205 std::ofstream file("cookie");
207 fail("Failed to create the cookie");
212 ATF_TEST_CASE_WITH_CLEANUP(create_cookie_from_cleanup);
213 ATF_TEST_CASE_HEAD(create_cookie_from_cleanup)
216 ATF_TEST_CASE_BODY(create_cookie_from_cleanup)
219 ATF_TEST_CASE_CLEANUP(create_cookie_from_cleanup)
221 create_cookie(this, "control_dir", "cookie");
225 ATF_TEST_CASE_WITH_CLEANUP(expect_timeout);
226 ATF_TEST_CASE_HEAD(expect_timeout)
228 if (has_config_var("timeout"))
229 set_md_var("timeout", get_config_var("timeout"));
231 ATF_TEST_CASE_BODY(expect_timeout)
233 expect_timeout("Times out on purpose");
235 create_cookie(this, "control_dir", "cookie");
237 ATF_TEST_CASE_CLEANUP(expect_timeout)
239 create_cookie(this, "control_dir", "cookie.cleanup");
243 ATF_TEST_CASE_WITH_CLEANUP(output);
244 ATF_TEST_CASE_HEAD(output)
247 ATF_TEST_CASE_BODY(output)
249 std::cout << "Body message to stdout\n";
250 std::cerr << "Body message to stderr\n";
252 ATF_TEST_CASE_CLEANUP(output)
254 std::cout << "Cleanup message to stdout\n";
255 std::cerr << "Cleanup message to stderr\n";
259 ATF_TEST_CASE(output_in_list);
260 ATF_TEST_CASE_HEAD(output_in_list)
262 std::cerr << "Should not write anything!\n";
264 ATF_TEST_CASE_BODY(output_in_list)
270 ATF_TEST_CASE_HEAD(pass)
272 set_md_var("descr", "Always-passing test case");
274 ATF_TEST_CASE_BODY(pass)
279 ATF_TEST_CASE_WITH_CLEANUP(shared_workdir);
280 ATF_TEST_CASE_HEAD(shared_workdir)
283 ATF_TEST_CASE_BODY(shared_workdir)
285 atf::utils::create_file("shared_cookie", "");
287 ATF_TEST_CASE_CLEANUP(shared_workdir)
289 if (!atf::utils::file_exists("shared_cookie"))
290 utils::abort_without_coredump();
294 ATF_TEST_CASE(spawn_blocking_child);
295 ATF_TEST_CASE_HEAD(spawn_blocking_child)
297 set_md_var("require.config", "control_dir");
299 ATF_TEST_CASE_BODY(spawn_blocking_child)
301 pid_t pid = ::fork();
303 fail("Cannot fork subprocess");
308 const fs::path name = fs::path(get_config_var("control_dir")) / "pid";
309 std::ofstream pidfile(name.c_str());
310 ATF_REQUIRE(pidfile);
317 ATF_TEST_CASE_WITH_CLEANUP(timeout_body);
318 ATF_TEST_CASE_HEAD(timeout_body)
320 if (has_config_var("timeout"))
321 set_md_var("timeout", get_config_var("timeout"));
323 ATF_TEST_CASE_BODY(timeout_body)
326 create_cookie(this, "control_dir", "cookie");
328 ATF_TEST_CASE_CLEANUP(timeout_body)
330 create_cookie(this, "control_dir", "cookie.cleanup");
334 ATF_TEST_CASE_WITH_CLEANUP(timeout_cleanup);
335 ATF_TEST_CASE_HEAD(timeout_cleanup)
338 ATF_TEST_CASE_BODY(timeout_cleanup)
341 ATF_TEST_CASE_CLEANUP(timeout_cleanup)
344 create_cookie(this, "control_dir", "cookie");
348 ATF_TEST_CASE_WITHOUT_HEAD(validate_isolation);
349 ATF_TEST_CASE_BODY(validate_isolation)
351 ATF_REQUIRE(utils::getenv("HOME").get() != "fake-value");
352 ATF_REQUIRE(!utils::getenv("LANG"));
356 /// Wrapper around ATF_ADD_TEST_CASE to only add a test when requested.
358 /// The caller can set the TEST_CASES environment variable to a
359 /// whitespace-separated list of test case names to enable. If not empty, the
360 /// list acts as a filter for the tests to add.
362 /// \param tcs List of test cases into which to register the test.
363 /// \param filters List of filters to determine whether the test applies or not.
364 /// \param name Name of the test case being added.
365 #define ADD_TEST_CASE(tcs, filters, name) \
367 if (filters.empty() || filters.find(#name) != filters.end()) \
368 ATF_ADD_TEST_CASE(tcs, name); \
372 ATF_INIT_TEST_CASES(tcs)
374 logging::set_inmemory();
376 // TODO(jmmv): Instead of using "filters", we should make TEST_CASES
377 // explicitly list all the test cases to enable. This would let us get rid
378 // of some of the hacks below...
379 std::set< std::string > filters;
381 const optional< std::string > names_raw = utils::getenv("TEST_CASES");
383 if (names_raw.get().empty())
384 return; // See TODO above.
386 const std::vector< std::string > names = text::split(
387 names_raw.get(), ' ');
388 std::copy(names.begin(), names.end(),
389 std::inserter(filters, filters.begin()));
392 if (filters.find("crash_head") != filters.end()) // See TODO above.
393 ATF_ADD_TEST_CASE(tcs, crash_head);
394 if (filters.find("output_in_list") != filters.end()) // See TODO above.
395 ATF_ADD_TEST_CASE(tcs, output_in_list);
397 ADD_TEST_CASE(tcs, filters, check_cleanup_workdir);
398 ADD_TEST_CASE(tcs, filters, check_configuration_variables);
399 ADD_TEST_CASE(tcs, filters, check_list_config);
400 ADD_TEST_CASE(tcs, filters, check_unprivileged);
401 ADD_TEST_CASE(tcs, filters, crash);
402 ADD_TEST_CASE(tcs, filters, crash_cleanup);
403 ADD_TEST_CASE(tcs, filters, create_cookie_in_control_dir);
404 ADD_TEST_CASE(tcs, filters, create_cookie_in_workdir);
405 ADD_TEST_CASE(tcs, filters, create_cookie_from_cleanup);
406 ADD_TEST_CASE(tcs, filters, expect_timeout);
407 ADD_TEST_CASE(tcs, filters, output);
408 ADD_TEST_CASE(tcs, filters, pass);
409 ADD_TEST_CASE(tcs, filters, shared_workdir);
410 ADD_TEST_CASE(tcs, filters, spawn_blocking_child);
411 ADD_TEST_CASE(tcs, filters, timeout_body);
412 ADD_TEST_CASE(tcs, filters, timeout_cleanup);
413 ADD_TEST_CASE(tcs, filters, validate_isolation);