William A. Kennington III | 770c556 | 2020-02-13 13:07:42 -0800 | [diff] [blame^] | 1 | #include <catch2/catch.hpp> |
William A. Kennington III | 9c97d2c | 2018-11-02 17:25:36 -0700 | [diff] [blame] | 2 | #include <cstring> |
William A. Kennington III | 9c97d2c | 2018-11-02 17:25:36 -0700 | [diff] [blame] | 3 | #include <signal.h> |
| 4 | #include <stdplus/signal.hpp> |
| 5 | |
| 6 | namespace stdplus |
| 7 | { |
| 8 | namespace signal |
| 9 | { |
| 10 | namespace |
| 11 | { |
| 12 | |
William A. Kennington III | 770c556 | 2020-02-13 13:07:42 -0800 | [diff] [blame^] | 13 | TEST_CASE("Signals are blocked", "[signal]") |
William A. Kennington III | 9c97d2c | 2018-11-02 17:25:36 -0700 | [diff] [blame] | 14 | { |
| 15 | constexpr int s = SIGINT; |
| 16 | constexpr int otherS = SIGTERM; |
| 17 | constexpr int notBlocked = SIGPROF; |
| 18 | |
| 19 | sigset_t expectedSet; |
William A. Kennington III | 770c556 | 2020-02-13 13:07:42 -0800 | [diff] [blame^] | 20 | 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 III | 9c97d2c | 2018-11-02 17:25:36 -0700 | [diff] [blame] | 26 | |
| 27 | block(s); |
| 28 | |
| 29 | sigset_t newSet; |
William A. Kennington III | 770c556 | 2020-02-13 13:07:42 -0800 | [diff] [blame^] | 30 | 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 III | 9c97d2c | 2018-11-02 17:25:36 -0700 | [diff] [blame] | 35 | } |
| 36 | |
William A. Kennington III | 770c556 | 2020-02-13 13:07:42 -0800 | [diff] [blame^] | 37 | TEST_CASE("Signals stay blocked if already blocked", "[signal]") |
William A. Kennington III | 9c97d2c | 2018-11-02 17:25:36 -0700 | [diff] [blame] | 38 | { |
| 39 | constexpr int s = SIGINT; |
| 40 | constexpr int otherS = SIGTERM; |
| 41 | constexpr int notBlocked = SIGPROF; |
| 42 | |
| 43 | sigset_t expectedSet; |
William A. Kennington III | 770c556 | 2020-02-13 13:07:42 -0800 | [diff] [blame^] | 44 | 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 III | 9c97d2c | 2018-11-02 17:25:36 -0700 | [diff] [blame] | 49 | |
| 50 | block(s); |
| 51 | |
| 52 | sigset_t newSet; |
William A. Kennington III | 770c556 | 2020-02-13 13:07:42 -0800 | [diff] [blame^] | 53 | 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 III | 9c97d2c | 2018-11-02 17:25:36 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | } // namespace |
| 61 | } // namespace signal |
| 62 | } // namespace stdplus |