blob: b100372c9c33a1d9d91d4ac3f82a2b58c4582bbf [file] [log] [blame]
Elliott Hughesda73f652012-11-30 16:40:55 -08001/*
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
Elliott Hughesfb5e5cb2013-01-07 13:58:49 -080051#if defined(__BIONIC__) && !defined(__mips__)
52 // bionic's sigset_t is too small for ARM and x86: 32 bits instead of 64.
Elliott Hughesda73f652012-11-30 16:40:55 -080053 // This means you can't refer to any of the real-time signals.
54 // See http://b/3038348 and http://b/5828899.
Elliott Hughesfb5e5cb2013-01-07 13:58:49 -080055 max_signal = 32;
Elliott Hughesda73f652012-11-30 16:40:55 -080056#else
Elliott Hughesfb5e5cb2013-01-07 13:58:49 -080057 // Other C libraries (or bionic for MIPS) are perfectly capable of using their largest signal.
Elliott Hughesda73f652012-11-30 16:40:55 -080058 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}
Chris Dearmand8a5a6f2012-12-07 18:41:10 -080098
99TEST(signal, raise_invalid) {
100 errno = 0;
101 ASSERT_EQ(-1, raise(-1));
102 ASSERT_EQ(EINVAL, errno);
103}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800104
105static void HandleSIGALRM(int signal_number) {
106 ASSERT_EQ(SIGALRM, signal_number);
107}
108
109TEST(signal, sigwait) {
110 struct sigaction action;
111 sigemptyset(&action.sa_mask);
112 action.sa_flags = 0;
113 action.sa_handler = HandleSIGALRM;
114 sigaction(SIGALRM, &action, NULL);
115
116 sigset_t wait_set;
117 sigemptyset(&wait_set);
118 sigaddset(&wait_set, SIGALRM);
119
120 alarm(1);
121
122 int received_signal;
123 errno = 0;
124 ASSERT_EQ(0, sigwait(&wait_set, &received_signal));
125 ASSERT_EQ(0, errno);
126 ASSERT_EQ(SIGALRM, received_signal);
127}