mavtables  0.2.1
MAVLink router and firewall.
test_AddressPool.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 <algorithm>
19 #include <chrono>
20 
21 #include <catch.hpp>
22 #include <fake_clock.hh>
23 
24 #include <AddressPool.hpp>
25 #include <MAVAddress.hpp>
26 
27 #include "utility.hpp"
28 
29 
30 using namespace std::chrono_literals;
31 using namespace testing;
32 
33 
34 TEST_CASE("AddressPool's can be constructed.", "[AddressPool]")
35 {
36  REQUIRE_NOTHROW(AddressPool<>());
37  REQUIRE_NOTHROW(AddressPool<>(10s));
38 }
39 
40 
41 TEST_CASE("AddressPool's 'add' method adds an address to the pool.",
42  "[AddressPool]")
43 {
44  SECTION("With fake_clock.")
45  {
47  pool.add(MAVAddress("192.168"));
48  pool.add(MAVAddress("172.16"));
49  pool.add(MAVAddress("10.10"));
50  auto addr = pool.addresses();
51  std::sort(addr.begin(), addr.end(), std::greater<MAVAddress>());
52  REQUIRE(addr.size() == 3);
53  std::vector<MAVAddress> compare =
54  {
55  MAVAddress("192.168"),
56  MAVAddress("172.16"),
57  MAVAddress("10.10")
58  };
59  REQUIRE(addr == compare);
60  }
61  SECTION("With std::chrono::steady_clock.") // for complete coverage
62  {
64  pool.add(MAVAddress("192.168"));
65  pool.add(MAVAddress("172.16"));
66  pool.add(MAVAddress("10.10"));
67  auto addr = pool.addresses();
68  std::sort(addr.begin(), addr.end(), std::greater<MAVAddress>());
69  REQUIRE(addr.size() == 3);
70  std::vector<MAVAddress> compare =
71  {
72  MAVAddress("192.168"),
73  MAVAddress("172.16"),
74  MAVAddress("10.10")
75  };
76  REQUIRE(addr == compare);
77  }
78 }
79 
80 
81 TEST_CASE("AddressPool's 'contains' method determines whether an address is "
82  "in the pool or not.", "[AddressPool]")
83 {
84  SECTION("With fake_clock.")
85  {
87  pool.add(MAVAddress("192.168"));
88  pool.add(MAVAddress("172.16"));
89  pool.add(MAVAddress("10.10"));
90  REQUIRE(pool.contains(MAVAddress("192.168")));
91  REQUIRE(pool.contains(MAVAddress("172.16")));
92  REQUIRE(pool.contains(MAVAddress("10.10")));
93  REQUIRE_FALSE(pool.contains(MAVAddress("0.0")));
94  }
95  SECTION("With std::chrono::steady_clock.") // for complete coverage
96  {
98  pool.add(MAVAddress("192.168"));
99  pool.add(MAVAddress("172.16"));
100  pool.add(MAVAddress("10.10"));
101  REQUIRE(pool.contains(MAVAddress("192.168")));
102  REQUIRE(pool.contains(MAVAddress("172.16")));
103  REQUIRE(pool.contains(MAVAddress("10.10")));
104  REQUIRE_FALSE(pool.contains(MAVAddress("0.0")));
105  }
106 }
107 
108 
109 TEST_CASE("AddressPool removes expired addresses.", "[AddressPool]")
110 {
111  SECTION("When using the 'contains' method (and default timeout of 2 min).")
112  {
114  pool.add(MAVAddress("0.0"));
115  fake_clock::advance(1s); // 00:00:01
116  pool.add(MAVAddress("1.1"));
117  fake_clock::advance(1s); // 00:00:02
118  pool.add(MAVAddress("2.2"));
119  fake_clock::advance(1s); // 00:00:03
120  pool.add(MAVAddress("3.3"));
121  REQUIRE(pool.contains(MAVAddress("0.0")));
122  REQUIRE(pool.contains(MAVAddress("1.1")));
123  REQUIRE(pool.contains(MAVAddress("2.2")));
124  REQUIRE(pool.contains(MAVAddress("3.3")));
125  fake_clock::advance(117s); // 00:02:00
126  REQUIRE(pool.contains(MAVAddress("0.0")));
127  REQUIRE(pool.contains(MAVAddress("1.1")));
128  REQUIRE(pool.contains(MAVAddress("2.2")));
129  REQUIRE(pool.contains(MAVAddress("3.3")));
130  fake_clock::advance(1s); // 00:02:01
131  REQUIRE_FALSE(pool.contains(MAVAddress("0.0")));
132  REQUIRE(pool.contains(MAVAddress("1.1")));
133  REQUIRE(pool.contains(MAVAddress("2.2")));
134  REQUIRE(pool.contains(MAVAddress("3.3")));
135  fake_clock::advance(1s); // 00:02:02
136  REQUIRE_FALSE(pool.contains(MAVAddress("0.0")));
137  REQUIRE_FALSE(pool.contains(MAVAddress("1.1")));
138  REQUIRE(pool.contains(MAVAddress("2.2")));
139  REQUIRE(pool.contains(MAVAddress("3.3")));
140  fake_clock::advance(1s); // 00:02:03
141  REQUIRE_FALSE(pool.contains(MAVAddress("0.0")));
142  REQUIRE_FALSE(pool.contains(MAVAddress("1.1")));
143  REQUIRE_FALSE(pool.contains(MAVAddress("2.2")));
144  REQUIRE(pool.contains(MAVAddress("3.3")));
145  fake_clock::advance(1s); // 00:02:04
146  REQUIRE_FALSE(pool.contains(MAVAddress("0.0")));
147  REQUIRE_FALSE(pool.contains(MAVAddress("1.1")));
148  REQUIRE_FALSE(pool.contains(MAVAddress("2.2")));
149  REQUIRE_FALSE(pool.contains(MAVAddress("3.3")));
150  }
151  SECTION("When using the 'contains' method (and a custom timeout).")
152  {
153  AddressPool<fake_clock> pool(1h);
154  pool.add(MAVAddress("0.0"));
155  fake_clock::advance(1s); // 00:00:01
156  pool.add(MAVAddress("1.1"));
157  fake_clock::advance(1s); // 00:00:02
158  pool.add(MAVAddress("2.2"));
159  fake_clock::advance(1s); // 00:00:03
160  pool.add(MAVAddress("3.3"));
161  REQUIRE(pool.contains(MAVAddress("0.0")));
162  REQUIRE(pool.contains(MAVAddress("1.1")));
163  REQUIRE(pool.contains(MAVAddress("2.2")));
164  REQUIRE(pool.contains(MAVAddress("3.3")));
165  fake_clock::advance(3597s); // 01:00:00
166  REQUIRE(pool.contains(MAVAddress("0.0")));
167  REQUIRE(pool.contains(MAVAddress("1.1")));
168  REQUIRE(pool.contains(MAVAddress("2.2")));
169  REQUIRE(pool.contains(MAVAddress("3.3")));
170  fake_clock::advance(1s); // 01:00:01
171  REQUIRE_FALSE(pool.contains(MAVAddress("0.0")));
172  REQUIRE(pool.contains(MAVAddress("1.1")));
173  REQUIRE(pool.contains(MAVAddress("2.2")));
174  REQUIRE(pool.contains(MAVAddress("3.3")));
175  fake_clock::advance(1s); // 01:00:02
176  REQUIRE_FALSE(pool.contains(MAVAddress("0.0")));
177  REQUIRE_FALSE(pool.contains(MAVAddress("1.1")));
178  REQUIRE(pool.contains(MAVAddress("2.2")));
179  REQUIRE(pool.contains(MAVAddress("3.3")));
180  fake_clock::advance(1s); // 01:00:03
181  REQUIRE_FALSE(pool.contains(MAVAddress("0.0")));
182  REQUIRE_FALSE(pool.contains(MAVAddress("1.1")));
183  REQUIRE_FALSE(pool.contains(MAVAddress("2.2")));
184  REQUIRE(pool.contains(MAVAddress("3.3")));
185  fake_clock::advance(1s); // 01:00:04
186  REQUIRE_FALSE(pool.contains(MAVAddress("0.0")));
187  REQUIRE_FALSE(pool.contains(MAVAddress("1.1")));
188  REQUIRE_FALSE(pool.contains(MAVAddress("2.2")));
189  REQUIRE_FALSE(pool.contains(MAVAddress("3.3")));
190  }
191  SECTION("When using the 'addresses' method (and default timeout of 2 min).")
192  {
194  pool.add(MAVAddress("0.0"));
195  fake_clock::advance(1s); // 00:00:01
196  pool.add(MAVAddress("1.1"));
197  fake_clock::advance(1s); // 00:00:02
198  pool.add(MAVAddress("2.2"));
199  fake_clock::advance(1s); // 00:00:03
200  pool.add(MAVAddress("3.3"));
201  {
202  std::vector<MAVAddress> vec =
203  {
204  MAVAddress("0.0"),
205  MAVAddress("1.1"),
206  MAVAddress("2.2"),
207  MAVAddress("3.3"),
208  };
209  auto addr = pool.addresses();
210  std::sort(addr.begin(), addr.end());
211  REQUIRE(vec == addr);
212  }
213  fake_clock::advance(117s); // 00:02:00
214  {
215  std::vector<MAVAddress> vec =
216  {
217  MAVAddress("0.0"),
218  MAVAddress("1.1"),
219  MAVAddress("2.2"),
220  MAVAddress("3.3"),
221  };
222  auto addr = pool.addresses();
223  std::sort(addr.begin(), addr.end());
224  REQUIRE(vec == addr);
225  }
226  fake_clock::advance(1s); // 00:02:01
227  {
228  std::vector<MAVAddress> vec =
229  {
230  MAVAddress("1.1"),
231  MAVAddress("2.2"),
232  MAVAddress("3.3"),
233  };
234  auto addr = pool.addresses();
235  std::sort(addr.begin(), addr.end());
236  REQUIRE(vec == addr);
237  }
238  fake_clock::advance(1s); // 00:02:02
239  {
240  std::vector<MAVAddress> vec =
241  {
242  MAVAddress("2.2"),
243  MAVAddress("3.3"),
244  };
245  auto addr = pool.addresses();
246  std::sort(addr.begin(), addr.end());
247  REQUIRE(vec == addr);
248  }
249  fake_clock::advance(1s); // 00:02:03
250  {
251  std::vector<MAVAddress> vec =
252  {
253  MAVAddress("3.3"),
254  };
255  auto addr = pool.addresses();
256  std::sort(addr.begin(), addr.end());
257  REQUIRE(vec == addr);
258  }
259  fake_clock::advance(1s); // 00:02:04
260  {
261  std::vector<MAVAddress> vec;
262  REQUIRE(pool.addresses().empty());
263  }
264  }
265  SECTION("When using the 'addresses' method (and a custom timeout).")
266  {
267  AddressPool<fake_clock> pool(1h);
268  pool.add(MAVAddress("0.0"));
269  fake_clock::advance(1s); // 00:00:01
270  pool.add(MAVAddress("1.1"));
271  fake_clock::advance(1s); // 00:00:02
272  pool.add(MAVAddress("2.2"));
273  fake_clock::advance(1s); // 00:00:03
274  pool.add(MAVAddress("3.3"));
275  {
276  std::vector<MAVAddress> vec =
277  {
278  MAVAddress("0.0"),
279  MAVAddress("1.1"),
280  MAVAddress("2.2"),
281  MAVAddress("3.3"),
282  };
283  auto addr = pool.addresses();
284  std::sort(addr.begin(), addr.end());
285  REQUIRE(vec == addr);
286  }
287  fake_clock::advance(3597s); // 01:00:00
288  {
289  std::vector<MAVAddress> vec =
290  {
291  MAVAddress("0.0"),
292  MAVAddress("1.1"),
293  MAVAddress("2.2"),
294  MAVAddress("3.3"),
295  };
296  auto addr = pool.addresses();
297  std::sort(addr.begin(), addr.end());
298  REQUIRE(vec == addr);
299  }
300  fake_clock::advance(1s); // 01:00:01
301  {
302  std::vector<MAVAddress> vec =
303  {
304  MAVAddress("1.1"),
305  MAVAddress("2.2"),
306  MAVAddress("3.3"),
307  };
308  auto addr = pool.addresses();
309  std::sort(addr.begin(), addr.end());
310  REQUIRE(vec == addr);
311  }
312  fake_clock::advance(1s); // 01:00:02
313  {
314  std::vector<MAVAddress> vec =
315  {
316  MAVAddress("2.2"),
317  MAVAddress("3.3"),
318  };
319  auto addr = pool.addresses();
320  std::sort(addr.begin(), addr.end());
321  REQUIRE(vec == addr);
322  }
323  fake_clock::advance(1s); // 01:00:03
324  {
325  std::vector<MAVAddress> vec =
326  {
327  MAVAddress("3.3"),
328  };
329  auto addr = pool.addresses();
330  std::sort(addr.begin(), addr.end());
331  REQUIRE(vec == addr);
332  }
333  fake_clock::advance(1s); // 01:00:04
334  {
335  std::vector<MAVAddress> vec;
336  REQUIRE(pool.addresses().empty());
337  }
338  }
339 }
TEST_VIRTUAL void add(MAVAddress address)
Definition: AddressPool.hpp:77
TEST_CASE("AddressPool's can be constructed.", "[AddressPool]")
TEST_VIRTUAL bool contains(const MAVAddress &address)
TEST_VIRTUAL std::vector< MAVAddress > addresses()
Definition: AddressPool.hpp:94