xref: /freebsd/lib/libc/tests/gen/sigsetops_test.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #include <errno.h>
31 #include <signal.h>
32 #include <stdbool.h>
33 
34 #include <atf-c.h>
35 
36 /* Return the status of the specified sig's bit. */
37 static bool
38 sigbitstatus(const sigset_t *set, int sig)
39 {
40 
41 	return (set->__bits[_SIG_WORD(sig)] & _SIG_BIT(sig)) != 0;
42 }
43 
44 /* Verify that sig is the lone bit set in the sigset. */
45 static void
46 siglonebit(const sigset_t *set, int sig)
47 {
48 	int i;
49 
50 	for (i = 0; i < _SIG_WORDS; ++i) {
51 		if (i != _SIG_WORD(sig))
52 			ATF_REQUIRE_MSG(set->__bits[i] == 0,
53 			    "word %d altered to %x", i, set->__bits[i]);
54 		else
55 			ATF_REQUIRE_MSG((set->__bits[i] & ~_SIG_BIT(sig)) == 0,
56 			    "word %d has other bits set: %x", i,
57 			    set->__bits[i] & ~_SIG_BIT(sig));
58 	}
59 }
60 
61 static void
62 sigcompare(const sigset_t *left, const sigset_t *right)
63 {
64 	int i;
65 
66 	for (i = 0; i < _SIG_WORDS; ++i) {
67 		ATF_REQUIRE_MSG(left->__bits[i] == right->__bits[i],
68 		    "sig comparison failed at %d; left=%x, right=%x",
69 		    i, left->__bits[i], right->__bits[i]);
70 	}
71 }
72 
73 /*
74  * Test implementation details of our sigsetops... make sure the correct bits
75  * are getting set, for the most part, and that duplicate operations don't
76  * error out.
77  */
78 ATF_TC_WITHOUT_HEAD(posix_sigsetop_test);
79 ATF_TC_BODY(posix_sigsetop_test, tc)
80 {
81 	sigset_t set;
82 	int i;
83 
84 	ATF_REQUIRE(sigfillset(&set) == 0);
85 	for (i = 0; i < _SIG_WORDS; ++i) {
86 		ATF_REQUIRE_MSG(set.__bits[i] == ~0U, "sigfillset failed @ %d",
87 		    i);
88 	}
89 	ATF_REQUIRE(sigemptyset(&set) == 0);
90 	for (i = 0; i < _SIG_WORDS; ++i) {
91 		ATF_REQUIRE_MSG(set.__bits[i] == 0, "sigemptyset failed @ %d",
92 		    i);
93 	}
94 	/* Ensure that sigismember reflects the empty set status. */
95 	for (i = 1; i < NSIG; ++i) {
96 		ATF_REQUIRE(sigismember(&set, i) == 0);
97 	}
98 
99 	ATF_REQUIRE(sigaddset(&set, -1) == -1 && errno == EINVAL);
100 	ATF_REQUIRE(sigaddset(&set, _SIG_MAXSIG + 1) == -1 && errno == EINVAL);
101 	ATF_REQUIRE(sigdelset(&set, -1) == -1 && errno == EINVAL);
102 	ATF_REQUIRE(sigdelset(&set, _SIG_MAXSIG + 1) == -1 && errno == EINVAL);
103 
104 	ATF_REQUIRE(sigaddset(&set, SIGSEGV) == 0);
105 	ATF_REQUIRE(sigismember(&set, SIGSEGV) != 0);
106 	ATF_REQUIRE_MSG(sigbitstatus(&set, SIGSEGV), "sigaddset failure");
107 	siglonebit(&set, SIGSEGV);
108 
109 	/*
110 	 * A second addition should succeed without altering the state.  This
111 	 * should be trivially true.
112 	 */
113 	ATF_REQUIRE(sigaddset(&set, SIGSEGV) == 0);
114 	ATF_REQUIRE_MSG(sigbitstatus(&set, SIGSEGV),
115 	    "sigaddset twice changed bit");
116 
117 	ATF_REQUIRE(sigdelset(&set, SIGSEGV) == 0);
118 	ATF_REQUIRE_MSG(!sigbitstatus(&set, SIGSEGV), "sigdelset failure");
119 	ATF_REQUIRE(sigismember(&set, SIGSEGV) == 0);
120 	ATF_REQUIRE(sigdelset(&set, SIGSEGV) == 0);
121 	ATF_REQUIRE_MSG(!sigbitstatus(&set, SIGSEGV),
122 	    "sigdelset twice changed bit");
123 	for (i = 0; i < _SIG_WORDS; ++i) {
124 		ATF_REQUIRE_MSG(set.__bits[i] == 0, "set not empty @ %d",
125 		    i);
126 	}
127 	for (i = 1; i < NSIG; ++i) {
128 		ATF_REQUIRE(sigismember(&set, i) == 0);
129 	}
130 }
131 
132 /*
133  * Test extended sigset ops for union/intersection and testing of empty set.
134  */
135 ATF_TC_WITHOUT_HEAD(extended_sigsetop_test);
136 ATF_TC_BODY(extended_sigsetop_test, tc)
137 {
138 	sigset_t chkset, set1, set2, set3;
139 
140 	sigemptyset(&chkset);
141 	sigemptyset(&set1);
142 	sigemptyset(&set2);
143 	ATF_REQUIRE(sigisemptyset(&chkset) != 0);
144 	sigaddset(&set1, SIGSEGV);
145 	sigaddset(&set2, SIGKILL);
146 	sigaddset(&chkset, SIGSEGV);
147 	ATF_REQUIRE(sigisemptyset(&chkset) == 0);
148 	sigaddset(&chkset, SIGKILL);
149 	ATF_REQUIRE(sigorset(&set3, &set1, &set2) == 0);
150 	ATF_REQUIRE(sigbitstatus(&set3, SIGSEGV));
151 	ATF_REQUIRE(sigbitstatus(&set3, SIGKILL));
152 
153 	/*
154 	 * chkset was built with our POSIX-specified set operations that we've
155 	 * already tested, so it's a good comparison.
156 	 */
157 	sigcompare(&chkset, &set3);
158 	/*
159 	 * Clear chkset; make sure sigisemptyset() still looks ok.  sigaddset
160 	 * and sigdelset have already been tested to make sure that they're not
161 	 * touching other bits.
162 	 */
163 	sigdelset(&chkset, SIGSEGV);
164 	sigdelset(&chkset, SIGKILL);
165 	ATF_REQUIRE(sigisemptyset(&chkset) != 0);
166 	ATF_REQUIRE(sigandset(&set3, &set1, &set2) == 0);
167 	/* Make sure we clobbered these. */
168 	ATF_REQUIRE(!sigbitstatus(&set3, SIGSEGV));
169 	ATF_REQUIRE(!sigbitstatus(&set3, SIGKILL));
170 	ATF_REQUIRE(sigisemptyset(&set3) != 0);
171 	/* Rebuild for sigandset test */
172 	sigemptyset(&set1);
173 	sigemptyset(&set2);
174 	sigaddset(&set1, SIGSEGV);
175 	sigaddset(&set1, SIGKILL);
176 	sigaddset(&set2, SIGSEGV);
177 	ATF_REQUIRE(sigandset(&set3, &set1, &set2) == 0);
178 	ATF_REQUIRE(sigbitstatus(&set3, SIGSEGV));
179 	ATF_REQUIRE(!sigbitstatus(&set3, SIGKILL));
180 }
181 
182 ATF_TP_ADD_TCS(tp)
183 {
184 
185 	ATF_TP_ADD_TC(tp, posix_sigsetop_test);
186 	ATF_TP_ADD_TC(tp, extended_sigsetop_test);
187 
188 	return (atf_no_error());
189 }
190