subnet_unittest.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // Copyright (C) 2012-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 <config.h>
  15. #include <asiolink/io_address.h>
  16. #include <dhcp/option.h>
  17. #include <dhcpsrv/subnet.h>
  18. #include <exceptions/exceptions.h>
  19. #include <boost/scoped_ptr.hpp>
  20. #include <gtest/gtest.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, false, "dhcp4"),
  89. isc::BadValue);
  90. // Create NULL pointer option. Attempt to add NULL option
  91. // should result in exception.
  92. OptionPtr option2;
  93. ASSERT_FALSE(option2);
  94. EXPECT_THROW(subnet->addOption(option2, false, "dhcp4"),
  95. isc::BadValue);
  96. }
  97. // This test verifies that inRange() and inPool() methods work properly.
  98. TEST(Subnet4Test, inRangeinPool) {
  99. Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.0.0"), 8, 1, 2, 3));
  100. // this one is in subnet
  101. Pool4Ptr pool1(new Pool4(IOAddress("192.2.0.0"), 16));
  102. subnet->addPool4(pool1);
  103. // 192.1.1.1 belongs to the subnet...
  104. EXPECT_TRUE(subnet->inRange(IOAddress("192.1.1.1")));
  105. // ... but it does not belong to any pool within
  106. EXPECT_FALSE(subnet->inPool(IOAddress("192.1.1.1")));
  107. // the last address that is in range, but out of pool
  108. EXPECT_TRUE(subnet->inRange(IOAddress("192.1.255.255")));
  109. EXPECT_FALSE(subnet->inPool(IOAddress("192.1.255.255")));
  110. // the first address that is in range, in pool
  111. EXPECT_TRUE(subnet->inRange(IOAddress("192.2.0.0")));
  112. EXPECT_TRUE (subnet->inPool(IOAddress("192.2.0.0")));
  113. // let's try something in the middle as well
  114. EXPECT_TRUE(subnet->inRange(IOAddress("192.2.3.4")));
  115. EXPECT_TRUE (subnet->inPool(IOAddress("192.2.3.4")));
  116. // the last address that is in range, in pool
  117. EXPECT_TRUE(subnet->inRange(IOAddress("192.2.255.255")));
  118. EXPECT_TRUE (subnet->inPool(IOAddress("192.2.255.255")));
  119. // the first address that is in range, but out of pool
  120. EXPECT_TRUE(subnet->inRange(IOAddress("192.3.0.0")));
  121. EXPECT_FALSE(subnet->inPool(IOAddress("192.3.0.0")));
  122. }
  123. // This test checks if the toText() method returns text representation
  124. TEST(Subnet4Test, toText) {
  125. Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 24, 1, 2, 3));
  126. EXPECT_EQ("192.0.2.0/24", subnet->toText());
  127. }
  128. // This test checks if the get() method returns proper parameters
  129. TEST(Subnet4Test, get) {
  130. Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 28, 1, 2, 3));
  131. EXPECT_EQ("192.0.2.0", subnet->get().first.toText());
  132. EXPECT_EQ(28, subnet->get().second);
  133. }
  134. // Tests for Subnet6
  135. TEST(Subnet6Test, constructor) {
  136. EXPECT_NO_THROW(Subnet6 subnet1(IOAddress("2001:db8:1::"), 64,
  137. 1, 2, 3, 4));
  138. EXPECT_THROW(Subnet6 subnet2(IOAddress("2001:db8:1::"), 129, 1, 2, 3, 4),
  139. BadValue); // invalid prefix length
  140. EXPECT_THROW(Subnet6 subnet3(IOAddress("192.168.0.0"), 32, 1, 2, 3, 4),
  141. BadValue); // IPv4 addresses are not allowed in Subnet6
  142. }
  143. TEST(Subnet6Test, in_range) {
  144. Subnet6 subnet(IOAddress("2001:db8:1::"), 64, 1000, 2000, 3000, 4000);
  145. EXPECT_EQ(1000, subnet.getT1());
  146. EXPECT_EQ(2000, subnet.getT2());
  147. EXPECT_EQ(3000, subnet.getPreferred());
  148. EXPECT_EQ(4000, subnet.getValid());
  149. EXPECT_FALSE(subnet.inRange(IOAddress("2001:db8:0:ffff:ffff:ffff:ffff:ffff")));
  150. EXPECT_TRUE(subnet.inRange(IOAddress("2001:db8:1::0")));
  151. EXPECT_TRUE(subnet.inRange(IOAddress("2001:db8:1::1")));
  152. EXPECT_TRUE(subnet.inRange(IOAddress("2001:db8:1::ffff:ffff:ffff:ffff")));
  153. EXPECT_FALSE(subnet.inRange(IOAddress("2001:db8:1:1::")));
  154. EXPECT_FALSE(subnet.inRange(IOAddress("::")));
  155. }
  156. TEST(Subnet6Test, Pool6InSubnet6) {
  157. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  158. Pool6Ptr pool1(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1:1::"), 64));
  159. Pool6Ptr pool2(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1:2::"), 64));
  160. Pool6Ptr pool3(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1:3::"), 64));
  161. subnet->addPool6(pool1);
  162. // If there's only one pool, get that pool
  163. Pool6Ptr mypool = subnet->getPool6();
  164. EXPECT_EQ(mypool, pool1);
  165. subnet->addPool6(pool2);
  166. subnet->addPool6(pool3);
  167. // If there are more than one pool and we didn't provide hint, we
  168. // should get the first pool
  169. mypool = subnet->getPool6();
  170. EXPECT_EQ(mypool, pool1);
  171. // If we provide a hint, we should get a pool that this hint belongs to
  172. mypool = subnet->getPool6(IOAddress("2001:db8:1:3::dead:beef"));
  173. EXPECT_EQ(mypool, pool3);
  174. }
  175. TEST(Subnet6Test, Subnet6_Pool6_checks) {
  176. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  177. // this one is in subnet
  178. Pool6Ptr pool1(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1:1::"), 64));
  179. subnet->addPool6(pool1);
  180. // this one is larger than the subnet!
  181. Pool6Ptr pool2(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8::"), 48));
  182. EXPECT_THROW(subnet->addPool6(pool2), BadValue);
  183. // this one is totally out of blue
  184. Pool6Ptr pool3(new Pool6(Pool6::TYPE_IA, IOAddress("3000::"), 16));
  185. EXPECT_THROW(subnet->addPool6(pool3), BadValue);
  186. Pool6Ptr pool4(new Pool6(Pool6::TYPE_IA, IOAddress("4001:db8:1::"), 80));
  187. EXPECT_THROW(subnet->addPool6(pool4), BadValue);
  188. }
  189. TEST(Subnet6Test, addOptions) {
  190. // Create as subnet to add options to it.
  191. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  192. // Differentiate options by their codes (100-109)
  193. for (uint16_t code = 100; code < 110; ++code) {
  194. OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
  195. ASSERT_NO_THROW(subnet->addOption(option, false, "dhcp6"));
  196. }
  197. // Add 7 options to another option space. The option codes partially overlap
  198. // with option codes that we have added to dhcp6 option space.
  199. for (uint16_t code = 105; code < 112; ++code) {
  200. OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
  201. ASSERT_NO_THROW(subnet->addOption(option, false, "isc"));
  202. }
  203. // Get options from the Subnet and check if all 10 are there.
  204. Subnet::OptionContainer options = subnet->getOptions("dhcp6");
  205. ASSERT_EQ(10, options.size());
  206. // Validate codes of options added to dhcp6 option space.
  207. uint16_t expected_code = 100;
  208. for (Subnet::OptionContainer::const_iterator option_desc = options.begin();
  209. option_desc != options.end(); ++option_desc) {
  210. ASSERT_TRUE(option_desc->option);
  211. EXPECT_EQ(expected_code, option_desc->option->getType());
  212. ++expected_code;
  213. }
  214. options = subnet->getOptions("isc");
  215. ASSERT_EQ(7, options.size());
  216. // Validate codes of options added to isc option space.
  217. expected_code = 105;
  218. for (Subnet::OptionContainer::const_iterator option_desc = options.begin();
  219. option_desc != options.end(); ++option_desc) {
  220. ASSERT_TRUE(option_desc->option);
  221. EXPECT_EQ(expected_code, option_desc->option->getType());
  222. ++expected_code;
  223. }
  224. // Try to get options from a non-existing option space.
  225. options = subnet->getOptions("abcd");
  226. EXPECT_TRUE(options.empty());
  227. // Delete options from all spaces.
  228. subnet->delOptions();
  229. // Make sure that all options have been removed.
  230. options = subnet->getOptions("dhcp6");
  231. EXPECT_EQ(0, options.size());
  232. options = subnet->getOptions("isc");
  233. EXPECT_EQ(0, options.size());
  234. }
  235. TEST(Subnet6Test, addNonUniqueOptions) {
  236. // Create as subnet to add options to it.
  237. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  238. // Create a set of options with non-unique codes.
  239. for (int i = 0; i < 2; ++i) {
  240. // In the inner loop we create options with unique codes (100-109).
  241. for (uint16_t code = 100; code < 110; ++code) {
  242. OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
  243. ASSERT_NO_THROW(subnet->addOption(option, false, "dhcp6"));
  244. }
  245. }
  246. // Sanity check that all options are there.
  247. Subnet::OptionContainer options = subnet->getOptions("dhcp6");
  248. ASSERT_EQ(20, options.size());
  249. // Use container index #1 to get the options by their codes.
  250. Subnet::OptionContainerTypeIndex& idx = options.get<1>();
  251. // Look for the codes 100-109.
  252. for (uint16_t code = 100; code < 110; ++ code) {
  253. // For each code we should get two instances of options.
  254. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  255. Subnet::OptionContainerTypeIndex::const_iterator> range =
  256. idx.equal_range(code);
  257. // Distance between iterators indicates how many options
  258. // have been retured for the particular code.
  259. ASSERT_EQ(2, distance(range.first, range.second));
  260. // Check that returned options actually have the expected option code.
  261. for (Subnet::OptionContainerTypeIndex::const_iterator option_desc = range.first;
  262. option_desc != range.second; ++option_desc) {
  263. ASSERT_TRUE(option_desc->option);
  264. EXPECT_EQ(code, option_desc->option->getType());
  265. }
  266. }
  267. // Let's try to find some non-exiting option.
  268. const uint16_t non_existing_code = 150;
  269. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  270. Subnet::OptionContainerTypeIndex::const_iterator> range =
  271. idx.equal_range(non_existing_code);
  272. // Empty set is expected.
  273. EXPECT_EQ(0, distance(range.first, range.second));
  274. subnet->delOptions();
  275. options = subnet->getOptions("dhcp6");
  276. EXPECT_EQ(0, options.size());
  277. }
  278. TEST(Subnet6Test, addInvalidOption) {
  279. // Create as subnet to add options to it.
  280. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  281. // Some dummy option code.
  282. uint16_t code = 100;
  283. // Create option with invalid universe (V4 instead of V6).
  284. // Attempt to add this option should result in exception.
  285. OptionPtr option1(new Option(Option::V4, code, OptionBuffer(10, 0xFF)));
  286. EXPECT_THROW(subnet->addOption(option1, false, "dhcp6"), isc::BadValue);
  287. // Create NULL pointer option. Attempt to add NULL option
  288. // should result in exception.
  289. OptionPtr option2;
  290. ASSERT_FALSE(option2);
  291. EXPECT_THROW(subnet->addOption(option2, false, "dhcp6"), isc::BadValue);
  292. }
  293. TEST(Subnet6Test, addPersistentOption) {
  294. // Create as subnet to add options to it.
  295. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  296. // Add 10 options to the subnet with option codes 100 - 109.
  297. for (uint16_t code = 100; code < 110; ++code) {
  298. OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
  299. // We create 10 options and want some of them to be flagged
  300. // persistent and some non-persistent. Persistent options are
  301. // those that server sends to clients regardless if they ask
  302. // for them or not. We pick 3 out of 10 options and mark them
  303. // non-persistent and 7 other options persistent.
  304. // Code values: 102, 105 and 108 are divisable by 3
  305. // and options with these codes will be flagged non-persistent.
  306. // Options with other codes will be flagged persistent.
  307. bool persistent = (code % 3) ? true : false;
  308. ASSERT_NO_THROW(subnet->addOption(option, persistent, "dhcp6"));
  309. }
  310. // Get added options from the subnet.
  311. Subnet::OptionContainer options = subnet->getOptions("dhcp6");
  312. // options.get<2> returns reference to container index #2. This
  313. // index is used to access options by the 'persistent' flag.
  314. Subnet::OptionContainerPersistIndex& idx = options.get<2>();
  315. // Get all persistent options.
  316. std::pair<Subnet::OptionContainerPersistIndex::const_iterator,
  317. Subnet::OptionContainerPersistIndex::const_iterator> range_persistent =
  318. idx.equal_range(true);
  319. // 3 out of 10 options have been flagged persistent.
  320. ASSERT_EQ(7, distance(range_persistent.first, range_persistent.second));
  321. // Get all non-persistent options.
  322. std::pair<Subnet::OptionContainerPersistIndex::const_iterator,
  323. Subnet::OptionContainerPersistIndex::const_iterator> range_non_persistent =
  324. idx.equal_range(false);
  325. // 7 out of 10 options have been flagged persistent.
  326. ASSERT_EQ(3, distance(range_non_persistent.first, range_non_persistent.second));
  327. subnet->delOptions();
  328. options = subnet->getOptions("dhcp6");
  329. EXPECT_EQ(0, options.size());
  330. }
  331. // This test verifies that inRange() and inPool() methods work properly.
  332. TEST(Subnet6Test, inRangeinPool) {
  333. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 32, 1, 2, 3, 4));
  334. // this one is in subnet
  335. Pool6Ptr pool1(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8::10"),
  336. IOAddress("2001:db8::20")));
  337. subnet->addPool6(pool1);
  338. // 192.1.1.1 belongs to the subnet...
  339. EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::1")));
  340. // ... but it does not belong to any pool within
  341. EXPECT_FALSE(subnet->inPool(IOAddress("2001:db8::1")));
  342. // the last address that is in range, but out of pool
  343. EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::f")));
  344. EXPECT_FALSE(subnet->inPool(IOAddress("2001:db8::f")));
  345. // the first address that is in range, in pool
  346. EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::10")));
  347. EXPECT_TRUE (subnet->inPool(IOAddress("2001:db8::10")));
  348. // let's try something in the middle as well
  349. EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::18")));
  350. EXPECT_TRUE (subnet->inPool(IOAddress("2001:db8::18")));
  351. // the last address that is in range, in pool
  352. EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::20")));
  353. EXPECT_TRUE (subnet->inPool(IOAddress("2001:db8::20")));
  354. // the first address that is in range, but out of pool
  355. EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::21")));
  356. EXPECT_FALSE(subnet->inPool(IOAddress("2001:db8::21")));
  357. }
  358. // This test checks if the toText() method returns text representation
  359. TEST(Subnet6Test, toText) {
  360. Subnet6 subnet(IOAddress("2001:db8::"), 32, 1, 2, 3, 4);
  361. EXPECT_EQ("2001:db8::/32", subnet.toText());
  362. }
  363. // This test checks if the get() method returns proper parameters
  364. TEST(Subnet6Test, get) {
  365. Subnet6 subnet(IOAddress("2001:db8::"), 32, 1, 2, 3, 4);
  366. EXPECT_EQ("2001:db8::", subnet.get().first.toText());
  367. EXPECT_EQ(32, subnet.get().second);
  368. }
  369. };