1 //===-- Unittests for sigprocmask -----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/errno/libc_errno.h"
10 #include "src/signal/raise.h"
11 #include "src/signal/sigaddset.h"
12 #include "src/signal/sigemptyset.h"
13 #include "src/signal/sigprocmask.h"
15 #include "test/UnitTest/ErrnoSetterMatcher.h"
16 #include "test/UnitTest/Test.h"
20 class LlvmLibcSignalTest
: public LIBC_NAMESPACE::testing::Test
{
24 void SetUp() override
{ LIBC_NAMESPACE::sigprocmask(0, nullptr, &oldSet
); }
26 void TearDown() override
{
27 LIBC_NAMESPACE::sigprocmask(SIG_SETMASK
, &oldSet
, nullptr);
31 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
32 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
34 // This tests for invalid input.
35 TEST_F(LlvmLibcSignalTest
, SigprocmaskInvalid
) {
36 LIBC_NAMESPACE::libc_errno
= 0;
39 // 17 and -4 are out of the range for sigprocmask's how paramater.
40 EXPECT_THAT(LIBC_NAMESPACE::sigprocmask(17, &valid
, nullptr), Fails(EINVAL
));
41 EXPECT_THAT(LIBC_NAMESPACE::sigprocmask(-4, &valid
, nullptr), Fails(EINVAL
));
43 // This pointer is out of this processes address range.
44 sigset_t
*invalid
= reinterpret_cast<sigset_t
*>(-1);
45 EXPECT_THAT(LIBC_NAMESPACE::sigprocmask(SIG_SETMASK
, invalid
, nullptr),
47 EXPECT_THAT(LIBC_NAMESPACE::sigprocmask(-4, nullptr, invalid
), Fails(EFAULT
));
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.
52 TEST_F(LlvmLibcSignalTest
, BlockUnblock
) {
54 EXPECT_EQ(LIBC_NAMESPACE::sigemptyset(&sigset
), 0);
55 EXPECT_EQ(LIBC_NAMESPACE::sigprocmask(SIG_SETMASK
, &sigset
, nullptr), 0);
56 EXPECT_DEATH([] { LIBC_NAMESPACE::raise(SIGUSR1
); }, WITH_SIGNAL(SIGUSR1
));
57 EXPECT_EQ(LIBC_NAMESPACE::sigaddset(&sigset
, SIGUSR1
), 0);
58 EXPECT_EQ(LIBC_NAMESPACE::sigprocmask(SIG_SETMASK
, &sigset
, nullptr), 0);
59 EXPECT_EXITS([] { LIBC_NAMESPACE::raise(SIGUSR1
); }, 0);