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