summaryrefslogtreecommitdiffstats
path: root/src/test/test-signal-util.c
blob: 335066a374110dd1febb100acc9722b7e0f12b86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <unistd.h>

#include "log.h"
#include "macro.h"
#include "signal-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "tests.h"
#include "process-util.h"

#define info(sig) log_info(#sig " = " STRINGIFY(sig) " = %d", sig)

TEST(rt_signals) {
        info(SIGRTMIN);
        info(SIGRTMAX);

        /* We use signals SIGRTMIN+0 to SIGRTMIN+24 unconditionally */
        assert_se(SIGRTMAX - SIGRTMIN >= 24);
}

static void test_signal_to_string_one(int val) {
        const char *p;

        assert_se(p = signal_to_string(val));

        assert_se(signal_from_string(p) == val);

        p = strjoina("SIG", p);
        assert_se(signal_from_string(p) == val);
}

static void test_signal_from_string_one(const char *s, int val) {
        const char *p;

        assert_se(signal_from_string(s) == val);

        p = strjoina("SIG", s);
        assert_se(signal_from_string(p) == val);
}

static void test_signal_from_string_number(const char *s, int val) {
        const char *p;

        assert_se(signal_from_string(s) == val);

        p = strjoina("SIG", s);
        assert_se(signal_from_string(p) == -EINVAL);
}

TEST(signal_from_string) {
        char buf[STRLEN("RTMIN+") + DECIMAL_STR_MAX(int) + 1];

        test_signal_to_string_one(SIGHUP);
        test_signal_to_string_one(SIGTERM);
        test_signal_to_string_one(SIGRTMIN);
        test_signal_to_string_one(SIGRTMIN+3);
        test_signal_to_string_one(SIGRTMAX-4);

        test_signal_from_string_one("RTMIN", SIGRTMIN);
        test_signal_from_string_one("RTMAX", SIGRTMAX);

        xsprintf(buf, "RTMIN+%d", SIGRTMAX-SIGRTMIN);
        test_signal_from_string_one(buf, SIGRTMAX);

        xsprintf(buf, "RTMIN+%d", INT_MAX);
        test_signal_from_string_one(buf, -ERANGE);

        xsprintf(buf, "RTMAX-%d", SIGRTMAX-SIGRTMIN);
        test_signal_from_string_one(buf, SIGRTMIN);

        xsprintf(buf, "RTMAX-%d", INT_MAX);
        test_signal_from_string_one(buf, -ERANGE);

        test_signal_from_string_one("", -EINVAL);
        test_signal_from_string_one("hup", -EINVAL);
        test_signal_from_string_one("HOGEHOGE", -EINVAL);

        test_signal_from_string_one("RTMIN-5", -EINVAL);
        test_signal_from_string_one("RTMIN-    5", -EINVAL);
        test_signal_from_string_one("RTMIN    -5", -EINVAL);
        test_signal_from_string_one("RTMIN+    5", -EINVAL);
        test_signal_from_string_one("RTMIN    +5", -EINVAL);
        test_signal_from_string_one("RTMIN+100", -ERANGE);
        test_signal_from_string_one("RTMIN+-3", -EINVAL);
        test_signal_from_string_one("RTMIN++3", -EINVAL);
        test_signal_from_string_one("RTMIN+HUP", -EINVAL);
        test_signal_from_string_one("RTMIN3", -EINVAL);

        test_signal_from_string_one("RTMAX+5", -EINVAL);
        test_signal_from_string_one("RTMAX+    5", -EINVAL);
        test_signal_from_string_one("RTMAX    +5", -EINVAL);
        test_signal_from_string_one("RTMAX-    5", -EINVAL);
        test_signal_from_string_one("RTMAX    -5", -EINVAL);
        test_signal_from_string_one("RTMAX-100", -ERANGE);
        test_signal_from_string_one("RTMAX-+3", -EINVAL);
        test_signal_from_string_one("RTMAX--3", -EINVAL);
        test_signal_from_string_one("RTMAX-HUP", -EINVAL);

        test_signal_from_string_number("3", 3);
        test_signal_from_string_number("+5", 5);
        test_signal_from_string_number("  +5", 5);
        test_signal_from_string_number("10000", -ERANGE);
        test_signal_from_string_number("-2", -ERANGE);
}

TEST(block_signals) {
        assert_se(signal_is_blocked(SIGUSR1) == 0);
        assert_se(signal_is_blocked(SIGALRM) == 0);
        assert_se(signal_is_blocked(SIGVTALRM) == 0);

        {
                BLOCK_SIGNALS(SIGUSR1, SIGVTALRM);

                assert_se(signal_is_blocked(SIGUSR1) > 0);
                assert_se(signal_is_blocked(SIGALRM) == 0);
                assert_se(signal_is_blocked(SIGVTALRM) > 0);
        }

        assert_se(signal_is_blocked(SIGUSR1) == 0);
        assert_se(signal_is_blocked(SIGALRM) == 0);
        assert_se(signal_is_blocked(SIGVTALRM) == 0);
}

TEST(ignore_signals) {
        assert_se(ignore_signals(SIGINT) >= 0);
        assert_se(kill(getpid_cached(), SIGINT) >= 0);
        assert_se(ignore_signals(SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE) >= 0);
        assert_se(kill(getpid_cached(), SIGUSR1) >= 0);
        assert_se(kill(getpid_cached(), SIGUSR2) >= 0);
        assert_se(kill(getpid_cached(), SIGTERM) >= 0);
        assert_se(kill(getpid_cached(), SIGPIPE) >= 0);
        assert_se(default_signals(SIGINT, SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE) >= 0);
}

TEST(pop_pending_signal) {

        assert_se(signal_is_blocked(SIGUSR1) == 0);
        assert_se(signal_is_blocked(SIGUSR2) == 0);
        assert_se(pop_pending_signal(SIGUSR1) == 0);
        assert_se(pop_pending_signal(SIGUSR2) == 0);

        {
                BLOCK_SIGNALS(SIGUSR1, SIGUSR2);

                assert_se(signal_is_blocked(SIGUSR1) > 0);
                assert_se(signal_is_blocked(SIGUSR2) > 0);

                assert_se(pop_pending_signal(SIGUSR1) == 0);
                assert_se(pop_pending_signal(SIGUSR2) == 0);

                assert_se(raise(SIGUSR1) >= 0);

                assert_se(pop_pending_signal(SIGUSR2) == 0);
                assert_se(pop_pending_signal(SIGUSR1) == SIGUSR1);
                assert_se(pop_pending_signal(SIGUSR1) == 0);

                assert_se(raise(SIGUSR1) >= 0);
                assert_se(raise(SIGUSR2) >= 0);

                assert_cc(SIGUSR1 < SIGUSR2);

                assert_se(pop_pending_signal(SIGUSR1, SIGUSR2) == SIGUSR1);
                assert_se(pop_pending_signal(SIGUSR1, SIGUSR2) == SIGUSR2);
                assert_se(pop_pending_signal(SIGUSR1, SIGUSR2) == 0);
        }

        assert_se(signal_is_blocked(SIGUSR1) == 0);
        assert_se(signal_is_blocked(SIGUSR2) == 0);
        assert_se(pop_pending_signal(SIGUSR1) == 0);
        assert_se(pop_pending_signal(SIGUSR2) == 0);
}

DEFINE_TEST_MAIN(LOG_INFO);