mavtables  0.2.1
MAVLink router and firewall.
test_Rule.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 <ostream>
19 
20 #include <catch.hpp>
21 
22 #include "Action.hpp"
23 #include "If.hpp"
24 #include "MAVAddress.hpp"
25 #include "MAVSubnet.hpp"
26 #include "Packet.hpp"
27 #include "PacketVersion1.hpp"
28 #include "PacketVersion2.hpp"
29 #include "Rule.hpp"
30 #include "utility.hpp"
31 
32 #include "common_Packet.hpp"
33 
34 
35 namespace
36 {
37 
38  class RuleTestClass : public Rule
39  {
40  protected:
41  virtual std::ostream &print_(std::ostream &os) const
42  {
43  os << "test_rule";
44  return os;
45  }
46 
47  public:
48  virtual std::unique_ptr<Rule> clone() const
49  {
50  return std::make_unique<RuleTestClass>();
51  }
52  virtual Action action(
53  const Packet &packet, const MAVAddress &address) const
54  {
55  if (packet.name() == "PING")
56  {
57  if (MAVSubnet("192.0/14").contains(address))
58  {
59  return Action::make_accept();
60  }
61 
62  return Action::make_reject();
63  }
64 
65  return Action::make_continue();
66  }
67  virtual bool operator==(const Rule &other) const
68  {
69  (void)other;
70  return true;
71  }
72  virtual bool operator!=(const Rule &other) const
73  {
74  (void)other;
75  return false;
76  }
77  };
78 
79 }
80 
81 
82 TEST_CASE("Rule's can be constructed.", "[Rule]")
83 {
84  REQUIRE_NOTHROW(RuleTestClass());
85 }
86 
87 
88 TEST_CASE("Rule's are comparable.", "[Rule]")
89 {
90  REQUIRE(RuleTestClass() == RuleTestClass());
91  REQUIRE_FALSE(RuleTestClass() != RuleTestClass());
92 }
93 
94 
95 TEST_CASE("Rule's 'action' method determines what to do with a packet with "
96  "respect to a destination address.", "[Rule]")
97 {
98  auto ping = packet_v1::Packet(to_vector(PingV1()));
99  auto set_mode = packet_v2::Packet(to_vector(SetModeV2()));
100  RuleTestClass rule;
101  REQUIRE(rule.action(ping, MAVAddress("192.0")) == Action::make_accept());
102  REQUIRE(rule.action(ping, MAVAddress("192.1")) == Action::make_accept());
103  REQUIRE(rule.action(ping, MAVAddress("192.2")) == Action::make_accept());
104  REQUIRE(rule.action(ping, MAVAddress("192.3")) == Action::make_accept());
105  REQUIRE(rule.action(ping, MAVAddress("192.4")) == Action::make_reject());
106  REQUIRE(rule.action(ping, MAVAddress("192.5")) == Action::make_reject());
107  REQUIRE(rule.action(ping, MAVAddress("192.6")) == Action::make_reject());
108  REQUIRE(rule.action(ping, MAVAddress("192.7")) == Action::make_reject());
109  REQUIRE(
110  rule.action(set_mode, MAVAddress("192.0")) == Action::make_continue());
111  REQUIRE(
112  rule.action(set_mode, MAVAddress("192.1")) == Action::make_continue());
113  REQUIRE(
114  rule.action(set_mode, MAVAddress("192.2")) == Action::make_continue());
115  REQUIRE(
116  rule.action(set_mode, MAVAddress("192.3")) == Action::make_continue());
117  REQUIRE(
118  rule.action(set_mode, MAVAddress("192.4")) == Action::make_continue());
119  REQUIRE(
120  rule.action(set_mode, MAVAddress("192.5")) == Action::make_continue());
121  REQUIRE(
122  rule.action(set_mode, MAVAddress("192.6")) == Action::make_continue());
123  REQUIRE(
124  rule.action(set_mode, MAVAddress("192.7")) == Action::make_continue());
125 }
126 
127 
128 TEST_CASE("Rule's are printable.", "[Rule]")
129 {
130  auto ping = packet_v2::Packet(to_vector(PingV2()));
131  RuleTestClass rule;
132  Rule &polymorphic = rule;
133  SECTION("By direct type.")
134  {
135  REQUIRE(str(rule) == "test_rule");
136  }
137  SECTION("By polymorphic type.")
138  {
139  REQUIRE(str(polymorphic) == "test_rule");
140  }
141 }
142 
143 
144 TEST_CASE("Rule's 'clone' method returns a polymorphic copy.", "[Rule]")
145 {
146  RuleTestClass rule;
147  Rule &polymorphic = rule;
148  std::unique_ptr<Rule> polymorphic_copy = polymorphic.clone();
149  REQUIRE(rule == *polymorphic_copy);
150 }
static Action make_reject()
Definition: Action.cpp:113
virtual bool operator==(const Rule &other) const =0
std::string str(const T &object)
Definition: utility.hpp:128
virtual std::string name() const =0
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
Definition: Rule.hpp:38
virtual std::ostream & print_(std::ostream &os) const =0
TEST_CASE("Rule's can be constructed.", "[Rule]")
Definition: test_Rule.cpp:82
bool contains(const MAVAddress &address) const
Definition: MAVSubnet.cpp:250
auto ping
Definition: test_Call.cpp:229
Rule & rule
Definition: test_Call.cpp:231
virtual bool operator!=(const Rule &other) const =0
virtual Action action(const Packet &packet, const MAVAddress &address) const =0