mavtables  0.2.1
MAVLink router and firewall.
test_ConnectionPool.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 <memory>
19 
20 #include <catch.hpp>
21 #include <fakeit.hpp>
22 
23 #include "Connection.hpp"
24 #include "ConnectionPool.hpp"
25 #include "Logger.hpp"
26 #include "PacketVersion2.hpp"
27 #include "utility.hpp"
28 
29 #include "common.hpp"
30 #include "common_Packet.hpp"
31 
32 
33 TEST_CASE("ConnectionPool's can be constructed.", "[ConnectionPool]")
34 {
35  REQUIRE_NOTHROW(ConnectionPool());
36 }
37 
38 
39 TEST_CASE("ConnectionPool's can store at least one connection and send a "
40  "packet over it.", "[ConnectionPool]")
41 {
42  Logger::level(1);
43  auto packet = std::make_unique<packet_v2::Packet>(to_vector(PingV2()));
44  fakeit::Mock<Connection> mock;
45  fakeit::Fake(Method(mock, send));
46  std::shared_ptr<Connection> connection = mock_shared(mock);
47  fakeit::Mock<Filter> mock_filter;
48  auto filter = mock_shared(mock_filter);
49  auto source_connection = std::make_shared<Connection>("SOURCE", filter);
50  auto ping = std::make_unique<packet_v2::Packet>(to_vector(PingV2()));
51  ping->connection(source_connection);
52  ConnectionPool pool;
53  SECTION("with logging")
54  {
55  Logger::level(2);
56  MockCOut mock_cout;
57  pool.add(connection);
58  pool.send(std::move(ping));
59  fakeit::Verify(Method(mock, send).Matching([&](auto a)
60  {
61  return *a == *packet;
62  })).Once();
63  REQUIRE(
64  mock_cout.buffer().substr(21) ==
65  "received PING (#4) from 192.168 to 127.1 (v2.0) "
66  "source SOURCE\n");
67  }
68  SECTION("without logging")
69  {
70  MockCOut mock_cout;
71  pool.add(connection);
72  pool.send(std::move(ping));
73  fakeit::Verify(Method(mock, send).Matching([&](auto a)
74  {
75  return *a == *packet;
76  })).Once();
77  REQUIRE(mock_cout.buffer().empty());
78  }
79  Logger::level(0);
80 }
81 
82 
83 TEST_CASE("ConnectionPool's can store more than one connection and send a "
84  "packet over them.", "[ConnectionPool]")
85 {
86  Logger::level(1);
87  auto packet = std::make_unique<packet_v2::Packet>(to_vector(PingV2()));
88  fakeit::Mock<Connection> mock1;
89  fakeit::Mock<Connection> mock2;
90  fakeit::Fake(Method(mock1, send));
91  fakeit::Fake(Method(mock2, send));
92  std::shared_ptr<Connection> connection1 = mock_shared(mock1);
93  std::shared_ptr<Connection> connection2 = mock_shared(mock2);
94  fakeit::Mock<Filter> mock_filter;
95  auto filter = mock_shared(mock_filter);
96  auto source_connection = std::make_shared<Connection>("SOURCE", filter);
97  auto ping = std::make_unique<packet_v2::Packet>(to_vector(PingV2()));
98  ping->connection(source_connection);
99  ConnectionPool pool;
100  SECTION("with logging")
101  {
102  Logger::level(2);
103  MockCOut mock_cout;
104  pool.add(connection1);
105  pool.add(connection2);
106  pool.send(std::move(ping));
107  fakeit::Verify(Method(mock1, send).Matching([&](auto a)
108  {
109  return *a == *packet;
110  })).Once();
111  fakeit::Verify(Method(mock2, send).Matching([&](auto a)
112  {
113  return *a == *packet;
114  })).Once();
115  REQUIRE(
116  mock_cout.buffer().substr(21) ==
117  "received PING (#4) from 192.168 to 127.1 (v2.0) "
118  "source SOURCE\n");
119  }
120  SECTION("without logging")
121  {
122  MockCOut mock_cout;
123  pool.add(connection1);
124  pool.add(connection2);
125  pool.send(std::move(ping));
126  fakeit::Verify(Method(mock1, send).Matching([&](auto a)
127  {
128  return *a == *packet;
129  })).Once();
130  fakeit::Verify(Method(mock2, send).Matching([&](auto a)
131  {
132  return *a == *packet;
133  })).Once();
134  REQUIRE(mock_cout.buffer().empty());
135  }
136  Logger::level(0);
137 }
138 
139 
140 TEST_CASE("ConnectionPool's 'remove' method removes a connection.",
141  "[ConnectionPool]")
142 {
143  auto packet = std::make_unique<packet_v2::Packet>(to_vector(PingV2()));
144  fakeit::Mock<Connection> mock1;
145  fakeit::Mock<Connection> mock2;
146  fakeit::Fake(Method(mock1, send));
147  fakeit::Fake(Method(mock2, send));
148  std::shared_ptr<Connection> connection1 = mock_shared(mock1);
149  std::shared_ptr<Connection> connection2 = mock_shared(mock2);
150  ConnectionPool pool;
151  pool.add(connection1);
152  pool.add(connection2);
153  pool.send(std::make_unique<packet_v2::Packet>(to_vector(PingV2())));
154  pool.remove(connection1);
155  pool.send(std::make_unique<packet_v2::Packet>(to_vector(PingV2())));
156  fakeit::Verify(Method(mock1, send).Matching([&](auto a)
157  {
158  return *a == *packet;
159  })).Once();
160  fakeit::Verify(Method(mock2, send).Matching([&](auto a)
161  {
162  return *a == *packet;
163  })).Exactly(2);
164 }
165 
166 
167 TEST_CASE("ConnectionPool's 'send' method removes connections that have "
168  "expired.", "[ConnectionPool]")
169 {
170  auto packet = std::make_unique<packet_v2::Packet>(to_vector(PingV2()));
171  fakeit::Mock<Connection> mock1;
172  fakeit::Mock<Connection> mock2;
173  fakeit::Fake(Method(mock1, send));
174  fakeit::Fake(Method(mock2, send));
175  std::shared_ptr<Connection> connection1 = mock_shared(mock1);
176  std::shared_ptr<Connection> connection2 = mock_shared(mock2);
177  ConnectionPool pool;
178  pool.add(connection1);
179  pool.add(connection2);
180  pool.send(std::make_unique<packet_v2::Packet>(to_vector(PingV2())));
181  connection1.reset();
182  pool.send(std::make_unique<packet_v2::Packet>(to_vector(PingV2())));
183  fakeit::Verify(Method(mock1, send).Matching([&](auto a)
184  {
185  return *a == *packet;
186  })).Once();
187  fakeit::Verify(Method(mock2, send).Matching([&](auto a)
188  {
189  return *a == *packet;
190  })).Exactly(2);
191 }
TEST_VIRTUAL void send(std::unique_ptr< const Packet > packet)
TEST_CASE("ConnectionPool's can be constructed.", "[ConnectionPool]")
static unsigned int level()
Definition: Logger.cpp:96
std::string buffer()
Definition: common.hpp:75
TEST_VIRTUAL void remove(const std::weak_ptr< Connection > &connection)
std::shared_ptr< T > mock_shared(fakeit::Mock< T > &mock)
Definition: common.hpp:33
auto ping
Definition: test_Call.cpp:229
TEST_VIRTUAL void add(std::weak_ptr< Connection > connection)