mavtables  0.2.1
MAVLink router and firewall.
test_Action.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 
22 #include "Action.hpp"
23 #include "utility.hpp"
24 
25 
26 TEST_CASE("Action's 'make_accept' factory method constructs an ACCEPT action.",
27  "[Action]")
28 {
29  SECTION("Without a priority.")
30  {
31  auto result = Action::make_accept();
32  REQUIRE(result.action() == Action::ACCEPT);
33  REQUIRE(result.priority() == 0);
34  }
35  SECTION("With a priority.")
36  {
37  auto result = Action::make_accept(-10);
38  REQUIRE(result.action() == Action::ACCEPT);
39  REQUIRE(result.priority() == -10);
40  }
41 }
42 
43 
44 TEST_CASE("Action's 'make_reject' factory method constructs a REJECT action.",
45  "[Action]")
46 {
47  auto result = Action::make_reject();
48  REQUIRE(result.action() == Action::REJECT);
49  REQUIRE(result.priority() == 0);
50 }
51 
52 
53 TEST_CASE("Action's 'make_continue' factory method constructs a CONTINUE "
54  "action.", "[Action]")
55 {
56  auto result = Action::make_continue();
57  REQUIRE(result.action() == Action::CONTINUE);
58  REQUIRE(result.priority() == 0);
59 }
60 
61 
62 TEST_CASE("Action's 'make_default' factory method constructs a DEFAULT action.",
63  "[Action]")
64 {
65  auto result = Action::make_default();
66  REQUIRE(result.action() == Action::DEFAULT);
67  REQUIRE(result.priority() == 0);
68 }
69 
70 
71 TEST_CASE("Action's 'priority' method sets and gets the priority.",
72  "[Action]")
73 {
74  SECTION("Can be set exactly once for accept results without a priority.")
75  {
76  auto result = Action::make_accept();
77  REQUIRE(result.priority() == 0);
78  result.priority(10);
79  REQUIRE(result.priority() == 10);
80  result.priority(100);
81  REQUIRE(result.priority() == 10);
82  }
83  SECTION("Cannot be set for accept results with a priority.")
84  {
85  auto result = Action::make_accept(10);
86  REQUIRE(result.priority() == 10);
87  result.priority(100);
88  REQUIRE(result.priority() == 10);
89  }
90  SECTION("Cannot be set on reject, continue, or default actions.")
91  {
92  auto reject = Action::make_reject();
93  reject.priority(10);
94  REQUIRE(reject.priority() == 0);
95  auto continue_ = Action::make_continue();
96  continue_.priority(10);
97  REQUIRE(continue_.priority() == 0);
98  auto default_ = Action::make_default();
99  default_.priority(10);
100  REQUIRE(default_.priority() == 0);
101  }
102 }
103 
104 
105 TEST_CASE("Action's are comparable.", "[Action]")
106 {
107  SECTION("with ==")
108  {
110  REQUIRE(Action::make_accept(10) == Action::make_accept(10));
111  REQUIRE_FALSE(Action::make_accept(1) == Action::make_accept());
112  REQUIRE_FALSE(Action::make_accept(1) == Action::make_accept(-1));
113  REQUIRE_FALSE(Action::make_accept() == Action::make_reject());
114  REQUIRE_FALSE(Action::make_accept() == Action::make_continue());
115  REQUIRE_FALSE(Action::make_accept() == Action::make_default());
116  }
117  SECTION("with !=")
118  {
119  REQUIRE(Action::make_accept(1) != Action::make_accept());
120  REQUIRE(Action::make_accept(1) != Action::make_accept(-1));
124  REQUIRE_FALSE(Action::make_accept() != Action::make_accept());
125  REQUIRE_FALSE(Action::make_accept(10) != Action::make_accept(10));
126  }
127 }
128 
129 
130 TEST_CASE("Action's are copyable.", "[Action]")
131 {
132  auto original = Action::make_accept(10);
133  auto copy(original);
134  REQUIRE(copy == Action::make_accept(10));
135 }
136 
137 
138 TEST_CASE("Action's are movable.", "[Action]")
139 {
140  auto original = Action::make_accept(10);
141  auto moved(std::move(original));
142  REQUIRE(moved == Action::make_accept(10));
143 }
144 
145 
146 TEST_CASE("Action's are assignable.", "[Action]")
147 {
148  auto action_a = Action::make_accept(-10);
149  auto action_b = Action::make_accept(100);
150  REQUIRE(action_a == Action::make_accept(-10));
151  action_a = action_b;
152  REQUIRE(action_a == Action::make_accept(100));
153 }
154 
155 
156 TEST_CASE("Action's are assignable (by move semantics).", "[Action]")
157 {
158  auto action_a = Action::make_accept(-10);
160  REQUIRE(action_a == Action::make_accept(-10));
161  action_a = std::move(action_b);
162  REQUIRE(action_a == Action::make_accept(100));
163 }
164 
165 
166 TEST_CASE("Action's are printable.")
167 {
168  REQUIRE(str(Action::make_accept()) == "accept");
169  REQUIRE(str(Action::make_accept(-10)) == "accept with priority -10");
170  REQUIRE(str(Action::make_accept(10)) == "accept with priority 10");
171  REQUIRE(str(Action::make_reject()) == "reject");
172  REQUIRE(str(Action::make_continue()) == "continue");
173  REQUIRE(str(Action::make_default()) == "default");
174 }
static Action make_reject()
Definition: Action.cpp:113
The packet has been accepted, possibly with priority.
Definition: Action.hpp:37
std::string str(const T &object)
Definition: utility.hpp:128
Continue evaluating rules.
Definition: Action.hpp:39
auto action_b
static Action make_accept(std::optional< int > priority={})
Definition: Action.cpp:100
Reject reject
static Action make_continue()
Definition: Action.cpp:126
action_a
static Action make_default()
Definition: Action.cpp:140
TEST_CASE("Action's 'make_accept' factory method constructs an ACCEPT action.", "[Action]")
Definition: test_Action.cpp:26
Use the default rule.
Definition: Action.hpp:40
The packet has been rejected.
Definition: Action.hpp:38