comparison tests/src/libirccd/command-rule-edit/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-edit-command/main.cpp@22b3cd6f991f
children f69b1faf812f
comparison
equal deleted inserted replaced
610:22b3cd6f991f 611:9fbd1700435b
1 /*
2 * main.cpp -- test rule-edit 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-edit"
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_edit_test : public command_test<rule_edit_command, rule_info_command> {
34 public:
35 rule_edit_test()
36 {
37 daemon_->rules().add(rule(
38 { "s1", "s2" },
39 { "c1", "c2" },
40 { "o1", "o2" },
41 { "p1", "p2" },
42 { "onMessage", "onCommand" },
43 rule::action_type::drop
44 ));
45 }
46 };
47
48 } // !namespace
49
50 BOOST_FIXTURE_TEST_SUITE(rule_edit_test_suite, rule_edit_test)
51
52 BOOST_AUTO_TEST_CASE(add_server)
53 {
54 nlohmann::json result;
55
56 ctl_->send({
57 { "command", "rule-edit" },
58 { "add-servers", { "new-s3" } },
59 { "index", 0 }
60 });
61 ctl_->recv([&] (auto, auto msg) {
62 result = msg;
63 });
64
65 wait_for([&] () {
66 return result.is_object();
67 });
68
69 BOOST_TEST(result.is_object());
70
71 result = nullptr;
72 ctl_->send({
73 { "command", "rule-info" },
74 { "index", 0 }
75 });
76 ctl_->recv([&] (auto, auto msg) {
77 result = msg;
78 });
79
80 wait_for([&] () {
81 return result.is_object();
82 });
83
84 BOOST_TEST(result.is_object());
85 BOOST_TEST(json_util::contains(result["servers"], "s1"));
86 BOOST_TEST(json_util::contains(result["servers"], "s2"));
87 BOOST_TEST(json_util::contains(result["servers"], "new-s3"));
88 BOOST_TEST(json_util::contains(result["channels"], "c1"));
89 BOOST_TEST(json_util::contains(result["channels"], "c2"));
90 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
91 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
92 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
93 BOOST_TEST(json_util::contains(result["events"], "onCommand"));
94 BOOST_TEST(result["action"].get<std::string>() == "drop");
95 }
96
97 BOOST_AUTO_TEST_CASE(add_channel)
98 {
99 nlohmann::json result;
100
101 ctl_->send({
102 { "command", "rule-edit" },
103 { "add-channels", { "new-c3" } },
104 { "index", 0 }
105 });
106 ctl_->recv([&] (auto, auto msg) {
107 result = msg;
108 });
109
110 wait_for([&] () {
111 return result.is_object();
112 });
113
114 BOOST_TEST(result.is_object());
115
116 result = nullptr;
117 ctl_->send({
118 { "command", "rule-info" },
119 { "index", 0 }
120 });
121 ctl_->recv([&] (auto, auto msg) {
122 result = msg;
123 });
124
125 wait_for([&] () {
126 return result.is_object();
127 });
128
129 BOOST_TEST(result.is_object());
130 BOOST_TEST(json_util::contains(result["servers"], "s1"));
131 BOOST_TEST(json_util::contains(result["servers"], "s2"));
132 BOOST_TEST(json_util::contains(result["channels"], "c1"));
133 BOOST_TEST(json_util::contains(result["channels"], "c2"));
134 BOOST_TEST(json_util::contains(result["channels"], "new-c3"));
135 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
136 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
137 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
138 BOOST_TEST(json_util::contains(result["events"], "onCommand"));
139 BOOST_TEST(result["action"].get<std::string>() == "drop");
140 }
141
142 BOOST_AUTO_TEST_CASE(add_plugin)
143 {
144 nlohmann::json result;
145
146 ctl_->send({
147 { "command", "rule-edit" },
148 { "add-plugins", { "new-p3" } },
149 { "index", 0 }
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({
163 { "command", "rule-info" },
164 { "index", 0 }
165 });
166 ctl_->recv([&] (auto, auto msg) {
167 result = msg;
168 });
169
170 wait_for([&] () {
171 return result.is_object();
172 });
173
174 BOOST_TEST(result.is_object());
175 BOOST_TEST(json_util::contains(result["servers"], "s1"));
176 BOOST_TEST(json_util::contains(result["servers"], "s2"));
177 BOOST_TEST(json_util::contains(result["channels"], "c1"));
178 BOOST_TEST(json_util::contains(result["channels"], "c2"));
179 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
180 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
181 BOOST_TEST(json_util::contains(result["plugins"], "new-p3"));
182 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
183 BOOST_TEST(json_util::contains(result["events"], "onCommand"));
184 BOOST_TEST(result["action"].get<std::string>() == "drop");
185 }
186
187 BOOST_AUTO_TEST_CASE(add_event)
188 {
189 nlohmann::json result;
190
191 ctl_->send({
192 { "command", "rule-edit" },
193 { "add-events", { "onQuery" } },
194 { "index", 0 }
195 });
196 ctl_->recv([&] (auto, auto msg) {
197 result = msg;
198 });
199
200 wait_for([&] () {
201 return result.is_object();
202 });
203
204 BOOST_TEST(result.is_object());
205
206 result = nullptr;
207 ctl_->send({
208 { "command", "rule-info" },
209 { "index", 0 }
210 });
211 ctl_->recv([&] (auto, auto msg) {
212 result = msg;
213 });
214
215 wait_for([&] () {
216 return result.is_object();
217 });
218
219 BOOST_TEST(result.is_object());
220 BOOST_TEST(json_util::contains(result["servers"], "s1"));
221 BOOST_TEST(json_util::contains(result["servers"], "s2"));
222 BOOST_TEST(json_util::contains(result["channels"], "c1"));
223 BOOST_TEST(json_util::contains(result["channels"], "c2"));
224 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
225 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
226 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
227 BOOST_TEST(json_util::contains(result["events"], "onCommand"));
228 BOOST_TEST(json_util::contains(result["events"], "onQuery"));
229 BOOST_TEST(result["action"].get<std::string>() == "drop");
230 }
231
232 BOOST_AUTO_TEST_CASE(add_event_and_server)
233 {
234 nlohmann::json result;
235
236 ctl_->send({
237 { "command", "rule-edit" },
238 { "add-servers", { "new-s3" } },
239 { "add-events", { "onQuery" } },
240 { "index", 0 }
241 });
242 ctl_->recv([&] (auto, auto msg) {
243 result = msg;
244 });
245
246 wait_for([&] () {
247 return result.is_object();
248 });
249
250 BOOST_TEST(result.is_object());
251
252 result = nullptr;
253 ctl_->send({
254 { "command", "rule-info" },
255 { "index", 0 }
256 });
257 ctl_->recv([&] (auto, auto msg) {
258 result = msg;
259 });
260
261 wait_for([&] () {
262 return result.is_object();
263 });
264
265 BOOST_TEST(result.is_object());
266 BOOST_TEST(json_util::contains(result["servers"], "s1"));
267 BOOST_TEST(json_util::contains(result["servers"], "s2"));
268 BOOST_TEST(json_util::contains(result["servers"], "new-s3"));
269 BOOST_TEST(json_util::contains(result["channels"], "c1"));
270 BOOST_TEST(json_util::contains(result["channels"], "c2"));
271 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
272 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
273 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
274 BOOST_TEST(json_util::contains(result["events"], "onCommand"));
275 BOOST_TEST(json_util::contains(result["events"], "onQuery"));
276 BOOST_TEST(result["action"].get<std::string>() == "drop");
277 }
278
279 BOOST_AUTO_TEST_CASE(change_action)
280 {
281 nlohmann::json result;
282
283 ctl_->send({
284 { "command", "rule-edit" },
285 { "action", "accept" },
286 { "index", 0 }
287 });
288 ctl_->recv([&] (auto, auto msg) {
289 result = msg;
290 });
291
292 wait_for([&] () {
293 return result.is_object();
294 });
295
296 BOOST_TEST(result.is_object());
297
298 result = nullptr;
299 ctl_->send({
300 { "command", "rule-info" },
301 { "index", 0 }
302 });
303 ctl_->recv([&] (auto, auto msg) {
304 result = msg;
305 });
306
307 wait_for([&] () {
308 return result.is_object();
309 });
310
311 BOOST_TEST(result.is_object());
312 BOOST_TEST(json_util::contains(result["servers"], "s1"));
313 BOOST_TEST(json_util::contains(result["servers"], "s2"));
314 BOOST_TEST(json_util::contains(result["channels"], "c1"));
315 BOOST_TEST(json_util::contains(result["channels"], "c2"));
316 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
317 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
318 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
319 BOOST_TEST(json_util::contains(result["events"], "onCommand"));
320 BOOST_TEST(result["action"].get<std::string>() == "accept");
321 }
322
323 BOOST_AUTO_TEST_CASE(remove_server)
324 {
325 nlohmann::json result;
326
327 ctl_->send({
328 { "command", "rule-edit" },
329 { "remove-servers", { "s2" } },
330 { "index", 0 }
331 });
332 ctl_->recv([&] (auto, auto msg) {
333 result = msg;
334 });
335
336 wait_for([&] () {
337 return result.is_object();
338 });
339
340 BOOST_TEST(result.is_object());
341
342 result = nullptr;
343 ctl_->send({
344 { "command", "rule-info" },
345 { "index", 0 }
346 });
347 ctl_->recv([&] (auto, auto msg) {
348 result = msg;
349 });
350
351 wait_for([&] () {
352 return result.is_object();
353 });
354
355 BOOST_TEST(result.is_object());
356 BOOST_TEST(json_util::contains(result["servers"], "s1"));
357 BOOST_TEST(!json_util::contains(result["servers"], "s2"));
358 BOOST_TEST(json_util::contains(result["channels"], "c1"));
359 BOOST_TEST(json_util::contains(result["channels"], "c2"));
360 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
361 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
362 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
363 BOOST_TEST(json_util::contains(result["events"], "onCommand"));
364 BOOST_TEST(result["action"].get<std::string>() == "drop");
365 }
366
367 BOOST_AUTO_TEST_CASE(remove_channel)
368 {
369 nlohmann::json result;
370
371 ctl_->send({
372 { "command", "rule-edit" },
373 { "remove-channels", { "c2" } },
374 { "index", 0 }
375 });
376 ctl_->recv([&] (auto, auto msg) {
377 result = msg;
378 });
379
380 wait_for([&] () {
381 return result.is_object();
382 });
383
384 BOOST_TEST(result.is_object());
385
386 result = nullptr;
387 ctl_->send({
388 { "command", "rule-info" },
389 { "index", 0 }
390 });
391 ctl_->recv([&] (auto, auto msg) {
392 result = msg;
393 });
394
395 wait_for([&] () {
396 return result.is_object();
397 });
398
399 BOOST_TEST(result.is_object());
400 BOOST_TEST(json_util::contains(result["servers"], "s1"));
401 BOOST_TEST(json_util::contains(result["servers"], "s2"));
402 BOOST_TEST(json_util::contains(result["channels"], "c1"));
403 BOOST_TEST(!json_util::contains(result["channels"], "c2"));
404 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
405 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
406 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
407 BOOST_TEST(json_util::contains(result["events"], "onCommand"));
408 BOOST_TEST(result["action"].get<std::string>() == "drop");
409 }
410
411 BOOST_AUTO_TEST_CASE(remove_plugin)
412 {
413 nlohmann::json result;
414
415 ctl_->send({
416 { "command", "rule-edit" },
417 { "remove-plugins", { "p2" } },
418 { "index", 0 }
419 });
420 ctl_->recv([&] (auto, auto msg) {
421 result = msg;
422 });
423
424 wait_for([&] () {
425 return result.is_object();
426 });
427
428 BOOST_TEST(result.is_object());
429
430 result = nullptr;
431 ctl_->send({
432 { "command", "rule-info" },
433 { "index", 0 }
434 });
435 ctl_->recv([&] (auto, auto msg) {
436 result = msg;
437 });
438
439 wait_for([&] () {
440 return result.is_object();
441 });
442
443 BOOST_TEST(result.is_object());
444 BOOST_TEST(json_util::contains(result["servers"], "s1"));
445 BOOST_TEST(json_util::contains(result["servers"], "s2"));
446 BOOST_TEST(json_util::contains(result["channels"], "c1"));
447 BOOST_TEST(json_util::contains(result["channels"], "c2"));
448 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
449 BOOST_TEST(!json_util::contains(result["plugins"], "p2"));
450 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
451 BOOST_TEST(json_util::contains(result["events"], "onCommand"));
452 BOOST_TEST(result["action"].get<std::string>() == "drop");
453 }
454
455 BOOST_AUTO_TEST_CASE(remove_event)
456 {
457 nlohmann::json result;
458
459 ctl_->send({
460 { "command", "rule-edit" },
461 { "remove-events", { "onCommand" } },
462 { "index", 0 }
463 });
464 ctl_->recv([&] (auto, auto msg) {
465 result = msg;
466 });
467
468 wait_for([&] () {
469 return result.is_object();
470 });
471
472 BOOST_TEST(result.is_object());
473
474 result = nullptr;
475 ctl_->send({
476 { "command", "rule-info" },
477 { "index", 0 }
478 });
479 ctl_->recv([&] (auto, auto msg) {
480 result = msg;
481 });
482
483 wait_for([&] () {
484 return result.is_object();
485 });
486
487 BOOST_TEST(result.is_object());
488 BOOST_TEST(json_util::contains(result["servers"], "s1"));
489 BOOST_TEST(json_util::contains(result["servers"], "s2"));
490 BOOST_TEST(json_util::contains(result["channels"], "c1"));
491 BOOST_TEST(json_util::contains(result["channels"], "c2"));
492 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
493 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
494 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
495 BOOST_TEST(!json_util::contains(result["events"], "onCommand"));
496 BOOST_TEST(result["action"].get<std::string>() == "drop");
497 }
498
499 BOOST_AUTO_TEST_CASE(remove_event_and_server)
500 {
501 nlohmann::json result;
502
503 ctl_->send({
504 { "command", "rule-edit" },
505 { "remove-servers", { "s2" } },
506 { "remove-events", { "onCommand" } },
507 { "index", 0 }
508 });
509 ctl_->recv([&] (auto, auto msg) {
510 result = msg;
511 });
512
513 wait_for([&] () {
514 return result.is_object();
515 });
516
517 BOOST_TEST(result.is_object());
518
519 result = nullptr;
520 ctl_->send({
521 { "command", "rule-info" },
522 { "index", 0 }
523 });
524 ctl_->recv([&] (auto, auto msg) {
525 result = msg;
526 });
527
528 wait_for([&] () {
529 return result.is_object();
530 });
531
532 BOOST_TEST(result.is_object());
533 BOOST_TEST(json_util::contains(result["servers"], "s1"));
534 BOOST_TEST(!json_util::contains(result["servers"], "s2"));
535 BOOST_TEST(json_util::contains(result["channels"], "c1"));
536 BOOST_TEST(json_util::contains(result["channels"], "c2"));
537 BOOST_TEST(json_util::contains(result["plugins"], "p1"));
538 BOOST_TEST(json_util::contains(result["plugins"], "p2"));
539 BOOST_TEST(json_util::contains(result["events"], "onMessage"));
540 BOOST_TEST(!json_util::contains(result["events"], "onCommand"));
541 BOOST_TEST(result["action"].get<std::string>() == "drop");
542 }
543
544 BOOST_AUTO_TEST_SUITE(errors)
545
546 BOOST_AUTO_TEST_CASE(invalid_index_1)
547 {
548 boost::system::error_code result;
549 nlohmann::json message;
550
551 ctl_->send({
552 { "command", "rule-edit" },
553 { "index", -100 },
554 { "action", "drop" }
555 });
556 ctl_->recv([&] (auto rresult, auto rmessage) {
557 result = rresult;
558 message = rmessage;
559 });
560
561 wait_for([&] {
562 return result;
563 });
564
565 BOOST_TEST(result == rule_error::invalid_index);
566 BOOST_TEST(message["error"].template get<int>() == rule_error::invalid_index);
567 BOOST_TEST(message["errorCategory"].template get<std::string>() == "rule");
568 }
569
570 BOOST_AUTO_TEST_CASE(invalid_index_2)
571 {
572 boost::system::error_code result;
573 nlohmann::json message;
574
575 ctl_->send({
576 { "command", "rule-edit" },
577 { "index", 100 },
578 { "action", "drop" }
579 });
580 ctl_->recv([&] (auto rresult, auto rmessage) {
581 result = rresult;
582 message = rmessage;
583 });
584
585 wait_for([&] {
586 return result;
587 });
588
589 BOOST_TEST(result == rule_error::invalid_index);
590 BOOST_TEST(message["error"].template get<int>() == rule_error::invalid_index);
591 BOOST_TEST(message["errorCategory"].template get<std::string>() == "rule");
592 }
593
594 BOOST_AUTO_TEST_CASE(invalid_index_3)
595 {
596 boost::system::error_code result;
597 nlohmann::json message;
598
599 ctl_->send({
600 { "command", "rule-edit" },
601 { "index", "notaint" },
602 { "action", "drop" }
603 });
604 ctl_->recv([&] (auto rresult, auto rmessage) {
605 result = rresult;
606 message = rmessage;
607 });
608
609 wait_for([&] {
610 return result;
611 });
612
613 BOOST_TEST(result == rule_error::invalid_index);
614 BOOST_TEST(message["error"].template get<int>() == rule_error::invalid_index);
615 BOOST_TEST(message["errorCategory"].template get<std::string>() == "rule");
616 }
617
618 BOOST_AUTO_TEST_CASE(invalid_action)
619 {
620 boost::system::error_code result;
621 nlohmann::json message;
622
623 ctl_->send({
624 { "command", "rule-edit" },
625 { "index", 0 },
626 { "action", "unknown" }
627 });
628 ctl_->recv([&] (auto rresult, auto rmessage) {
629 result = rresult;
630 message = rmessage;
631 });
632
633 wait_for([&] {
634 return result;
635 });
636
637 BOOST_TEST(result == rule_error::invalid_action);
638 BOOST_TEST(message["error"].template get<int>() == rule_error::invalid_action);
639 BOOST_TEST(message["errorCategory"].template get<std::string>() == "rule");
640 }
641
642 BOOST_AUTO_TEST_SUITE_END()
643
644 BOOST_AUTO_TEST_SUITE_END()
645
646 } // !irccd