blob: 3ca2695f86da61896ae78522701c86a0c4fe9c7c [file] [log] [blame]
William A. Kennington III9c97d2c2018-11-02 17:25:36 -07001#include <signal.h>
Patrick Williamsd1984dd2023-05-10 16:12:44 -05002
William A. Kennington III9c97d2c2018-11-02 17:25:36 -07003#include <stdplus/signal.hpp>
4
Patrick Williamsd1984dd2023-05-10 16:12:44 -05005#include <cstring>
6
7#include <gtest/gtest.h>
8
William A. Kennington III9c97d2c2018-11-02 17:25:36 -07009namespace stdplus
10{
11namespace signal
12{
13namespace
14{
15
William A. Kennington III0bd13702022-08-24 13:33:34 -070016TEST(Signal, Block)
William A. Kennington III9c97d2c2018-11-02 17:25:36 -070017{
18 constexpr int s = SIGINT;
19 constexpr int otherS = SIGTERM;
20 constexpr int notBlocked = SIGPROF;
21
22 sigset_t expectedSet;
William A. Kennington III0bd13702022-08-24 13:33:34 -070023 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &expectedSet));
24 ASSERT_EQ(0, sigaddset(&expectedSet, otherS));
25 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &expectedSet, nullptr));
26 ASSERT_EQ(0, sigismember(&expectedSet, notBlocked));
27 ASSERT_EQ(0, sigismember(&expectedSet, s));
28 ASSERT_EQ(0, sigaddset(&expectedSet, s));
William A. Kennington III9c97d2c2018-11-02 17:25:36 -070029
30 block(s);
31
32 sigset_t newSet;
William A. Kennington III0bd13702022-08-24 13:33:34 -070033 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &newSet));
34 EXPECT_EQ(sigismember(&expectedSet, s), sigismember(&newSet, s));
35 EXPECT_EQ(sigismember(&expectedSet, otherS), sigismember(&newSet, otherS));
36 EXPECT_EQ(sigismember(&expectedSet, notBlocked),
37 sigismember(&newSet, notBlocked));
William A. Kennington III9c97d2c2018-11-02 17:25:36 -070038}
39
William A. Kennington III0bd13702022-08-24 13:33:34 -070040TEST(Signal, StayBlocked)
William A. Kennington III9c97d2c2018-11-02 17:25:36 -070041{
42 constexpr int s = SIGINT;
43 constexpr int otherS = SIGTERM;
44 constexpr int notBlocked = SIGPROF;
45
46 sigset_t expectedSet;
William A. Kennington III0bd13702022-08-24 13:33:34 -070047 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &expectedSet));
48 ASSERT_EQ(0, sigaddset(&expectedSet, s));
49 ASSERT_EQ(0, sigaddset(&expectedSet, otherS));
50 ASSERT_EQ(0, sigismember(&expectedSet, notBlocked));
51 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &expectedSet, nullptr));
William A. Kennington III9c97d2c2018-11-02 17:25:36 -070052
53 block(s);
54
55 sigset_t newSet;
William A. Kennington III0bd13702022-08-24 13:33:34 -070056 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &newSet));
57 EXPECT_EQ(sigismember(&expectedSet, s), sigismember(&newSet, s));
58 EXPECT_EQ(sigismember(&expectedSet, otherS), sigismember(&newSet, otherS));
59 EXPECT_EQ(sigismember(&expectedSet, notBlocked),
60 sigismember(&newSet, notBlocked));
William A. Kennington III9c97d2c2018-11-02 17:25:36 -070061}
62
63} // namespace
64} // namespace signal
65} // namespace stdplus