xref: /minix3/external/bsd/kyua-cli/dist/utils/process/status_test.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*11be35a1SLionel Sambuc // Copyright 2010 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 #include "utils/process/status.hpp"
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc extern "C" {
32*11be35a1SLionel Sambuc #include <sys/resource.h>
33*11be35a1SLionel Sambuc #include <sys/wait.h>
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc #include <signal.h>
36*11be35a1SLionel Sambuc #include <unistd.h>
37*11be35a1SLionel Sambuc }
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc #include <cstdlib>
40*11be35a1SLionel Sambuc 
41*11be35a1SLionel Sambuc #include <atf-c++.hpp>
42*11be35a1SLionel Sambuc 
43*11be35a1SLionel Sambuc using utils::process::status;
44*11be35a1SLionel Sambuc 
45*11be35a1SLionel Sambuc 
46*11be35a1SLionel Sambuc namespace {
47*11be35a1SLionel Sambuc 
48*11be35a1SLionel Sambuc 
49*11be35a1SLionel Sambuc /// Body of a subprocess that exits with a particular exit status.
50*11be35a1SLionel Sambuc ///
51*11be35a1SLionel Sambuc /// \tparam ExitStatus The status to exit with.
52*11be35a1SLionel Sambuc template< int ExitStatus >
child_exit(void)53*11be35a1SLionel Sambuc void child_exit(void)
54*11be35a1SLionel Sambuc {
55*11be35a1SLionel Sambuc     std::exit(ExitStatus);
56*11be35a1SLionel Sambuc }
57*11be35a1SLionel Sambuc 
58*11be35a1SLionel Sambuc 
59*11be35a1SLionel Sambuc /// Body of a subprocess that sends a particular signal to itself.
60*11be35a1SLionel Sambuc ///
61*11be35a1SLionel Sambuc /// \tparam Signo The signal to send to self.
62*11be35a1SLionel Sambuc template< int Signo >
child_signal(void)63*11be35a1SLionel Sambuc void child_signal(void)
64*11be35a1SLionel Sambuc {
65*11be35a1SLionel Sambuc     ::kill(::getpid(), Signo);
66*11be35a1SLionel Sambuc }
67*11be35a1SLionel Sambuc 
68*11be35a1SLionel Sambuc 
69*11be35a1SLionel Sambuc /// Spawns a process and waits for completion.
70*11be35a1SLionel Sambuc ///
71*11be35a1SLionel Sambuc /// \param hook The function to run within the child.  Should not return.
72*11be35a1SLionel Sambuc ///
73*11be35a1SLionel Sambuc /// \return The termination status of the spawned subprocess.
74*11be35a1SLionel Sambuc status
fork_and_wait(void (* hook)(void))75*11be35a1SLionel Sambuc fork_and_wait(void (*hook)(void))
76*11be35a1SLionel Sambuc {
77*11be35a1SLionel Sambuc     pid_t pid = ::fork();
78*11be35a1SLionel Sambuc     ATF_REQUIRE(pid != -1);
79*11be35a1SLionel Sambuc     if (pid == 0) {
80*11be35a1SLionel Sambuc         hook();
81*11be35a1SLionel Sambuc         std::abort();
82*11be35a1SLionel Sambuc     } else {
83*11be35a1SLionel Sambuc         int stat_loc;
84*11be35a1SLionel Sambuc         ATF_REQUIRE(::waitpid(pid, &stat_loc, 0) != -1);
85*11be35a1SLionel Sambuc         const status s = status(pid, stat_loc);
86*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(pid, s.dead_pid());
87*11be35a1SLionel Sambuc         return s;
88*11be35a1SLionel Sambuc     }
89*11be35a1SLionel Sambuc }
90*11be35a1SLionel Sambuc 
91*11be35a1SLionel Sambuc 
92*11be35a1SLionel Sambuc }  // anonymous namespace
93*11be35a1SLionel Sambuc 
94*11be35a1SLionel Sambuc 
95*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(fake_exited)
ATF_TEST_CASE_BODY(fake_exited)96*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(fake_exited)
97*11be35a1SLionel Sambuc {
98*11be35a1SLionel Sambuc     const status fake = status::fake_exited(123);
99*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(-1, fake.dead_pid());
100*11be35a1SLionel Sambuc     ATF_REQUIRE(fake.exited());
101*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(123, fake.exitstatus());
102*11be35a1SLionel Sambuc     ATF_REQUIRE(!fake.signaled());
103*11be35a1SLionel Sambuc }
104*11be35a1SLionel Sambuc 
105*11be35a1SLionel Sambuc 
106*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(fake_signaled)
ATF_TEST_CASE_BODY(fake_signaled)107*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(fake_signaled)
108*11be35a1SLionel Sambuc {
109*11be35a1SLionel Sambuc     const status fake = status::fake_signaled(567, true);
110*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(-1, fake.dead_pid());
111*11be35a1SLionel Sambuc     ATF_REQUIRE(!fake.exited());
112*11be35a1SLionel Sambuc     ATF_REQUIRE(fake.signaled());
113*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(567, fake.termsig());
114*11be35a1SLionel Sambuc     ATF_REQUIRE(fake.coredump());
115*11be35a1SLionel Sambuc }
116*11be35a1SLionel Sambuc 
117*11be35a1SLionel Sambuc 
118*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(integration__exited);
ATF_TEST_CASE_BODY(integration__exited)119*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(integration__exited)
120*11be35a1SLionel Sambuc {
121*11be35a1SLionel Sambuc     const status exit_success = fork_and_wait(child_exit< EXIT_SUCCESS >);
122*11be35a1SLionel Sambuc     ATF_REQUIRE(exit_success.exited());
123*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(EXIT_SUCCESS, exit_success.exitstatus());
124*11be35a1SLionel Sambuc     ATF_REQUIRE(!exit_success.signaled());
125*11be35a1SLionel Sambuc 
126*11be35a1SLionel Sambuc     const status exit_failure = fork_and_wait(child_exit< EXIT_FAILURE >);
127*11be35a1SLionel Sambuc     ATF_REQUIRE(exit_failure.exited());
128*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(EXIT_FAILURE, exit_failure.exitstatus());
129*11be35a1SLionel Sambuc     ATF_REQUIRE(!exit_failure.signaled());
130*11be35a1SLionel Sambuc }
131*11be35a1SLionel Sambuc 
132*11be35a1SLionel Sambuc 
133*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(integration__signaled);
ATF_TEST_CASE_BODY(integration__signaled)134*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(integration__signaled)
135*11be35a1SLionel Sambuc {
136*11be35a1SLionel Sambuc     const status sigterm = fork_and_wait(child_signal< SIGTERM >);
137*11be35a1SLionel Sambuc     ATF_REQUIRE(!sigterm.exited());
138*11be35a1SLionel Sambuc     ATF_REQUIRE(sigterm.signaled());
139*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(SIGTERM, sigterm.termsig());
140*11be35a1SLionel Sambuc     ATF_REQUIRE(!sigterm.coredump());
141*11be35a1SLionel Sambuc 
142*11be35a1SLionel Sambuc     const status sigkill = fork_and_wait(child_signal< SIGKILL >);
143*11be35a1SLionel Sambuc     ATF_REQUIRE(!sigkill.exited());
144*11be35a1SLionel Sambuc     ATF_REQUIRE(sigkill.signaled());
145*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(SIGKILL, sigkill.termsig());
146*11be35a1SLionel Sambuc     ATF_REQUIRE(!sigkill.coredump());
147*11be35a1SLionel Sambuc }
148*11be35a1SLionel Sambuc 
149*11be35a1SLionel Sambuc 
150*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(integration__coredump);
ATF_TEST_CASE_BODY(integration__coredump)151*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(integration__coredump)
152*11be35a1SLionel Sambuc {
153*11be35a1SLionel Sambuc     struct rlimit rl;
154*11be35a1SLionel Sambuc     rl.rlim_cur = RLIM_INFINITY;
155*11be35a1SLionel Sambuc     rl.rlim_max = RLIM_INFINITY;
156*11be35a1SLionel Sambuc     if (::setrlimit(RLIMIT_CORE, &rl) == -1)
157*11be35a1SLionel Sambuc         skip("Cannot unlimit the core file size; check limits manually");
158*11be35a1SLionel Sambuc 
159*11be35a1SLionel Sambuc     const status coredump = fork_and_wait(child_signal< SIGQUIT >);
160*11be35a1SLionel Sambuc     ATF_REQUIRE(!coredump.exited());
161*11be35a1SLionel Sambuc     ATF_REQUIRE(coredump.signaled());
162*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(SIGQUIT, coredump.termsig());
163*11be35a1SLionel Sambuc #if !defined(WCOREDUMP)
164*11be35a1SLionel Sambuc     expect_fail("Platform does not support checking for coredump");
165*11be35a1SLionel Sambuc #endif
166*11be35a1SLionel Sambuc     ATF_REQUIRE(coredump.coredump());
167*11be35a1SLionel Sambuc }
168*11be35a1SLionel Sambuc 
169*11be35a1SLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)170*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
171*11be35a1SLionel Sambuc {
172*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, fake_exited);
173*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, fake_signaled);
174*11be35a1SLionel Sambuc 
175*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, integration__exited);
176*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, integration__signaled);
177*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, integration__coredump);
178*11be35a1SLionel Sambuc }
179