xref: /freebsd/lib/libc/tests/gen/sigsetops_test.c (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 Kyle Evans <kevans@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <errno.h>
33 #include <signal.h>
34 #include <stdbool.h>
35 
36 #include <atf-c.h>
37 
38 /* Return the status of the specified sig's bit. */
39 static bool
40 sigbitstatus(const sigset_t *set, int sig)
41 {
42 
43 	return (set->__bits[_SIG_WORD(sig)] & _SIG_BIT(sig)) != 0;
44 }
45 
46 /* Verify that sig is the lone bit set in the sigset. */
47 static void
48 siglonebit(const sigset_t *set, int sig)
49 {
50 	int i;
51 
52 	for (i = 0; i < _SIG_WORDS; ++i) {
53 		if (i != _SIG_WORD(sig))
54 			ATF_REQUIRE_MSG(set->__bits[i] == 0,
55 			    "word %d altered to %x", i, set->__bits[i]);
56 		else
57 			ATF_REQUIRE_MSG((set->__bits[i] & ~_SIG_BIT(sig)) == 0,
58 			    "word %d has other bits set: %x", i,
59 			    set->__bits[i] & ~_SIG_BIT(sig));
60 	}
61 }
62 
63 static void
64 sigcompare(const sigset_t *left, const sigset_t *right)
65 {
66 	int i;
67 
68 	for (i = 0; i < _SIG_WORDS; ++i) {
69 		ATF_REQUIRE_MSG(left->__bits[i] == right->__bits[i],
70 		    "sig comparison failed at %d; left=%x, right=%x",
71 		    i, left->__bits[i], right->__bits[i]);
72 	}
73 }
74 
75 /*
76  * Test implementation details of our sigsetops... make sure the correct bits
77  * are getting set, for the most part, and that duplicate operations don't
78  * error out.
79  */
80 ATF_TC_WITHOUT_HEAD(posix_sigsetop_test);
81 ATF_TC_BODY(posix_sigsetop_test, tc)
82 {
83 	sigset_t set;
84 	int i;
85 
86 	ATF_REQUIRE(sigfillset(&set) == 0);
87 	for (i = 0; i < _SIG_WORDS; ++i) {
88 		ATF_REQUIRE_MSG(set.__bits[i] == ~0U, "sigfillset failed @ %d",
89 		    i);
90 	}
91 	ATF_REQUIRE(sigemptyset(&set) == 0);
92 	for (i = 0; i < _SIG_WORDS; ++i) {
93 		ATF_REQUIRE_MSG(set.__bits[i] == 0, "sigemptyset failed @ %d",
94 		    i);
95 	}
96 	/* Ensure that sigismember reflects the empty set status. */
97 	for (i = 1; i < NSIG; ++i) {
98 		ATF_REQUIRE(sigismember(&set, i) == 0);
99 	}
100 
101 	ATF_REQUIRE(sigaddset(&set, -1) == -1 && errno == EINVAL);
102 	ATF_REQUIRE(sigaddset(&set, _SIG_MAXSIG + 1) == -1 && errno == EINVAL);
103 	ATF_REQUIRE(sigdelset(&set, -1) == -1 && errno == EINVAL);
104 	ATF_REQUIRE(sigdelset(&set, _SIG_MAXSIG + 1) == -1 && errno == EINVAL);
105 
106 	ATF_REQUIRE(sigaddset(&set, SIGSEGV) == 0);
107 	ATF_REQUIRE(sigismember(&set, SIGSEGV) != 0);
108 	ATF_REQUIRE_MSG(sigbitstatus(&set, SIGSEGV), "sigaddset failure");
109 	siglonebit(&set, SIGSEGV);
110 
111 	/*
112 	 * A second addition should succeed without altering the state.  This
113 	 * should be trivially true.
114 	 */
115 	ATF_REQUIRE(sigaddset(&set, SIGSEGV) == 0);
116 	ATF_REQUIRE_MSG(sigbitstatus(&set, SIGSEGV),
117 	    "sigaddset twice changed bit");
118 
119 	ATF_REQUIRE(sigdelset(&set, SIGSEGV) == 0);
120 	ATF_REQUIRE_MSG(!sigbitstatus(&set, SIGSEGV), "sigdelset failure");
121 	ATF_REQUIRE(sigismember(&set, SIGSEGV) == 0);
122 	ATF_REQUIRE(sigdelset(&set, SIGSEGV) == 0);
123 	ATF_REQUIRE_MSG(!sigbitstatus(&set, SIGSEGV),
124 	    "sigdelset twice changed bit");
125 	for (i = 0; i < _SIG_WORDS; ++i) {
126 		ATF_REQUIRE_MSG(set.__bits[i] == 0, "set not empty @ %d",
127 		    i);
128 	}
129 	for (i = 1; i < NSIG; ++i) {
130 		ATF_REQUIRE(sigismember(&set, i) == 0);
131 	}
132 }
133 
134 /*
135  * Test extended sigset ops for union/intersection and testing of empty set.
136  */
137 ATF_TC_WITHOUT_HEAD(extended_sigsetop_test);
138 ATF_TC_BODY(extended_sigsetop_test, tc)
139 {
140 	sigset_t chkset, set1, set2, set3;
141 
142 	sigemptyset(&chkset);
143 	sigemptyset(&set1);
144 	sigemptyset(&set2);
145 	ATF_REQUIRE(sigisemptyset(&chkset) != 0);
146 	sigaddset(&set1, SIGSEGV);
147 	sigaddset(&set2, SIGKILL);
148 	sigaddset(&chkset, SIGSEGV);
149 	ATF_REQUIRE(sigisemptyset(&chkset) == 0);
150 	sigaddset(&chkset, SIGKILL);
151 	ATF_REQUIRE(sigorset(&set3, &set1, &set2) == 0);
152 	ATF_REQUIRE(sigbitstatus(&set3, SIGSEGV));
153 	ATF_REQUIRE(sigbitstatus(&set3, SIGKILL));
154 
155 	/*
156 	 * chkset was built with our POSIX-specified set operations that we've
157 	 * already tested, so it's a good comparison.
158 	 */
159 	sigcompare(&chkset, &set3);
160 	/*
161 	 * Clear chkset; make sure sigisemptyset() still looks ok.  sigaddset
162 	 * and sigdelset have already been tested to make sure that they're not
163 	 * touching other bits.
164 	 */
165 	sigdelset(&chkset, SIGSEGV);
166 	sigdelset(&chkset, SIGKILL);
167 	ATF_REQUIRE(sigisemptyset(&chkset) != 0);
168 	ATF_REQUIRE(sigandset(&set3, &set1, &set2) == 0);
169 	/* Make sure we clobbered these. */
170 	ATF_REQUIRE(!sigbitstatus(&set3, SIGSEGV));
171 	ATF_REQUIRE(!sigbitstatus(&set3, SIGKILL));
172 	ATF_REQUIRE(sigisemptyset(&set3) != 0);
173 	/* Rebuild for sigandset test */
174 	sigemptyset(&set1);
175 	sigemptyset(&set2);
176 	sigaddset(&set1, SIGSEGV);
177 	sigaddset(&set1, SIGKILL);
178 	sigaddset(&set2, SIGSEGV);
179 	ATF_REQUIRE(sigandset(&set3, &set1, &set2) == 0);
180 	ATF_REQUIRE(sigbitstatus(&set3, SIGSEGV));
181 	ATF_REQUIRE(!sigbitstatus(&set3, SIGKILL));
182 }
183 
184 ATF_TP_ADD_TCS(tp)
185 {
186 
187 	ATF_TP_ADD_TC(tp, posix_sigsetop_test);
188 	ATF_TP_ADD_TC(tp, extended_sigsetop_test);
189 
190 	return (atf_no_error());
191 }
192