xref: /minix3/external/bsd/kyua-cli/dist/engine/test_case_plain_helpers.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2011 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc //   without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc extern "C" {
30*11be35a1SLionel Sambuc #include <sys/stat.h>
31*11be35a1SLionel Sambuc 
32*11be35a1SLionel Sambuc #include <unistd.h>
33*11be35a1SLionel Sambuc }
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc #include <cstdlib>
36*11be35a1SLionel Sambuc #include <fstream>
37*11be35a1SLionel Sambuc #include <iostream>
38*11be35a1SLionel Sambuc #include <sstream>
39*11be35a1SLionel Sambuc 
40*11be35a1SLionel Sambuc #include "utils/defs.hpp"
41*11be35a1SLionel Sambuc #include "utils/env.hpp"
42*11be35a1SLionel Sambuc #include "utils/fs/operations.hpp"
43*11be35a1SLionel Sambuc #include "utils/fs/path.hpp"
44*11be35a1SLionel Sambuc #include "utils/optional.ipp"
45*11be35a1SLionel Sambuc 
46*11be35a1SLionel Sambuc namespace fs = utils::fs;
47*11be35a1SLionel Sambuc 
48*11be35a1SLionel Sambuc using utils::optional;
49*11be35a1SLionel Sambuc 
50*11be35a1SLionel Sambuc 
51*11be35a1SLionel Sambuc namespace {
52*11be35a1SLionel Sambuc 
53*11be35a1SLionel Sambuc 
54*11be35a1SLionel Sambuc /// Logs an error message and exits the test with an error code.
55*11be35a1SLionel Sambuc ///
56*11be35a1SLionel Sambuc /// \param str The error message to log.
57*11be35a1SLionel Sambuc static void
fail(const char * str)58*11be35a1SLionel Sambuc fail(const char* str)
59*11be35a1SLionel Sambuc {
60*11be35a1SLionel Sambuc     std::cerr << str << '\n';
61*11be35a1SLionel Sambuc     std::exit(EXIT_FAILURE);
62*11be35a1SLionel Sambuc }
63*11be35a1SLionel Sambuc 
64*11be35a1SLionel Sambuc 
65*11be35a1SLionel Sambuc /// A test case that crashes.
66*11be35a1SLionel Sambuc static void
test_crash(void)67*11be35a1SLionel Sambuc test_crash(void)
68*11be35a1SLionel Sambuc {
69*11be35a1SLionel Sambuc     std::abort();
70*11be35a1SLionel Sambuc }
71*11be35a1SLionel Sambuc 
72*11be35a1SLionel Sambuc 
73*11be35a1SLionel Sambuc /// A test case that exits with a non-zero exit code, and not 1.
74*11be35a1SLionel Sambuc static void
test_fail(void)75*11be35a1SLionel Sambuc test_fail(void)
76*11be35a1SLionel Sambuc {
77*11be35a1SLionel Sambuc     std::exit(8);
78*11be35a1SLionel Sambuc }
79*11be35a1SLionel Sambuc 
80*11be35a1SLionel Sambuc 
81*11be35a1SLionel Sambuc /// A test case that passes.
82*11be35a1SLionel Sambuc static void
test_pass(void)83*11be35a1SLionel Sambuc test_pass(void)
84*11be35a1SLionel Sambuc {
85*11be35a1SLionel Sambuc }
86*11be35a1SLionel Sambuc 
87*11be35a1SLionel Sambuc 
88*11be35a1SLionel Sambuc /// A test case that spawns a subchild that gets stuck.
89*11be35a1SLionel Sambuc ///
90*11be35a1SLionel Sambuc /// This test case is used by the caller to validate that the whole process tree
91*11be35a1SLionel Sambuc /// is terminated when the test case is killed.
92*11be35a1SLionel Sambuc static void
test_spawn_blocking_child(void)93*11be35a1SLionel Sambuc test_spawn_blocking_child(void)
94*11be35a1SLionel Sambuc {
95*11be35a1SLionel Sambuc     pid_t pid = ::fork();
96*11be35a1SLionel Sambuc     if (pid == -1)
97*11be35a1SLionel Sambuc         fail("Cannot fork subprocess");
98*11be35a1SLionel Sambuc     else if (pid == 0) {
99*11be35a1SLionel Sambuc         for (;;)
100*11be35a1SLionel Sambuc             ::pause();
101*11be35a1SLionel Sambuc     } else {
102*11be35a1SLionel Sambuc         const fs::path name = fs::path(utils::getenv("CONTROL_DIR").get()) /
103*11be35a1SLionel Sambuc             "pid";
104*11be35a1SLionel Sambuc         std::ofstream pidfile(name.c_str());
105*11be35a1SLionel Sambuc         if (!pidfile)
106*11be35a1SLionel Sambuc             fail("Failed to create the pidfile");
107*11be35a1SLionel Sambuc         pidfile << pid;
108*11be35a1SLionel Sambuc         pidfile.close();
109*11be35a1SLionel Sambuc     }
110*11be35a1SLionel Sambuc }
111*11be35a1SLionel Sambuc 
112*11be35a1SLionel Sambuc 
113*11be35a1SLionel Sambuc /// A test case that times out.
114*11be35a1SLionel Sambuc ///
115*11be35a1SLionel Sambuc /// Note that the timeout is defined in the Kyuafile, as the plain interface has
116*11be35a1SLionel Sambuc /// no means for test programs to specify this by themselves.
117*11be35a1SLionel Sambuc static void
test_timeout(void)118*11be35a1SLionel Sambuc test_timeout(void)
119*11be35a1SLionel Sambuc {
120*11be35a1SLionel Sambuc     ::sleep(10);
121*11be35a1SLionel Sambuc     const fs::path control_dir = fs::path(utils::getenv("CONTROL_DIR").get());
122*11be35a1SLionel Sambuc     std::ofstream file((control_dir / "cookie").c_str());
123*11be35a1SLionel Sambuc     if (!file)
124*11be35a1SLionel Sambuc         fail("Failed to create the control cookie");
125*11be35a1SLionel Sambuc     file.close();
126*11be35a1SLionel Sambuc }
127*11be35a1SLionel Sambuc 
128*11be35a1SLionel Sambuc 
129*11be35a1SLionel Sambuc /// A test case that performs basic checks on the runtime environment.
130*11be35a1SLionel Sambuc ///
131*11be35a1SLionel Sambuc /// If the runtime environment does not look clean (according to the rules in
132*11be35a1SLionel Sambuc /// the Kyua runtime properties), the test fails.
133*11be35a1SLionel Sambuc static void
test_validate_isolation(void)134*11be35a1SLionel Sambuc test_validate_isolation(void)
135*11be35a1SLionel Sambuc {
136*11be35a1SLionel Sambuc     if (utils::getenv("HOME").get() == "fake-value")
137*11be35a1SLionel Sambuc         fail("HOME not reset");
138*11be35a1SLionel Sambuc     if (utils::getenv("LANG"))
139*11be35a1SLionel Sambuc         fail("LANG not unset");
140*11be35a1SLionel Sambuc }
141*11be35a1SLionel Sambuc 
142*11be35a1SLionel Sambuc 
143*11be35a1SLionel Sambuc }  // anonymous namespace
144*11be35a1SLionel Sambuc 
145*11be35a1SLionel Sambuc 
146*11be35a1SLionel Sambuc /// Entry point to the test program.
147*11be35a1SLionel Sambuc ///
148*11be35a1SLionel Sambuc /// The caller can select which test case to run by defining the TEST_CASE
149*11be35a1SLionel Sambuc /// environment variable.  This is not "standard", in the sense this is not a
150*11be35a1SLionel Sambuc /// generic property of the plain test case interface.
151*11be35a1SLionel Sambuc ///
152*11be35a1SLionel Sambuc /// \todo It may be worth to split this binary into separate, smaller binaries,
153*11be35a1SLionel Sambuc /// one for every "test case".  We use this program as a dispatcher for
154*11be35a1SLionel Sambuc /// different "main"s, the only reason being to keep the amount of helper test
155*11be35a1SLionel Sambuc /// programs to a minimum.  However, putting this each function in its own
156*11be35a1SLionel Sambuc /// binary could simplify many other things.
157*11be35a1SLionel Sambuc ///
158*11be35a1SLionel Sambuc /// \param argc The number of CLI arguments.
159*11be35a1SLionel Sambuc /// \param unused_argv The CLI arguments themselves.  These are not used because
160*11be35a1SLionel Sambuc ///     Kyua will not pass any arguments to the plain test program.
161*11be35a1SLionel Sambuc int
main(int argc,char ** UTILS_UNUSED_PARAM (argv))162*11be35a1SLionel Sambuc main(int argc, char** UTILS_UNUSED_PARAM(argv))
163*11be35a1SLionel Sambuc {
164*11be35a1SLionel Sambuc     if (argc != 1) {
165*11be35a1SLionel Sambuc         std::cerr << "No arguments allowed; select the test case with the "
166*11be35a1SLionel Sambuc             "TEST_CASE variable";
167*11be35a1SLionel Sambuc         return EXIT_FAILURE;
168*11be35a1SLionel Sambuc     }
169*11be35a1SLionel Sambuc 
170*11be35a1SLionel Sambuc     const optional< std::string > test_case_env = utils::getenv("TEST_CASE");
171*11be35a1SLionel Sambuc     if (!test_case_env) {
172*11be35a1SLionel Sambuc         std::cerr << "TEST_CASE not defined";
173*11be35a1SLionel Sambuc         return EXIT_FAILURE;
174*11be35a1SLionel Sambuc     }
175*11be35a1SLionel Sambuc     const std::string& test_case = test_case_env.get();
176*11be35a1SLionel Sambuc 
177*11be35a1SLionel Sambuc     if (test_case == "crash")
178*11be35a1SLionel Sambuc         test_crash();
179*11be35a1SLionel Sambuc     else if (test_case == "fail")
180*11be35a1SLionel Sambuc         test_fail();
181*11be35a1SLionel Sambuc     else if (test_case == "pass")
182*11be35a1SLionel Sambuc         test_pass();
183*11be35a1SLionel Sambuc     else if (test_case == "spawn_blocking_child")
184*11be35a1SLionel Sambuc         test_spawn_blocking_child();
185*11be35a1SLionel Sambuc     else if (test_case == "timeout")
186*11be35a1SLionel Sambuc         test_timeout();
187*11be35a1SLionel Sambuc     else if (test_case == "validate_isolation")
188*11be35a1SLionel Sambuc         test_validate_isolation();
189*11be35a1SLionel Sambuc     else {
190*11be35a1SLionel Sambuc         std::cerr << "Unknown test case";
191*11be35a1SLionel Sambuc         return EXIT_FAILURE;
192*11be35a1SLionel Sambuc     }
193*11be35a1SLionel Sambuc 
194*11be35a1SLionel Sambuc     return EXIT_SUCCESS;
195*11be35a1SLionel Sambuc }
196