memfile_lease_mgr_unittest.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright (C) 2012-2014 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/duid.h>
  17. #include <dhcpsrv/lease_mgr.h>
  18. #include <dhcpsrv/memfile_lease_mgr.h>
  19. #include <dhcpsrv/tests/test_utils.h>
  20. #include <gtest/gtest.h>
  21. #include <iostream>
  22. #include <sstream>
  23. using namespace std;
  24. using namespace isc;
  25. using namespace isc::asiolink;
  26. using namespace isc::dhcp;
  27. using namespace isc::dhcp::test;
  28. namespace {
  29. // empty class for now, but may be extended once Addr6 becomes bigger
  30. class MemfileLeaseMgrTest : public GenericLeaseMgrTest {
  31. public:
  32. MemfileLeaseMgrTest() {
  33. const LeaseMgr::ParameterMap pmap;
  34. lmptr_ = new Memfile_LeaseMgr(pmap);
  35. }
  36. virtual ~MemfileLeaseMgrTest() {
  37. delete lmptr_;
  38. }
  39. };
  40. // This test checks if the LeaseMgr can be instantiated and that it
  41. // parses parameters string properly.
  42. TEST_F(MemfileLeaseMgrTest, constructor) {
  43. const LeaseMgr::ParameterMap pmap; // Empty parameter map
  44. boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr;
  45. ASSERT_NO_THROW(lease_mgr.reset(new Memfile_LeaseMgr(pmap)));
  46. }
  47. // Checks if the getType() and getName() methods both return "memfile".
  48. TEST_F(MemfileLeaseMgrTest, getTypeAndName) {
  49. const LeaseMgr::ParameterMap pmap; // Empty parameter map
  50. boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr(new Memfile_LeaseMgr(pmap));
  51. EXPECT_EQ(std::string("memfile"), lease_mgr->getType());
  52. EXPECT_EQ(std::string("memory"), lease_mgr->getName());
  53. }
  54. // Checks that adding/getting/deleting a Lease6 object works.
  55. TEST_F(MemfileLeaseMgrTest, addGetDelete6) {
  56. const LeaseMgr::ParameterMap pmap; // Empty parameter map
  57. boost::scoped_ptr<LeaseMgr> lease_mgr(new Memfile_LeaseMgr(pmap));
  58. IOAddress addr("2001:db8:1::456");
  59. uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  60. DuidPtr duid(new DUID(llt, sizeof(llt)));
  61. uint32_t iaid = 7; // just a number
  62. SubnetID subnet_id = 8; // just another number
  63. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr,
  64. duid, iaid, 100, 200, 50, 80,
  65. subnet_id));
  66. EXPECT_TRUE(lease_mgr->addLease(lease));
  67. // should not be allowed to add a second lease with the same address
  68. EXPECT_FALSE(lease_mgr->addLease(lease));
  69. Lease6Ptr x = lease_mgr->getLease6(Lease::TYPE_NA,
  70. IOAddress("2001:db8:1::234"));
  71. EXPECT_EQ(Lease6Ptr(), x);
  72. x = lease_mgr->getLease6(Lease::TYPE_NA,
  73. IOAddress("2001:db8:1::456"));
  74. ASSERT_TRUE(x);
  75. EXPECT_EQ(x->addr_.toText(), addr.toText());
  76. EXPECT_TRUE(*x->duid_ == *duid);
  77. EXPECT_EQ(x->iaid_, iaid);
  78. EXPECT_EQ(x->subnet_id_, subnet_id);
  79. // These are not important from lease management perspective, but
  80. // let's check them anyway.
  81. EXPECT_EQ(x->type_, Lease::TYPE_NA);
  82. EXPECT_EQ(x->preferred_lft_, 100);
  83. EXPECT_EQ(x->valid_lft_, 200);
  84. EXPECT_EQ(x->t1_, 50);
  85. EXPECT_EQ(x->t2_, 80);
  86. // Test getLease6(duid, iaid, subnet_id) - positive case
  87. Lease6Ptr y = lease_mgr->getLease6(Lease::TYPE_NA, *duid, iaid,
  88. subnet_id);
  89. ASSERT_TRUE(y);
  90. EXPECT_TRUE(*y->duid_ == *duid);
  91. EXPECT_EQ(y->iaid_, iaid);
  92. EXPECT_EQ(y->addr_.toText(), addr.toText());
  93. // Test getLease6(duid, iaid, subnet_id) - wrong iaid
  94. uint32_t invalid_iaid = 9; // no such iaid
  95. y = lease_mgr->getLease6(Lease::TYPE_NA, *duid, invalid_iaid,
  96. subnet_id);
  97. EXPECT_FALSE(y);
  98. uint32_t invalid_subnet_id = 999;
  99. y = lease_mgr->getLease6(Lease::TYPE_NA, *duid, iaid,
  100. invalid_subnet_id);
  101. EXPECT_FALSE(y);
  102. // truncated duid
  103. DuidPtr invalid_duid(new DUID(llt, sizeof(llt) - 1));
  104. y = lease_mgr->getLease6(Lease::TYPE_NA, *invalid_duid, iaid,
  105. subnet_id);
  106. EXPECT_FALSE(y);
  107. // should return false - there's no such address
  108. EXPECT_FALSE(lease_mgr->deleteLease(IOAddress("2001:db8:1::789")));
  109. // this one should succeed
  110. EXPECT_TRUE(lease_mgr->deleteLease(IOAddress("2001:db8:1::456")));
  111. // after the lease is deleted, it should really be gone
  112. x = lease_mgr->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::456"));
  113. EXPECT_EQ(Lease6Ptr(), x);
  114. }
  115. /// @todo Write more memfile tests
  116. // Simple test about lease4 retrieval through client id method
  117. TEST_F(MemfileLeaseMgrTest, getLease4ClientId) {
  118. testGetLease4ClientId();
  119. }
  120. // Checks that lease4 retrieval client id is null is working
  121. TEST_F(MemfileLeaseMgrTest, getLease4NullClientId) {
  122. testGetLease4NullClientId();
  123. }
  124. // Checks lease4 retrieval through HWAddr
  125. TEST_F(MemfileLeaseMgrTest, getLease4HWAddr) {
  126. testGetLease4HWAddr();
  127. }
  128. // Checks lease4 retrieval with clientId, HWAddr and subnet_id
  129. TEST_F(MemfileLeaseMgrTest, getLease4ClientIdHWAddrSubnetId) {
  130. testGetLease4ClientIdHWAddrSubnetId();
  131. }
  132. }; // end of anonymous namespace