subnet_unittest.cc 17 KB

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