option6_iaprefix_unittest.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // Copyright (C) 2011-2015 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 <config.h>
  7. #include <dhcp/dhcp6.h>
  8. #include <dhcp/option.h>
  9. #include <dhcp/option_int.h>
  10. #include <dhcp/option6_iaprefix.h>
  11. #include <util/buffer.h>
  12. #include <boost/scoped_ptr.hpp>
  13. #include <gtest/gtest.h>
  14. #include <iostream>
  15. #include <sstream>
  16. #include <arpa/inet.h>
  17. using namespace std;
  18. using namespace isc;
  19. using namespace isc::dhcp;
  20. using namespace isc::util;
  21. using namespace isc::asiolink;
  22. namespace {
  23. class Option6IAPrefixTest : public ::testing::Test {
  24. public:
  25. Option6IAPrefixTest() : buf_(255), out_buf_(255) {
  26. for (unsigned i = 0; i < 255; i++) {
  27. buf_[i] = 255 - i;
  28. }
  29. }
  30. /// @brief creates on-wire representation of IAPREFIX option
  31. ///
  32. /// buf_ field is set up to have IAPREFIX with preferred=1000,
  33. /// valid=3000000000 and prefix being 2001:db8:1:0:afaf:0:dead:beef/77
  34. void setExampleBuffer() {
  35. for (int i = 0; i < 255; i++) {
  36. buf_[i] = 0;
  37. }
  38. buf_[ 0] = 0x00;
  39. buf_[ 1] = 0x00;
  40. buf_[ 2] = 0x03;
  41. buf_[ 3] = 0xe8; // preferred lifetime = 1000
  42. buf_[ 4] = 0xb2;
  43. buf_[ 5] = 0xd0;
  44. buf_[ 6] = 0x5e;
  45. buf_[ 7] = 0x00; // valid lifetime = 3,000,000,000
  46. buf_[ 8] = 77; // Prefix length = 77
  47. buf_[ 9] = 0x20;
  48. buf_[10] = 0x01;
  49. buf_[11] = 0x0d;
  50. buf_[12] = 0xb8;
  51. buf_[13] = 0x00;
  52. buf_[14] = 0x01;
  53. buf_[17] = 0xaf;
  54. buf_[18] = 0xaf;
  55. buf_[21] = 0xde;
  56. buf_[22] = 0xad;
  57. buf_[23] = 0xbe;
  58. buf_[24] = 0xef; // 2001:db8:1:0:afaf:0:dead:beef
  59. }
  60. /// @brief Checks whether specified IAPREFIX option meets expected values
  61. ///
  62. /// To be used with option generated by setExampleBuffer
  63. ///
  64. /// @param opt IAPREFIX option being tested
  65. /// @param expected_type expected option type
  66. /// @param expected_length Expected length of the prefix.
  67. /// @param expected_address Expected prefix value.
  68. void checkOption(Option6IAPrefix& opt, const uint16_t expected_type,
  69. const uint8_t expected_length,
  70. const IOAddress& expected_address) {
  71. // Check if all fields have expected values
  72. EXPECT_EQ(Option::V6, opt.getUniverse());
  73. EXPECT_EQ(expected_type, opt.getType());
  74. EXPECT_EQ(expected_address, opt.getAddress());
  75. EXPECT_EQ(1000, opt.getPreferred());
  76. EXPECT_EQ(3000000000U, opt.getValid());
  77. // uint8_t is often represented as a character type (char). Convert it
  78. // to integer so as it is logged as a numeric value instead.
  79. EXPECT_EQ(static_cast<int>(expected_length),
  80. static_cast<int>(opt.getLength()));
  81. // 4 bytes header + 25 bytes content
  82. EXPECT_EQ(Option::OPTION6_HDR_LEN + Option6IAPrefix::OPTION6_IAPREFIX_LEN,
  83. opt.len());
  84. }
  85. /// @brief Checks whether content of output buffer is correct
  86. ///
  87. /// Output buffer is expected to be filled with an option matching
  88. /// buf_ content as defined in setExampleBuffer().
  89. ///
  90. /// @param expected_type expected option type
  91. void checkOutputBuffer(uint16_t expected_type) {
  92. // Check if pack worked properly:
  93. const uint8_t* out = static_cast<const uint8_t*>(out_buf_.getData());
  94. // - if option type is correct
  95. EXPECT_EQ(expected_type, out[0]*256 + out[1]);
  96. // - if option length is correct
  97. EXPECT_EQ(25, out[2]*256 + out[3]);
  98. // - if option content is correct
  99. EXPECT_EQ(0, memcmp(out + 4, &buf_[0], 25));
  100. }
  101. OptionBuffer buf_;
  102. OutputBuffer out_buf_;
  103. };
  104. // Tests if a received option is parsed correctly. For the prefix length between
  105. // 0 and 128 the non-significant bits should be set to 0.
  106. TEST_F(Option6IAPrefixTest, parseShort) {
  107. setExampleBuffer();
  108. // Create an option (unpack content)
  109. boost::scoped_ptr<Option6IAPrefix> opt;
  110. ASSERT_NO_THROW(opt.reset(new Option6IAPrefix(D6O_IAPREFIX, buf_.begin(),
  111. buf_.begin() + 25)));
  112. ASSERT_TRUE(opt);
  113. // Pack this option
  114. opt->pack(out_buf_);
  115. EXPECT_EQ(29, out_buf_.getLength());
  116. // The non-significant bits (above 77) of the received prefix should be
  117. // set to zero.
  118. checkOption(*opt, D6O_IAPREFIX, 77, IOAddress("2001:db8:1:0:afa8::"));
  119. // Set non-significant bits in the reference buffer to 0, so as the buffer
  120. // can be directly compared with the option buffer.
  121. buf_[18] = 0xa8;
  122. buf_.insert(buf_.begin() + 19, 5, 0);
  123. checkOutputBuffer(D6O_IAPREFIX);
  124. // Check that option can be disposed safely
  125. EXPECT_NO_THROW(opt.reset());
  126. }
  127. // Tests if a received option holding prefix of 128 bits is parsed correctly.
  128. TEST_F(Option6IAPrefixTest, parseLong) {
  129. setExampleBuffer();
  130. // Set prefix length to the maximal value.
  131. buf_[8] = 128;
  132. // Create an option (unpack content)
  133. boost::scoped_ptr<Option6IAPrefix> opt;
  134. ASSERT_NO_THROW(opt.reset(new Option6IAPrefix(D6O_IAPREFIX, buf_.begin(),
  135. buf_.begin() + 25)));
  136. ASSERT_TRUE(opt);
  137. // Pack this option
  138. opt->pack(out_buf_);
  139. EXPECT_EQ(29, out_buf_.getLength());
  140. checkOption(*opt, D6O_IAPREFIX, 128,
  141. IOAddress("2001:db8:1:0:afaf:0:dead:beef"));
  142. checkOutputBuffer(D6O_IAPREFIX);
  143. // Check that option can be disposed safely
  144. EXPECT_NO_THROW(opt.reset());
  145. }
  146. // Check that the prefix having length of zero is represented as a "::".
  147. TEST_F(Option6IAPrefixTest, parseZero) {
  148. setExampleBuffer();
  149. // Set prefix length to 0.
  150. buf_[8] = 0;
  151. // Create an option (unpack content)
  152. boost::scoped_ptr<Option6IAPrefix> opt;
  153. ASSERT_NO_THROW(opt.reset(new Option6IAPrefix(D6O_IAPREFIX, buf_.begin(),
  154. buf_.begin() + 25)));
  155. ASSERT_TRUE(opt);
  156. // Pack this option
  157. opt->pack(out_buf_);
  158. EXPECT_EQ(29, out_buf_.getLength());
  159. checkOption(*opt, D6O_IAPREFIX, 0, IOAddress("::"));
  160. // Fill the address in the reference buffer with zeros.
  161. buf_.insert(buf_.begin() + 9, 16, 0);
  162. checkOutputBuffer(D6O_IAPREFIX);
  163. // Check that option can be disposed safely
  164. EXPECT_NO_THROW(opt.reset());
  165. }
  166. // Checks whether a new option can be built correctly
  167. TEST_F(Option6IAPrefixTest, build) {
  168. boost::scoped_ptr<Option6IAPrefix> opt;
  169. setExampleBuffer();
  170. ASSERT_NO_THROW(opt.reset(new Option6IAPrefix(12345,
  171. IOAddress("2001:db8:1:0:afaf:0:dead:beef"), 77,
  172. 1000, 3000000000u)));
  173. ASSERT_TRUE(opt);
  174. checkOption(*opt, 12345, 77, IOAddress("2001:db8:1:0:afaf:0:dead:beef"));
  175. // Check if we can build it properly
  176. EXPECT_NO_THROW(opt->pack(out_buf_));
  177. EXPECT_EQ(29, out_buf_.getLength());
  178. checkOutputBuffer(12345);
  179. // Check that option can be disposed safely
  180. EXPECT_NO_THROW(opt.reset());
  181. }
  182. // Checks negative cases
  183. TEST_F(Option6IAPrefixTest, negative) {
  184. // Truncated option (at least 25 bytes is needed)
  185. EXPECT_THROW(Option6IAPrefix(D6O_IAPREFIX, buf_.begin(), buf_.begin() + 24),
  186. OutOfRange);
  187. // Empty option
  188. EXPECT_THROW(Option6IAPrefix(D6O_IAPREFIX, buf_.begin(), buf_.begin()),
  189. OutOfRange);
  190. // This is for IPv6 prefixes only
  191. EXPECT_THROW(Option6IAPrefix(12345, IOAddress("192.0.2.1"), 77, 1000, 2000),
  192. BadValue);
  193. // Prefix length can't be larger than 128
  194. EXPECT_THROW(Option6IAPrefix(12345, IOAddress("2001:db8:1::"),
  195. 255, 1000, 2000),
  196. BadValue);
  197. }
  198. // Checks if the option is converted to textual format correctly.
  199. TEST_F(Option6IAPrefixTest, toText) {
  200. // Create option without suboptions.
  201. Option6IAPrefix opt(D6O_IAPREFIX, IOAddress("2001:db8:1::"), 64, 300, 400);
  202. EXPECT_EQ("type=00026(IAPREFIX), len=00025: prefix=2001:db8:1::/64,"
  203. " preferred-lft=300, valid-lft=400",
  204. opt.toText());
  205. // Add suboptions and make sure they are printed.
  206. opt.addOption(OptionPtr(new OptionUint32(Option::V6, 123, 234)));
  207. opt.addOption(OptionPtr(new OptionUint32(Option::V6, 222, 333)));
  208. EXPECT_EQ("type=00026(IAPREFIX), len=00041: prefix=2001:db8:1::/64,"
  209. " preferred-lft=300, valid-lft=400,\noptions:\n"
  210. " type=00123, len=00004: 234 (uint32)\n"
  211. " type=00222, len=00004: 333 (uint32)",
  212. opt.toText());
  213. }
  214. }