comparison tests/rules/main.cpp @ 0:1158cffe5a5e

Initial import
author David Demelier <markand@malikania.fr>
date Mon, 08 Feb 2016 16:43:14 +0100
parents
children f8160d515a76
comparison
equal deleted inserted replaced
-1:000000000000 0:1158cffe5a5e
1 /*
2 * main.cpp -- test irccd rules
3 *
4 * Copyright (c) 2013-2016 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 #include <gtest/gtest.h>
20
21 #include <rule.h>
22
23 namespace irccd {
24
25 /*
26 * Simulate the following rules configuration:
27 *
28 * #
29 * # On all servers, each channel #staff can't use the onCommand event,
30 * # everything else is allowed.
31 * #
32 * [rule] #1
33 * servers = ""
34 * channels = "#staff"
35 * events = "onCommand"
36 * action = drop
37 *
38 * #
39 * # However, the same onCommand on #staff is allowed on server "unsafe"
40 * #
41 * [rule] #2
42 * servers = "unsafe"
43 * channels = "#staff"
44 * events = "onCommand"
45 * action = accept
46 *
47 * #
48 * # Plugin game is only allowed on server "malikania" and "localhost",
49 * # channel "#games" and events "onMessage, onCommand".
50 * #
51 * # The first rule #3-1 disable the plugin game for every server, it is
52 * # reenabled again with the #3-2.
53 * #
54 * [rule] #3-1
55 * plugins = "game"
56 * action = drop
57 *
58 * [rule] #3-2
59 * servers = "malikania localhost"
60 * channels = "#games"
61 * plugins = "game"
62 * events = "onMessage onCommand"
63 * action = accept
64 */
65 class RulesTest : public testing::Test {
66 protected:
67 std::vector<Rule> m_rules;
68
69 RulesTest()
70 {
71 // #1
72 {
73 m_rules.push_back({
74 RuleSet{ }, // Servers
75 RuleSet{ "#staff" }, // Channels
76 RuleSet{ }, // Origins
77 RuleSet{ }, // Plugins
78 RuleSet{ "onCommand" }, // Events
79 RuleAction::Drop
80 });
81 }
82
83 // #2
84 {
85 m_rules.push_back({
86 RuleSet{ "unsafe" },
87 RuleSet{ "#staff" },
88 RuleSet{ },
89 RuleSet{ },
90 RuleSet{ "onCommand" },
91 RuleAction::Accept
92 });
93 }
94
95 // #3-1
96 {
97 m_rules.push_back({
98 RuleSet{},
99 RuleSet{},
100 RuleSet{},
101 RuleSet{"game"},
102 RuleSet{},
103 RuleAction::Drop
104 });
105 }
106
107 // #3-2
108 {
109 m_rules.push_back({
110 RuleSet{ "malikania", "localhost" },
111 RuleSet{ "#games" },
112 RuleSet{ },
113 RuleSet{ "game" },
114 RuleSet{ "onCommand", "onMessage" },
115 RuleAction::Accept
116 });
117 }
118 }
119
120 ~RulesTest()
121 {
122 m_rules.clear();
123 }
124 };
125
126 TEST_F(RulesTest, basicMatch1)
127 {
128 Rule m;
129
130 /*
131 * [rule]
132 */
133 ASSERT_TRUE(m.match("freenode", "#test", "a", "", ""));
134 ASSERT_TRUE(m.match("", "", "", "", ""));
135 }
136
137 TEST_F(RulesTest, basicMatch2)
138 {
139 Rule m(RuleSet{"freenode"});
140
141 /*
142 * [rule]
143 * servers = "freenode"
144 */
145
146 ASSERT_TRUE(m.match("freenode", "#test", "a", "", ""));
147 ASSERT_FALSE(m.match("malikania", "#test", "a", "", ""));
148 ASSERT_TRUE(m.match("freenode", "", "jean", "", "onMessage"));
149 }
150
151 TEST_F(RulesTest, basicMatch3)
152 {
153 Rule m(RuleSet{"freenode"}, RuleSet{"#staff"});
154
155 /*
156 * [rule]
157 * servers = "freenode"
158 * channels = "#staff"
159 */
160
161 ASSERT_TRUE(m.match("freenode", "#staff", "a", "", ""));
162 ASSERT_FALSE(m.match("freenode", "#test", "a", "", ""));
163 ASSERT_FALSE(m.match("malikania", "#staff", "a", "", ""));
164 }
165
166 TEST_F(RulesTest, basicMatch4)
167 {
168 Rule m(RuleSet{"malikania"}, RuleSet{"#staff"}, RuleSet{"a"});
169
170 /*
171 * [rule]
172 * servers = "malikania"
173 * channels = "#staff"
174 * plugins = "a"
175 */
176
177 ASSERT_TRUE(m.match("malikania", "#staff", "a", "",""));
178 ASSERT_FALSE(m.match("malikania", "#staff", "b", "", ""));
179 ASSERT_FALSE(m.match("freenode", "#staff", "a", "", ""));
180 }
181
182 TEST_F(RulesTest, complexMatch1)
183 {
184 Rule m(RuleSet{"malikania", "freenode"});
185
186 /*
187 * [rule]
188 * servers = "malikania freenode"
189 */
190
191 ASSERT_TRUE(m.match("malikania", "", "", "", ""));
192 ASSERT_TRUE(m.match("freenode", "", "", "", ""));
193 ASSERT_FALSE(m.match("no", "", "", "", ""));
194 }
195
196 TEST_F(RulesTest, basicSolve)
197 {
198 /* Allowed */
199 ASSERT_TRUE(Rule::solve(m_rules, "malikania", "#staff", "", "a", "onMessage"));
200
201 /* Allowed */
202 ASSERT_TRUE(Rule::solve(m_rules, "freenode", "#staff", "", "b", "onTopic"));
203
204 /* Not allowed */
205 ASSERT_FALSE(Rule::solve(m_rules, "malikania", "#staff", "", "", "onCommand"));
206
207 /* Not allowed */
208 ASSERT_FALSE(Rule::solve(m_rules, "freenode", "#staff", "", "c", "onCommand"));
209
210 /* Allowed */
211 ASSERT_TRUE(Rule::solve(m_rules, "unsafe", "#staff", "", "c", "onCommand"));
212 }
213
214 TEST_F(RulesTest, gamesSolve)
215 {
216 /* Allowed */
217 ASSERT_TRUE(Rule::solve(m_rules, "malikania", "#games", "", "game", "onMessage"));
218
219 /* Allowed */
220 ASSERT_TRUE(Rule::solve(m_rules, "localhost", "#games", "", "game", "onMessage"));
221
222 /* Allowed */
223 ASSERT_TRUE(Rule::solve(m_rules, "malikania", "#games", "", "game", "onCommand"));
224
225 /* Not allowed */
226 ASSERT_FALSE(Rule::solve(m_rules, "malikania", "#games", "", "game", "onQuery"));
227
228 /* Not allowed */
229 ASSERT_FALSE(Rule::solve(m_rules, "freenode", "#no", "", "game", "onMessage"));
230
231 /* Not allowed */
232 ASSERT_FALSE(Rule::solve(m_rules, "malikania", "#test", "", "game", "onMessage"));
233 }
234
235 } // !irccd
236
237 int main(int argc, char **argv)
238 {
239 testing::InitGoogleTest(&argc, argv);
240
241 return RUN_ALL_TESTS();
242 }