userid_unittests.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <exceptions/exceptions.h>
  7. #include <user.h>
  8. #include <boost/function.hpp>
  9. #include <boost/bind.hpp>
  10. #include <boost/shared_ptr.hpp>
  11. #include <gtest/gtest.h>
  12. using namespace std;
  13. using namespace user_chk;
  14. namespace {
  15. /// @brief Test invalid constructors.
  16. TEST(UserIdTest, invalidConstructors) {
  17. // Verify that constructor does not allow empty id vector.
  18. std::vector<uint8_t> empty_bytes;
  19. ASSERT_THROW(UserId(UserId::HW_ADDRESS, empty_bytes), isc::BadValue);
  20. ASSERT_THROW(UserId(UserId::DUID, empty_bytes), isc::BadValue);
  21. // Verify that constructor does not allow empty id string.
  22. ASSERT_THROW(UserId(UserId::HW_ADDRESS, ""), isc::BadValue);
  23. ASSERT_THROW(UserId(UserId::DUID, ""), isc::BadValue);
  24. }
  25. /// @brief Test making and using HW_ADDRESS type UserIds
  26. TEST(UserIdTest, hwAddress_type) {
  27. // Verify text label look up for HW_ADDRESS enum.
  28. EXPECT_EQ(std::string(UserId::HW_ADDRESS_STR),
  29. UserId::lookupTypeStr(UserId::HW_ADDRESS));
  30. // Verify enum look up for HW_ADDRESS text label.
  31. EXPECT_EQ(UserId::HW_ADDRESS,
  32. UserId::lookupType(UserId::HW_ADDRESS_STR));
  33. // Build a test address vector.
  34. uint8_t tmp[] = { 0x01, 0xFF, 0x02, 0xAC, 0x03, 0x0B, 0x07, 0x08 };
  35. std::vector<uint8_t> bytes(tmp, tmp + (sizeof(tmp)/sizeof(uint8_t)));
  36. // Verify construction from an HW_ADDRESS id type and address vector.
  37. UserIdPtr id;
  38. ASSERT_NO_THROW(id.reset(new UserId(UserId::HW_ADDRESS, bytes)));
  39. // Verify that the id can be fetched.
  40. EXPECT_EQ(id->getType(), UserId::HW_ADDRESS);
  41. EXPECT_TRUE(bytes == id->getId());
  42. // Check relational operators when a == b.
  43. UserIdPtr id2;
  44. ASSERT_NO_THROW(id2.reset(new UserId(UserId::HW_ADDRESS, id->toText())));
  45. EXPECT_TRUE(*id == *id2);
  46. EXPECT_FALSE(*id != *id2);
  47. EXPECT_FALSE(*id < *id2);
  48. // Check relational operators when a < b.
  49. ASSERT_NO_THROW(id2.reset(new UserId(UserId::HW_ADDRESS,
  50. "01FF02AC030B0709")));
  51. EXPECT_FALSE(*id == *id2);
  52. EXPECT_TRUE(*id != *id2);
  53. EXPECT_TRUE(*id < *id2);
  54. // Check relational operators when a > b.
  55. ASSERT_NO_THROW(id2.reset(new UserId(UserId::HW_ADDRESS,
  56. "01FF02AC030B0707")));
  57. EXPECT_FALSE(*id == *id2);
  58. EXPECT_TRUE(*id != *id2);
  59. EXPECT_FALSE(*id < *id2);
  60. // Verify that colon delimiters are ok.
  61. ASSERT_NO_THROW(id2.reset(new UserId(UserId::HW_ADDRESS,
  62. "01:FF:02:AC:03:0B:07:07")));
  63. EXPECT_FALSE(*id == *id2);
  64. }
  65. /// @brief Test making and using DUID type UserIds
  66. TEST(UserIdTest, duid_type) {
  67. // Verify text label look up for DUID enum.
  68. EXPECT_EQ(std::string(UserId::DUID_STR),
  69. UserId::lookupTypeStr(UserId::DUID));
  70. // Verify enum look up for DUID text label.
  71. EXPECT_EQ(UserId::DUID,
  72. UserId::lookupType(UserId::DUID_STR));
  73. // Build a test DUID vector.
  74. uint8_t tmp[] = { 0x01, 0xFF, 0x02, 0xAC, 0x03, 0x0B, 0x07, 0x08 };
  75. std::vector<uint8_t> bytes(tmp, tmp + (sizeof(tmp)/sizeof(uint8_t)));
  76. // Verify construction from an DUID id type and address vector.
  77. UserIdPtr id;
  78. ASSERT_NO_THROW(id.reset(new UserId(UserId::DUID, bytes)));
  79. // Verify that the id can be fetched.
  80. EXPECT_EQ(id->getType(), UserId::DUID);
  81. EXPECT_TRUE(bytes == id->getId());
  82. // Check relational operators when a == b.
  83. UserIdPtr id2;
  84. ASSERT_NO_THROW(id2.reset(new UserId(UserId::DUID, id->toText())));
  85. EXPECT_TRUE(*id == *id2);
  86. EXPECT_FALSE(*id != *id2);
  87. EXPECT_FALSE(*id < *id2);
  88. // Check relational operators when a < b.
  89. ASSERT_NO_THROW(id2.reset(new UserId(UserId::DUID, "01FF02AC030B0709")));
  90. EXPECT_FALSE(*id == *id2);
  91. EXPECT_TRUE(*id != *id2);
  92. EXPECT_TRUE(*id < *id2);
  93. // Check relational operators when a > b.
  94. ASSERT_NO_THROW(id2.reset(new UserId(UserId::DUID, "01FF02AC030B0707")));
  95. EXPECT_FALSE(*id == *id2);
  96. EXPECT_TRUE(*id != *id2);
  97. EXPECT_FALSE(*id < *id2);
  98. // Verify that colon delimiters are ok.
  99. ASSERT_NO_THROW(id2.reset(new UserId(UserId::DUID,
  100. "01:FF:02:AC:03:0B:07:08")));
  101. EXPECT_TRUE(*id == *id2);
  102. }
  103. /// @brief Tests that UserIds of different types compare correctly.
  104. TEST(UserIdTest, mixed_type_compare) {
  105. UserIdPtr hw, duid;
  106. // Create UserIds with different types, but same id data.
  107. ASSERT_NO_THROW(hw.reset(new UserId(UserId::HW_ADDRESS,
  108. "01FF02AC030B0709")));
  109. ASSERT_NO_THROW(duid.reset(new UserId(UserId::DUID,
  110. "01FF02AC030B0709")));
  111. // Verify that UserIdType influences logical comparators.
  112. EXPECT_FALSE(*hw == *duid);
  113. EXPECT_TRUE(*hw != *duid);
  114. EXPECT_TRUE(*hw < *duid);
  115. }
  116. } // end of anonymous namespace