hwaddr_unittest.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright (C) 2012, 2014 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 <config.h>
  15. #include <asiolink/io_address.h>
  16. #include <dhcp/hwaddr.h>
  17. #include <dhcp/dhcp4.h>
  18. #include <exceptions/exceptions.h>
  19. #include <boost/scoped_ptr.hpp>
  20. #include <gtest/gtest.h>
  21. #include <iostream>
  22. #include <sstream>
  23. using namespace std;
  24. using namespace isc;
  25. using namespace isc::dhcp;
  26. using namespace isc::asiolink;
  27. using boost::scoped_ptr;
  28. namespace {
  29. // This test verifies if the constructors are working as expected
  30. // and process passed parameters.
  31. TEST(HWAddrTest, constructor) {
  32. const uint8_t data1[] = {0, 1, 2, 3, 4, 5, 6};
  33. const uint8_t htype = HTYPE_ETHER;
  34. vector<uint8_t> data2(data1, data1 + sizeof(data1));
  35. // Over the limit data
  36. vector<uint8_t> big_data_vector(HWAddr::MAX_HWADDR_LEN + 1, 0);
  37. scoped_ptr<HWAddr> hwaddr1(new HWAddr(data1, sizeof(data1), htype));
  38. scoped_ptr<HWAddr> hwaddr2(new HWAddr(data2, htype));
  39. scoped_ptr<HWAddr> hwaddr3(new HWAddr());
  40. EXPECT_TRUE(data2 == hwaddr1->hwaddr_);
  41. EXPECT_EQ(htype, hwaddr1->htype_);
  42. EXPECT_TRUE(data2 == hwaddr2->hwaddr_);
  43. EXPECT_EQ(htype, hwaddr2->htype_);
  44. EXPECT_EQ(0, hwaddr3->hwaddr_.size());
  45. EXPECT_EQ(htype, hwaddr3->htype_);
  46. // Check that over the limit data length throws exception
  47. EXPECT_THROW(HWAddr(&big_data_vector[0], big_data_vector.size(), HTYPE_ETHER),
  48. InvalidParameter);
  49. // Check that over the limit vector throws exception
  50. EXPECT_THROW(HWAddr(big_data_vector, HTYPE_ETHER), InvalidParameter);
  51. }
  52. // This test checks if the comparison operators are sane.
  53. TEST(HWAddrTest, operators) {
  54. uint8_t data1[] = {0, 1, 2, 3, 4, 5, 6};
  55. uint8_t data2[] = {0, 1, 2, 3, 4};
  56. uint8_t data3[] = {0, 1, 2, 3, 4, 5, 7}; // last digit different
  57. uint8_t data4[] = {0, 1, 2, 3, 4, 5, 6}; // the same as 1
  58. uint8_t htype1 = HTYPE_ETHER;
  59. uint8_t htype2 = HTYPE_FDDI;
  60. scoped_ptr<HWAddr> hw1(new HWAddr(data1, sizeof(data1), htype1));
  61. scoped_ptr<HWAddr> hw2(new HWAddr(data2, sizeof(data2), htype1));
  62. scoped_ptr<HWAddr> hw3(new HWAddr(data3, sizeof(data3), htype1));
  63. scoped_ptr<HWAddr> hw4(new HWAddr(data4, sizeof(data4), htype1));
  64. // MAC address the same as data1 and data4, but different hardware type
  65. scoped_ptr<HWAddr> hw5(new HWAddr(data4, sizeof(data4), htype2));
  66. EXPECT_TRUE(*hw1 == *hw4);
  67. EXPECT_FALSE(*hw1 == *hw2);
  68. EXPECT_FALSE(*hw1 == *hw3);
  69. EXPECT_FALSE(*hw1 != *hw4);
  70. EXPECT_TRUE(*hw1 != *hw2);
  71. EXPECT_TRUE(*hw1 != *hw3);
  72. EXPECT_FALSE(*hw1 == *hw5);
  73. EXPECT_FALSE(*hw4 == *hw5);
  74. EXPECT_TRUE(*hw1 != *hw5);
  75. EXPECT_TRUE(*hw4 != *hw5);
  76. }
  77. // Checks that toText() method produces appropriate text representation
  78. TEST(HWAddrTest, toText) {
  79. uint8_t data[] = {0, 1, 2, 3, 4, 5};
  80. uint8_t htype = 15;
  81. HWAddrPtr hw(new HWAddr(data, sizeof(data), htype));
  82. EXPECT_EQ("hwtype=15 00:01:02:03:04:05", hw->toText());
  83. // In some cases we don't want htype value to be included. Check that
  84. // it can be forced.
  85. EXPECT_EQ("00:01:02:03:04:05", hw->toText(false));
  86. }
  87. TEST(HWAddrTest, stringConversion) {
  88. // Check that an empty vector returns an appropriate string
  89. HWAddr hwaddr;
  90. std::string result = hwaddr.toText();
  91. EXPECT_EQ(std::string("hwtype=1 "), result);
  92. // ... that a single-byte string is OK
  93. hwaddr.hwaddr_.push_back(0xc3);
  94. result = hwaddr.toText();
  95. EXPECT_EQ(std::string("hwtype=1 c3"), result);
  96. // ... and that a multi-byte string works
  97. hwaddr.hwaddr_.push_back(0x7);
  98. hwaddr.hwaddr_.push_back(0xa2);
  99. hwaddr.hwaddr_.push_back(0xe8);
  100. hwaddr.hwaddr_.push_back(0x42);
  101. result = hwaddr.toText();
  102. EXPECT_EQ(std::string("hwtype=1 c3:07:a2:e8:42"), result);
  103. }
  104. // Checks that the HW address can be created from the textual format.
  105. TEST(HWAddrTest, fromText) {
  106. scoped_ptr<HWAddr> hwaddr;
  107. // Create HWAddr from text.
  108. ASSERT_NO_THROW(
  109. hwaddr.reset(new HWAddr(HWAddr::fromText("00:01:A:bc:d:67")));
  110. );
  111. EXPECT_EQ("00:01:0a:bc:0d:67", hwaddr->toText(false));
  112. // HWAddr class should allow empty address.
  113. ASSERT_NO_THROW(
  114. hwaddr.reset(new HWAddr(HWAddr::fromText("")));
  115. );
  116. EXPECT_TRUE(hwaddr->toText(false).empty());
  117. // HWAddr should not allow multiple consecutive colons.
  118. EXPECT_THROW(
  119. hwaddr.reset(new HWAddr(HWAddr::fromText("00::01:00:bc:0d:67"))),
  120. isc::BadValue
  121. );
  122. // There should be no more than two digits per byte of the HW addr.
  123. EXPECT_THROW(
  124. hwaddr.reset(new HWAddr(HWAddr::fromText("00:01:00A:bc:0d:67"))),
  125. isc::BadValue
  126. );
  127. }
  128. // Checks that 16 bits values can be stored in HWaddr
  129. TEST(HWAddrTest, 16bits) {
  130. uint8_t data[] = {0, 1, 2, 3, 4, 5};
  131. uint16_t htype = 257;
  132. HWAddrPtr hw(new HWAddr(data, sizeof(data), htype));
  133. EXPECT_EQ("hwtype=257 00:01:02:03:04:05", hw->toText());
  134. }
  135. } // end of anonymous namespace