subnet_unittest.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. // Tests for Subnet6
  80. TEST(Subnet6Test, constructor) {
  81. EXPECT_NO_THROW(Subnet6 subnet1(IOAddress("2001:db8:1::"), 64,
  82. 1, 2, 3, 4));
  83. EXPECT_THROW(Subnet6 subnet2(IOAddress("2001:db8:1::"), 129, 1, 2, 3, 4),
  84. BadValue); // invalid prefix length
  85. EXPECT_THROW(Subnet6 subnet3(IOAddress("192.168.0.0"), 32, 1, 2, 3, 4),
  86. BadValue); // IPv4 addresses are not allowed in Subnet6
  87. }
  88. TEST(Subnet6Test, in_range) {
  89. Subnet6 subnet(IOAddress("2001:db8:1::"), 64, 1000, 2000, 3000, 4000);
  90. EXPECT_EQ(1000, subnet.getT1());
  91. EXPECT_EQ(2000, subnet.getT2());
  92. EXPECT_EQ(3000, subnet.getPreferred());
  93. EXPECT_EQ(4000, subnet.getValid());
  94. EXPECT_FALSE(subnet.inRange(IOAddress("2001:db8:0:ffff:ffff:ffff:ffff:ffff")));
  95. EXPECT_TRUE(subnet.inRange(IOAddress("2001:db8:1::0")));
  96. EXPECT_TRUE(subnet.inRange(IOAddress("2001:db8:1::1")));
  97. EXPECT_TRUE(subnet.inRange(IOAddress("2001:db8:1::ffff:ffff:ffff:ffff")));
  98. EXPECT_FALSE(subnet.inRange(IOAddress("2001:db8:1:1::")));
  99. EXPECT_FALSE(subnet.inRange(IOAddress("::")));
  100. }
  101. TEST(Subnet6Test, Pool6InSubnet6) {
  102. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  103. Pool6Ptr pool1(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1:1::"), 64));
  104. Pool6Ptr pool2(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1:2::"), 64));
  105. Pool6Ptr pool3(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1:3::"), 64));
  106. subnet->addPool6(pool1);
  107. // If there's only one pool, get that pool
  108. Pool6Ptr mypool = subnet->getPool6();
  109. EXPECT_EQ(mypool, pool1);
  110. subnet->addPool6(pool2);
  111. subnet->addPool6(pool3);
  112. // If there are more than one pool and we didn't provide hint, we
  113. // should get the first pool
  114. mypool = subnet->getPool6();
  115. EXPECT_EQ(mypool, pool1);
  116. // If we provide a hint, we should get a pool that this hint belongs to
  117. mypool = subnet->getPool6(IOAddress("2001:db8:1:3::dead:beef"));
  118. EXPECT_EQ(mypool, pool3);
  119. }
  120. TEST(Subnet6Test, Subnet6_Pool6_checks) {
  121. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  122. // this one is in subnet
  123. Pool6Ptr pool1(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1:1::"), 64));
  124. subnet->addPool6(pool1);
  125. // this one is larger than the subnet!
  126. Pool6Ptr pool2(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8::"), 48));
  127. EXPECT_THROW(subnet->addPool6(pool2), BadValue);
  128. // this one is totally out of blue
  129. Pool6Ptr pool3(new Pool6(Pool6::TYPE_IA, IOAddress("3000::"), 16));
  130. EXPECT_THROW(subnet->addPool6(pool3), BadValue);
  131. Pool6Ptr pool4(new Pool6(Pool6::TYPE_IA, IOAddress("4001:db8:1::"), 80));
  132. EXPECT_THROW(subnet->addPool6(pool4), BadValue);
  133. }
  134. };