comparison tests/test-rule.c @ 944:d63a360811dd

irccd: add rules
author David Demelier <markand@malikania.fr>
date Fri, 15 Jan 2021 10:38:45 +0100
parents
children 533639ec5e9c
comparison
equal deleted inserted replaced
943:aef1568a76bf 944:d63a360811dd
1 /*
2 * test-rule.c -- test rule.h functions
3 *
4 * Copyright (c) 2013-2021 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #define GREATEST_USE_ABBREVS 0
20 #include <greatest.h>
21
22 #include <irccd/rule.h>
23 #include <irccd/irccd.h>
24
25 static void
26 clean(void *udata)
27 {
28 (void)udata;
29
30 memset(&irc, 0, sizeof (irc));
31 }
32
33 GREATEST_TEST
34 basics_insert(void)
35 {
36 struct irc_rule r1 = {0}, r2 = {0};
37
38 irc_rule_add(r1.servers, "s1");
39 irc_rule_add(r2.servers, "s2");
40
41 irc_bot_insert_rule(&r1, 0);
42 irc_bot_insert_rule(&r2, 0);
43
44 GREATEST_ASSERT_EQ(2U, irc.rulesz);
45 GREATEST_ASSERT(memcmp(&irc.rules[0], &r2, sizeof (r2)) == 0);
46 GREATEST_ASSERT(memcmp(&irc.rules[1], &r1, sizeof (r1)) == 0);
47 GREATEST_PASS();
48 }
49
50 GREATEST_TEST
51 basics_remove(void)
52 {
53 struct irc_rule r1 = {0}, r2 = {0}, r3 = {0};
54
55 irc_rule_add(r1.servers, "s1");
56 irc_rule_add(r2.servers, "s2");
57 irc_rule_add(r3.servers, "s3");
58
59 irc_bot_insert_rule(&r1, -1);
60 irc_bot_insert_rule(&r2, -1);
61 irc_bot_insert_rule(&r3, -1);
62 irc_bot_remove_rule(1);
63
64 GREATEST_ASSERT_EQ(2U, irc.rulesz);
65 GREATEST_ASSERT(memcmp(&irc.rules[0], &r1, sizeof (r1)) == 0);
66 GREATEST_ASSERT(memcmp(&irc.rules[1], &r3, sizeof (r3)) == 0);
67
68 irc_bot_remove_rule(1);
69 GREATEST_ASSERT_EQ(1U, irc.rulesz);
70 GREATEST_ASSERT(memcmp(&irc.rules[0], &r1, sizeof (r1)) == 0);
71
72 irc_bot_remove_rule(0);
73 GREATEST_ASSERT_EQ(0U, irc.rulesz);
74
75 GREATEST_PASS();
76 }
77
78 /*
79 * Simulate the following rules configuration:
80 *
81 * #
82 * # On all servers, each channel #staff can't use the onCommand event,
83 * # everything else is allowed.
84 * #
85 * [rule] #1
86 * servers = ""
87 * channels = "#staff"
88 * events = "onCommand"
89 * action = drop
90 *
91 * #
92 * # However, the same onCommand on #staff is allowed on server "unsafe"
93 * #
94 * [rule] #2
95 * servers = "unsafe"
96 * channels = "#staff"
97 * events = "onCommand"
98 * action = accept
99 *
100 * #
101 * # Plugin game is only allowed on server "malikania" and "localhost",
102 * # channel "#games" and events "onMessage, onCommand".
103 * #
104 * # The first rule #3-1 disable the plugin game for every server, it is
105 * # reenabled again with the #3-2.
106 * #
107 * [rule] #3-1
108 * plugins = "game"
109 * action = drop
110 *
111 * [rule] #3-2
112 * servers = "malikania localhost"
113 * channels = "#games"
114 * plugins = "game"
115 * events = "onMessage onCommand"
116 * action = accept
117 */
118
119 static void
120 set(void *udata)
121 {
122 (void)udata;
123
124 struct irc_rule r1 = {0}, r2 = {0}, r31 = {0}, r32 = {0};
125
126 /* Deinit irccd first. */
127 memset(&irc, 0, sizeof (irc));
128
129 /* #1 */
130 r1.action = IRC_RULE_DROP;
131 irc_rule_add(r1.channels, "#staff");
132 irc_rule_add(r1.events, "onCommand");
133 irc_bot_insert_rule(&r1, -1);
134
135 /* #2 */
136 r2.action = IRC_RULE_ACCEPT;
137 irc_rule_add(r2.servers, "unsafe");
138 irc_rule_add(r2.channels, "#staff");
139 irc_rule_add(r2.events, "onCommand");
140 irc_bot_insert_rule(&r2, -1);
141
142 /* #3-1 */
143 r31.action = IRC_RULE_DROP;
144 irc_rule_add(r31.plugins, "game");
145 irc_bot_insert_rule(&r31, -1);
146
147 /* #3-2 */
148 r32.action = IRC_RULE_ACCEPT;
149 irc_rule_add(r32.servers, "malikania");
150 irc_rule_add(r32.servers, "localhost");
151 irc_rule_add(r32.channels, "#games");
152 irc_rule_add(r32.plugins, "game");
153 irc_rule_add(r32.events, "onCommand");
154 irc_rule_add(r32.events, "onMessage");
155 irc_bot_insert_rule(&r32, -1);
156 };
157
158 GREATEST_TEST
159 solve_match1(void)
160 {
161 struct irc_rule m = {0};
162
163 GREATEST_ASSERT(irc_rule_match(&m, "freenode", "#test", "a", "", ""));
164 GREATEST_ASSERT(irc_rule_match(&m, "", "", "", "", ""));
165 GREATEST_PASS();
166 }
167
168 GREATEST_TEST
169 solve_match2(void)
170 {
171 struct irc_rule m = {0};
172
173 irc_rule_add(m.servers, "freenode");
174
175 GREATEST_ASSERT(irc_rule_match(&m, "FreeNode", "#test", "a", "", ""));
176 GREATEST_ASSERT(!irc_rule_match(&m, "malikania", "#test", "a", "", ""));
177 GREATEST_ASSERT(irc_rule_match(&m, "freenode", "", "jean", "", "onMessage"));
178 GREATEST_PASS();
179 }
180
181 GREATEST_TEST
182 solve_match3(void)
183 {
184 struct irc_rule m = {0};
185
186 irc_rule_add(m.servers, "freenode");
187 irc_rule_add(m.channels, "#staff");
188
189 GREATEST_ASSERT(irc_rule_match(&m, "freenode", "#staff", "a", "", ""));
190 GREATEST_ASSERT(!irc_rule_match(&m, "freenode", "#test", "a", "", ""));
191 GREATEST_ASSERT(!irc_rule_match(&m, "malikania", "#staff", "a", "", ""));
192 GREATEST_PASS();
193 }
194
195 GREATEST_TEST
196 solve_match4(void)
197 {
198 struct irc_rule m = {0};
199
200 irc_rule_add(m.servers, "malikania");
201 irc_rule_add(m.channels, "#staff");
202 irc_rule_add(m.origins, "a");
203
204 GREATEST_ASSERT(irc_rule_match(&m, "malikania", "#staff", "a", "",""));
205 GREATEST_ASSERT(!irc_rule_match(&m, "malikania", "#staff", "b", "", ""));
206 GREATEST_ASSERT(!irc_rule_match(&m, "freenode", "#staff", "a", "", ""));
207 GREATEST_PASS();
208 }
209
210 GREATEST_TEST
211 solve_match5(void)
212 {
213 struct irc_rule m = {0};
214
215 irc_rule_add(m.servers, "malikania");
216 irc_rule_add(m.servers, "freenode");
217
218 GREATEST_ASSERT(irc_rule_match(&m, "malikania", "", "", "", ""));
219 GREATEST_ASSERT(irc_rule_match(&m, "freenode", "", "", "", ""));
220 GREATEST_ASSERT(!irc_rule_match(&m, "no", "", "", "", ""));
221 GREATEST_PASS();
222 }
223
224 GREATEST_TEST
225 solve_match6(void)
226 {
227 struct irc_rule m = {0};
228
229 irc_rule_add(m.servers, "malikania");
230 irc_rule_add(m.origins, "markand");
231
232 GREATEST_ASSERT(irc_rule_match(&m, "malikania", "#staff", "markand", "system", "onCommand"));
233 GREATEST_ASSERT(!irc_rule_match(&m, "malikania", "#staff", "", "system", "onNames"));
234 GREATEST_ASSERT(!irc_rule_match(&m, "malikania", "#staff", "jean", "system", "onMessage"));
235 GREATEST_PASS();
236 }
237
238 GREATEST_TEST
239 solve_match7(void)
240 {
241 /* Allowed */
242 GREATEST_ASSERT(irc_rule_matchlist(irc.rules, irc.rulesz, "malikania", "#staff", "", "a", "onMessage"));
243
244 /* Allowed */
245 GREATEST_ASSERT(irc_rule_matchlist(irc.rules, irc.rulesz, "freenode", "#staff", "", "b", "onTopic"));
246
247 /* Not allowed */
248 GREATEST_ASSERT(!irc_rule_matchlist(irc.rules, irc.rulesz, "malikania", "#staff", "", "", "onCommand"));
249
250 /* Not allowed */
251 GREATEST_ASSERT(!irc_rule_matchlist(irc.rules, irc.rulesz, "freenode", "#staff", "", "c", "onCommand"));
252
253 /* Allowed */
254 GREATEST_ASSERT(irc_rule_matchlist(irc.rules, irc.rulesz, "unsafe", "#staff", "", "c", "onCommand"));
255
256 GREATEST_PASS();
257 }
258
259 GREATEST_TEST
260 solve_match8(void)
261 {
262 /* Allowed */
263 GREATEST_ASSERT(irc_rule_matchlist(irc.rules, irc.rulesz, "malikania", "#games", "", "game", "onMessage"));
264
265 /* Allowed */
266 GREATEST_ASSERT(irc_rule_matchlist(irc.rules, irc.rulesz, "localhost", "#games", "", "game", "onMessage"));
267
268 /* Allowed */
269 GREATEST_ASSERT(irc_rule_matchlist(irc.rules, irc.rulesz, "malikania", "#games", "", "game", "onCommand"));
270
271 /* Not allowed */
272 GREATEST_ASSERT(!irc_rule_matchlist(irc.rules, irc.rulesz, "malikania", "#games", "", "game", "onQuery"));
273
274 /* Not allowed */
275 GREATEST_ASSERT(!irc_rule_matchlist(irc.rules, irc.rulesz, "freenode", "#no", "", "game", "onMessage"));
276
277 /* Not allowed */
278 GREATEST_ASSERT(!irc_rule_matchlist(irc.rules, irc.rulesz, "malikania", "#test", "", "game", "onMessage"));
279 GREATEST_PASS();
280 }
281
282 GREATEST_TEST
283 solve_match9(void)
284 {
285 GREATEST_ASSERT(!irc_rule_matchlist(irc.rules, irc.rulesz, "MALIKANIA", "#STAFF", "", "SYSTEM", "onCommand"));
286 GREATEST_PASS();
287 }
288
289 GREATEST_SUITE(suite_basics)
290 {
291 GREATEST_SET_SETUP_CB(clean, NULL);
292 GREATEST_RUN_TEST(basics_insert);
293 GREATEST_RUN_TEST(basics_remove);
294 }
295
296 GREATEST_SUITE(suite_solve)
297 {
298 GREATEST_SET_SETUP_CB(set, NULL);
299 GREATEST_RUN_TEST(solve_match1);
300 GREATEST_RUN_TEST(solve_match2);
301 GREATEST_RUN_TEST(solve_match3);
302 GREATEST_RUN_TEST(solve_match4);
303 GREATEST_RUN_TEST(solve_match5);
304 GREATEST_RUN_TEST(solve_match6);
305 GREATEST_RUN_TEST(solve_match7);
306 GREATEST_RUN_TEST(solve_match8);
307 GREATEST_RUN_TEST(solve_match9);
308 }
309
310 GREATEST_MAIN_DEFS();
311
312 int
313 main(int argc, char **argv)
314 {
315 GREATEST_MAIN_BEGIN();
316 GREATEST_RUN_SUITE(suite_basics);
317 GREATEST_RUN_SUITE(suite_solve);
318 GREATEST_MAIN_END();
319
320 return 0;
321 }