subnet_unittest.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 <exceptions/exceptions.h>
  17. #include <boost/scoped_ptr.hpp>
  18. #include <gtest/gtest.h>
  19. #include <asiolink/io_address.h>
  20. // don't import the entire boost namespace. It will unexpectedly hide uint8_t
  21. // for some systems.
  22. using boost::scoped_ptr;
  23. using namespace isc;
  24. using namespace isc::dhcp;
  25. using namespace isc::asiolink;
  26. namespace {
  27. TEST(Subnet4Test, constructor) {
  28. EXPECT_NO_THROW(Subnet4 subnet1(IOAddress("192.0.2.2"), 16,
  29. 1, 2, 3));
  30. EXPECT_THROW(Subnet4 subnet2(IOAddress("192.0.2.0"), 33, 1, 2, 3),
  31. BadValue); // invalid prefix length
  32. EXPECT_THROW(Subnet4 subnet3(IOAddress("2001:db8::1"), 24, 1, 2, 3),
  33. BadValue); // IPv6 addresses are not allowed in Subnet4
  34. }
  35. TEST(Subnet4Test, in_range) {
  36. Subnet4 subnet(IOAddress("192.0.2.1"), 24, 1000, 2000, 3000);
  37. EXPECT_EQ(1000, subnet.getT1());
  38. EXPECT_EQ(2000, subnet.getT2());
  39. EXPECT_EQ(3000, subnet.getValid());
  40. EXPECT_FALSE(subnet.inRange(IOAddress("192.0.0.0")));
  41. EXPECT_TRUE(subnet.inRange(IOAddress("192.0.2.0")));
  42. EXPECT_TRUE(subnet.inRange(IOAddress("192.0.2.1")));
  43. EXPECT_TRUE(subnet.inRange(IOAddress("192.0.2.255")));
  44. EXPECT_FALSE(subnet.inRange(IOAddress("192.0.3.0")));
  45. EXPECT_FALSE(subnet.inRange(IOAddress("0.0.0.0")));
  46. EXPECT_FALSE(subnet.inRange(IOAddress("255.255.255.255")));
  47. }
  48. TEST(Subnet4Test, Pool4InSubnet4) {
  49. Subnet4Ptr subnet(new Subnet4(IOAddress("192.1.2.0"), 24, 1, 2, 3));
  50. Pool4Ptr pool1(new Pool4(IOAddress("192.1.2.0"), 25));
  51. Pool4Ptr pool2(new Pool4(IOAddress("192.1.2.128"), 26));
  52. Pool4Ptr pool3(new Pool4(IOAddress("192.1.2.192"), 30));
  53. subnet->addPool4(pool1);
  54. // If there's only one pool, get that pool
  55. Pool4Ptr mypool = subnet->getPool4();
  56. EXPECT_EQ(mypool, pool1);
  57. subnet->addPool4(pool2);
  58. subnet->addPool4(pool3);
  59. // If there are more than one pool and we didn't provide hint, we
  60. // should get the first pool
  61. mypool = subnet->getPool4();
  62. EXPECT_EQ(mypool, pool1);
  63. // If we provide a hint, we should get a pool that this hint belongs to
  64. mypool = subnet->getPool4(IOAddress("192.1.2.195"));
  65. EXPECT_EQ(mypool, pool3);
  66. }
  67. TEST(Subnet4Test, Subnet4_Pool4_checks) {
  68. Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 8, 1, 2, 3));
  69. // this one is in subnet
  70. Pool4Ptr pool1(new Pool4(IOAddress("192.255.0.0"), 16));
  71. subnet->addPool4(pool1);
  72. // this one is larger than the subnet!
  73. Pool4Ptr pool2(new Pool4(IOAddress("193.0.0.0"), 24));
  74. EXPECT_THROW(subnet->addPool4(pool2), BadValue);
  75. // this one is totally out of blue
  76. Pool4Ptr pool3(new Pool4(IOAddress("1.2.3.4"), 16));
  77. EXPECT_THROW(subnet->addPool4(pool3), BadValue);
  78. }
  79. // This test verifies that inRange() and inPool() methods work properly.
  80. TEST(Subnet4Test, inRangeinPool) {
  81. Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.0.0"), 8, 1, 2, 3));
  82. // this one is in subnet
  83. Pool4Ptr pool1(new Pool4(IOAddress("192.2.0.0"), 16));
  84. subnet->addPool4(pool1);
  85. // 192.1.1.1 belongs to the subnet...
  86. EXPECT_TRUE(subnet->inRange(IOAddress("192.1.1.1")));
  87. // ... but it does not belong to any pool within
  88. EXPECT_FALSE(subnet->inPool(IOAddress("192.1.1.1")));
  89. // the last address that is in range, but out of pool
  90. EXPECT_TRUE(subnet->inRange(IOAddress("192.1.255.255")));
  91. EXPECT_FALSE(subnet->inPool(IOAddress("192.1.255.255")));
  92. // the first address that is in range, in pool
  93. EXPECT_TRUE(subnet->inRange(IOAddress("192.2.0.0")));
  94. EXPECT_TRUE (subnet->inPool(IOAddress("192.2.0.0")));
  95. // let's try something in the middle as well
  96. EXPECT_TRUE(subnet->inRange(IOAddress("192.2.3.4")));
  97. EXPECT_TRUE (subnet->inPool(IOAddress("192.2.3.4")));
  98. // the last address that is in range, in pool
  99. EXPECT_TRUE(subnet->inRange(IOAddress("192.2.255.255")));
  100. EXPECT_TRUE (subnet->inPool(IOAddress("192.2.255.255")));
  101. // the first address that is in range, but out of pool
  102. EXPECT_TRUE(subnet->inRange(IOAddress("192.3.0.0")));
  103. EXPECT_FALSE(subnet->inPool(IOAddress("192.3.0.0")));
  104. }
  105. // Tests for Subnet6
  106. TEST(Subnet6Test, constructor) {
  107. EXPECT_NO_THROW(Subnet6 subnet1(IOAddress("2001:db8:1::"), 64,
  108. 1, 2, 3, 4));
  109. EXPECT_THROW(Subnet6 subnet2(IOAddress("2001:db8:1::"), 129, 1, 2, 3, 4),
  110. BadValue); // invalid prefix length
  111. EXPECT_THROW(Subnet6 subnet3(IOAddress("192.168.0.0"), 32, 1, 2, 3, 4),
  112. BadValue); // IPv4 addresses are not allowed in Subnet6
  113. }
  114. TEST(Subnet6Test, in_range) {
  115. Subnet6 subnet(IOAddress("2001:db8:1::"), 64, 1000, 2000, 3000, 4000);
  116. EXPECT_EQ(1000, subnet.getT1());
  117. EXPECT_EQ(2000, subnet.getT2());
  118. EXPECT_EQ(3000, subnet.getPreferred());
  119. EXPECT_EQ(4000, subnet.getValid());
  120. EXPECT_FALSE(subnet.inRange(IOAddress("2001:db8:0:ffff:ffff:ffff:ffff:ffff")));
  121. EXPECT_TRUE(subnet.inRange(IOAddress("2001:db8:1::0")));
  122. EXPECT_TRUE(subnet.inRange(IOAddress("2001:db8:1::1")));
  123. EXPECT_TRUE(subnet.inRange(IOAddress("2001:db8:1::ffff:ffff:ffff:ffff")));
  124. EXPECT_FALSE(subnet.inRange(IOAddress("2001:db8:1:1::")));
  125. EXPECT_FALSE(subnet.inRange(IOAddress("::")));
  126. }
  127. TEST(Subnet6Test, Pool6InSubnet6) {
  128. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  129. Pool6Ptr pool1(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1:1::"), 64));
  130. Pool6Ptr pool2(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1:2::"), 64));
  131. Pool6Ptr pool3(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1:3::"), 64));
  132. subnet->addPool6(pool1);
  133. // If there's only one pool, get that pool
  134. Pool6Ptr mypool = subnet->getPool6();
  135. EXPECT_EQ(mypool, pool1);
  136. subnet->addPool6(pool2);
  137. subnet->addPool6(pool3);
  138. // If there are more than one pool and we didn't provide hint, we
  139. // should get the first pool
  140. mypool = subnet->getPool6();
  141. EXPECT_EQ(mypool, pool1);
  142. // If we provide a hint, we should get a pool that this hint belongs to
  143. mypool = subnet->getPool6(IOAddress("2001:db8:1:3::dead:beef"));
  144. EXPECT_EQ(mypool, pool3);
  145. }
  146. TEST(Subnet6Test, Subnet6_Pool6_checks) {
  147. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  148. // this one is in subnet
  149. Pool6Ptr pool1(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1:1::"), 64));
  150. subnet->addPool6(pool1);
  151. // this one is larger than the subnet!
  152. Pool6Ptr pool2(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8::"), 48));
  153. EXPECT_THROW(subnet->addPool6(pool2), BadValue);
  154. // this one is totally out of blue
  155. Pool6Ptr pool3(new Pool6(Pool6::TYPE_IA, IOAddress("3000::"), 16));
  156. EXPECT_THROW(subnet->addPool6(pool3), BadValue);
  157. Pool6Ptr pool4(new Pool6(Pool6::TYPE_IA, IOAddress("4001:db8:1::"), 80));
  158. EXPECT_THROW(subnet->addPool6(pool4), BadValue);
  159. }
  160. // This test verifies that inRange() and inPool() methods work properly.
  161. TEST(Subnet6Test, inRangeinPool) {
  162. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 32, 1, 2, 3, 4));
  163. // this one is in subnet
  164. Pool6Ptr pool1(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8::10"),
  165. IOAddress("2001:db8::20")));
  166. subnet->addPool6(pool1);
  167. // 192.1.1.1 belongs to the subnet...
  168. EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::1")));
  169. // ... but it does not belong to any pool within
  170. EXPECT_FALSE(subnet->inPool(IOAddress("2001:db8::1")));
  171. // the last address that is in range, but out of pool
  172. EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::f")));
  173. EXPECT_FALSE(subnet->inPool(IOAddress("2001:db8::f")));
  174. // the first address that is in range, in pool
  175. EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::10")));
  176. EXPECT_TRUE (subnet->inPool(IOAddress("2001:db8::10")));
  177. // let's try something in the middle as well
  178. EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::18")));
  179. EXPECT_TRUE (subnet->inPool(IOAddress("2001:db8::18")));
  180. // the last address that is in range, in pool
  181. EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::20")));
  182. EXPECT_TRUE (subnet->inPool(IOAddress("2001:db8::20")));
  183. // the first address that is in range, but out of pool
  184. EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::21")));
  185. EXPECT_FALSE(subnet->inPool(IOAddress("2001:db8::21")));
  186. }
  187. };