comparison tests/src/libirccd/command-rule-info/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-info-command/main.cpp@22b3cd6f991f
children f69b1faf812f
comparison
equal deleted inserted replaced
610:22b3cd6f991f 611:9fbd1700435b
1 /*
2 * main.cpp -- test rule-info 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-info"
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_info_test : public command_test<rule_info_command> {
34 public:
35 rule_info_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 daemon_->rules().add(rule(
46 { "s1", },
47 { "c1", },
48 { "o1", },
49 { "p1", },
50 { "onMessage", },
51 rule::action_type::accept
52 ));
53 }
54 };
55
56 } // !namespace
57
58 BOOST_FIXTURE_TEST_SUITE(rule_info_test_suite, rule_info_test)
59
60 BOOST_AUTO_TEST_CASE(basic)
61 {
62 nlohmann::json result;
63
64 ctl_->send({
65 { "command", "rule-info" },
66 { "index", 0 }
67 });
68 ctl_->recv([&] (auto, auto msg) {
69 result = msg;
70 });
71
72 wait_for([&] () {
73 return result.is_object();
74 });
75
76 BOOST_TEST(result.is_object());
77
78 auto servers = result["servers"];
79 auto channels = result["channels"];
80 auto plugins = result["plugins"];
81 auto events = result["events"];
82
83 BOOST_TEST(json_util::contains(servers, "s1"));
84 BOOST_TEST(json_util::contains(servers, "s2"));
85 BOOST_TEST(json_util::contains(channels, "c1"));
86 BOOST_TEST(json_util::contains(channels, "c2"));
87 BOOST_TEST(json_util::contains(plugins, "p1"));
88 BOOST_TEST(json_util::contains(plugins, "p2"));
89 BOOST_TEST(json_util::contains(events, "onMessage"));
90 BOOST_TEST(json_util::contains(events, "onCommand"));
91 BOOST_TEST(result["action"].get<std::string>() == "drop");
92 }
93
94 BOOST_AUTO_TEST_SUITE(errors)
95
96 BOOST_AUTO_TEST_CASE(invalid_index_1)
97 {
98 boost::system::error_code result;
99 nlohmann::json message;
100
101 ctl_->send({
102 { "command", "rule-info" },
103 { "index", -100 }
104 });
105 ctl_->recv([&] (auto rresult, auto rmessage) {
106 result = rresult;
107 message = rmessage;
108 });
109
110 wait_for([&] {
111 return result;
112 });
113
114 BOOST_TEST(result == rule_error::invalid_index);
115 BOOST_TEST(message["error"].template get<int>() == rule_error::invalid_index);
116 BOOST_TEST(message["errorCategory"].template get<std::string>() == "rule");
117 }
118
119 BOOST_AUTO_TEST_CASE(invalid_index_2)
120 {
121 boost::system::error_code result;
122 nlohmann::json message;
123
124 ctl_->send({
125 { "command", "rule-info" },
126 { "index", 100 }
127 });
128 ctl_->recv([&] (auto rresult, auto rmessage) {
129 result = rresult;
130 message = rmessage;
131 });
132
133 wait_for([&] {
134 return result;
135 });
136
137 BOOST_TEST(result == rule_error::invalid_index);
138 BOOST_TEST(message["error"].template get<int>() == rule_error::invalid_index);
139 BOOST_TEST(message["errorCategory"].template get<std::string>() == "rule");
140 }
141
142 BOOST_AUTO_TEST_CASE(invalid_index_3)
143 {
144 boost::system::error_code result;
145 nlohmann::json message;
146
147 ctl_->send({
148 { "command", "rule-info" },
149 { "index", "notaint" }
150 });
151 ctl_->recv([&] (auto rresult, auto rmessage) {
152 result = rresult;
153 message = rmessage;
154 });
155
156 wait_for([&] {
157 return result;
158 });
159
160 BOOST_TEST(result == rule_error::invalid_index);
161 BOOST_TEST(message["error"].template get<int>() == rule_error::invalid_index);
162 BOOST_TEST(message["errorCategory"].template get<std::string>() == "rule");
163 }
164
165 BOOST_AUTO_TEST_SUITE_END()
166
167 BOOST_AUTO_TEST_SUITE_END()
168
169 } // !irccd