49 TEST_CASE(
"Connection's can be constructed.",
"[Connection]")
51 fakeit::Mock<Filter> mock_filter;
52 fakeit::Mock<AddressPool<>> mock_pool;
53 fakeit::Mock<PacketQueue> mock_queue;
57 SECTION(
"With default arguments.")
61 SECTION(
"As a regular connection.")
65 "name", filter,
false, std::move(pool), std::move(queue)));
67 SECTION(
"As a mirror connection.")
71 "name", filter,
true, std::move(pool), std::move(queue)));
73 SECTION(
"Ensures the filter pointer is not null.")
77 "name",
nullptr,
false, std::move(pool), std::move(queue)),
78 std::invalid_argument);
83 "name",
nullptr,
false, std::move(pool), std::move(queue)),
84 "Given filter pointer is null.");
86 SECTION(
"Ensures the pool pointer is not null.")
89 Connection(
"name", filter,
false,
nullptr, std::move(queue)),
90 std::invalid_argument);
93 Connection(
"name", filter,
false,
nullptr, std::move(queue)),
94 "Given pool pointer is null.");
96 SECTION(
"Ensures the queue pointer is not null.")
99 Connection(
"name", filter,
false, std::move(pool),
nullptr),
100 std::invalid_argument);
103 Connection(
"name", filter,
false, std::move(pool),
nullptr),
104 "Given queue pointer is null.");
111 fakeit::Mock<Filter> mock_filter;
113 REQUIRE(
str(
Connection(
"some_name", filter)) ==
"some_name");
114 REQUIRE(
str(
Connection(
"/dev/ttyUSB0", filter)) ==
"/dev/ttyUSB0");
115 REQUIRE(
str(
Connection(
"127.0.0.1:14555", filter)) ==
"127.0.0.1:14555");
119 TEST_CASE(
"Connection's 'add_address' method adds/updates addresses.",
123 fakeit::Mock<Filter> mock_filter;
124 fakeit::Mock<AddressPool<>> mock_pool;
125 fakeit::Mock<PacketQueue> mock_queue;
126 fakeit::Fake(Method(mock_pool, add));
130 Connection conn(
"DEVICE", filter,
false, std::move(pool), std::move(queue));
131 SECTION(
"does not contain address (with logging)")
135 fakeit::When(Method(mock_pool, contains)).AlwaysReturn(
false);
137 fakeit::Verify(Method(mock_pool, add).Matching([](
auto a)
142 mock_cout.
buffer().substr(21) ==
143 "new component 192.168 on DEVICE\n");
145 SECTION(
"does not contain address (without logging)")
148 fakeit::When(Method(mock_pool, contains)).AlwaysReturn(
false);
150 fakeit::Verify(Method(mock_pool, add).Matching([](
auto a)
154 REQUIRE(mock_cout.
buffer().empty());
156 SECTION(
"already contains address (with logging)")
160 fakeit::When(Method(mock_pool, contains)).AlwaysReturn(
true);
162 fakeit::Verify(Method(mock_pool, add).Matching([](
auto a)
166 REQUIRE(mock_cout.
buffer().empty());
168 SECTION(
"already contains address (without logging)")
171 fakeit::When(Method(mock_pool, contains)).AlwaysReturn(
true);
173 fakeit::Verify(Method(mock_pool, add).Matching([](
auto a)
177 REQUIRE(mock_cout.
buffer().empty());
183 TEST_CASE(
"Connection's 'next_packet' method.",
"[Connection]")
185 auto ping = std::make_shared<packet_v2::Packet>(to_vector(PingV2()));
186 fakeit::Mock<Filter> mock_filter;
187 fakeit::Mock<AddressPool<>> mock_pool;
188 fakeit::Mock<PacketQueue> mock_queue;
192 Connection conn(
"name", filter,
false, std::move(pool), std::move(queue));
193 SECTION(
"Returns the next packet.")
198 std::shared_ptr<const Packet>(
199 const std::chrono::nanoseconds &))).Return(
ping);
200 std::chrono::nanoseconds timeout = 1ms;
202 REQUIRE(packet !=
nullptr);
203 REQUIRE(*packet == *
ping);
207 std::shared_ptr<const Packet>(
208 const std::chrono::nanoseconds &)).Matching([](
auto a)
213 SECTION(
"Or times out and returns nullptr.")
218 std::shared_ptr<const Packet>(
219 const std::chrono::nanoseconds &))).Return(
nullptr);
220 std::chrono::nanoseconds timeout = 0ms;
225 std::shared_ptr<const Packet>(
226 const std::chrono::nanoseconds &)).Matching([](
auto a)
234 TEST_CASE(
"Connection's 'send' method ensures the given packet is not " 235 "nullptr.",
"[Connection]")
237 fakeit::Mock<Filter> mock_filter;
238 fakeit::Mock<AddressPool<>> mock_pool;
239 fakeit::Mock<PacketQueue> mock_queue;
243 Connection conn(
"name", filter,
false, std::move(pool), std::move(queue));
244 SECTION(
"Ensures the given packet is not nullptr.")
246 REQUIRE_THROWS_AS(conn.
send(
nullptr), std::invalid_argument);
248 conn.
send(
nullptr),
"Given packet pointer is null.");
253 TEST_CASE(
"Connection's 'send' method (with destination address).",
258 fakeit::Mock<Filter> mock_filter;
259 fakeit::Mock<AddressPool<>> mock_pool;
260 fakeit::Mock<PacketQueue> mock_queue;
261 fakeit::Fake(Method(mock_queue, push));
266 auto source_connection = std::make_shared<Connection>(
"SOURCE", filter);
267 auto ping = std::make_shared<packet_v2::Packet>(to_vector(PingV2()));
268 ping->connection(source_connection);
270 Connection conn(
"DEST", filter,
false, std::move(pool), std::move(queue));
271 SECTION(
"Adds the packet to the PacketQueue if the destination can be " 272 "reached on this connection and the filter allows it " 277 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
278 [&](
auto & a,
auto & b)
282 return std::pair<bool, int>(
true, 2);
284 fakeit::When(Method(mock_pool, contains)).AlwaysDo([&](
auto & a)
289 fakeit::Verify(Method(mock_queue, push)).Once();
290 fakeit::Verify(Method(mock_queue, push).Matching([&](
auto a,
auto b)
292 return a !=
nullptr && *a == *
ping && b == 2;
295 mock_cout.
buffer().substr(21) ==
296 "accepted PING (#4) from 192.168 to 127.1 (v2.0) " 297 "source SOURCE dest DEST\n");
299 SECTION(
"Adds the packet to the PacketQueue if the destination can be " 300 "reached on this connection and the filter allows it " 301 "(without logging).")
304 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
305 [&](
auto & a,
auto & b)
309 return std::pair<bool, int>(
true, 2);
311 fakeit::When(Method(mock_pool, contains)).AlwaysDo([&](
auto & a)
316 fakeit::Verify(Method(mock_queue, push)).Once();
317 fakeit::Verify(Method(mock_queue, push).Matching([&](
auto a,
auto b)
319 return a !=
nullptr && *a == *
ping && b == 2;
321 REQUIRE(mock_cout.
buffer().empty());
323 SECTION(
"Silently drops the packet if the filter rejects it " 328 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
329 [&](
auto & a,
auto & b)
333 return std::pair<bool, int>(
false, 0);
335 fakeit::When(Method(mock_pool, contains)).AlwaysDo([&](
auto & a)
340 fakeit::Verify(Method(mock_queue, push)).Exactly(0);
342 mock_cout.
buffer().substr(21) ==
343 "rejected PING (#4) from 192.168 to 127.1 (v2.0) " 344 "source SOURCE dest DEST\n");
346 SECTION(
"Silently drops the packet if the filter rejects it " 347 "(without logging).")
350 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
351 [&](
auto & a,
auto & b)
355 return std::pair<bool, int>(
false, 0);
357 fakeit::When(Method(mock_pool, contains)).AlwaysDo([&](
auto & a)
362 fakeit::Verify(Method(mock_queue, push)).Exactly(0);
363 REQUIRE(mock_cout.
buffer().empty());
365 SECTION(
"Silently drops the packet if the destination cannot be " 366 "reached on this connection (with logging).")
370 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
371 [&](
auto & a,
auto & b)
375 return std::pair<bool, int>(
true, 0);
377 fakeit::When(Method(mock_pool, addresses)).AlwaysReturn(
378 std::vector<MAVAddress>());
379 fakeit::When(Method(mock_pool, contains)).AlwaysDo([&](
auto & a)
385 fakeit::Verify(Method(mock_queue, push)).Exactly(0);
386 REQUIRE(mock_cout.
buffer().empty());
388 SECTION(
"Silently drops the packet if the destination cannot be " 389 "reached on this connection (without logging).")
392 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
393 [&](
auto & a,
auto & b)
397 return std::pair<bool, int>(
true, 0);
399 fakeit::When(Method(mock_pool, addresses)).AlwaysReturn(
400 std::vector<MAVAddress>());
401 fakeit::When(Method(mock_pool, contains)).AlwaysDo([&](
auto & a)
407 fakeit::Verify(Method(mock_queue, push)).Exactly(0);
408 REQUIRE(mock_cout.
buffer().empty());
414 TEST_CASE(
"Connection's 'send' method (without destination address).",
419 fakeit::Mock<Filter> mock_filter;
420 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
421 [&](
auto & a,
auto & b)
427 return std::pair<bool, int>(
true, 2);
432 return std::pair<bool, int>(
true, -3);
435 return std::pair<bool, int>(
false, 0);
437 fakeit::Mock<AddressPool<>> mock_pool;
438 fakeit::When(Method(mock_pool, contains)).AlwaysDo([](
MAVAddress addr)
457 fakeit::Mock<PacketQueue> mock_queue;
458 fakeit::Fake(Method(mock_queue, push));
463 auto source_connection = std::make_shared<Connection>(
"SOURCE", filter);
465 std::make_shared<packet_v2::Packet>(to_vector(HeartbeatV2()));
466 heartbeat->connection(source_connection);
468 Connection conn(
"DEST", filter,
false, std::move(pool), std::move(queue));
469 SECTION(
"Adds the packet to the PacketQueue if the filter allows it for " 470 "any of the reachable addresses and favors the higher priority " 471 "(increasing priority) (with logging).")
475 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
477 std::vector<MAVAddress> addr =
486 fakeit::Verify(Method(mock_queue, push)).Once();
487 fakeit::Verify(Method(mock_queue, push).Matching([&](
auto a,
auto b)
489 return a !=
nullptr && *a == *
heartbeat && b == 2;
492 mock_cout.
buffer().substr(21) ==
493 "accepted HEARTBEAT (#0) from 127.1 (v2.0) " 494 "source SOURCE dest DEST\n");
496 SECTION(
"Adds the packet to the PacketQueue if the filter allows it for " 497 "any of the reachable addresses and favors the higher priority " 498 "(increasing priority) (without logging).")
501 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
503 std::vector<MAVAddress> addr =
512 fakeit::Verify(Method(mock_queue, push)).Once();
513 fakeit::Verify(Method(mock_queue, push).Matching([&](
auto a,
auto b)
515 return a !=
nullptr && *a == *
heartbeat && b == 2;
517 REQUIRE(mock_cout.
buffer().empty());
519 SECTION(
"Adds the packet to the PacketQueue if the filter allows it for " 520 "any of the reachable addresses and favors the higher priority " 521 "(decreasing priority) (with logging).")
525 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
527 std::vector<MAVAddress> addr =
536 fakeit::Verify(Method(mock_queue, push)).Once();
537 fakeit::Verify(Method(mock_queue, push).Matching([&](
auto a,
auto b)
539 return a !=
nullptr && *a == *
heartbeat && b == 2;
542 mock_cout.
buffer().substr(21) ==
543 "accepted HEARTBEAT (#0) from 127.1 (v2.0) " 544 "source SOURCE dest DEST\n");
546 SECTION(
"Adds the packet to the PacketQueue if the filter allows it for " 547 "any of the reachable addresses and favors the higher priority " 548 "(decreasing priority) (without logging).")
551 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
553 std::vector<MAVAddress> addr =
562 fakeit::Verify(Method(mock_queue, push)).Once();
563 fakeit::Verify(Method(mock_queue, push).Matching([&](
auto a,
auto b)
565 return a !=
nullptr && *a == *
heartbeat && b == 2;
567 REQUIRE(mock_cout.
buffer().empty());
569 SECTION(
"Silently drops the packet if the filter rejects it for all " 570 "reachable addresses (with logging).")
574 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
575 [&](
auto & a,
auto & b)
579 return std::pair<bool, int>(
false, 0);
581 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
583 std::vector<MAVAddress> addr =
592 fakeit::Verify(Method(mock_queue, push)).Exactly(0);
594 mock_cout.
buffer().substr(21) ==
595 "rejected HEARTBEAT (#0) from 127.1 (v2.0) " 596 "source SOURCE dest DEST\n");
598 SECTION(
"Silently drops the packet if the filter rejects it for all " 599 "reachable addresses (without logging).")
602 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
603 [&](
auto & a,
auto & b)
607 return std::pair<bool, int>(
false, 0);
609 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
611 std::vector<MAVAddress> addr =
620 fakeit::Verify(Method(mock_queue, push)).Exactly(0);
621 REQUIRE(mock_cout.
buffer().empty());
627 TEST_CASE(
"Connection's 'send' method (with broadcast address 0.0).",
632 fakeit::Mock<Filter> mock_filter;
633 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
634 [&](
auto & a,
auto & b)
640 return std::pair<bool, int>(
true, 2);
645 return std::pair<bool, int>(
true, -3);
648 return std::pair<bool, int>(
false, 0);
650 fakeit::Mock<AddressPool<>> mock_pool;
651 fakeit::When(Method(mock_pool, contains)).AlwaysDo([](
MAVAddress addr)
670 fakeit::Mock<PacketQueue> mock_queue;
671 fakeit::Fake(Method(mock_queue, push));
676 auto source_connection = std::make_shared<Connection>(
"SOURCE", filter);
677 auto mission_set_current =
678 std::make_shared<packet_v2::Packet>(to_vector(MissionSetCurrentV2()));
679 mission_set_current->connection(source_connection);
681 Connection conn(
"DEST", filter,
false, std::move(pool), std::move(queue));
682 SECTION(
"Adds the packet to the PacketQueue if the filter allows it for " 683 "any of the reachable addresses and favors the higher priority " 684 "(increasing priority) (with logging).")
688 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
690 std::vector<MAVAddress> addr =
698 conn.
send(mission_set_current);
699 fakeit::Verify(Method(mock_queue, push)).Once();
700 fakeit::Verify(Method(mock_queue, push).Matching([&](
auto a,
auto b)
702 return a !=
nullptr && *a == *mission_set_current && b == 2;
705 mock_cout.
buffer().substr(21) ==
706 "accepted MISSION_SET_CURRENT (#41) from 255.0 to 0.0 (v2.0) " 707 "source SOURCE dest DEST\n");
709 SECTION(
"Adds the packet to the PacketQueue if the filter allows it for " 710 "any of the reachable addresses and favors the higher priority " 711 "(increasing priority) (without logging).")
714 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
716 std::vector<MAVAddress> addr =
724 conn.
send(mission_set_current);
725 fakeit::Verify(Method(mock_queue, push)).Once();
726 fakeit::Verify(Method(mock_queue, push).Matching([&](
auto a,
auto b)
728 return a !=
nullptr && *a == *mission_set_current && b == 2;
730 REQUIRE(mock_cout.
buffer().empty());
732 SECTION(
"Adds the packet to the PacketQueue if the filter allows it for " 733 "any of the reachable addresses and favors the higher priority " 734 "(decreasing priority) (with logging).")
738 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
740 std::vector<MAVAddress> addr =
748 conn.
send(mission_set_current);
749 fakeit::Verify(Method(mock_queue, push)).Once();
750 fakeit::Verify(Method(mock_queue, push).Matching([&](
auto a,
auto b)
752 return a !=
nullptr && *a == *mission_set_current && b == 2;
755 mock_cout.
buffer().substr(21) ==
756 "accepted MISSION_SET_CURRENT (#41) from 255.0 to 0.0 (v2.0) " 757 "source SOURCE dest DEST\n");
759 SECTION(
"Adds the packet to the PacketQueue if the filter allows it for " 760 "any of the reachable addresses and favors the higher priority " 761 "(decreasing priority) (without logging).")
764 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
766 std::vector<MAVAddress> addr =
774 conn.
send(mission_set_current);
775 fakeit::Verify(Method(mock_queue, push)).Once();
776 fakeit::Verify(Method(mock_queue, push).Matching([&](
auto a,
auto b)
778 return a !=
nullptr && *a == *mission_set_current && b == 2;
780 REQUIRE(mock_cout.
buffer().empty());
782 SECTION(
"Silently drops the packet if the filter rejects it for all " 783 "reachable addresses (with logging).")
787 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
788 [&](
auto & a,
auto & b)
792 return std::pair<bool, int>(
false, 0);
794 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
796 std::vector<MAVAddress> addr =
804 conn.
send(mission_set_current);
805 fakeit::Verify(Method(mock_queue, push)).Exactly(0);
807 mock_cout.
buffer().substr(21) ==
808 "rejected MISSION_SET_CURRENT (#41) from 255.0 to 0.0 (v2.0) " 809 "source SOURCE dest DEST\n");
811 SECTION(
"Silently drops the packet if the filter rejects it for all " 812 "reachable addresses (without logging).")
815 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
816 [&](
auto & a,
auto & b)
820 return std::pair<bool, int>(
false, 0);
822 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
824 std::vector<MAVAddress> addr =
832 conn.
send(mission_set_current);
833 fakeit::Verify(Method(mock_queue, push)).Exactly(0);
834 REQUIRE(mock_cout.
buffer().empty());
840 TEST_CASE(
"Connection's 'send' method (with component broadcast address x.0).",
845 fakeit::Mock<Filter> mock_filter;
846 fakeit::Mock<AddressPool<>> mock_pool;
847 fakeit::When(Method(mock_pool, contains)).AlwaysDo([](
MAVAddress addr)
871 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
872 [&](
auto & a,
auto & b)
878 return std::pair<bool, int>(
true, 2);
883 return std::pair<bool, int>(
true, -3);
886 return std::pair<bool, int>(
false, 0);
888 fakeit::Mock<PacketQueue> mock_queue;
889 fakeit::Fake(Method(mock_queue, push));
894 auto set_mode = std::make_shared<packet_v2::Packet>(to_vector(SetModeV2()));
895 auto source_connection = std::make_shared<Connection>(
"SOURCE", filter);
896 set_mode->connection(source_connection);
898 Connection conn(
"DEST", filter,
false, std::move(pool), std::move(queue));
899 SECTION(
"Adds the packet to the PacketQueue if the filter allows it for " 900 "any reachable component address of the given system address and " 901 "favors the higher priority (increasing priority) (with logging).")
905 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
907 std::vector<MAVAddress> addr =
917 fakeit::Verify(Method(mock_queue, push)).Once();
918 fakeit::Verify(Method(mock_queue, push).Matching([&](
auto a,
auto b)
920 return a !=
nullptr && *a == *set_mode && b == 2;
923 mock_cout.
buffer().substr(21) ==
924 "accepted SET_MODE (#11) from 172.0 to 123.0 (v2.0) " 925 "source SOURCE dest DEST\n");
927 SECTION(
"Adds the packet to the PacketQueue if the filter allows it for " 928 "any reachable component address of the given system address and " 929 "favors the higher priority (increasing priority) " 930 "(without logging).")
933 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
935 std::vector<MAVAddress> addr =
945 fakeit::Verify(Method(mock_queue, push)).Once();
946 fakeit::Verify(Method(mock_queue, push).Matching([&](
auto a,
auto b)
948 return a !=
nullptr && *a == *set_mode && b == 2;
950 REQUIRE(mock_cout.
buffer().empty());
952 SECTION(
"Adds the packet to the PacketQueue if the filter allows it for " 953 "any reachable component address of the given system address and " 954 "favors the higher priority (decreasing priority) (with logging).")
958 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
960 std::vector<MAVAddress> addr =
970 fakeit::Verify(Method(mock_queue, push)).Once();
971 fakeit::Verify(Method(mock_queue, push).Matching([&](
auto a,
auto b)
973 return a !=
nullptr && *a == *set_mode && b == 2;
976 mock_cout.
buffer().substr(21) ==
977 "accepted SET_MODE (#11) from 172.0 to 123.0 (v2.0) " 978 "source SOURCE dest DEST\n");
980 SECTION(
"Adds the packet to the PacketQueue if the filter allows it for " 981 "any reachable component address of the given system address and " 982 "favors the higher priority (decreasing priority) " 983 "(without logging).")
986 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
988 std::vector<MAVAddress> addr =
998 fakeit::Verify(Method(mock_queue, push)).Once();
999 fakeit::Verify(Method(mock_queue, push).Matching([&](
auto a,
auto b)
1001 return a !=
nullptr && *a == *set_mode && b == 2;
1003 REQUIRE(mock_cout.
buffer().empty());
1005 SECTION(
"Silently drops the packet if the filter rejects it for all " 1006 "matching addresses (with logging).")
1010 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
1011 [&](
auto & a,
auto & b)
1015 return std::pair<bool, int>(
false, 0);
1017 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
1019 std::vector<MAVAddress> addr =
1028 conn.
send(set_mode);
1029 fakeit::Verify(Method(mock_queue, push)).Exactly(0);
1031 mock_cout.
buffer().substr(21) ==
1032 "rejected SET_MODE (#11) from 172.0 to 123.0 (v2.0) " 1033 "source SOURCE dest DEST\n");
1035 SECTION(
"Silently drops the packet if the filter rejects it for all " 1036 "matching addresses (without logging).")
1039 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
1040 [&](
auto & a,
auto & b)
1044 return std::pair<bool, int>(
false, 0);
1046 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
1048 std::vector<MAVAddress> addr =
1057 conn.
send(set_mode);
1058 fakeit::Verify(Method(mock_queue, push)).Exactly(0);
1059 REQUIRE(mock_cout.
buffer().empty());
1061 SECTION(
"Silently drops the packet if the destination system cannot be " 1062 "reached on this connection (with logging).")
1066 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
1067 [&](
auto & a,
auto & b)
1071 return std::pair<bool, int>(
true, 0);
1073 fakeit::When(Method(mock_pool, addresses)).AlwaysReturn(
1074 std::vector<MAVAddress>());
1075 fakeit::When(Method(mock_pool, contains)).AlwaysDo([&](
auto & a)
1080 conn.
send(set_mode);
1081 fakeit::Verify(Method(mock_queue, push)).Exactly(0);
1082 REQUIRE(mock_cout.
buffer().empty());
1084 SECTION(
"Silently drops the packet if the destination system cannot be " 1085 "reached on this connection (without logging).")
1088 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
1089 [&](
auto & a,
auto & b)
1093 return std::pair<bool, int>(
true, 0);
1095 fakeit::When(Method(mock_pool, addresses)).AlwaysReturn(
1096 std::vector<MAVAddress>());
1097 fakeit::When(Method(mock_pool, contains)).AlwaysDo([&](
auto & a)
1102 conn.
send(set_mode);
1103 fakeit::Verify(Method(mock_queue, push)).Exactly(0);
1104 REQUIRE(mock_cout.
buffer().empty());
1110 TEST_CASE(
"Connection's 'send' method (destination address, system reachable, " 1111 "component unreachable.",
"[Connection]")
1115 fakeit::Mock<Filter> mock_filter;
1116 fakeit::Mock<AddressPool<>> mock_pool;
1117 fakeit::When(Method(mock_pool, addresses)).AlwaysDo([]()
1119 std::vector<MAVAddress> addr =
1128 fakeit::When(Method(mock_pool, contains)).AlwaysDo([](
MAVAddress addr)
1152 fakeit::Mock<PacketQueue> mock_queue;
1157 auto ping = std::make_shared<packet_v2::Packet>(to_vector(PingV2()));
1158 auto source_connection = std::make_shared<Connection>(
"SOURCE", filter);
1159 ping->connection(source_connection);
1161 Connection conn(
"DEST", filter,
false, std::move(pool), std::move(queue));
1162 SECTION(
"Adds the packet to the PacketQueue if any component of the " 1163 "system is reachable on the connection and the filter allows it " 1164 "to the original component (with logging).")
1168 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
1169 [&](
auto & a,
auto & b)
1175 return std::pair<bool, int>(
true, 2);
1178 return std::pair<bool, int>(
false, 0);
1180 fakeit::Fake(Method(mock_queue, push));
1182 fakeit::Verify(Method(mock_queue, push)).Once();
1183 fakeit::Verify(Method(mock_queue, push).Matching([&](
auto a,
auto b)
1185 return a !=
nullptr && *a == *
ping && b == 2;
1188 mock_cout.
buffer().substr(21) ==
1189 "accepted PING (#4) from 192.168 to 127.1 (v2.0) " 1190 "source SOURCE dest DEST\n");
1192 SECTION(
"Adds the packet to the PacketQueue if any component of the " 1193 "system is reachable on the connection and the filter allows it " 1194 "to the original component (without logging).")
1197 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
1198 [&](
auto & a,
auto & b)
1204 return std::pair<bool, int>(
true, 2);
1207 return std::pair<bool, int>(
false, 0);
1209 fakeit::Fake(Method(mock_queue, push));
1211 fakeit::Verify(Method(mock_queue, push)).Once();
1212 fakeit::Verify(Method(mock_queue, push).Matching([&](
auto a,
auto b)
1214 return a !=
nullptr && *a == *
ping && b == 2;
1216 REQUIRE(mock_cout.
buffer().empty());
1218 SECTION(
"Silently drops the packet if the filter rejects it (using " 1219 "it's original address) (with logging).")
1223 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
1224 [&](
auto & a,
auto & b)
1230 return std::pair<bool, int>(
false, 0);
1233 return std::pair<bool, int>(
true, 2);
1235 fakeit::Fake(Method(mock_queue, push));
1237 fakeit::Verify(Method(mock_queue, push)).Exactly(0);
1239 mock_cout.
buffer().substr(21) ==
1240 "rejected PING (#4) from 192.168 to 127.1 (v2.0) " 1241 "source SOURCE dest DEST\n");
1243 SECTION(
"Silently drops the packet if the filter rejects it (using " 1244 "it's original address) (without logging).")
1247 fakeit::When(Method(mock_filter, will_accept)).AlwaysDo(
1248 [&](
auto & a,
auto & b)
1254 return std::pair<bool, int>(
false, 0);
1257 return std::pair<bool, int>(
true, 2);
1259 fakeit::Fake(Method(mock_queue, push));
1261 fakeit::Verify(Method(mock_queue, push)).Exactly(0);
1262 REQUIRE(mock_cout.
buffer().empty());
std::string str(const T &object)
TEST_CASE("Connection's can be constructed.", "[Connection]")
static unsigned int level()
TEST_VIRTUAL std::shared_ptr< const Packet > next_packet(const std::chrono::nanoseconds &timeout=std::chrono::nanoseconds(0))
std::shared_ptr< T > mock_shared(fakeit::Mock< T > &mock)
TEST_VIRTUAL void add_address(MAVAddress address)
std::unique_ptr< T > mock_unique(fakeit::Mock< T > &mock)
TEST_VIRTUAL void send(std::shared_ptr< const Packet > packet)