option4_addrlst_unittest.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Copyright (C) 2011 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/dhcp4.h>
  17. #include <dhcp/option.h>
  18. #include <dhcp/option4_addrlst.h>
  19. #include <util/buffer.h>
  20. #include <gtest/gtest.h>
  21. #include <iostream>
  22. #include <sstream>
  23. #include <arpa/inet.h>
  24. using namespace std;
  25. using namespace isc;
  26. using namespace isc::dhcp;
  27. using namespace isc::asiolink;
  28. using namespace isc::util;
  29. namespace {
  30. // a sample data (list of 4 addresses)
  31. const uint8_t sampledata[] = {
  32. 192, 0, 2, 3, // 192.0.2.3
  33. 255, 255, 255, 0, // 255.255.255.0 - popular netmask
  34. 0, 0, 0 , 0, // used for default routes or (any address)
  35. 127, 0, 0, 1 // loopback
  36. };
  37. // expected on-wire format for an option with 1 address
  38. const uint8_t expected1[] = { // 1 address
  39. DHO_DOMAIN_NAME_SERVERS, 4, // type, length
  40. 192, 0, 2, 3, // 192.0.2.3
  41. };
  42. // expected on-wire format for an option with 4 addresses
  43. const uint8_t expected4[] = { // 4 addresses
  44. 254, 16, // type = 254, len = 16
  45. 192, 0, 2, 3, // 192.0.2.3
  46. 255, 255, 255, 0, // 255.255.255.0 - popular netmask
  47. 0, 0, 0 ,0, // used for default routes or (any address)
  48. 127, 0, 0, 1 // loopback
  49. };
  50. class Option4AddrLstTest : public ::testing::Test {
  51. protected:
  52. Option4AddrLstTest():
  53. vec_(vector<uint8_t>(300,0)) // 300 bytes long filled with 0s
  54. {
  55. sampleAddrs_.push_back(IOAddress("192.0.2.3"));
  56. sampleAddrs_.push_back(IOAddress("255.255.255.0"));
  57. sampleAddrs_.push_back(IOAddress("0.0.0.0"));
  58. sampleAddrs_.push_back(IOAddress("127.0.0.1"));
  59. }
  60. vector<uint8_t> vec_;
  61. Option4AddrLst::AddressContainer sampleAddrs_;
  62. };
  63. TEST_F(Option4AddrLstTest, parse1) {
  64. memcpy(&vec_[0], sampledata, sizeof(sampledata));
  65. // just one address
  66. Option4AddrLst* opt1 = 0;
  67. EXPECT_NO_THROW(
  68. opt1 = new Option4AddrLst(DHO_DOMAIN_NAME_SERVERS,
  69. vec_.begin(),
  70. vec_.begin()+4);
  71. // use just first address (4 bytes), not the whole
  72. // sampledata
  73. );
  74. EXPECT_EQ(Option::V4, opt1->getUniverse());
  75. EXPECT_EQ(DHO_DOMAIN_NAME_SERVERS, opt1->getType());
  76. EXPECT_EQ(6, opt1->len()); // 2 (header) + 4 (1x IPv4 addr)
  77. Option4AddrLst::AddressContainer addrs = opt1->getAddresses();
  78. ASSERT_EQ(1, addrs.size());
  79. EXPECT_EQ("192.0.2.3", addrs[0].toText());
  80. EXPECT_NO_THROW(
  81. delete opt1;
  82. opt1 = 0;
  83. );
  84. // 1 address
  85. }
  86. TEST_F(Option4AddrLstTest, parse4) {
  87. vector<uint8_t> buffer(300,0); // 300 bytes long filled with 0s
  88. memcpy(&buffer[0], sampledata, sizeof(sampledata));
  89. // 4 addresses
  90. Option4AddrLst* opt4 = 0;
  91. EXPECT_NO_THROW(
  92. opt4 = new Option4AddrLst(254,
  93. buffer.begin(),
  94. buffer.begin()+sizeof(sampledata));
  95. );
  96. EXPECT_EQ(Option::V4, opt4->getUniverse());
  97. EXPECT_EQ(254, opt4->getType());
  98. EXPECT_EQ(18, opt4->len()); // 2 (header) + 16 (4x IPv4 addrs)
  99. Option4AddrLst::AddressContainer addrs = opt4->getAddresses();
  100. ASSERT_EQ(4, addrs.size());
  101. EXPECT_EQ("192.0.2.3", addrs[0].toText());
  102. EXPECT_EQ("255.255.255.0", addrs[1].toText());
  103. EXPECT_EQ("0.0.0.0", addrs[2].toText());
  104. EXPECT_EQ("127.0.0.1", addrs[3].toText());
  105. EXPECT_NO_THROW(
  106. delete opt4;
  107. opt4 = 0;
  108. );
  109. }
  110. TEST_F(Option4AddrLstTest, assembly1) {
  111. Option4AddrLst* opt = 0;
  112. EXPECT_NO_THROW(
  113. opt = new Option4AddrLst(DHO_DOMAIN_NAME_SERVERS, IOAddress("192.0.2.3"));
  114. );
  115. EXPECT_EQ(Option::V4, opt->getUniverse());
  116. EXPECT_EQ(DHO_DOMAIN_NAME_SERVERS, opt->getType());
  117. Option4AddrLst::AddressContainer addrs = opt->getAddresses();
  118. ASSERT_EQ(1, addrs.size() );
  119. EXPECT_EQ("192.0.2.3", addrs[0].toText());
  120. OutputBuffer buf(100);
  121. EXPECT_NO_THROW(
  122. opt->pack(buf);
  123. );
  124. ASSERT_EQ(6, opt->len());
  125. ASSERT_EQ(6, buf.getLength());
  126. EXPECT_EQ(0, memcmp(expected1, buf.getData(), 6));
  127. EXPECT_NO_THROW(
  128. delete opt;
  129. opt = 0;
  130. );
  131. // This is old-fashioned option. We don't serve IPv6 types here!
  132. EXPECT_THROW(
  133. opt = new Option4AddrLst(DHO_DOMAIN_NAME_SERVERS, IOAddress("2001:db8::1")),
  134. BadValue
  135. );
  136. if (opt) {
  137. // test failed. Exception was not thrown, but option was created instead.
  138. delete opt;
  139. }
  140. }
  141. TEST_F(Option4AddrLstTest, assembly4) {
  142. Option4AddrLst* opt = 0;
  143. EXPECT_NO_THROW(
  144. opt = new Option4AddrLst(254, sampleAddrs_);
  145. );
  146. EXPECT_EQ(Option::V4, opt->getUniverse());
  147. EXPECT_EQ(254, opt->getType());
  148. Option4AddrLst::AddressContainer addrs = opt->getAddresses();
  149. ASSERT_EQ(4, addrs.size() );
  150. EXPECT_EQ("192.0.2.3", addrs[0].toText());
  151. EXPECT_EQ("255.255.255.0", addrs[1].toText());
  152. EXPECT_EQ("0.0.0.0", addrs[2].toText());
  153. EXPECT_EQ("127.0.0.1", addrs[3].toText());
  154. OutputBuffer buf(100);
  155. EXPECT_NO_THROW(
  156. opt->pack(buf);
  157. );
  158. ASSERT_EQ(18, opt->len()); // 2(header) + 4xsizeof(IPv4addr)
  159. ASSERT_EQ(18, buf.getLength());
  160. ASSERT_EQ(0, memcmp(expected4, buf.getData(), 18));
  161. EXPECT_NO_THROW(
  162. delete opt;
  163. opt = 0;
  164. );
  165. // This is old-fashioned option. We don't serve IPv6 types here!
  166. sampleAddrs_.push_back(IOAddress("2001:db8::1"));
  167. EXPECT_THROW(
  168. opt = new Option4AddrLst(DHO_DOMAIN_NAME_SERVERS, sampleAddrs_),
  169. BadValue
  170. );
  171. if (opt) {
  172. // test failed. Exception was not thrown, but option was created instead.
  173. delete opt;
  174. }
  175. }
  176. TEST_F(Option4AddrLstTest, setAddress) {
  177. Option4AddrLst* opt = 0;
  178. EXPECT_NO_THROW(
  179. opt = new Option4AddrLst(123, IOAddress("1.2.3.4"));
  180. );
  181. opt->setAddress(IOAddress("192.0.255.255"));
  182. Option4AddrLst::AddressContainer addrs = opt->getAddresses();
  183. ASSERT_EQ(1, addrs.size() );
  184. EXPECT_EQ("192.0.255.255", addrs[0].toText());
  185. // We should accept IPv4-only addresses.
  186. EXPECT_THROW(
  187. opt->setAddress(IOAddress("2001:db8::1")),
  188. BadValue
  189. );
  190. EXPECT_NO_THROW(
  191. delete opt;
  192. );
  193. }
  194. TEST_F(Option4AddrLstTest, setAddresses) {
  195. Option4AddrLst* opt = 0;
  196. EXPECT_NO_THROW(
  197. opt = new Option4AddrLst(123); // empty list
  198. );
  199. opt->setAddresses(sampleAddrs_);
  200. Option4AddrLst::AddressContainer addrs = opt->getAddresses();
  201. ASSERT_EQ(4, addrs.size() );
  202. EXPECT_EQ("192.0.2.3", addrs[0].toText());
  203. EXPECT_EQ("255.255.255.0", addrs[1].toText());
  204. EXPECT_EQ("0.0.0.0", addrs[2].toText());
  205. EXPECT_EQ("127.0.0.1", addrs[3].toText());
  206. // We should accept IPv4-only addresses.
  207. sampleAddrs_.push_back(IOAddress("2001:db8::1"));
  208. EXPECT_THROW(
  209. opt->setAddresses(sampleAddrs_),
  210. BadValue
  211. );
  212. EXPECT_NO_THROW(
  213. delete opt;
  214. );
  215. }
  216. } // namespace