39 fakeit::Mock<Chain> mock;
41 SECTION(
"Without a condition (match all packet/address combinations) or a " 44 REQUIRE_NOTHROW(
GoTo(chain));
46 SECTION(
"Without a condition (match all packet/address combinations) but " 49 REQUIRE_NOTHROW(
GoTo(chain, 3));
51 SECTION(
"With a condition and without a priority.")
53 REQUIRE_NOTHROW(
GoTo(chain,
If()));
54 REQUIRE_NOTHROW(
GoTo(chain,
If().type(
"PING")));
55 REQUIRE_NOTHROW(
GoTo(chain,
If().from(
"192.168")));
56 REQUIRE_NOTHROW(
GoTo(chain,
If().to(
"172.16")));
58 SECTION(
"With both a condition and a priority.")
60 REQUIRE_NOTHROW(
GoTo(chain, 3,
If()));
61 REQUIRE_NOTHROW(
GoTo(chain, 3,
If().type(
"PING")));
62 REQUIRE_NOTHROW(
GoTo(chain, 3,
If().from(
"192.168")));
63 REQUIRE_NOTHROW(
GoTo(chain, 3,
If().to(
"172.16")));
65 SECTION(
"Ensures the chain's shared pointer is not null.")
67 REQUIRE_THROWS_AS(
GoTo(
nullptr), std::invalid_argument);
68 REQUIRE_THROWS_AS(
GoTo(
nullptr, 3), std::invalid_argument);
70 GoTo(
nullptr, 3,
If().type(
"PING")), std::invalid_argument);
71 REQUIRE_THROWS_WITH(
GoTo(
nullptr),
"Given chain pointer is null.");
72 REQUIRE_THROWS_WITH(
GoTo(
nullptr, 3),
"Given chain pointer is null.");
74 GoTo(
nullptr, 3,
If().type(
"PING")),
75 "Given chain pointer is null.");
82 fakeit::Mock<Chain> mock1;
83 fakeit::Mock<Chain> mock2;
88 REQUIRE(
GoTo(chain1) ==
GoTo(chain1));
90 GoTo(chain1,
If().type(
"PING")) ==
GoTo(chain1,
If().type(
"PING")));
91 REQUIRE(
GoTo(chain1, 3) ==
GoTo(chain1, 3));
93 GoTo(chain1, 3,
If().type(
"PING")) ==
94 GoTo(chain1, 3,
If().type(
"PING")));
95 REQUIRE_FALSE(
GoTo(chain1) ==
GoTo(chain2));
97 GoTo(chain1,
If().type(
"PING")) ==
98 GoTo(chain1,
If().type(
"SET_MODE")));
99 REQUIRE_FALSE(
GoTo(chain1,
If().type(
"PING")) ==
GoTo(chain1,
If()));
100 REQUIRE_FALSE(
GoTo(chain1,
If().type(
"PING")) ==
GoTo(chain1));
101 REQUIRE_FALSE(
GoTo(chain1, 3) ==
GoTo(chain1, -3));
102 REQUIRE_FALSE(
GoTo(chain1, 3) ==
GoTo(chain1));
106 REQUIRE(
GoTo(chain1,
If().type(
"PING")) !=
GoTo(chain1));
107 REQUIRE(
GoTo(chain1,
If().type(
"PING")) !=
GoTo(chain1,
If()));
109 GoTo(chain1,
If().type(
"PING")) !=
110 GoTo(chain1,
If().type(
"SET_MODE")));
111 REQUIRE(
GoTo(chain1, 3) !=
GoTo(chain1, -3));
112 REQUIRE(
GoTo(chain1, 3) !=
GoTo(chain1));
113 REQUIRE(
GoTo(chain1) !=
GoTo(chain2));
114 REQUIRE_FALSE(
GoTo(chain1) !=
GoTo(chain1));
116 GoTo(chain1,
If().type(
"PING")) !=
GoTo(chain1,
If().type(
"PING")));
117 REQUIRE_FALSE(
GoTo(chain1, 3) !=
GoTo(chain1, 3));
119 GoTo(chain1, 3,
If().type(
"PING")) !=
120 GoTo(chain1, 3,
If().type(
"PING")));
125 TEST_CASE(
"GoTo's 'action' method determines what to do with a " 126 "packet/address combination.",
"[GoTo]")
128 fakeit::Mock<Chain> accept_mock;
129 fakeit::When(Method(accept_mock, action)).AlwaysReturn(
131 std::shared_ptr<Chain> accept_chain =
mock_shared(accept_mock);
132 fakeit::Mock<Chain> reject_mock;
133 fakeit::When(Method(reject_mock, action)).AlwaysReturn(
135 std::shared_ptr<Chain> reject_chain =
mock_shared(reject_mock);
136 fakeit::Mock<Chain> continue_mock;
137 fakeit::When(Method(continue_mock, action)).AlwaysReturn(
139 std::shared_ptr<Chain> continue_chain =
mock_shared(continue_mock);
140 fakeit::Mock<Chain> default_mock;
141 fakeit::When(Method(default_mock, action)).AlwaysReturn(
143 std::shared_ptr<Chain> default_chain =
mock_shared(default_mock);
144 fakeit::Mock<Chain> accept10_mock;
145 fakeit::When(Method(accept10_mock, action)).AlwaysReturn(
147 std::shared_ptr<Chain> accept10_chain =
mock_shared(accept10_mock);
149 SECTION(
"Check call to chain's action method.")
151 REQUIRE(
GoTo(std::make_shared<TestChain>()).action(
153 fakeit::Mock<Chain> mock;
159 Method(mock, action).Matching([&](
auto & a,
auto & b)
164 SECTION(
"Delegates to the contained chain if there is no conditional.")
188 SECTION(
"Delegates to the contained chain if the conditional is a match.")
192 GoTo(accept_chain,
If().to(
"192.168")).action(
195 GoTo(reject_chain,
If().to(
"192.168")).action(
198 GoTo(continue_chain,
If().to(
"192.168")).action(
201 GoTo(default_chain,
If().to(
"192.168")).action(
205 GoTo(accept_chain, 3,
If().to(
"192.168")).action(
209 GoTo(accept10_chain, 3,
If().to(
"192.168")).action(
212 SECTION(
"Returns the continue action if the conditional does not match.")
216 GoTo(accept_chain,
If().to(
"172.16")).action(
220 GoTo(accept_chain, 3,
If().to(
"172.16")).action(
226 TEST_CASE(
"GoTo's are printable (without a condition or a priority).",
"[GoTo]")
228 auto chain = std::make_shared<TestChain>();
232 SECTION(
"By direct type.")
234 REQUIRE(
str(
goto_) ==
"goto test_chain");
236 SECTION(
"By polymorphic type.")
238 REQUIRE(
str(
rule) ==
"goto test_chain");
243 TEST_CASE(
"GoTo's are printable (without a condition but with a priority).",
246 auto chain = std::make_shared<TestChain>();
250 SECTION(
"By direct type.")
252 REQUIRE(
str(
goto_) ==
"goto test_chain with priority -3");
254 SECTION(
"By polymorphic type.")
256 REQUIRE(
str(
rule) ==
"goto test_chain with priority -3");
261 TEST_CASE(
"GoTo's are printable (with a condition but without a priority).",
264 auto chain = std::make_shared<TestChain>();
266 GoTo goto_(chain,
If().type(
"PING").from(
"192.168/8").to(
"172.16/4"));
268 SECTION(
"By direct type.")
271 str(
goto_) ==
"goto test_chain if PING from 192.168/8 to 172.16/4");
273 SECTION(
"By polymorphic type.")
276 str(
rule) ==
"goto test_chain if PING from 192.168/8 to 172.16/4");
281 TEST_CASE(
"GoTo's are printable (with a condition and a priority).",
"[GoTo]")
283 auto chain = std::make_shared<TestChain>();
285 GoTo goto_(chain, -3,
If().type(
"PING").from(
"192.168/8").to(
"172.16/4"));
287 SECTION(
"By direct type.")
291 "goto test_chain with priority -3 " 292 "if PING from 192.168/8 to 172.16/4");
294 SECTION(
"By polymorphic type.")
298 "goto test_chain with priority -3 " 299 "if PING from 192.168/8 to 172.16/4");
304 TEST_CASE(
"GoTo's 'clone' method returns a polymorphic copy.",
"[GoTo]")
306 auto chain = std::make_shared<TestChain>();
307 SECTION(
"Without a priority.")
311 std::unique_ptr<Rule> polymorphic_copy =
rule.
clone();
312 REQUIRE(
goto_ == *polymorphic_copy);
314 SECTION(
"With a priority.")
318 std::unique_ptr<Rule> polymorphic_copy =
rule.
clone();
319 REQUIRE(
goto_ == *polymorphic_copy);
static Action make_reject()
std::string str(const T &object)
TEST_CASE("GoTo's are constructable.", "[GoTo]")
static Action make_accept(std::optional< int > priority={})
virtual std::unique_ptr< Rule > clone() const =0
virtual Action action(const Packet &packet, const MAVAddress &address) const
static Action make_continue()
static Action make_default()
std::shared_ptr< T > mock_shared(fakeit::Mock< T > &mock)