cfgmgr_unittest.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 <iostream>
  16. #include <sstream>
  17. #include <arpa/inet.h>
  18. #include <gtest/gtest.h>
  19. #include <dhcp/cfgmgr.h>
  20. #include <exceptions/exceptions.h>
  21. using namespace std;
  22. using namespace isc::asiolink;
  23. using namespace isc::dhcp;
  24. using namespace isc::util;
  25. using namespace isc;
  26. // don't import the entire boost namespace. It will unexpectedly hide uint8_t
  27. // for some systems.
  28. using boost::scoped_ptr;
  29. namespace {
  30. // This test verifies if the configuration manager is able to hold and return
  31. // valid leases
  32. TEST(CfgMgrTest, subnet4) {
  33. CfgMgr& cfg_mgr = CfgMgr::instance();
  34. ASSERT_TRUE(&cfg_mgr != 0);
  35. Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"), 26, 1, 2, 3));
  36. Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"), 26, 1, 2, 3));
  37. Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"), 26, 1, 2, 3));
  38. // there shouldn't be any subnet configured at this stage
  39. EXPECT_EQ( Subnet4Ptr(), cfg_mgr.getSubnet4(IOAddress("192.0.2.0")));
  40. cfg_mgr.addSubnet4(subnet1);
  41. // Now we have only one subnet, any request will be served from it
  42. EXPECT_EQ(subnet1, cfg_mgr.getSubnet4(IOAddress("192.0.2.63")));
  43. // Now we add more subnets and check that both old and new subnets
  44. // are accessible.
  45. cfg_mgr.addSubnet4(subnet2);
  46. cfg_mgr.addSubnet4(subnet3);
  47. EXPECT_EQ(subnet3, cfg_mgr.getSubnet4(IOAddress("192.0.2.191")));
  48. EXPECT_EQ(subnet1, cfg_mgr.getSubnet4(IOAddress("192.0.2.15")));
  49. EXPECT_EQ(subnet2, cfg_mgr.getSubnet4(IOAddress("192.0.2.85")));
  50. // Try to find an address that does not belong to any subnet
  51. EXPECT_EQ(Subnet4Ptr(), cfg_mgr.getSubnet4(IOAddress("192.0.2.192")));
  52. }
  53. // This test verifies if the configuration manager is able to hold and return
  54. // valid leases
  55. TEST(CfgMgrTest, subnet6) {
  56. CfgMgr& cfg_mgr = CfgMgr::instance();
  57. ASSERT_TRUE(&cfg_mgr != 0);
  58. Subnet6Ptr subnet1(new Subnet6(IOAddress("2000::"), 48, 1, 2, 3, 4));
  59. Subnet6Ptr subnet2(new Subnet6(IOAddress("3000::"), 48, 1, 2, 3, 4));
  60. Subnet6Ptr subnet3(new Subnet6(IOAddress("4000::"), 48, 1, 2, 3, 4));
  61. // there shouldn't be any subnet configured at this stage
  62. EXPECT_EQ( Subnet6Ptr(), cfg_mgr.getSubnet6(IOAddress("2000::1")));
  63. cfg_mgr.addSubnet6(subnet1);
  64. // Now we have only one subnet, any request will be served from it
  65. EXPECT_EQ(subnet1, cfg_mgr.getSubnet6(IOAddress("2001:db8::1")));
  66. cfg_mgr.addSubnet6(subnet2);
  67. cfg_mgr.addSubnet6(subnet3);
  68. EXPECT_EQ(subnet3, cfg_mgr.getSubnet6(IOAddress("4000::123")));
  69. EXPECT_EQ(subnet2, cfg_mgr.getSubnet6(IOAddress("3000::dead:beef")));
  70. EXPECT_EQ(Subnet6Ptr(), cfg_mgr.getSubnet6(IOAddress("5000::1")));
  71. cfg_mgr.deleteSubnets6();
  72. EXPECT_EQ(Subnet6Ptr(), cfg_mgr.getSubnet6(IOAddress("200::123")));
  73. EXPECT_EQ(Subnet6Ptr(), cfg_mgr.getSubnet6(IOAddress("3000::123")));
  74. EXPECT_EQ(Subnet6Ptr(), cfg_mgr.getSubnet6(IOAddress("4000::123")));
  75. }
  76. } // end of anonymous namespace