mavtables  0.2.1
MAVLink router and firewall.
test_Accept.cpp
Go to the documentation of this file.
1 // MAVLink router and firewall.
2 // Copyright (C) 2018 Michael R. Shannon <mrshannon.aerospace@gmail.com>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 
18 #include <catch.hpp>
19 
20 #include "Accept.hpp"
21 #include "Action.hpp"
22 #include "If.hpp"
23 #include "MAVAddress.hpp"
24 #include "Packet.hpp"
25 #include "PacketVersion1.hpp"
26 #include "PacketVersion2.hpp"
27 #include "Rule.hpp"
28 #include "utility.hpp"
29 
30 #include "common_Packet.hpp"
31 
32 
33 TEST_CASE("Accept's are constructable.", "[Accept]")
34 {
35  SECTION("Without a condition (match all packet/address combinations) or a "
36  "priority.")
37  {
38  REQUIRE_NOTHROW(Accept());
39  }
40  SECTION("Without a condition (match all packet/address combinations) but "
41  "with a priority.")
42  {
43  REQUIRE_NOTHROW(Accept(3));
44  }
45  SECTION("With a condition and without a priority.")
46  {
47  REQUIRE_NOTHROW(Accept(If()));
48  REQUIRE_NOTHROW(Accept(If().type("PING")));
49  REQUIRE_NOTHROW(Accept(If().from("192.168")));
50  REQUIRE_NOTHROW(Accept(If().to("172.16")));
51  }
52  SECTION("With both a condition and a priority.")
53  {
54  REQUIRE_NOTHROW(Accept(3, If()));
55  REQUIRE_NOTHROW(Accept(3, If().type("PING")));
56  REQUIRE_NOTHROW(Accept(3, If().from("192.168")));
57  REQUIRE_NOTHROW(Accept(3, If().to("172.16")));
58  }
59 }
60 
61 
62 TEST_CASE("Accept's are comparable.", "[Accept]")
63 {
64  SECTION("with ==")
65  {
66  REQUIRE(Accept() == Accept());
67  REQUIRE(Accept(If().type("PING")) == Accept(If().type("PING")));
68  REQUIRE(Accept(3) == Accept(3));
69  REQUIRE(Accept(3, If().type("PING")) == Accept(3, If().type("PING")));
70  REQUIRE_FALSE(
71  Accept(If().type("PING")) == Accept(If().type("SET_MODE")));
72  REQUIRE_FALSE(Accept(If().type("PING")) == Accept(If()));
73  REQUIRE_FALSE(Accept(If().type("PING")) == Accept());
74  REQUIRE_FALSE(Accept(3) == Accept(-3));
75  REQUIRE_FALSE(Accept(3) == Accept());
76  }
77  SECTION("with !=")
78  {
79  REQUIRE(Accept(If().type("PING")) != Accept());
80  REQUIRE(Accept(If().type("PING")) != Accept(If()));
81  REQUIRE(Accept(If().type("PING")) != Accept(If().type("SET_MODE")));
82  REQUIRE(Accept(3) != Accept(-3));
83  REQUIRE(Accept(3) != Accept());
84  REQUIRE_FALSE(Accept() != Accept());
85  REQUIRE_FALSE(Accept(If().type("PING")) != Accept(If().type("PING")));
86  REQUIRE_FALSE(Accept(3) != Accept(3));
87  REQUIRE_FALSE(
88  Accept(3, If().type("PING")) != Accept(3, If().type("PING")));
89  }
90 }
91 
92 
93 TEST_CASE("Accept's 'action' method determines what to do with a "
94  "packet/address combination.", "[Accept]")
95 {
96  auto ping = packet_v2::Packet(to_vector(PingV2()));
97  SECTION("Returns the accept action if there is no conditional.")
98  {
99  // Without priority.
100  REQUIRE(
101  Accept().action(ping, MAVAddress("192.168")) ==
103  // With priority.
104  REQUIRE(
105  Accept(3).action(ping, MAVAddress("192.168")) ==
107  }
108  SECTION("Returns the accept action if the conditional is a match.")
109  {
110  // Without priority.
111  REQUIRE(
112  Accept(If().type("PING")).action(
113  ping, MAVAddress("192.168")) == Action::make_accept());
114  REQUIRE(
115  Accept(If().to("192.168")).action(
116  ping, MAVAddress("192.168")) == Action::make_accept());
117  // With priority.
118  REQUIRE(
119  Accept(3, If().type("PING")).action(
120  ping, MAVAddress("192.168")) == Action::make_accept(3));
121  REQUIRE(
122  Accept(3, If().to("192.168")).action(
123  ping, MAVAddress("192.168")) == Action::make_accept(3));
124  }
125  SECTION("Returns the continue action if the conditional does not match.")
126  {
127  // Without priority.
128  REQUIRE(
129  Accept(If().type("SET_MODE")).action(
130  ping, MAVAddress("192.168")) == Action::make_continue());
131  REQUIRE(
132  Accept(If().to("172.16")).action(
133  ping, MAVAddress("192.168")) == Action::make_continue());
134  // With priority.
135  REQUIRE(
136  Accept(3, If().type("SET_MODE")).action(
137  ping, MAVAddress("192.168")) == Action::make_continue());
138  REQUIRE(
139  Accept(3, If().to("172.16")).action(
140  ping, MAVAddress("192.168")) == Action::make_continue());
141  }
142 }
143 
144 
145 TEST_CASE("Accept's are printable (without a condition or a priority).",
146  "[Accept]")
147 {
148  auto ping = packet_v2::Packet(to_vector(PingV2()));
149  Accept accept;
150  Rule &rule = accept;
151  SECTION("By direct type.")
152  {
153  REQUIRE(str(accept) == "accept");
154  }
155  SECTION("By polymorphic type.")
156  {
157  REQUIRE(str(rule) == "accept");
158  }
159 }
160 
161 
162 TEST_CASE("Accept's are printable (without a condition but with a priority).",
163  "[Accept]")
164 {
165  auto ping = packet_v2::Packet(to_vector(PingV2()));
166  Accept accept(-3);
167  Rule &rule = accept;
168  SECTION("By direct type.")
169  {
170  REQUIRE(str(accept) == "accept with priority -3");
171  }
172  SECTION("By polymorphic type.")
173  {
174  REQUIRE(str(rule) == "accept with priority -3");
175  }
176 }
177 
178 
179 TEST_CASE("Accept's are printable (with a condition but without a priority).",
180  "[Accept]")
181 {
182  auto ping = packet_v2::Packet(to_vector(PingV2()));
183  Accept accept(If().type("PING").from("192.168/8").to("172.16/4"));
184  Rule &rule = accept;
185  SECTION("By direct type.")
186  {
187  REQUIRE(str(accept) == "accept if PING from 192.168/8 to 172.16/4");
188  }
189  SECTION("By polymorphic type.")
190  {
191  REQUIRE(str(rule) == "accept if PING from 192.168/8 to 172.16/4");
192  }
193 }
194 
195 
196 TEST_CASE("Accept's are printable (with a condition and a priority).",
197  "[Accept]")
198 {
199  auto ping = packet_v2::Packet(to_vector(PingV2()));
200  Accept accept(-3, If().type("PING").from("192.168/8").to("172.16/4"));
201  Rule &rule = accept;
202  SECTION("By direct type.")
203  {
204  REQUIRE(
205  str(accept) ==
206  "accept with priority -3 if PING from 192.168/8 to 172.16/4");
207  }
208  SECTION("By polymorphic type.")
209  {
210  REQUIRE(
211  str(rule) ==
212  "accept with priority -3 if PING from 192.168/8 to 172.16/4");
213  }
214 }
215 
216 
217 TEST_CASE("Accept's 'clone' method returns a polymorphic copy.", "[Accept]")
218 {
219  SECTION("Without a priority.")
220  {
221  Accept accept(If().type("PING"));
222  Rule &rule = accept;
223  std::unique_ptr<Rule> polymorphic_copy = rule.clone();
224  REQUIRE(accept == *polymorphic_copy);
225  }
226  SECTION("With a priority.")
227  {
228  Accept accept(4, If().type("PING"));
229  Rule &rule = accept;
230  std::unique_ptr<Rule> polymorphic_copy = rule.clone();
231  REQUIRE(accept == *polymorphic_copy);
232  }
233 }
std::string str(const T &object)
Definition: utility.hpp:128
Definition: If.hpp:35
static Action make_accept(std::optional< int > priority={})
Definition: Action.cpp:100
virtual std::unique_ptr< Rule > clone() const =0
static Action make_continue()
Definition: Action.cpp:126
TEST_CASE("Accept's are constructable.", "[Accept]")
Definition: test_Accept.cpp:33
Definition: Rule.hpp:38
auto ping
Definition: test_Call.cpp:229
Rule & rule
Definition: test_Call.cpp:231