signal_test.cpp revision fb5e5cbdd4e1d75594c37ebb544c0f46482a027b
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <errno.h>
20#include <signal.h>
21
22template <typename Fn>
23static void TestSigSet1(Fn fn) {
24  // NULL sigset_t*.
25  sigset_t* set_ptr = NULL;
26  errno = 0;
27  ASSERT_EQ(-1, fn(set_ptr));
28  ASSERT_EQ(EINVAL, errno);
29
30  // Non-NULL.
31  sigset_t set;
32  errno = 0;
33  ASSERT_EQ(0, fn(&set));
34  ASSERT_EQ(0, errno);
35}
36
37template <typename Fn>
38static void TestSigSet2(Fn fn) {
39  // NULL sigset_t*.
40  sigset_t* set_ptr = NULL;
41  errno = 0;
42  ASSERT_EQ(-1, fn(set_ptr, SIGSEGV));
43  ASSERT_EQ(EINVAL, errno);
44
45  sigset_t set;
46  sigemptyset(&set);
47
48  int min_signal = SIGHUP;
49  int max_signal = SIGRTMAX;
50
51#if defined(__BIONIC__) && !defined(__mips__)
52  // bionic's sigset_t is too small for ARM and x86: 32 bits instead of 64.
53  // This means you can't refer to any of the real-time signals.
54  // See http://b/3038348 and http://b/5828899.
55  max_signal = 32;
56#else
57  // Other C libraries (or bionic for MIPS) are perfectly capable of using their largest signal.
58  ASSERT_GE(sizeof(sigset_t) * 8, static_cast<size_t>(SIGRTMAX));
59#endif
60
61  // Bad signal number: too small.
62  errno = 0;
63  ASSERT_EQ(-1, fn(&set, 0));
64  ASSERT_EQ(EINVAL, errno);
65
66  // Bad signal number: too high.
67  errno = 0;
68  ASSERT_EQ(-1, fn(&set, max_signal + 1));
69  ASSERT_EQ(EINVAL, errno);
70
71  // Good signal numbers, low and high ends of range.
72  errno = 0;
73  ASSERT_EQ(0, fn(&set, min_signal));
74  ASSERT_EQ(0, errno);
75  ASSERT_EQ(0, fn(&set, max_signal));
76  ASSERT_EQ(0, errno);
77}
78
79TEST(signal, sigismember_invalid) {
80  TestSigSet2(sigismember);
81}
82
83TEST(signal, sigaddset_invalid) {
84  TestSigSet2(sigaddset);
85}
86
87TEST(signal, sigdelset_invalid) {
88  TestSigSet2(sigdelset);
89}
90
91TEST(signal, sigemptyset_invalid) {
92  TestSigSet1(sigemptyset);
93}
94
95TEST(signal, sigfillset_invalid) {
96  TestSigSet1(sigfillset);
97}
98
99TEST(signal, raise_invalid) {
100  errno = 0;
101  ASSERT_EQ(-1, raise(-1));
102  ASSERT_EQ(EINVAL, errno);
103}
104