userid_unittests.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <exceptions/exceptions.h>
  15. #include <user.h>
  16. #include <boost/function.hpp>
  17. #include <boost/bind.hpp>
  18. #include <boost/shared_ptr.hpp>
  19. #include <gtest/gtest.h>
  20. using namespace std;
  21. using namespace user_chk;
  22. namespace {
  23. /// @brief Test invalid constructors.
  24. TEST(UserIdTest, invalidConstructors) {
  25. // Verify that constructor does not allow empty id vector.
  26. std::vector<uint8_t> empty_bytes;
  27. ASSERT_THROW(UserId(UserId::HW_ADDRESS, empty_bytes), isc::BadValue);
  28. ASSERT_THROW(UserId(UserId::DUID, empty_bytes), isc::BadValue);
  29. // Verify that constructor does not allow empty id string.
  30. ASSERT_THROW(UserId(UserId::HW_ADDRESS, ""), isc::BadValue);
  31. ASSERT_THROW(UserId(UserId::DUID, ""), isc::BadValue);
  32. }
  33. /// @brief Test making and using HW_ADDRESS type UserIds
  34. TEST(UserIdTest, hwAddress_type) {
  35. // Verify text label look up for HW_ADDRESS enum.
  36. EXPECT_EQ(std::string(UserId::HW_ADDRESS_STR),
  37. UserId::lookupTypeStr(UserId::HW_ADDRESS));
  38. // Verify enum look up for HW_ADDRESS text label.
  39. EXPECT_EQ(UserId::HW_ADDRESS,
  40. UserId::lookupType(UserId::HW_ADDRESS_STR));
  41. // Build a test address vector.
  42. uint8_t tmp[] = { 0x01, 0xFF, 0x02, 0xAC, 0x03, 0x0B, 0x07, 0x08 };
  43. std::vector<uint8_t> bytes(tmp, tmp + (sizeof(tmp)/sizeof(uint8_t)));
  44. // Verify construction from an HW_ADDRESS id type and address vector.
  45. UserIdPtr id;
  46. ASSERT_NO_THROW(id.reset(new UserId(UserId::HW_ADDRESS, bytes)));
  47. // Verify that the id can be fetched.
  48. EXPECT_EQ(id->getType(), UserId::HW_ADDRESS);
  49. EXPECT_TRUE(bytes == id->getId());
  50. // Check relational oeprators when a == b.
  51. UserIdPtr id2;
  52. ASSERT_NO_THROW(id2.reset(new UserId(UserId::HW_ADDRESS, id->toText())));
  53. EXPECT_TRUE(*id == *id2);
  54. EXPECT_FALSE(*id != *id2);
  55. EXPECT_FALSE(*id < *id2);
  56. // Check relational oeprators when a < b.
  57. ASSERT_NO_THROW(id2.reset(new UserId(UserId::HW_ADDRESS,
  58. "01FF02AC030B0709")));
  59. EXPECT_FALSE(*id == *id2);
  60. EXPECT_TRUE(*id != *id2);
  61. EXPECT_TRUE(*id < *id2);
  62. // Check relational oeprators when a > b.
  63. ASSERT_NO_THROW(id2.reset(new UserId(UserId::HW_ADDRESS,
  64. "01FF02AC030B0707")));
  65. EXPECT_FALSE(*id == *id2);
  66. EXPECT_TRUE(*id != *id2);
  67. EXPECT_FALSE(*id < *id2);
  68. // Verify that colon delimiters are ok.
  69. ASSERT_NO_THROW(id2.reset(new UserId(UserId::HW_ADDRESS,
  70. "01:FF:02:AC:03:0B:07:07")));
  71. EXPECT_FALSE(*id == *id2);
  72. }
  73. /// @brief Test making and using DUID type UserIds
  74. TEST(UserIdTest, duid_type) {
  75. // Verify text label look up for DUID enum.
  76. EXPECT_EQ(std::string(UserId::DUID_STR),
  77. UserId::lookupTypeStr(UserId::DUID));
  78. // Verify enum look up for DUID text label.
  79. EXPECT_EQ(UserId::DUID,
  80. UserId::lookupType(UserId::DUID_STR));
  81. // Build a test DUID vector.
  82. uint8_t tmp[] = { 0x01, 0xFF, 0x02, 0xAC, 0x03, 0x0B, 0x07, 0x08 };
  83. std::vector<uint8_t> bytes(tmp, tmp + (sizeof(tmp)/sizeof(uint8_t)));
  84. // Verify construction from an DUID id type and address vector.
  85. UserIdPtr id;
  86. ASSERT_NO_THROW(id.reset(new UserId(UserId::DUID, bytes)));
  87. // Verify that the id can be fetched.
  88. EXPECT_EQ(id->getType(), UserId::DUID);
  89. EXPECT_TRUE(bytes == id->getId());
  90. // Check relational oeprators when a == b.
  91. UserIdPtr id2;
  92. ASSERT_NO_THROW(id2.reset(new UserId(UserId::DUID, id->toText())));
  93. EXPECT_TRUE(*id == *id2);
  94. EXPECT_FALSE(*id != *id2);
  95. EXPECT_FALSE(*id < *id2);
  96. // Check relational oeprators when a < b.
  97. ASSERT_NO_THROW(id2.reset(new UserId(UserId::DUID, "01FF02AC030B0709")));
  98. EXPECT_FALSE(*id == *id2);
  99. EXPECT_TRUE(*id != *id2);
  100. EXPECT_TRUE(*id < *id2);
  101. // Check relational oeprators when a > b.
  102. ASSERT_NO_THROW(id2.reset(new UserId(UserId::DUID, "01FF02AC030B0707")));
  103. EXPECT_FALSE(*id == *id2);
  104. EXPECT_TRUE(*id != *id2);
  105. EXPECT_FALSE(*id < *id2);
  106. // Verify that colon delimiters are ok.
  107. ASSERT_NO_THROW(id2.reset(new UserId(UserId::DUID,
  108. "01:FF:02:AC:03:0B:07:08")));
  109. EXPECT_TRUE(*id == *id2);
  110. }
  111. /// @brief Tests that UserIds of different types compare correctly.
  112. TEST(UserIdTest, mixed_type_compare) {
  113. UserIdPtr hw, duid;
  114. // Create UserIds with different types, but same id data.
  115. ASSERT_NO_THROW(hw.reset(new UserId(UserId::HW_ADDRESS,
  116. "01FF02AC030B0709")));
  117. ASSERT_NO_THROW(duid.reset(new UserId(UserId::DUID,
  118. "01FF02AC030B0709")));
  119. // Verify that UserIdType influences logical comparators.
  120. EXPECT_FALSE(*hw == *duid);
  121. EXPECT_TRUE(*hw != *duid);
  122. EXPECT_TRUE(*hw < *duid);
  123. }
  124. } // end of anonymous namespace