subnet_unittest.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // Copyright (C) 2012 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 <dhcp/subnet.h>
  16. #include <dhcp/option.h>
  17. #include <exceptions/exceptions.h>
  18. #include <boost/scoped_ptr.hpp>
  19. #include <gtest/gtest.h>
  20. #include <asiolink/io_address.h>
  21. // don't import the entire boost namespace. It will unexpectedly hide uint8_t
  22. // for some systems.
  23. using boost::scoped_ptr;
  24. using namespace isc;
  25. using namespace isc::dhcp;
  26. using namespace isc::asiolink;
  27. namespace {
  28. TEST(Subnet4Test, constructor) {
  29. EXPECT_NO_THROW(Subnet4 subnet1(IOAddress("192.0.2.2"), 16,
  30. 1, 2, 3));
  31. EXPECT_THROW(Subnet4 subnet2(IOAddress("192.0.2.0"), 33, 1, 2, 3),
  32. BadValue); // invalid prefix length
  33. EXPECT_THROW(Subnet4 subnet3(IOAddress("2001:db8::1"), 24, 1, 2, 3),
  34. BadValue); // IPv6 addresses are not allowed in Subnet4
  35. }
  36. TEST(Subnet4Test, in_range) {
  37. Subnet4 subnet(IOAddress("192.0.2.1"), 24, 1000, 2000, 3000);
  38. EXPECT_EQ(1000, subnet.getT1());
  39. EXPECT_EQ(2000, subnet.getT2());
  40. EXPECT_EQ(3000, subnet.getValid());
  41. EXPECT_FALSE(subnet.inRange(IOAddress("192.0.0.0")));
  42. EXPECT_TRUE(subnet.inRange(IOAddress("192.0.2.0")));
  43. EXPECT_TRUE(subnet.inRange(IOAddress("192.0.2.1")));
  44. EXPECT_TRUE(subnet.inRange(IOAddress("192.0.2.255")));
  45. EXPECT_FALSE(subnet.inRange(IOAddress("192.0.3.0")));
  46. EXPECT_FALSE(subnet.inRange(IOAddress("0.0.0.0")));
  47. EXPECT_FALSE(subnet.inRange(IOAddress("255.255.255.255")));
  48. }
  49. TEST(Subnet4Test, Pool4InSubnet4) {
  50. Subnet4Ptr subnet(new Subnet4(IOAddress("192.1.2.0"), 24, 1, 2, 3));
  51. Pool4Ptr pool1(new Pool4(IOAddress("192.1.2.0"), 25));
  52. Pool4Ptr pool2(new Pool4(IOAddress("192.1.2.128"), 26));
  53. Pool4Ptr pool3(new Pool4(IOAddress("192.1.2.192"), 30));
  54. subnet->addPool4(pool1);
  55. // If there's only one pool, get that pool
  56. Pool4Ptr mypool = subnet->getPool4();
  57. EXPECT_EQ(mypool, pool1);
  58. subnet->addPool4(pool2);
  59. subnet->addPool4(pool3);
  60. // If there are more than one pool and we didn't provide hint, we
  61. // should get the first pool
  62. mypool = subnet->getPool4();
  63. EXPECT_EQ(mypool, pool1);
  64. // If we provide a hint, we should get a pool that this hint belongs to
  65. mypool = subnet->getPool4(IOAddress("192.1.2.195"));
  66. EXPECT_EQ(mypool, pool3);
  67. }
  68. TEST(Subnet4Test, Subnet4_Pool4_checks) {
  69. Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 8, 1, 2, 3));
  70. // this one is in subnet
  71. Pool4Ptr pool1(new Pool4(IOAddress("192.255.0.0"), 16));
  72. subnet->addPool4(pool1);
  73. // this one is larger than the subnet!
  74. Pool4Ptr pool2(new Pool4(IOAddress("193.0.0.0"), 24));
  75. EXPECT_THROW(subnet->addPool4(pool2), BadValue);
  76. // this one is totally out of blue
  77. Pool4Ptr pool3(new Pool4(IOAddress("1.2.3.4"), 16));
  78. EXPECT_THROW(subnet->addPool4(pool3), BadValue);
  79. }
  80. TEST(Subnet4Test, addInvalidOption) {
  81. // Create the V4 subnet.
  82. Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 8, 1, 2, 3));
  83. // Some dummy option code.
  84. uint16_t code = 100;
  85. // Create option with invalid universe (V6 instead of V4).
  86. // Attempt to add this option should result in exception.
  87. OptionPtr option1(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
  88. EXPECT_THROW(subnet->addOption(option1), isc::BadValue);
  89. // Create NULL pointer option. Attempt to add NULL option
  90. // should result in exception.
  91. OptionPtr option2;
  92. ASSERT_FALSE(option2);
  93. EXPECT_THROW(subnet->addOption(option2), isc::BadValue);
  94. }
  95. // Tests for Subnet6
  96. TEST(Subnet6Test, constructor) {
  97. EXPECT_NO_THROW(Subnet6 subnet1(IOAddress("2001:db8:1::"), 64,
  98. 1, 2, 3, 4));
  99. EXPECT_THROW(Subnet6 subnet2(IOAddress("2001:db8:1::"), 129, 1, 2, 3, 4),
  100. BadValue); // invalid prefix length
  101. EXPECT_THROW(Subnet6 subnet3(IOAddress("192.168.0.0"), 32, 1, 2, 3, 4),
  102. BadValue); // IPv4 addresses are not allowed in Subnet6
  103. }
  104. TEST(Subnet6Test, in_range) {
  105. Subnet6 subnet(IOAddress("2001:db8:1::"), 64, 1000, 2000, 3000, 4000);
  106. EXPECT_EQ(1000, subnet.getT1());
  107. EXPECT_EQ(2000, subnet.getT2());
  108. EXPECT_EQ(3000, subnet.getPreferred());
  109. EXPECT_EQ(4000, subnet.getValid());
  110. EXPECT_FALSE(subnet.inRange(IOAddress("2001:db8:0:ffff:ffff:ffff:ffff:ffff")));
  111. EXPECT_TRUE(subnet.inRange(IOAddress("2001:db8:1::0")));
  112. EXPECT_TRUE(subnet.inRange(IOAddress("2001:db8:1::1")));
  113. EXPECT_TRUE(subnet.inRange(IOAddress("2001:db8:1::ffff:ffff:ffff:ffff")));
  114. EXPECT_FALSE(subnet.inRange(IOAddress("2001:db8:1:1::")));
  115. EXPECT_FALSE(subnet.inRange(IOAddress("::")));
  116. }
  117. TEST(Subnet6Test, Pool6InSubnet6) {
  118. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  119. Pool6Ptr pool1(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1:1::"), 64));
  120. Pool6Ptr pool2(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1:2::"), 64));
  121. Pool6Ptr pool3(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1:3::"), 64));
  122. subnet->addPool6(pool1);
  123. // If there's only one pool, get that pool
  124. Pool6Ptr mypool = subnet->getPool6();
  125. EXPECT_EQ(mypool, pool1);
  126. subnet->addPool6(pool2);
  127. subnet->addPool6(pool3);
  128. // If there are more than one pool and we didn't provide hint, we
  129. // should get the first pool
  130. mypool = subnet->getPool6();
  131. EXPECT_EQ(mypool, pool1);
  132. // If we provide a hint, we should get a pool that this hint belongs to
  133. mypool = subnet->getPool6(IOAddress("2001:db8:1:3::dead:beef"));
  134. EXPECT_EQ(mypool, pool3);
  135. }
  136. TEST(Subnet6Test, Subnet6_Pool6_checks) {
  137. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  138. // this one is in subnet
  139. Pool6Ptr pool1(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1:1::"), 64));
  140. subnet->addPool6(pool1);
  141. // this one is larger than the subnet!
  142. Pool6Ptr pool2(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8::"), 48));
  143. EXPECT_THROW(subnet->addPool6(pool2), BadValue);
  144. // this one is totally out of blue
  145. Pool6Ptr pool3(new Pool6(Pool6::TYPE_IA, IOAddress("3000::"), 16));
  146. EXPECT_THROW(subnet->addPool6(pool3), BadValue);
  147. Pool6Ptr pool4(new Pool6(Pool6::TYPE_IA, IOAddress("4001:db8:1::"), 80));
  148. EXPECT_THROW(subnet->addPool6(pool4), BadValue);
  149. }
  150. TEST(Subnet6Test, addOptions) {
  151. // Create as subnet to add options to it.
  152. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  153. // Differentiate options by their codes (100-109)
  154. for (uint16_t code = 100; code < 110; ++code) {
  155. OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
  156. ASSERT_NO_THROW(subnet->addOption(option));
  157. }
  158. // Get options from the Subnet and check if all 10 are there.
  159. Subnet::OptionContainer options = subnet->getOptions();
  160. ASSERT_EQ(10, options.size());
  161. // Validate codes of added options.
  162. uint16_t expected_code = 100;
  163. for (Subnet::OptionContainer::const_iterator option_desc = options.begin();
  164. option_desc != options.end(); ++option_desc) {
  165. ASSERT_TRUE(option_desc->option);
  166. EXPECT_EQ(expected_code, option_desc->option->getType());
  167. ++expected_code;
  168. }
  169. subnet->delOptions();
  170. options = subnet->getOptions();
  171. EXPECT_EQ(0, options.size());
  172. }
  173. TEST(Subnet6Test, addNonUniqueOptions) {
  174. // Create as subnet to add options to it.
  175. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  176. // Create a set of options with non-unique codes.
  177. for (int i = 0; i < 2; ++i) {
  178. // In the inner loop we create options with unique codes (100-109).
  179. for (uint16_t code = 100; code < 110; ++code) {
  180. OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
  181. ASSERT_NO_THROW(subnet->addOption(option));
  182. }
  183. }
  184. // Sanity check that all options are there.
  185. Subnet::OptionContainer options = subnet->getOptions();
  186. ASSERT_EQ(20, options.size());
  187. // Use container index #1 to get the options by their codes.
  188. Subnet::OptionContainerTypeIndex& idx = options.get<1>();
  189. // Look for the codes 100-109.
  190. for (uint16_t code = 100; code < 110; ++ code) {
  191. // For each code we should get two instances of options.
  192. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  193. Subnet::OptionContainerTypeIndex::const_iterator> range =
  194. idx.equal_range(code);
  195. // Distance between iterators indicates how many options
  196. // have been retured for the particular code.
  197. ASSERT_EQ(2, distance(range.first, range.second));
  198. // Check that returned options actually have the expected option code.
  199. for (Subnet::OptionContainerTypeIndex::const_iterator option_desc = range.first;
  200. option_desc != range.second; ++option_desc) {
  201. ASSERT_TRUE(option_desc->option);
  202. EXPECT_EQ(code, option_desc->option->getType());
  203. }
  204. }
  205. // Let's try to find some non-exiting option.
  206. const uint16_t non_existing_code = 150;
  207. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  208. Subnet::OptionContainerTypeIndex::const_iterator> range =
  209. idx.equal_range(non_existing_code);
  210. // Empty set is expected.
  211. EXPECT_EQ(0, distance(range.first, range.second));
  212. subnet->delOptions();
  213. options = subnet->getOptions();
  214. EXPECT_EQ(0, options.size());
  215. }
  216. TEST(Subnet6Test, addInvalidOption) {
  217. // Create as subnet to add options to it.
  218. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  219. // Some dummy option code.
  220. uint16_t code = 100;
  221. // Create option with invalid universe (V4 instead of V6).
  222. // Attempt to add this option should result in exception.
  223. OptionPtr option1(new Option(Option::V4, code, OptionBuffer(10, 0xFF)));
  224. EXPECT_THROW(subnet->addOption(option1), isc::BadValue);
  225. // Create NULL pointer option. Attempt to add NULL option
  226. // should result in exception.
  227. OptionPtr option2;
  228. ASSERT_FALSE(option2);
  229. EXPECT_THROW(subnet->addOption(option2), isc::BadValue);
  230. }
  231. TEST(Subnet6Test, addPersistentOption) {
  232. // Create as subnet to add options to it.
  233. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  234. // Add 10 options to the subnet with option codes 100 - 109.
  235. for (uint16_t code = 100; code < 110; ++code) {
  236. OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
  237. // We create 10 options and want some of them to be flagged
  238. // persistent and some non-persistent. Persistent options are
  239. // those that server sends to clients regardless if they ask
  240. // for them or not. We pick 3 out of 10 options and mark them
  241. // non-persistent and 7 other options persistent.
  242. // Code values: 102, 105 and 108 are divisable by 3
  243. // and options with these codes will be flagged non-persistent.
  244. // Options with other codes will be flagged persistent.
  245. bool persistent = (code % 3) ? true : false;
  246. ASSERT_NO_THROW(subnet->addOption(option, persistent));
  247. }
  248. // Get added options from the subnet.
  249. Subnet::OptionContainer options = subnet->getOptions();
  250. // options.get<2> returns reference to container index #2. This
  251. // index is used to access options by the 'persistent' flag.
  252. Subnet::OptionContainerPersistIndex& idx = options.get<2>();
  253. // Get all persistent options.
  254. std::pair<Subnet::OptionContainerPersistIndex::const_iterator,
  255. Subnet::OptionContainerPersistIndex::const_iterator> range_persistent =
  256. idx.equal_range(true);
  257. // 3 out of 10 options have been flagged persistent.
  258. ASSERT_EQ(7, distance(range_persistent.first, range_persistent.second));
  259. // Get all non-persistent options.
  260. std::pair<Subnet::OptionContainerPersistIndex::const_iterator,
  261. Subnet::OptionContainerPersistIndex::const_iterator> range_non_persistent =
  262. idx.equal_range(false);
  263. // 7 out of 10 options have been flagged persistent.
  264. ASSERT_EQ(3, distance(range_non_persistent.first, range_non_persistent.second));
  265. subnet->delOptions();
  266. options = subnet->getOptions();
  267. EXPECT_EQ(0, options.size());
  268. }
  269. };