1 // Copyright 2015 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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.
16 //
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.
28 
29 #include "utils/process/deadline_killer.hpp"
30 
31 extern "C" {
32 #include <signal.h>
33 #include <unistd.h>
34 }
35 
36 #include <cstdlib>
37 
38 #include <atf-c++.hpp>
39 
40 #include "utils/datetime.hpp"
41 #include "utils/process/child.ipp"
42 #include "utils/process/status.hpp"
43 
44 namespace datetime = utils::datetime;
45 namespace process = utils::process;
46 
47 
48 namespace {
49 
50 
51 /// Body of a child process that sleeps and then exits.
52 ///
53 /// \tparam Seconds The delay the subprocess has to sleep for.
54 template< int Seconds >
55 static void
56 child_sleep(void)
57 {
58     ::sleep(Seconds);
59     std::exit(EXIT_SUCCESS);
60 }
61 
62 
63 }  // anonymous namespace
64 
65 
66 ATF_TEST_CASE_WITHOUT_HEAD(activation);
67 ATF_TEST_CASE_BODY(activation)
68 {
69     std::auto_ptr< process::child > child = process::child::fork_capture(
70         child_sleep< 60 >);
71 
72     datetime::timestamp start = datetime::timestamp::now();
73     process::deadline_killer killer(datetime::delta(1, 0), child->pid());
74     const process::status status = child->wait();
75     killer.unprogram();
76     datetime::timestamp end = datetime::timestamp::now();
77 
78     ATF_REQUIRE(killer.fired());
79     ATF_REQUIRE(end - start <= datetime::delta(10, 0));
80     ATF_REQUIRE(status.signaled());
81     ATF_REQUIRE_EQ(SIGKILL, status.termsig());
82 }
83 
84 
85 ATF_TEST_CASE_WITHOUT_HEAD(no_activation);
86 ATF_TEST_CASE_BODY(no_activation)
87 {
88     std::auto_ptr< process::child > child = process::child::fork_capture(
89         child_sleep< 1 >);
90 
91     datetime::timestamp start = datetime::timestamp::now();
92     process::deadline_killer killer(datetime::delta(60, 0), child->pid());
93     const process::status status = child->wait();
94     killer.unprogram();
95     datetime::timestamp end = datetime::timestamp::now();
96 
97     ATF_REQUIRE(!killer.fired());
98     ATF_REQUIRE(end - start <= datetime::delta(10, 0));
99     ATF_REQUIRE(status.exited());
100     ATF_REQUIRE_EQ(EXIT_SUCCESS, status.exitstatus());
101 }
102 
103 
104 ATF_INIT_TEST_CASES(tcs)
105 {
106     ATF_ADD_TEST_CASE(tcs, activation);
107     ATF_ADD_TEST_CASE(tcs, no_activation);
108 }
109