blob: 91c717fc5f191a0119df2f87726a6eed7fb9b73d [file] [log] [blame]
William A. Kennington III770c5562020-02-13 13:07:42 -08001#include <catch2/catch.hpp>
William A. Kennington III9c97d2c2018-11-02 17:25:36 -07002#include <cstring>
William A. Kennington III9c97d2c2018-11-02 17:25:36 -07003#include <signal.h>
4#include <stdplus/signal.hpp>
5
6namespace stdplus
7{
8namespace signal
9{
10namespace
11{
12
William A. Kennington III770c5562020-02-13 13:07:42 -080013TEST_CASE("Signals are blocked", "[signal]")
William A. Kennington III9c97d2c2018-11-02 17:25:36 -070014{
15 constexpr int s = SIGINT;
16 constexpr int otherS = SIGTERM;
17 constexpr int notBlocked = SIGPROF;
18
19 sigset_t expectedSet;
William A. Kennington III770c5562020-02-13 13:07:42 -080020 REQUIRE(0 == sigprocmask(SIG_BLOCK, nullptr, &expectedSet));
21 REQUIRE(0 == sigaddset(&expectedSet, otherS));
22 REQUIRE(0 == sigprocmask(SIG_BLOCK, &expectedSet, nullptr));
23 REQUIRE(0 == sigismember(&expectedSet, notBlocked));
24 REQUIRE(0 == sigismember(&expectedSet, s));
25 REQUIRE(0 == sigaddset(&expectedSet, s));
William A. Kennington III9c97d2c2018-11-02 17:25:36 -070026
27 block(s);
28
29 sigset_t newSet;
William A. Kennington III770c5562020-02-13 13:07:42 -080030 REQUIRE(0 == sigprocmask(SIG_BLOCK, nullptr, &newSet));
31 REQUIRE(sigismember(&expectedSet, s) == sigismember(&newSet, s));
32 REQUIRE(sigismember(&expectedSet, otherS) == sigismember(&newSet, otherS));
33 REQUIRE(sigismember(&expectedSet, notBlocked) ==
34 sigismember(&newSet, notBlocked));
William A. Kennington III9c97d2c2018-11-02 17:25:36 -070035}
36
William A. Kennington III770c5562020-02-13 13:07:42 -080037TEST_CASE("Signals stay blocked if already blocked", "[signal]")
William A. Kennington III9c97d2c2018-11-02 17:25:36 -070038{
39 constexpr int s = SIGINT;
40 constexpr int otherS = SIGTERM;
41 constexpr int notBlocked = SIGPROF;
42
43 sigset_t expectedSet;
William A. Kennington III770c5562020-02-13 13:07:42 -080044 REQUIRE(0 == sigprocmask(SIG_BLOCK, nullptr, &expectedSet));
45 REQUIRE(0 == sigaddset(&expectedSet, s));
46 REQUIRE(0 == sigaddset(&expectedSet, otherS));
47 REQUIRE(0 == sigismember(&expectedSet, notBlocked));
48 REQUIRE(0 == sigprocmask(SIG_BLOCK, &expectedSet, nullptr));
William A. Kennington III9c97d2c2018-11-02 17:25:36 -070049
50 block(s);
51
52 sigset_t newSet;
William A. Kennington III770c5562020-02-13 13:07:42 -080053 REQUIRE(0 == sigprocmask(SIG_BLOCK, nullptr, &newSet));
54 REQUIRE(sigismember(&expectedSet, s) == sigismember(&newSet, s));
55 REQUIRE(sigismember(&expectedSet, otherS) == sigismember(&newSet, otherS));
56 REQUIRE(sigismember(&expectedSet, notBlocked) ==
57 sigismember(&newSet, notBlocked));
William A. Kennington III9c97d2c2018-11-02 17:25:36 -070058}
59
60} // namespace
61} // namespace signal
62} // namespace stdplus