mavtables  0.2.1
MAVLink router and firewall.
test_Reject.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 "Action.hpp"
21 #include "If.hpp"
22 #include "MAVAddress.hpp"
23 #include "Packet.hpp"
24 #include "PacketVersion1.hpp"
25 #include "PacketVersion2.hpp"
26 #include "Reject.hpp"
27 #include "Rule.hpp"
28 #include "utility.hpp"
29 
30 #include "common_Packet.hpp"
31 
32 
33 TEST_CASE("Reject's are constructable.", "[Reject]")
34 {
35  SECTION("Without a condition (match all packet/address combinations).")
36  {
37  REQUIRE_NOTHROW(Reject());
38  }
39  SECTION("With a condition.")
40  {
41  REQUIRE_NOTHROW(Reject(If()));
42  REQUIRE_NOTHROW(Reject(If().type("PING")));
43  REQUIRE_NOTHROW(Reject(If().from("192.168")));
44  REQUIRE_NOTHROW(Reject(If().to("172.16")));
45  }
46 }
47 
48 
49 TEST_CASE("Reject's are comparable.", "[Reject]")
50 {
51  SECTION("with ==")
52  {
53  REQUIRE(Reject() == Reject());
54  REQUIRE(Reject(If().type("PING")) == Reject(If().type("PING")));
55  REQUIRE_FALSE(
56  Reject(If().type("PING")) == Reject(If().type("SET_MODE")));
57  REQUIRE_FALSE(Reject(If().type("PING")) == Reject(If()));
58  REQUIRE_FALSE(Reject(If().type("PING")) == Reject());
59  }
60  SECTION("with !=")
61  {
62  REQUIRE(Reject(If().type("PING")) != Reject());
63  REQUIRE(Reject(If().type("PING")) != Reject(If()));
64  REQUIRE(Reject(If().type("PING")) != Reject(If().type("SET_MODE")));
65  REQUIRE_FALSE(Reject() != Reject());
66  REQUIRE_FALSE(Reject(If().type("PING")) != Reject(If().type("PING")));
67  }
68 }
69 
70 
71 TEST_CASE("Reject's 'action' method determines what to do with a "
72  "packet/address combination.", "[Reject]")
73 {
74  auto ping = packet_v2::Packet(to_vector(PingV2()));
75  SECTION("Returns the reject action if there is no conditional.")
76  {
77  REQUIRE(
78  Reject().action(ping, MAVAddress("192.168")) ==
80  }
81  SECTION("Returns the reject action if the conditional is a match.")
82  {
83  REQUIRE(
84  Reject(If().type("PING")).action(ping, MAVAddress("192.168")) ==
86  REQUIRE(
87  Reject(If().to("192.168")).action(ping, MAVAddress("192.168")) ==
89  }
90  SECTION("Returns the continue action if the conditional does not match.")
91  {
92  REQUIRE(
93  Reject(If().type("SET_MODE")).action(ping, MAVAddress("192.168")) ==
95  REQUIRE(
96  Reject(If().to("172.16")).action(ping, MAVAddress("192.168")) ==
98  }
99 }
100 
101 
102 TEST_CASE("Reject's are printable (without a condition).", "[Reject]")
103 {
104  auto ping = packet_v2::Packet(to_vector(PingV2()));
105  Reject reject;
107  SECTION("By direct type.")
108  {
109  REQUIRE(str(reject) == "reject");
110  }
111  SECTION("By polymorphic type.")
112  {
113  REQUIRE(str(rule) == "reject");
114  }
115 }
116 
117 
118 TEST_CASE("Reject's are printable (with a condition).", "[Reject]")
119 {
120  auto ping = packet_v2::Packet(to_vector(PingV2()));
121  Reject reject(If().type("PING").from("192.168/8").to("172.16/4"));
122  Rule &rule = reject;
123  SECTION("By direct type.")
124  {
125  REQUIRE(str(reject) == "reject if PING from 192.168/8 to 172.16/4");
126  }
127  SECTION("By polymorphic type.")
128  {
129  REQUIRE(str(rule) == "reject if PING from 192.168/8 to 172.16/4");
130  }
131 }
132 
133 
134 TEST_CASE("Reject's 'clone' method returns a polymorphic copy.", "[Reject]")
135 {
136  Reject reject(If().type("PING"));
137  Rule &rule = reject;
138  std::unique_ptr<Rule> polymorphic_copy = rule.clone();
139  REQUIRE(reject == *polymorphic_copy);
140 }
static Action make_reject()
Definition: Action.cpp:113
std::string str(const T &object)
Definition: utility.hpp:128
TEST_CASE("Reject's are constructable.", "[Reject]")
Definition: test_Reject.cpp:33
Definition: If.hpp:35
Reject reject
virtual std::unique_ptr< Rule > clone() const =0
static Action make_continue()
Definition: Action.cpp:126
Rule & rule
Definition: Rule.hpp:38
auto ping
Definition: test_Call.cpp:229