chiark / gitweb /
d78d368c0bba5b9525debae297bfee2d18065490
[elogind.git] / src / test / test-signal-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <signal.h>
4 #include <unistd.h>
5
6 //#include "log.h"
7 #include "macro.h"
8 #include "signal-util.h"
9 /// Additional includes needed by elogind
10 //#include "stdio-util.h"
11 //#include "string-util.h"
12 //#include "stdio-util.h"
13 //#include "string-util.h"
14 //#include "stdio-util.h"
15 //#include "string-util.h"
16 //#include "stdio-util.h"
17 //#include "string-util.h"
18 //#include "stdio-util.h"
19 //#include "string-util.h"
20 #include "process-util.h"
21
22 #define info(sig) log_info(#sig " = " STRINGIFY(sig) " = %d", sig)
23
24 static void test_rt_signals(void) {
25         info(SIGRTMIN);
26         info(SIGRTMAX);
27
28         /* We use signals SIGRTMIN+0 to SIGRTMIN+24 unconditionally */
29         assert(SIGRTMAX - SIGRTMIN >= 24);
30 }
31
32 static void test_signal_to_string_one(int val) {
33         const char *p;
34
35         assert_se(p = signal_to_string(val));
36
37         assert_se(signal_from_string(p) == val);
38
39         p = strjoina("SIG", p);
40         assert_se(signal_from_string(p) == val);
41 }
42
43 static void test_signal_from_string_one(const char *s, int val) {
44         const char *p;
45
46         assert_se(signal_from_string(s) == val);
47
48         p = strjoina("SIG", s);
49         assert_se(signal_from_string(p) == val);
50 }
51
52 static void test_signal_from_string_number(const char *s, int val) {
53         const char *p;
54
55         assert_se(signal_from_string(s) == val);
56
57         p = strjoina("SIG", s);
58         assert_se(signal_from_string(p) == -EINVAL);
59 }
60
61 static void test_signal_from_string(void) {
62         char buf[STRLEN("RTMIN+") + DECIMAL_STR_MAX(int) + 1];
63
64         test_signal_to_string_one(SIGHUP);
65         test_signal_to_string_one(SIGTERM);
66         test_signal_to_string_one(SIGRTMIN);
67         test_signal_to_string_one(SIGRTMIN+3);
68         test_signal_to_string_one(SIGRTMAX-4);
69
70         test_signal_from_string_one("RTMIN", SIGRTMIN);
71         test_signal_from_string_one("RTMAX", SIGRTMAX);
72
73         xsprintf(buf, "RTMIN+%d", SIGRTMAX-SIGRTMIN);
74         test_signal_from_string_one(buf, SIGRTMAX);
75
76         xsprintf(buf, "RTMIN+%d", INT_MAX);
77         test_signal_from_string_one(buf, -ERANGE);
78
79         xsprintf(buf, "RTMAX-%d", SIGRTMAX-SIGRTMIN);
80         test_signal_from_string_one(buf, SIGRTMIN);
81
82         xsprintf(buf, "RTMAX-%d", INT_MAX);
83         test_signal_from_string_one(buf, -ERANGE);
84
85         test_signal_from_string_one("", -EINVAL);
86         test_signal_from_string_one("hup", -EINVAL);
87         test_signal_from_string_one("HOGEHOGE", -EINVAL);
88
89         test_signal_from_string_one("RTMIN-5", -EINVAL);
90         test_signal_from_string_one("RTMIN-    5", -EINVAL);
91         test_signal_from_string_one("RTMIN    -5", -EINVAL);
92         test_signal_from_string_one("RTMIN+    5", -EINVAL);
93         test_signal_from_string_one("RTMIN    +5", -EINVAL);
94         test_signal_from_string_one("RTMIN+100", -ERANGE);
95         test_signal_from_string_one("RTMIN+-3", -EINVAL);
96         test_signal_from_string_one("RTMIN++3", -EINVAL);
97         test_signal_from_string_one("RTMIN+HUP", -EINVAL);
98         test_signal_from_string_one("RTMIN3", -EINVAL);
99
100         test_signal_from_string_one("RTMAX+5", -EINVAL);
101         test_signal_from_string_one("RTMAX+    5", -EINVAL);
102         test_signal_from_string_one("RTMAX    +5", -EINVAL);
103         test_signal_from_string_one("RTMAX-    5", -EINVAL);
104         test_signal_from_string_one("RTMAX    -5", -EINVAL);
105         test_signal_from_string_one("RTMAX-100", -ERANGE);
106         test_signal_from_string_one("RTMAX-+3", -EINVAL);
107         test_signal_from_string_one("RTMAX--3", -EINVAL);
108         test_signal_from_string_one("RTMAX-HUP", -EINVAL);
109
110         test_signal_from_string_number("3", 3);
111         test_signal_from_string_number("+5", 5);
112         test_signal_from_string_number("  +5", 5);
113         test_signal_from_string_number("10000", -ERANGE);
114         test_signal_from_string_number("-2", -ERANGE);
115 }
116
117 static void test_block_signals(void) {
118         sigset_t ss;
119
120         assert_se(sigprocmask(0, NULL, &ss) >= 0);
121
122         assert_se(sigismember(&ss, SIGUSR1) == 0);
123         assert_se(sigismember(&ss, SIGALRM) == 0);
124         assert_se(sigismember(&ss, SIGVTALRM) == 0);
125
126         {
127                 BLOCK_SIGNALS(SIGUSR1, SIGVTALRM);
128
129                 assert_se(sigprocmask(0, NULL, &ss) >= 0);
130                 assert_se(sigismember(&ss, SIGUSR1) == 1);
131                 assert_se(sigismember(&ss, SIGALRM) == 0);
132                 assert_se(sigismember(&ss, SIGVTALRM) == 1);
133
134         }
135
136         assert_se(sigprocmask(0, NULL, &ss) >= 0);
137         assert_se(sigismember(&ss, SIGUSR1) == 0);
138         assert_se(sigismember(&ss, SIGALRM) == 0);
139         assert_se(sigismember(&ss, SIGVTALRM) == 0);
140 }
141
142 static void test_ignore_signals(void) {
143         assert_se(ignore_signals(SIGINT, -1) >= 0);
144         assert_se(kill(getpid_cached(), SIGINT) >= 0);
145         assert_se(ignore_signals(SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0);
146         assert_se(kill(getpid_cached(), SIGUSR1) >= 0);
147         assert_se(kill(getpid_cached(), SIGUSR2) >= 0);
148         assert_se(kill(getpid_cached(), SIGTERM) >= 0);
149         assert_se(kill(getpid_cached(), SIGPIPE) >= 0);
150         assert_se(default_signals(SIGINT, SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0);
151 }
152
153 int main(int argc, char *argv[]) {
154         test_rt_signals();
155         test_signal_from_string();
156         test_block_signals();
157         test_ignore_signals();
158
159         return 0;
160 }