comparison tests/src/rule-edit-command/main.cpp @ 581:a51b5dd5b761

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