nodeinfo_test.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <gtest/gtest.h>
  2. #include <memory>
  3. #include <sstream>
  4. #include <boost/functional/hash.hpp>
  5. #include <tins/tins.h>
  6. #include "nodeinfo.hpp"
  7. TEST(NodeInfoTest, MacAddress)
  8. {
  9. const Tins::HWAddress<6> mac_address(std::string("52:54:00:b2:fa:7f"));
  10. NodeInfo nodeinfo;
  11. nodeinfo.set_mac_address(mac_address);
  12. EXPECT_EQ(mac_address, nodeinfo.get_mac_address());
  13. EXPECT_EQ(mac_address.to_string(), nodeinfo.get_mac_address().to_string());
  14. }
  15. TEST(NodeInfoTest, HopLimit)
  16. {
  17. constexpr int hoplimit = 150;
  18. NodeInfo nodeinfo;
  19. EXPECT_EQ(64, nodeinfo.get_hoplimit());
  20. nodeinfo.set_hoplimit(hoplimit);
  21. EXPECT_EQ(hoplimit, nodeinfo.get_hoplimit());
  22. }
  23. TEST(NodeInfoTest, IPv6Address)
  24. {
  25. const Tins::IPv6Address address1(std::string("fd00::1"));
  26. const Tins::IPv6Address address2(std::string("fd00::2"));
  27. NodeInfo nodeinfo;
  28. nodeinfo.add_address(address1);
  29. EXPECT_EQ(address1, nodeinfo.get_address());
  30. EXPECT_EQ(address1.to_string(), nodeinfo.get_address().to_string());
  31. nodeinfo.add_address(address2);
  32. Tins::IPv6Address random_address = nodeinfo.get_address();
  33. while (random_address != address1)
  34. random_address = nodeinfo.get_address();
  35. EXPECT_EQ(random_address, address1);
  36. while (random_address != address2)
  37. random_address = nodeinfo.get_address();
  38. EXPECT_EQ(random_address, address2);
  39. }
  40. TEST(NodeInfoTest, HasAddress)
  41. {
  42. const Tins::IPv6Address address1(std::string("fd00::1"));
  43. const Tins::IPv6Address address2(std::string("fd00::2"));
  44. NodeInfo nodeinfo;
  45. EXPECT_FALSE(nodeinfo.has_address(address1));
  46. EXPECT_FALSE(nodeinfo.has_address(address2));
  47. nodeinfo.add_address(address1);
  48. EXPECT_TRUE(nodeinfo.has_address(address1));
  49. EXPECT_FALSE(nodeinfo.has_address(address2));
  50. nodeinfo.add_address(address2);
  51. EXPECT_TRUE(nodeinfo.has_address(address1));
  52. EXPECT_TRUE(nodeinfo.has_address(address2));
  53. }
  54. TEST(NodeInfoTest, Comparison)
  55. {
  56. NodeInfo nodeinfo1;
  57. NodeInfo nodeinfo2;
  58. EXPECT_EQ(nodeinfo1, nodeinfo2);
  59. nodeinfo1.set_hoplimit(10);
  60. EXPECT_NE(nodeinfo1, nodeinfo2);
  61. nodeinfo2.set_hoplimit(10);
  62. EXPECT_EQ(nodeinfo1, nodeinfo2);
  63. const std::shared_ptr<NodeInfo> child1 = std::make_shared<NodeInfo>();
  64. child1->set_hoplimit(1);
  65. nodeinfo1.add_node(child1);
  66. EXPECT_NE(nodeinfo1, nodeinfo2);
  67. nodeinfo2.add_node(child1);
  68. EXPECT_EQ(nodeinfo1, nodeinfo2);
  69. const std::shared_ptr<NodeInfo> child2_1 = std::make_shared<NodeInfo>();
  70. child1->set_hoplimit(2);
  71. const std::shared_ptr<NodeInfo> child2_2 = std::make_shared<NodeInfo>();
  72. child1->set_hoplimit(2);
  73. nodeinfo1.add_node(child2_1);
  74. EXPECT_NE(nodeinfo1, nodeinfo2);
  75. nodeinfo2.add_node(child2_2);
  76. EXPECT_EQ(nodeinfo1, nodeinfo2);
  77. child2_1->add_address(Tins::IPv6Address("fd00::1"));
  78. EXPECT_NE(nodeinfo1, nodeinfo2);
  79. child2_2->add_address(Tins::IPv6Address("fd00::1"));
  80. EXPECT_EQ(nodeinfo1, nodeinfo2);
  81. }
  82. TEST(NodeInfoTest, Output)
  83. {
  84. const Tins::IPv6Address address1(std::string("fd00::1"));
  85. const Tins::IPv6Address address2(std::string("fd00::2"));
  86. NodeInfo nodeinfo;
  87. nodeinfo.add_address(address1);
  88. std::ostringstream test_output;
  89. test_output << nodeinfo;
  90. EXPECT_EQ(test_output.str(), "NodeInfo: hoplimit=64 fd00::1");
  91. test_output.str("");
  92. nodeinfo.add_address(address2);
  93. test_output << nodeinfo;
  94. EXPECT_EQ(test_output.str(), "NodeInfo: hoplimit=64 fd00::1 fd00::2");
  95. test_output.str("");
  96. nodeinfo.set_hoplimit(30);
  97. test_output << nodeinfo;
  98. EXPECT_EQ(test_output.str(), "NodeInfo: hoplimit=30 fd00::1 fd00::2");
  99. test_output.str("");
  100. nodeinfo.set_mac_address(
  101. Tins::HWAddress<6>(std::string("52:54:00:b2:fa:7f")));
  102. test_output << nodeinfo;
  103. EXPECT_EQ(test_output.str(),
  104. "NodeInfo: hoplimit=30 fd00::1 fd00::2 52:54:00:b2:fa:7f");
  105. }