mavtables  0.2.1
MAVLink router and firewall.
test_Filter.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 <utility>
19 
20 #include <catch.hpp>
21 #include <fakeit.hpp>
22 
23 #include "Accept.hpp"
24 #include "Call.hpp"
25 #include "Chain.hpp"
26 #include "Filter.hpp"
27 #include "GoTo.hpp"
28 #include "PacketVersion2.hpp"
29 #include "Reject.hpp"
30 
31 #include "common.hpp"
32 #include "common_Packet.hpp"
33 
34 
35 TEST_CASE("Filter's are constructable.", "[Filter]")
36 {
37  Chain chain("test_chain");
38  SECTION("Without default accept.")
39  {
40  REQUIRE_NOTHROW(Filter(chain));
41  }
42  SECTION("With default accept.")
43  {
44  REQUIRE_NOTHROW(Filter(chain, true));
45  }
46 }
47 
48 
49 TEST_CASE("Filter's are comparable.", "[Filter]")
50 {
51  Chain chain1("test_chain_1");
52  Chain chain2("test_chain_2");
53  SECTION("with ==")
54  {
55  REQUIRE(Filter(chain1) == Filter(chain1));
56  REQUIRE(Filter(chain2) == Filter(chain2));
57  // Reject is default.
58  REQUIRE(Filter(chain1, false) == Filter(chain1));
59  REQUIRE(Filter(chain1, true) == Filter(chain1, true));
60  REQUIRE(Filter(chain1, false) == Filter(chain1, false));
61  REQUIRE_FALSE(Filter(chain1) == Filter(chain2));
62  REQUIRE_FALSE(Filter(chain1, true) == Filter(chain1));
63  }
64  SECTION("with !=")
65  {
66  REQUIRE(Filter(chain1) != Filter(chain2));
67  REQUIRE(Filter(chain1, true) != Filter(chain1));
68  REQUIRE_FALSE(Filter(chain1) != Filter(chain1));
69  REQUIRE_FALSE(Filter(chain2) != Filter(chain2));
70  REQUIRE_FALSE(Filter(chain1, false) != Filter(chain1));
71  REQUIRE_FALSE(Filter(chain1, true) != Filter(chain1, true));
72  REQUIRE_FALSE(Filter(chain1, false) != Filter(chain1, false));
73  }
74 }
75 
76 
77 TEST_CASE("Filter's are copyable.", "[Filter]")
78 {
79  auto original = Filter(Chain("test_chain"));
80  auto copy(original);
81  REQUIRE(copy == Filter(Chain("test_chain")));
82 }
83 
84 
85 TEST_CASE("Filter's are movable.", "[Filter]")
86 {
87  auto original = Filter(Chain("test_chain"));
88  auto moved(std::move(original));
89  REQUIRE(moved == Filter(Chain("test_chain")));
90 }
91 
92 
93 TEST_CASE("Filter's are assignable.", "[Filter]")
94 {
95  auto filter_a = Filter(Chain("test_chain_a"));
96  auto filter_b = Filter(Chain("test_chain_b"));
97  REQUIRE(filter_a == Filter(Chain("test_chain_a")));
99  REQUIRE(filter_a == Filter(Chain("test_chain_b")));
100 }
101 
102 
103 TEST_CASE("Filter's are assignable (by move semantics).", "[Filter]")
104 {
105  auto filter_a = Filter(Chain("test_chain_a"));
106  auto filter_b = Filter(Chain("test_chain_b"));
107  REQUIRE(filter_a == Filter(Chain("test_chain_a")));
108  filter_a = std::move(filter_b);
109  REQUIRE(filter_a == Filter(Chain("test_chain_b")));
110 }
111 
112 
113 TEST_CASE("Filter's 'will_accept' method determines whether to accept or "
114  "reject a packet/address combination.", "[Filter]")
115 {
116  auto ping = packet_v2::Packet(to_vector(PingV2()));
117  SECTION("Accept packet, default priority.")
118  {
119  Chain chain("test_chain");
120  chain.append(std::make_unique<Accept>());
121  REQUIRE(
122  Filter(chain).will_accept(ping, MAVAddress("192.168")) ==
123  std::make_pair(true, 0));
124  }
125  SECTION("Accept packet, with priority.")
126  {
127  Chain chain("test_chain");
128  chain.append(std::make_unique<Accept>(3));
129  REQUIRE(
130  Filter(chain).will_accept(ping, MAVAddress("192.168")) ==
131  std::make_pair(true, 3));
132  }
133  SECTION("Reject packet.")
134  {
135  Chain chain("test_chain");
136  chain.append(std::make_unique<Reject>());
137  REQUIRE_FALSE(
138  Filter(chain).will_accept(ping, MAVAddress("192.168")).first);
139  }
140  SECTION("Default action.")
141  {
142  auto subchain = std::make_shared<Chain>("test_subchain");
143  Chain chain("test_chain");
144  chain.append(std::make_unique<GoTo>(subchain));
145  REQUIRE_FALSE(
146  Filter(chain).will_accept(ping, MAVAddress("192.168")).first);
147  REQUIRE(
148  Filter(chain, true).will_accept(ping, MAVAddress("192.168")) ==
149  std::make_pair(true, 0));
150  }
151  SECTION("Undecided action.")
152  {
153  auto subchain = std::make_shared<Chain>("test_subchain");
154  Chain chain("test_chain");
155  chain.append(std::make_unique<Call>(subchain));
156  REQUIRE_FALSE(
157  Filter(chain).will_accept(ping, MAVAddress("192.168")).first);
158  REQUIRE(
159  Filter(chain, true).will_accept(ping, MAVAddress("192.168")) ==
160  std::make_pair(true, 0));
161  }
162 }
TEST_CASE("Filter's are constructable.", "[Filter]")
Definition: test_Filter.cpp:35
Definition: Chain.hpp:37
void append(std::unique_ptr< Rule > rule)
Definition: Chain.cpp:116
auto ping
Definition: test_Call.cpp:229
auto filter_b
filter_a