forked from frc971/971-Robot-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignalfd_test.cc
76 lines (69 loc) · 2.16 KB
/
signalfd_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "aos/ipc_lib/signalfd.h"
#include "aos/testing/test_logging.h"
#include "glog/logging.h"
#include "gtest/gtest.h"
namespace aos {
namespace ipc_lib {
namespace testing {
// Tests in this file use separate threads to isolate all manipulation of signal
// masks between test cases.
// Verify that SignalFd will leave signals unblocked if we ask it to.
TEST(SignalFdTest, LeaveSignalBlocked) {
::aos::testing::EnableTestLogging();
std::thread thread([]() {
{
sigset_t test_mask;
CHECK_EQ(0, sigemptyset(&test_mask));
CHECK_EQ(0, sigaddset(&test_mask, SIGUSR1));
PCHECK(sigprocmask(SIG_BLOCK, &test_mask, nullptr) == 0);
}
SignalFd({SIGUSR1});
{
sigset_t blocked_now;
PCHECK(sigprocmask(SIG_BLOCK, nullptr, &blocked_now) == 0);
ASSERT_TRUE(sigismember(&blocked_now, SIGUSR1));
}
});
thread.join();
}
// Verify that SignalFd actually blocks the requested signals, and unblocks them
// afterwards.
TEST(SignalFdTest, BlockSignal) {
::aos::testing::EnableTestLogging();
std::thread thread([]() {
{
sigset_t blocked_now;
PCHECK(sigprocmask(SIG_BLOCK, nullptr, &blocked_now) == 0);
ASSERT_FALSE(sigismember(&blocked_now, SIGUSR1));
}
{
SignalFd signalfd({SIGUSR1});
sigset_t blocked_now;
PCHECK(sigprocmask(SIG_BLOCK, nullptr, &blocked_now) == 0);
ASSERT_TRUE(sigismember(&blocked_now, SIGUSR1));
}
{
sigset_t blocked_now;
PCHECK(sigprocmask(SIG_BLOCK, nullptr, &blocked_now) == 0);
ASSERT_FALSE(sigismember(&blocked_now, SIGUSR1));
}
});
thread.join();
}
// Verify that SignalFd responds correctly when some other code unblocks one of
// its signals.
TEST(SignalFdDeathTest, ExternalUnblockSignal) {
::aos::testing::EnableTestLogging();
auto t = []() {
SignalFd signalfd({SIGUSR1});
sigset_t test_mask;
CHECK_EQ(0, sigemptyset(&test_mask));
CHECK_EQ(0, sigaddset(&test_mask, SIGUSR1));
PCHECK(sigprocmask(SIG_UNBLOCK, &test_mask, nullptr) == 0);
};
EXPECT_DEATH({ t(); },
"Some other code unblocked one or more of our signals");
}
} // namespace testing
} // namespace ipc_lib
} // namespace aos