Mercurial > irccd
comparison tests/rules/main.cpp @ 489:349fe29d86d5
Tests: switch to Boost, closes #680
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 20 Aug 2017 08:16:39 +0200 |
parents | 7e273b7f4f92 |
children | a5e1c91abb8e |
comparison
equal
deleted
inserted
replaced
488:7e273b7f4f92 | 489:349fe29d86d5 |
---|---|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 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 | 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 */ | 17 */ |
18 | 18 |
19 #include <gtest/gtest.h> | 19 #define BOOST_TEST_MODULE "Rules" |
20 #include <boost/test/unit_test.hpp> | |
20 | 21 |
21 #include <irccd/logger.hpp> | 22 #include <irccd/logger.hpp> |
22 #include <irccd/rule.hpp> | 23 #include <irccd/rule.hpp> |
23 #include <irccd/service.hpp> | 24 #include <irccd/service.hpp> |
24 | 25 |
62 * channels = "#games" | 63 * channels = "#games" |
63 * plugins = "game" | 64 * plugins = "game" |
64 * events = "onMessage onCommand" | 65 * events = "onMessage onCommand" |
65 * action = accept | 66 * action = accept |
66 */ | 67 */ |
67 class RulesTest : public testing::Test { | 68 class rules_test { |
68 protected: | 69 protected: |
69 rule_service m_rules; | 70 rule_service rules_; |
70 | 71 |
71 RulesTest() | 72 rules_test() |
72 { | 73 { |
74 log::set_logger(std::make_unique<log::silent_logger>()); | |
75 | |
73 // #1 | 76 // #1 |
74 { | 77 { |
75 m_rules.add({ | 78 rules_.add({ |
76 rule::set{ }, // Servers | 79 rule::set{ }, // Servers |
77 rule::set{ "#staff" }, // Channels | 80 rule::set{ "#staff" }, // Channels |
78 rule::set{ }, // Origins | 81 rule::set{ }, // Origins |
79 rule::set{ }, // Plugins | 82 rule::set{ }, // Plugins |
80 rule::set{ "onCommand" }, // Events | 83 rule::set{ "onCommand" }, // Events |
82 }); | 85 }); |
83 } | 86 } |
84 | 87 |
85 // #2 | 88 // #2 |
86 { | 89 { |
87 m_rules.add({ | 90 rules_.add({ |
88 rule::set{ "unsafe" }, | 91 rule::set{ "unsafe" }, |
89 rule::set{ "#staff" }, | 92 rule::set{ "#staff" }, |
90 rule::set{ }, | 93 rule::set{ }, |
91 rule::set{ }, | 94 rule::set{ }, |
92 rule::set{ "onCommand" }, | 95 rule::set{ "onCommand" }, |
94 }); | 97 }); |
95 } | 98 } |
96 | 99 |
97 // #3-1 | 100 // #3-1 |
98 { | 101 { |
99 m_rules.add({ | 102 rules_.add({ |
100 rule::set{}, | 103 rule::set{}, |
101 rule::set{}, | 104 rule::set{}, |
102 rule::set{}, | 105 rule::set{}, |
103 rule::set{"game"}, | 106 rule::set{"game"}, |
104 rule::set{}, | 107 rule::set{}, |
106 }); | 109 }); |
107 } | 110 } |
108 | 111 |
109 // #3-2 | 112 // #3-2 |
110 { | 113 { |
111 m_rules.add({ | 114 rules_.add({ |
112 rule::set{ "malikania", "localhost" }, | 115 rule::set{ "malikania", "localhost" }, |
113 rule::set{ "#games" }, | 116 rule::set{ "#games" }, |
114 rule::set{ }, | 117 rule::set{ }, |
115 rule::set{ "game" }, | 118 rule::set{ "game" }, |
116 rule::set{ "onCommand", "onMessage" }, | 119 rule::set{ "onCommand", "onMessage" }, |
118 }); | 121 }); |
119 } | 122 } |
120 } | 123 } |
121 }; | 124 }; |
122 | 125 |
123 TEST_F(RulesTest, basicMatch1) | 126 BOOST_FIXTURE_TEST_SUITE(rules_test_suite, rules_test) |
127 | |
128 BOOST_AUTO_TEST_CASE(basic_match1) | |
124 { | 129 { |
125 rule m; | 130 rule m; |
126 | 131 |
127 /* | 132 /* |
128 * [rule] | 133 * [rule] |
129 */ | 134 */ |
130 ASSERT_TRUE(m.match("freenode", "#test", "a", "", "")); | 135 BOOST_REQUIRE(m.match("freenode", "#test", "a", "", "")); |
131 ASSERT_TRUE(m.match("", "", "", "", "")); | 136 BOOST_REQUIRE(m.match("", "", "", "", "")); |
132 } | 137 } |
133 | 138 |
134 TEST_F(RulesTest, basicMatch2) | 139 BOOST_AUTO_TEST_CASE(basic_match2) |
135 { | 140 { |
136 rule m(rule::set{"freenode"}); | 141 rule m(rule::set{"freenode"}); |
137 | 142 |
138 /* | 143 /* |
139 * [rule] | 144 * [rule] |
140 * servers = "freenode" | 145 * servers = "freenode" |
141 */ | 146 */ |
142 | 147 |
143 ASSERT_TRUE(m.match("freenode", "#test", "a", "", "")); | 148 BOOST_REQUIRE(m.match("freenode", "#test", "a", "", "")); |
144 ASSERT_FALSE(m.match("malikania", "#test", "a", "", "")); | 149 BOOST_REQUIRE(!m.match("malikania", "#test", "a", "", "")); |
145 ASSERT_TRUE(m.match("freenode", "", "jean", "", "onMessage")); | 150 BOOST_REQUIRE(m.match("freenode", "", "jean", "", "onMessage")); |
146 } | 151 } |
147 | 152 |
148 TEST_F(RulesTest, basicMatch3) | 153 BOOST_AUTO_TEST_CASE(basic_match3) |
149 { | 154 { |
150 rule m(rule::set{"freenode"}, rule::set{"#staff"}); | 155 rule m(rule::set{"freenode"}, rule::set{"#staff"}); |
151 | 156 |
152 /* | 157 /* |
153 * [rule] | 158 * [rule] |
154 * servers = "freenode" | 159 * servers = "freenode" |
155 * channels = "#staff" | 160 * channels = "#staff" |
156 */ | 161 */ |
157 | 162 |
158 ASSERT_TRUE(m.match("freenode", "#staff", "a", "", "")); | 163 BOOST_REQUIRE(m.match("freenode", "#staff", "a", "", "")); |
159 ASSERT_FALSE(m.match("freenode", "#test", "a", "", "")); | 164 BOOST_REQUIRE(!m.match("freenode", "#test", "a", "", "")); |
160 ASSERT_FALSE(m.match("malikania", "#staff", "a", "", "")); | 165 BOOST_REQUIRE(!m.match("malikania", "#staff", "a", "", "")); |
161 } | 166 } |
162 | 167 |
163 TEST_F(RulesTest, basicMatch4) | 168 BOOST_AUTO_TEST_CASE(basic_match4) |
164 { | 169 { |
165 rule m(rule::set{"malikania"}, rule::set{"#staff"}, rule::set{"a"}); | 170 rule m(rule::set{"malikania"}, rule::set{"#staff"}, rule::set{"a"}); |
166 | 171 |
167 /* | 172 /* |
168 * [rule] | 173 * [rule] |
169 * servers = "malikania" | 174 * servers = "malikania" |
170 * channels = "#staff" | 175 * channels = "#staff" |
171 * plugins = "a" | 176 * plugins = "a" |
172 */ | 177 */ |
173 | 178 |
174 ASSERT_TRUE(m.match("malikania", "#staff", "a", "","")); | 179 BOOST_REQUIRE(m.match("malikania", "#staff", "a", "","")); |
175 ASSERT_FALSE(m.match("malikania", "#staff", "b", "", "")); | 180 BOOST_REQUIRE(!m.match("malikania", "#staff", "b", "", "")); |
176 ASSERT_FALSE(m.match("freenode", "#staff", "a", "", "")); | 181 BOOST_REQUIRE(!m.match("freenode", "#staff", "a", "", "")); |
177 } | 182 } |
178 | 183 |
179 TEST_F(RulesTest, complexMatch1) | 184 BOOST_AUTO_TEST_CASE(complex_match1) |
180 { | 185 { |
181 rule m(rule::set{"malikania", "freenode"}); | 186 rule m(rule::set{"malikania", "freenode"}); |
182 | 187 |
183 /* | 188 /* |
184 * [rule] | 189 * [rule] |
185 * servers = "malikania freenode" | 190 * servers = "malikania freenode" |
186 */ | 191 */ |
187 | 192 |
188 ASSERT_TRUE(m.match("malikania", "", "", "", "")); | 193 BOOST_REQUIRE(m.match("malikania", "", "", "", "")); |
189 ASSERT_TRUE(m.match("freenode", "", "", "", "")); | 194 BOOST_REQUIRE(m.match("freenode", "", "", "", "")); |
190 ASSERT_FALSE(m.match("no", "", "", "", "")); | 195 BOOST_REQUIRE(!m.match("no", "", "", "", "")); |
191 } | 196 } |
192 | 197 |
193 TEST_F(RulesTest, basicSolve) | 198 BOOST_AUTO_TEST_CASE(basic_solve) |
194 { | 199 { |
195 /* Allowed */ | 200 /* Allowed */ |
196 ASSERT_TRUE(m_rules.solve("malikania", "#staff", "", "a", "onMessage")); | 201 BOOST_REQUIRE(rules_.solve("malikania", "#staff", "", "a", "onMessage")); |
197 | 202 |
198 /* Allowed */ | 203 /* Allowed */ |
199 ASSERT_TRUE(m_rules.solve("freenode", "#staff", "", "b", "onTopic")); | 204 BOOST_REQUIRE(rules_.solve("freenode", "#staff", "", "b", "onTopic")); |
200 | 205 |
201 /* Not allowed */ | 206 /* Not allowed */ |
202 ASSERT_FALSE(m_rules.solve("malikania", "#staff", "", "", "onCommand")); | 207 BOOST_REQUIRE(!rules_.solve("malikania", "#staff", "", "", "onCommand")); |
203 | 208 |
204 /* Not allowed */ | 209 /* Not allowed */ |
205 ASSERT_FALSE(m_rules.solve("freenode", "#staff", "", "c", "onCommand")); | 210 BOOST_REQUIRE(!rules_.solve("freenode", "#staff", "", "c", "onCommand")); |
206 | 211 |
207 /* Allowed */ | 212 /* Allowed */ |
208 ASSERT_TRUE(m_rules.solve("unsafe", "#staff", "", "c", "onCommand")); | 213 BOOST_REQUIRE(rules_.solve("unsafe", "#staff", "", "c", "onCommand")); |
209 } | 214 } |
210 | 215 |
211 TEST_F(RulesTest, gamesSolve) | 216 BOOST_AUTO_TEST_CASE(games_solve) |
212 { | 217 { |
213 /* Allowed */ | 218 /* Allowed */ |
214 ASSERT_TRUE(m_rules.solve("malikania", "#games", "", "game", "onMessage")); | 219 BOOST_REQUIRE(rules_.solve("malikania", "#games", "", "game", "onMessage")); |
215 | 220 |
216 /* Allowed */ | 221 /* Allowed */ |
217 ASSERT_TRUE(m_rules.solve("localhost", "#games", "", "game", "onMessage")); | 222 BOOST_REQUIRE(rules_.solve("localhost", "#games", "", "game", "onMessage")); |
218 | 223 |
219 /* Allowed */ | 224 /* Allowed */ |
220 ASSERT_TRUE(m_rules.solve("malikania", "#games", "", "game", "onCommand")); | 225 BOOST_REQUIRE(rules_.solve("malikania", "#games", "", "game", "onCommand")); |
221 | 226 |
222 /* Not allowed */ | 227 /* Not allowed */ |
223 ASSERT_FALSE(m_rules.solve("malikania", "#games", "", "game", "onQuery")); | 228 BOOST_REQUIRE(!rules_.solve("malikania", "#games", "", "game", "onQuery")); |
224 | 229 |
225 /* Not allowed */ | 230 /* Not allowed */ |
226 ASSERT_FALSE(m_rules.solve("freenode", "#no", "", "game", "onMessage")); | 231 BOOST_REQUIRE(!rules_.solve("freenode", "#no", "", "game", "onMessage")); |
227 | 232 |
228 /* Not allowed */ | 233 /* Not allowed */ |
229 ASSERT_FALSE(m_rules.solve("malikania", "#test", "", "game", "onMessage")); | 234 BOOST_REQUIRE(!rules_.solve("malikania", "#test", "", "game", "onMessage")); |
230 } | 235 } |
231 | 236 |
232 TEST_F(RulesTest, case_fix_645) | 237 BOOST_AUTO_TEST_CASE(fix_645) |
233 { | 238 { |
234 ASSERT_FALSE(m_rules.solve("MALIKANIA", "#STAFF", "", "SYSTEM", "onCommand")); | 239 BOOST_REQUIRE(!rules_.solve("MALIKANIA", "#STAFF", "", "SYSTEM", "onCommand")); |
235 } | 240 } |
241 | |
242 BOOST_AUTO_TEST_SUITE_END() | |
236 | 243 |
237 } // !irccd | 244 } // !irccd |
238 | |
239 int main(int argc, char **argv) | |
240 { | |
241 irccd::log::set_logger(std::make_unique<irccd::log::silent_logger>()); | |
242 testing::InitGoogleTest(&argc, argv); | |
243 | |
244 return RUN_ALL_TESTS(); | |
245 } |