2 // Automated Testing Framework (atf)
4 // Copyright (c) 2008 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/types.h>
40 #include "atf-c/defs.h"
42 #include "atf-c++/macros.hpp"
44 #include "atf-c++/detail/exceptions.hpp"
45 #include "atf-c++/detail/process.hpp"
47 #include "signals.hpp"
49 // ------------------------------------------------------------------------
50 // Auxiliary functions.
51 // ------------------------------------------------------------------------
54 static bool happened
= false;
58 handler(int signo ATF_DEFS_ATTRIBUTE_UNUSED
)
68 sa
.sa_handler
= handler
;
69 sigemptyset(&sa
.sa_mask
);
71 if (::sigaction(SIGUSR1
, &sa
, NULL
) == -1)
72 throw atf::system_error("sigusr1::program",
73 "sigaction(2) failed", errno
);
75 } // namespace sigusr1
78 static bool happened
= false;
82 handler(int signo ATF_DEFS_ATTRIBUTE_UNUSED
)
86 } // namespace sigusr1_2
88 // ------------------------------------------------------------------------
89 // Tests for the "signal_holder" class.
90 // ------------------------------------------------------------------------
92 ATF_TEST_CASE(signal_holder_preserve
);
93 ATF_TEST_CASE_HEAD(signal_holder_preserve
)
95 set_md_var("descr", "Tests that signal_holder preserves the original "
96 "signal handler and restores it upon destruction");
98 ATF_TEST_CASE_BODY(signal_holder_preserve
)
100 using atf::atf_run::signal_holder
;
104 sigusr1::happened
= false;
105 ::kill(::getpid(), SIGUSR1
);
106 ATF_REQUIRE(sigusr1::happened
);
109 signal_holder
hld(SIGUSR1
);
110 ::kill(::getpid(), SIGUSR1
);
113 sigusr1::happened
= false;
114 ::kill(::getpid(), SIGUSR1
);
115 ATF_REQUIRE(sigusr1::happened
);
118 ATF_TEST_CASE(signal_holder_destructor
);
119 ATF_TEST_CASE_HEAD(signal_holder_destructor
)
121 set_md_var("descr", "Tests that signal_holder processes a pending "
122 "signal upon destruction");
124 ATF_TEST_CASE_BODY(signal_holder_destructor
)
126 using atf::atf_run::signal_holder
;
130 sigusr1::happened
= false;
131 ::kill(::getpid(), SIGUSR1
);
132 ATF_REQUIRE(sigusr1::happened
);
135 signal_holder
hld(SIGUSR1
);
137 sigusr1::happened
= false;
138 ::kill(::getpid(), SIGUSR1
);
139 ATF_REQUIRE(!sigusr1::happened
);
141 ATF_REQUIRE(sigusr1::happened
);
144 ATF_TEST_CASE(signal_holder_process
);
145 ATF_TEST_CASE_HEAD(signal_holder_process
)
147 set_md_var("descr", "Tests that signal_holder's process method works "
148 "to process a delayed signal explicitly");
150 ATF_TEST_CASE_BODY(signal_holder_process
)
152 using atf::atf_run::signal_holder
;
156 sigusr1::happened
= false;
157 ::kill(::getpid(), SIGUSR1
);
158 ATF_REQUIRE(sigusr1::happened
);
161 signal_holder
hld(SIGUSR1
);
163 sigusr1::happened
= false;
164 ::kill(::getpid(), SIGUSR1
);
165 ATF_REQUIRE(!sigusr1::happened
);
168 ATF_REQUIRE(sigusr1::happened
);
170 sigusr1::happened
= false;
172 ATF_REQUIRE(!sigusr1::happened
);
175 // ------------------------------------------------------------------------
176 // Tests for the "signal_programmer" class.
177 // ------------------------------------------------------------------------
179 ATF_TEST_CASE(signal_programmer_program
);
180 ATF_TEST_CASE_HEAD(signal_programmer_program
)
182 set_md_var("descr", "Tests that signal_programmer correctly installs a "
185 ATF_TEST_CASE_BODY(signal_programmer_program
)
187 using atf::atf_run::signal_programmer
;
189 signal_programmer
sp(SIGUSR1
, sigusr1_2::handler
);
191 sigusr1_2::happened
= false;
192 ::kill(::getpid(), SIGUSR1
);
193 ATF_REQUIRE(sigusr1_2::happened
);
196 ATF_TEST_CASE(signal_programmer_preserve
);
197 ATF_TEST_CASE_HEAD(signal_programmer_preserve
)
199 set_md_var("descr", "Tests that signal_programmer uninstalls the "
200 "handler during destruction");
202 ATF_TEST_CASE_BODY(signal_programmer_preserve
)
204 using atf::atf_run::signal_programmer
;
207 sigusr1::happened
= false;
210 signal_programmer
sp(SIGUSR1
, sigusr1_2::handler
);
212 sigusr1_2::happened
= false;
213 ::kill(::getpid(), SIGUSR1
);
214 ATF_REQUIRE(sigusr1_2::happened
);
217 ATF_REQUIRE(!sigusr1::happened
);
218 ::kill(::getpid(), SIGUSR1
);
219 ATF_REQUIRE(sigusr1::happened
);
222 // ------------------------------------------------------------------------
223 // Tests cases for the free functions.
224 // ------------------------------------------------------------------------
228 reset_child(void *v ATF_DEFS_ATTRIBUTE_UNUSED
)
232 sigusr1::happened
= false;
233 atf::atf_run::reset(SIGUSR1
);
234 kill(::getpid(), SIGUSR1
);
236 if (sigusr1::happened
) {
237 std::cerr
<< "Signal was not resetted correctly\n";
240 std::exit(EXIT_SUCCESS
);
244 ATF_TEST_CASE(reset
);
245 ATF_TEST_CASE_HEAD(reset
)
247 set_md_var("descr", "Tests the reset function");
249 ATF_TEST_CASE_BODY(reset
)
251 atf::process::child c
=
252 atf::process::fork(reset_child
, atf::process::stream_inherit(),
253 atf::process::stream_inherit(), NULL
);
255 const atf::process::status s
= c
.wait();
256 ATF_REQUIRE(s
.exited() || s
.signaled());
257 ATF_REQUIRE(!s
.signaled() || s
.termsig() == SIGUSR1
);
260 // ------------------------------------------------------------------------
262 // ------------------------------------------------------------------------
264 ATF_INIT_TEST_CASES(tcs
)
266 // Add the tests for the "signal_holder" class.
267 ATF_ADD_TEST_CASE(tcs
, signal_holder_preserve
);
268 ATF_ADD_TEST_CASE(tcs
, signal_holder_destructor
);
269 ATF_ADD_TEST_CASE(tcs
, signal_holder_process
);
271 // Add the tests for the "signal_programmer" class.
272 ATF_ADD_TEST_CASE(tcs
, signal_programmer_program
);
273 ATF_ADD_TEST_CASE(tcs
, signal_programmer_preserve
);
275 // Add the test cases for the free functions.
276 ATF_ADD_TEST_CASE(tcs
, reset
);