1 //===-- Unittests for sigprocmask -----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "include/errno.h"
10 #include "include/signal.h"
11 #include "src/errno/llvmlibc_errno.h"
12 #include "src/signal/raise.h"
13 #include "src/signal/sigaddset.h"
14 #include "src/signal/sigemptyset.h"
15 #include "src/signal/sigprocmask.h"
16
17 #include "test/ErrnoSetterMatcher.h"
18 #include "utils/UnitTest/Test.h"
19
20 class SignalTest : public __llvm_libc::testing::Test {
21 sigset_t oldSet;
22
23 public:
SetUp()24 void SetUp() override { __llvm_libc::sigprocmask(0, nullptr, &oldSet); }
25
TearDown()26 void TearDown() override {
27 __llvm_libc::sigprocmask(SIG_SETMASK, &oldSet, nullptr);
28 }
29 };
30
31 using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
32 using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
33
34 // This tests for invalid input.
TEST_F(SignalTest,SigprocmaskInvalid)35 TEST_F(SignalTest, SigprocmaskInvalid) {
36 llvmlibc_errno = 0;
37
38 sigset_t valid;
39 // 17 and -4 are out of the range for sigprocmask's how paramater.
40 EXPECT_THAT(__llvm_libc::sigprocmask(17, &valid, nullptr), Fails(EINVAL));
41 EXPECT_THAT(__llvm_libc::sigprocmask(-4, &valid, nullptr), Fails(EINVAL));
42
43 // This pointer is out of this processes address range.
44 sigset_t *invalid = reinterpret_cast<sigset_t *>(-1);
45 EXPECT_THAT(__llvm_libc::sigprocmask(SIG_SETMASK, invalid, nullptr),
46 Fails(EFAULT));
47 EXPECT_THAT(__llvm_libc::sigprocmask(-4, nullptr, invalid), Fails(EFAULT));
48 }
49
50 // This tests that when nothing is blocked, a process gets killed and alse tests
51 // that when signals are blocked they are not delivered to the process.
TEST_F(SignalTest,BlockUnblock)52 TEST_F(SignalTest, BlockUnblock) {
53 sigset_t sigset;
54 EXPECT_EQ(__llvm_libc::sigemptyset(&sigset), 0);
55 EXPECT_EQ(__llvm_libc::sigprocmask(SIG_SETMASK, &sigset, nullptr), 0);
56 EXPECT_DEATH([] { __llvm_libc::raise(SIGUSR1); }, SIGUSR1);
57 EXPECT_EQ(__llvm_libc::sigaddset(&sigset, SIGUSR1), 0);
58 EXPECT_EQ(__llvm_libc::sigprocmask(SIG_SETMASK, &sigset, nullptr), 0);
59 EXPECT_EXITS([] { __llvm_libc::raise(SIGUSR1); }, 0);
60 }
61