blob: 4778dacf967d1b84ae0f2b25d15ff8d85d0ccaba [file] [log] [blame]
William A. Kennington III01db6622021-01-31 15:24:13 -08001#if __has_include(<catch2/catch.hpp>)
William A. Kennington III770c5562020-02-13 13:07:42 -08002#include <catch2/catch.hpp>
William A. Kennington III01db6622021-01-31 15:24:13 -08003#else
4#include <catch2/catch_test_macros.hpp>
5#endif
William A. Kennington III9c97d2c2018-11-02 17:25:36 -07006#include <cstring>
William A. Kennington III9c97d2c2018-11-02 17:25:36 -07007#include <signal.h>
8#include <stdplus/signal.hpp>
9
10namespace stdplus
11{
12namespace signal
13{
14namespace
15{
16
William A. Kennington III770c5562020-02-13 13:07:42 -080017TEST_CASE("Signals are blocked", "[signal]")
William A. Kennington III9c97d2c2018-11-02 17:25:36 -070018{
19 constexpr int s = SIGINT;
20 constexpr int otherS = SIGTERM;
21 constexpr int notBlocked = SIGPROF;
22
23 sigset_t expectedSet;
William A. Kennington III770c5562020-02-13 13:07:42 -080024 REQUIRE(0 == sigprocmask(SIG_BLOCK, nullptr, &expectedSet));
25 REQUIRE(0 == sigaddset(&expectedSet, otherS));
26 REQUIRE(0 == sigprocmask(SIG_BLOCK, &expectedSet, nullptr));
27 REQUIRE(0 == sigismember(&expectedSet, notBlocked));
28 REQUIRE(0 == sigismember(&expectedSet, s));
29 REQUIRE(0 == sigaddset(&expectedSet, s));
William A. Kennington III9c97d2c2018-11-02 17:25:36 -070030
31 block(s);
32
33 sigset_t newSet;
William A. Kennington III770c5562020-02-13 13:07:42 -080034 REQUIRE(0 == sigprocmask(SIG_BLOCK, nullptr, &newSet));
35 REQUIRE(sigismember(&expectedSet, s) == sigismember(&newSet, s));
36 REQUIRE(sigismember(&expectedSet, otherS) == sigismember(&newSet, otherS));
37 REQUIRE(sigismember(&expectedSet, notBlocked) ==
38 sigismember(&newSet, notBlocked));
William A. Kennington III9c97d2c2018-11-02 17:25:36 -070039}
40
William A. Kennington III770c5562020-02-13 13:07:42 -080041TEST_CASE("Signals stay blocked if already blocked", "[signal]")
William A. Kennington III9c97d2c2018-11-02 17:25:36 -070042{
43 constexpr int s = SIGINT;
44 constexpr int otherS = SIGTERM;
45 constexpr int notBlocked = SIGPROF;
46
47 sigset_t expectedSet;
William A. Kennington III770c5562020-02-13 13:07:42 -080048 REQUIRE(0 == sigprocmask(SIG_BLOCK, nullptr, &expectedSet));
49 REQUIRE(0 == sigaddset(&expectedSet, s));
50 REQUIRE(0 == sigaddset(&expectedSet, otherS));
51 REQUIRE(0 == sigismember(&expectedSet, notBlocked));
52 REQUIRE(0 == sigprocmask(SIG_BLOCK, &expectedSet, nullptr));
William A. Kennington III9c97d2c2018-11-02 17:25:36 -070053
54 block(s);
55
56 sigset_t newSet;
William A. Kennington III770c5562020-02-13 13:07:42 -080057 REQUIRE(0 == sigprocmask(SIG_BLOCK, nullptr, &newSet));
58 REQUIRE(sigismember(&expectedSet, s) == sigismember(&newSet, s));
59 REQUIRE(sigismember(&expectedSet, otherS) == sigismember(&newSet, otherS));
60 REQUIRE(sigismember(&expectedSet, notBlocked) ==
61 sigismember(&newSet, notBlocked));
William A. Kennington III9c97d2c2018-11-02 17:25:36 -070062}
63
64} // namespace
65} // namespace signal
66} // namespace stdplus