Mercurial > irccd
comparison tests/src/libirccd/command-rule-move/main.cpp @ 611:9fbd1700435b
Tests: reoganize hierarchy
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 15 Dec 2017 15:37:58 +0100 |
parents | tests/src/rule-move-command/main.cpp@22b3cd6f991f |
children | f69b1faf812f |
comparison
equal
deleted
inserted
replaced
610:22b3cd6f991f | 611:9fbd1700435b |
---|---|
1 /* | |
2 * main.cpp -- test rule-move remote command | |
3 * | |
4 * Copyright (c) 2013-2017 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 BOOST_TEST_MODULE "rule-move" | |
20 #include <boost/test/unit_test.hpp> | |
21 | |
22 #include <irccd/json_util.hpp> | |
23 | |
24 #include <irccd/daemon/command.hpp> | |
25 #include <irccd/daemon/rule_service.hpp> | |
26 | |
27 #include <irccd/test/command_test.hpp> | |
28 | |
29 namespace irccd { | |
30 | |
31 namespace { | |
32 | |
33 class rule_move_test : public command_test<rule_move_command, rule_list_command> { | |
34 public: | |
35 rule_move_test() | |
36 { | |
37 daemon_->rules().add(rule( | |
38 { "s0" }, | |
39 { "c0" }, | |
40 { "o0" }, | |
41 { "p0" }, | |
42 { "onMessage" }, | |
43 rule::action_type::drop | |
44 )); | |
45 daemon_->rules().add(rule( | |
46 { "s1", }, | |
47 { "c1", }, | |
48 { "o1", }, | |
49 { "p1", }, | |
50 { "onMessage", }, | |
51 rule::action_type::accept | |
52 )); | |
53 daemon_->rules().add(rule( | |
54 { "s2", }, | |
55 { "c2", }, | |
56 { "o2", }, | |
57 { "p2", }, | |
58 { "onMessage", }, | |
59 rule::action_type::accept | |
60 )); | |
61 } | |
62 }; | |
63 | |
64 } // !namespace | |
65 | |
66 BOOST_FIXTURE_TEST_SUITE(rule_move_test_suite, rule_move_test) | |
67 | |
68 BOOST_AUTO_TEST_CASE(backward) | |
69 { | |
70 nlohmann::json result; | |
71 | |
72 ctl_->send({ | |
73 { "command", "rule-move" }, | |
74 { "from", 2 }, | |
75 { "to", 0 } | |
76 }); | |
77 ctl_->recv([&] (auto, auto msg) { | |
78 result = msg; | |
79 }); | |
80 | |
81 wait_for([&] () { | |
82 return result.is_object(); | |
83 }); | |
84 | |
85 BOOST_TEST(result.is_object()); | |
86 | |
87 result = nullptr; | |
88 ctl_->send({{ "command", "rule-list" }}); | |
89 ctl_->recv([&] (auto, auto msg) { | |
90 result = msg; | |
91 }); | |
92 | |
93 wait_for([&] () { | |
94 return result.is_object(); | |
95 }); | |
96 | |
97 BOOST_TEST(result.is_object()); | |
98 | |
99 // Rule 2. | |
100 { | |
101 auto servers = result["list"][0]["servers"]; | |
102 auto channels = result["list"][0]["channels"]; | |
103 auto plugins = result["list"][0]["plugins"]; | |
104 auto events = result["list"][0]["events"]; | |
105 | |
106 BOOST_TEST(json_util::contains(servers, "s2")); | |
107 BOOST_TEST(json_util::contains(channels, "c2")); | |
108 BOOST_TEST(json_util::contains(plugins, "p2")); | |
109 BOOST_TEST(json_util::contains(events, "onMessage")); | |
110 BOOST_TEST(result["list"][0]["action"].get<std::string>() == "accept"); | |
111 } | |
112 | |
113 // Rule 0. | |
114 { | |
115 auto servers = result["list"][1]["servers"]; | |
116 auto channels = result["list"][1]["channels"]; | |
117 auto plugins = result["list"][1]["plugins"]; | |
118 auto events = result["list"][1]["events"]; | |
119 | |
120 BOOST_TEST(json_util::contains(servers, "s0")); | |
121 BOOST_TEST(json_util::contains(channels, "c0")); | |
122 BOOST_TEST(json_util::contains(plugins, "p0")); | |
123 BOOST_TEST(json_util::contains(events, "onMessage")); | |
124 BOOST_TEST(result["list"][1]["action"].get<std::string>() == "drop"); | |
125 } | |
126 | |
127 // Rule 1. | |
128 { | |
129 auto servers = result["list"][2]["servers"]; | |
130 auto channels = result["list"][2]["channels"]; | |
131 auto plugins = result["list"][2]["plugins"]; | |
132 auto events = result["list"][2]["events"]; | |
133 | |
134 BOOST_TEST(json_util::contains(servers, "s1")); | |
135 BOOST_TEST(json_util::contains(channels, "c1")); | |
136 BOOST_TEST(json_util::contains(plugins, "p1")); | |
137 BOOST_TEST(json_util::contains(events, "onMessage")); | |
138 BOOST_TEST(result["list"][2]["action"].get<std::string>() == "accept"); | |
139 } | |
140 } | |
141 | |
142 BOOST_AUTO_TEST_CASE(upward) | |
143 { | |
144 nlohmann::json result; | |
145 | |
146 ctl_->send({ | |
147 { "command", "rule-move" }, | |
148 { "from", 0 }, | |
149 { "to", 2 } | |
150 }); | |
151 ctl_->recv([&] (auto, auto msg) { | |
152 result = msg; | |
153 }); | |
154 | |
155 wait_for([&] () { | |
156 return result.is_object(); | |
157 }); | |
158 | |
159 BOOST_TEST(result.is_object()); | |
160 | |
161 result = nullptr; | |
162 ctl_->send({{ "command", "rule-list" }}); | |
163 ctl_->recv([&] (auto, auto msg) { | |
164 result = msg; | |
165 }); | |
166 | |
167 wait_for([&] () { | |
168 return result.is_object(); | |
169 }); | |
170 | |
171 BOOST_TEST(result.is_object()); | |
172 | |
173 // Rule 1. | |
174 { | |
175 auto servers = result["list"][0]["servers"]; | |
176 auto channels = result["list"][0]["channels"]; | |
177 auto plugins = result["list"][0]["plugins"]; | |
178 auto events = result["list"][0]["events"]; | |
179 | |
180 BOOST_TEST(json_util::contains(servers, "s1")); | |
181 BOOST_TEST(json_util::contains(channels, "c1")); | |
182 BOOST_TEST(json_util::contains(plugins, "p1")); | |
183 BOOST_TEST(json_util::contains(events, "onMessage")); | |
184 BOOST_TEST(result["list"][0]["action"].get<std::string>() == "accept"); | |
185 } | |
186 | |
187 // Rule 2. | |
188 { | |
189 auto servers = result["list"][1]["servers"]; | |
190 auto channels = result["list"][1]["channels"]; | |
191 auto plugins = result["list"][1]["plugins"]; | |
192 auto events = result["list"][1]["events"]; | |
193 | |
194 BOOST_TEST(json_util::contains(servers, "s2")); | |
195 BOOST_TEST(json_util::contains(channels, "c2")); | |
196 BOOST_TEST(json_util::contains(plugins, "p2")); | |
197 BOOST_TEST(json_util::contains(events, "onMessage")); | |
198 BOOST_TEST(result["list"][1]["action"].get<std::string>() == "accept"); | |
199 } | |
200 | |
201 // Rule 0. | |
202 { | |
203 auto servers = result["list"][2]["servers"]; | |
204 auto channels = result["list"][2]["channels"]; | |
205 auto plugins = result["list"][2]["plugins"]; | |
206 auto events = result["list"][2]["events"]; | |
207 | |
208 BOOST_TEST(json_util::contains(servers, "s0")); | |
209 BOOST_TEST(json_util::contains(channels, "c0")); | |
210 BOOST_TEST(json_util::contains(plugins, "p0")); | |
211 BOOST_TEST(json_util::contains(events, "onMessage")); | |
212 BOOST_TEST(result["list"][2]["action"].get<std::string>() == "drop"); | |
213 } | |
214 } | |
215 | |
216 BOOST_AUTO_TEST_CASE(same) | |
217 { | |
218 nlohmann::json result; | |
219 | |
220 ctl_->send({ | |
221 { "command", "rule-move" }, | |
222 { "from", 1 }, | |
223 { "to", 1 } | |
224 }); | |
225 ctl_->recv([&] (auto, auto msg) { | |
226 result = msg; | |
227 }); | |
228 | |
229 wait_for([&] () { | |
230 return result.is_object(); | |
231 }); | |
232 | |
233 BOOST_TEST(result.is_object()); | |
234 | |
235 result = nullptr; | |
236 ctl_->send({{ "command", "rule-list" }}); | |
237 ctl_->recv([&] (auto, auto msg) { | |
238 result = msg; | |
239 }); | |
240 | |
241 wait_for([&] () { | |
242 return result.is_object(); | |
243 }); | |
244 | |
245 BOOST_TEST(result.is_object()); | |
246 | |
247 // Rule 0. | |
248 { | |
249 auto servers = result["list"][0]["servers"]; | |
250 auto channels = result["list"][0]["channels"]; | |
251 auto plugins = result["list"][0]["plugins"]; | |
252 auto events = result["list"][0]["events"]; | |
253 | |
254 BOOST_TEST(json_util::contains(servers, "s0")); | |
255 BOOST_TEST(json_util::contains(channels, "c0")); | |
256 BOOST_TEST(json_util::contains(plugins, "p0")); | |
257 BOOST_TEST(json_util::contains(events, "onMessage")); | |
258 BOOST_TEST(result["list"][0]["action"].get<std::string>() == "drop"); | |
259 } | |
260 | |
261 // Rule 1. | |
262 { | |
263 auto servers = result["list"][1]["servers"]; | |
264 auto channels = result["list"][1]["channels"]; | |
265 auto plugins = result["list"][1]["plugins"]; | |
266 auto events = result["list"][1]["events"]; | |
267 | |
268 BOOST_TEST(json_util::contains(servers, "s1")); | |
269 BOOST_TEST(json_util::contains(channels, "c1")); | |
270 BOOST_TEST(json_util::contains(plugins, "p1")); | |
271 BOOST_TEST(json_util::contains(events, "onMessage")); | |
272 BOOST_TEST(result["list"][1]["action"].get<std::string>() == "accept"); | |
273 } | |
274 | |
275 // Rule 2. | |
276 { | |
277 auto servers = result["list"][2]["servers"]; | |
278 auto channels = result["list"][2]["channels"]; | |
279 auto plugins = result["list"][2]["plugins"]; | |
280 auto events = result["list"][2]["events"]; | |
281 | |
282 BOOST_TEST(json_util::contains(servers, "s2")); | |
283 BOOST_TEST(json_util::contains(channels, "c2")); | |
284 BOOST_TEST(json_util::contains(plugins, "p2")); | |
285 BOOST_TEST(json_util::contains(events, "onMessage")); | |
286 BOOST_TEST(result["list"][2]["action"].get<std::string>() == "accept"); | |
287 } | |
288 } | |
289 | |
290 BOOST_AUTO_TEST_CASE(beyond) | |
291 { | |
292 nlohmann::json result; | |
293 | |
294 ctl_->send({ | |
295 { "command", "rule-move" }, | |
296 { "from", 0 }, | |
297 { "to", 123 } | |
298 }); | |
299 ctl_->recv([&] (auto, auto msg) { | |
300 result = msg; | |
301 }); | |
302 | |
303 wait_for([&] () { | |
304 return result.is_object(); | |
305 }); | |
306 | |
307 BOOST_TEST(result.is_object()); | |
308 | |
309 result = nullptr; | |
310 ctl_->send({{ "command", "rule-list" }}); | |
311 ctl_->recv([&] (auto, auto msg) { | |
312 result = msg; | |
313 }); | |
314 | |
315 wait_for([&] () { | |
316 return result.is_object(); | |
317 }); | |
318 | |
319 BOOST_TEST(result.is_object()); | |
320 | |
321 // Rule 1. | |
322 { | |
323 auto servers = result["list"][0]["servers"]; | |
324 auto channels = result["list"][0]["channels"]; | |
325 auto plugins = result["list"][0]["plugins"]; | |
326 auto events = result["list"][0]["events"]; | |
327 | |
328 BOOST_TEST(json_util::contains(servers, "s1")); | |
329 BOOST_TEST(json_util::contains(channels, "c1")); | |
330 BOOST_TEST(json_util::contains(plugins, "p1")); | |
331 BOOST_TEST(json_util::contains(events, "onMessage")); | |
332 BOOST_TEST(result["list"][0]["action"].get<std::string>() == "accept"); | |
333 } | |
334 | |
335 // Rule 2. | |
336 { | |
337 auto servers = result["list"][1]["servers"]; | |
338 auto channels = result["list"][1]["channels"]; | |
339 auto plugins = result["list"][1]["plugins"]; | |
340 auto events = result["list"][1]["events"]; | |
341 | |
342 BOOST_TEST(json_util::contains(servers, "s2")); | |
343 BOOST_TEST(json_util::contains(channels, "c2")); | |
344 BOOST_TEST(json_util::contains(plugins, "p2")); | |
345 BOOST_TEST(json_util::contains(events, "onMessage")); | |
346 BOOST_TEST(result["list"][1]["action"].get<std::string>() == "accept"); | |
347 } | |
348 | |
349 // Rule 0. | |
350 { | |
351 auto servers = result["list"][2]["servers"]; | |
352 auto channels = result["list"][2]["channels"]; | |
353 auto plugins = result["list"][2]["plugins"]; | |
354 auto events = result["list"][2]["events"]; | |
355 | |
356 BOOST_TEST(json_util::contains(servers, "s0")); | |
357 BOOST_TEST(json_util::contains(channels, "c0")); | |
358 BOOST_TEST(json_util::contains(plugins, "p0")); | |
359 BOOST_TEST(json_util::contains(events, "onMessage")); | |
360 BOOST_TEST(result["list"][2]["action"].get<std::string>() == "drop"); | |
361 } | |
362 } | |
363 | |
364 BOOST_AUTO_TEST_SUITE(errors) | |
365 | |
366 BOOST_AUTO_TEST_CASE(invalid_index_1_from) | |
367 { | |
368 boost::system::error_code result; | |
369 nlohmann::json message; | |
370 | |
371 ctl_->send({ | |
372 { "command", "rule-move" }, | |
373 { "from", -100 }, | |
374 { "to", 0 } | |
375 }); | |
376 ctl_->recv([&] (auto rresult, auto rmessage) { | |
377 result = rresult; | |
378 message = rmessage; | |
379 }); | |
380 | |
381 wait_for([&] { | |
382 return result; | |
383 }); | |
384 | |
385 BOOST_TEST(result == rule_error::invalid_index); | |
386 BOOST_TEST(message["error"].template get<int>() == rule_error::invalid_index); | |
387 BOOST_TEST(message["errorCategory"].template get<std::string>() == "rule"); | |
388 } | |
389 | |
390 BOOST_AUTO_TEST_CASE(invalid_index_1_to) | |
391 { | |
392 boost::system::error_code result; | |
393 nlohmann::json message; | |
394 | |
395 ctl_->send({ | |
396 { "command", "rule-move" }, | |
397 { "from", 0 }, | |
398 { "to", -100 } | |
399 }); | |
400 ctl_->recv([&] (auto rresult, auto rmessage) { | |
401 result = rresult; | |
402 message = rmessage; | |
403 }); | |
404 | |
405 wait_for([&] { | |
406 return result; | |
407 }); | |
408 | |
409 BOOST_TEST(result == rule_error::invalid_index); | |
410 BOOST_TEST(message["error"].template get<int>() == rule_error::invalid_index); | |
411 BOOST_TEST(message["errorCategory"].template get<std::string>() == "rule"); | |
412 } | |
413 | |
414 BOOST_AUTO_TEST_CASE(invalid_index_2_from) | |
415 { | |
416 boost::system::error_code result; | |
417 nlohmann::json message; | |
418 | |
419 ctl_->send({ | |
420 { "command", "rule-move" }, | |
421 { "from", 100 }, | |
422 { "to", 0 } | |
423 }); | |
424 ctl_->recv([&] (auto rresult, auto rmessage) { | |
425 result = rresult; | |
426 message = rmessage; | |
427 }); | |
428 | |
429 wait_for([&] { | |
430 return result; | |
431 }); | |
432 | |
433 BOOST_TEST(result == rule_error::invalid_index); | |
434 BOOST_TEST(message["error"].template get<int>() == rule_error::invalid_index); | |
435 BOOST_TEST(message["errorCategory"].template get<std::string>() == "rule"); | |
436 } | |
437 | |
438 BOOST_AUTO_TEST_CASE(invalid_index_3_from) | |
439 { | |
440 boost::system::error_code result; | |
441 nlohmann::json message; | |
442 | |
443 ctl_->send({ | |
444 { "command", "rule-move" }, | |
445 { "from", "notaint" }, | |
446 { "to", 0 } | |
447 }); | |
448 ctl_->recv([&] (auto rresult, auto rmessage) { | |
449 result = rresult; | |
450 message = rmessage; | |
451 }); | |
452 | |
453 wait_for([&] { | |
454 return result; | |
455 }); | |
456 | |
457 BOOST_TEST(result == rule_error::invalid_index); | |
458 BOOST_TEST(message["error"].template get<int>() == rule_error::invalid_index); | |
459 BOOST_TEST(message["errorCategory"].template get<std::string>() == "rule"); | |
460 } | |
461 | |
462 BOOST_AUTO_TEST_CASE(invalid_index_3_to) | |
463 { | |
464 boost::system::error_code result; | |
465 nlohmann::json message; | |
466 | |
467 ctl_->send({ | |
468 { "command", "rule-move" }, | |
469 { "from", 0 }, | |
470 { "to", "notaint" } | |
471 }); | |
472 ctl_->recv([&] (auto rresult, auto rmessage) { | |
473 result = rresult; | |
474 message = rmessage; | |
475 }); | |
476 | |
477 wait_for([&] { | |
478 return result; | |
479 }); | |
480 | |
481 BOOST_TEST(result == rule_error::invalid_index); | |
482 BOOST_TEST(message["error"].template get<int>() == rule_error::invalid_index); | |
483 BOOST_TEST(message["errorCategory"].template get<std::string>() == "rule"); | |
484 } | |
485 | |
486 BOOST_AUTO_TEST_SUITE_END() | |
487 | |
488 BOOST_AUTO_TEST_SUITE_END() | |
489 | |
490 } // !irccd |