noderequest_test.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include <gtest/gtest.h>
  2. #include <tins/tins.h>
  3. #include "noderequest.hpp"
  4. TEST(NodeRequestTest, EchoRequest)
  5. {
  6. const Tins::HWAddress<6> source_mac("52:54:00:b2:fa:7f");
  7. const Tins::HWAddress<6> destination_mac("52:54:00:b2:fa:7e");
  8. const Tins::IPv6Address source_address("fd00::1");
  9. const Tins::IPv6Address destination_address("fd00::2");
  10. constexpr int hoplimit = 55;
  11. constexpr int icmp_identifier = 56;
  12. constexpr int icmp_sequence = 1;
  13. const Tins::RawPDU::payload_type payload = {8, 4, 5, 9, 255, 0, 0, 0, 0, 0};
  14. Tins::EthernetII packet = Tins::EthernetII(destination_mac, source_mac) /
  15. Tins::IPv6(destination_address, source_address) /
  16. Tins::ICMPv6(Tins::ICMPv6::Types::ECHO_REQUEST);
  17. Tins::IPv6& inner_ipv6 = packet.rfind_pdu<Tins::IPv6>();
  18. inner_ipv6.hop_limit(hoplimit);
  19. Tins::ICMPv6& inner_icmpv6 = inner_ipv6.rfind_pdu<Tins::ICMPv6>();
  20. inner_icmpv6.identifier(icmp_identifier);
  21. inner_icmpv6.sequence(icmp_sequence);
  22. inner_icmpv6.inner_pdu(Tins::RawPDU(payload));
  23. /* Serializing the package is necessary to automatically calculate checksums
  24. * and other fields. */
  25. const Tins::PDU::serialization_type serialized_packet = packet.serialize();
  26. const Tins::EthernetII final_packet(serialized_packet.data(),
  27. serialized_packet.size());
  28. const NodeRequest request(final_packet);
  29. EXPECT_EQ(request.get_type(), NodeRequestType::ICMP_ECHO_REQUEST);
  30. EXPECT_EQ(request.get_source_mac(), source_mac);
  31. EXPECT_EQ(request.get_destination_mac(), destination_mac);
  32. EXPECT_EQ(request.get_source_address(), source_address);
  33. EXPECT_EQ(request.get_destination_address(), destination_address);
  34. EXPECT_EQ(request.get_hoplimit(), hoplimit);
  35. EXPECT_EQ(request.get_udp_sport(), 0);
  36. EXPECT_EQ(request.get_udp_dport(), 0);
  37. EXPECT_EQ(request.get_icmp_identifier(), icmp_identifier);
  38. EXPECT_EQ(request.get_icmp_sequence(), icmp_sequence);
  39. EXPECT_EQ(request.get_payload(), payload);
  40. std::ostringstream test_output;
  41. test_output << request;
  42. EXPECT_EQ(test_output.str(),
  43. "REQUEST ICMP_ECHO_REQUEST: fd00::1 (52:54:00:b2:fa:7f) -> "
  44. "fd00::2 (52:54:00:b2:fa:7e) Hoplimit=55: ID=56 SEQ=1 Payload: "
  45. "08 04 05 09 ff 00 00 00 00 00");
  46. }
  47. TEST(NodeRequestTest, NdpRequest)
  48. {
  49. const Tins::HWAddress<6> source_mac("52:54:00:b2:fa:7f");
  50. const Tins::HWAddress<6> destination_mac("33:33:ff:48:b2:ae");
  51. const Tins::IPv6Address source_address("fd00::1");
  52. const Tins::IPv6Address destination_address("ff02::1:ff48:b2ae");
  53. const Tins::IPv6Address target_address("fd00::2");
  54. constexpr int hoplimit = 2;
  55. Tins::EthernetII packet =
  56. Tins::EthernetII(destination_mac, source_mac) /
  57. Tins::IPv6(destination_address, source_address) /
  58. Tins::ICMPv6(Tins::ICMPv6::Types::NEIGHBOUR_SOLICIT);
  59. Tins::IPv6& inner_ipv6 = packet.rfind_pdu<Tins::IPv6>();
  60. inner_ipv6.hop_limit(hoplimit);
  61. Tins::ICMPv6& inner_icmpv6 = inner_ipv6.rfind_pdu<Tins::ICMPv6>();
  62. inner_icmpv6.target_addr(target_address);
  63. /* Serializing the package is necessary to automatically calculate checksums
  64. * and other fields. */
  65. const Tins::PDU::serialization_type serialized_packet = packet.serialize();
  66. const Tins::EthernetII final_packet(serialized_packet.data(),
  67. serialized_packet.size());
  68. const NodeRequest request(final_packet);
  69. EXPECT_EQ(request.get_type(), NodeRequestType::ICMP_NDP);
  70. EXPECT_EQ(request.get_source_mac(), source_mac);
  71. EXPECT_EQ(request.get_destination_mac(), destination_mac);
  72. EXPECT_EQ(request.get_source_address(), source_address);
  73. EXPECT_EQ(request.get_destination_address(), target_address);
  74. EXPECT_EQ(request.get_hoplimit(), hoplimit);
  75. EXPECT_EQ(request.get_udp_sport(), 0);
  76. EXPECT_EQ(request.get_udp_dport(), 0);
  77. EXPECT_EQ(request.get_icmp_identifier(), 0);
  78. EXPECT_EQ(request.get_icmp_sequence(), 0);
  79. EXPECT_TRUE(request.get_payload().empty());
  80. std::ostringstream test_output;
  81. test_output << request;
  82. EXPECT_EQ(test_output.str(),
  83. "REQUEST ICMP_NDP: fd00::1 (52:54:00:b2:fa:7f) -> fd00::2 "
  84. "(33:33:ff:48:b2:ae) Hoplimit=2: Looking for fd00::2");
  85. }
  86. TEST(NodeRequestTest, UdpRequest)
  87. {
  88. const Tins::HWAddress<6> source_mac("52:54:00:b2:fa:7f");
  89. const Tins::HWAddress<6> destination_mac("52:54:00:b2:fa:7e");
  90. const Tins::IPv6Address source_address("fd00::1");
  91. const Tins::IPv6Address destination_address("fd00::2");
  92. constexpr int hoplimit = 55;
  93. constexpr int udp_sport = 54739;
  94. constexpr int udp_dport = 33434;
  95. const Tins::RawPDU::payload_type payload = {8, 4, 5, 9, 255, 0, 0, 0, 0, 0};
  96. Tins::EthernetII packet = Tins::EthernetII(destination_mac, source_mac) /
  97. Tins::IPv6(destination_address, source_address) /
  98. Tins::UDP(udp_dport, udp_sport);
  99. Tins::IPv6& inner_ipv6 = packet.rfind_pdu<Tins::IPv6>();
  100. inner_ipv6.hop_limit(hoplimit);
  101. Tins::UDP& inner_udp = inner_ipv6.rfind_pdu<Tins::UDP>();
  102. inner_udp.inner_pdu(Tins::RawPDU(payload));
  103. /* Serializing the package is necessary to automatically calculate checksums
  104. * and other fields. */
  105. const Tins::PDU::serialization_type serialized_packet = packet.serialize();
  106. const Tins::EthernetII final_packet(serialized_packet.data(),
  107. serialized_packet.size());
  108. const NodeRequest request(final_packet);
  109. EXPECT_EQ(request.get_type(), NodeRequestType::UDP);
  110. EXPECT_EQ(request.get_source_mac(), source_mac);
  111. EXPECT_EQ(request.get_destination_mac(), destination_mac);
  112. EXPECT_EQ(request.get_source_address(), source_address);
  113. EXPECT_EQ(request.get_destination_address(), destination_address);
  114. EXPECT_EQ(request.get_hoplimit(), hoplimit);
  115. EXPECT_EQ(request.get_udp_sport(), udp_sport);
  116. EXPECT_EQ(request.get_udp_dport(), udp_dport);
  117. EXPECT_EQ(request.get_icmp_identifier(), 0);
  118. EXPECT_EQ(request.get_icmp_sequence(), 0);
  119. EXPECT_EQ(request.get_payload(), payload);
  120. std::ostringstream test_output;
  121. test_output << request;
  122. EXPECT_EQ(
  123. test_output.str(),
  124. "REQUEST UDP: fd00::1 (52:54:00:b2:fa:7f) -> fd00::2 "
  125. "(52:54:00:b2:fa:7e) Hoplimit=55: DPORT=33434 SPORT=54739 LENGTH=10");
  126. }
  127. TEST(NodeRequestTest, UnknownRequest)
  128. {
  129. const Tins::HWAddress<6> source_mac("52:54:00:b2:fa:7f");
  130. const Tins::HWAddress<6> destination_mac("52:54:00:b2:fa:7e");
  131. const Tins::IPv6Address source_address("fd00::1");
  132. const Tins::IPv6Address destination_address("fd00::2");
  133. constexpr int hoplimit = 2;
  134. Tins::EthernetII packet =
  135. Tins::EthernetII(destination_mac, source_mac) /
  136. Tins::IPv6(destination_address, source_address) /
  137. Tins::ICMPv6(Tins::ICMPv6::Types::NEIGHBOUR_ADVERT);
  138. Tins::IPv6& inner_ipv6 = packet.rfind_pdu<Tins::IPv6>();
  139. inner_ipv6.hop_limit(hoplimit);
  140. /* Serializing the package is necessary to automatically calculate checksums
  141. * and other fields. */
  142. const Tins::PDU::serialization_type serialized_packet = packet.serialize();
  143. const Tins::EthernetII final_packet(serialized_packet.data(),
  144. serialized_packet.size());
  145. const NodeRequest request(final_packet);
  146. EXPECT_EQ(request.get_type(), NodeRequestType::UNKNOWN);
  147. EXPECT_EQ(request.get_source_mac(), source_mac);
  148. EXPECT_EQ(request.get_destination_mac(), destination_mac);
  149. EXPECT_EQ(request.get_source_address(), source_address);
  150. EXPECT_EQ(request.get_destination_address(), destination_address);
  151. EXPECT_EQ(request.get_hoplimit(), hoplimit);
  152. EXPECT_EQ(request.get_udp_sport(), 0);
  153. EXPECT_EQ(request.get_udp_dport(), 0);
  154. EXPECT_EQ(request.get_icmp_identifier(), 0);
  155. EXPECT_EQ(request.get_icmp_sequence(), 0);
  156. EXPECT_TRUE(request.get_payload().empty());
  157. std::ostringstream test_output;
  158. test_output << request;
  159. EXPECT_EQ(test_output.str(),
  160. "REQUEST UNKNOWN: fd00::1 (52:54:00:b2:fa:7f) -> fd00::2 "
  161. "(52:54:00:b2:fa:7e) Hoplimit=2");
  162. }