From: Marcin Siodelski Date: Fri, 15 Nov 2013 11:03:21 +0000 (+0100) Subject: [2940] Moved the Memfile backend tests to a generic class. X-Git-Tag: bind10-1.2.0beta1-release~102^2~17^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=672d49074ea1a9a39e0cd53ef92db33980ceffc0;p=thirdparty%2Fkea.git [2940] Moved the Memfile backend tests to a generic class. --- diff --git a/src/lib/dhcpsrv/lease_mgr.h b/src/lib/dhcpsrv/lease_mgr.h index a4579b8631..9355736de3 100644 --- a/src/lib/dhcpsrv/lease_mgr.h +++ b/src/lib/dhcpsrv/lease_mgr.h @@ -203,6 +203,20 @@ public: /// @return lease collection virtual Lease4Collection getLease4(const ClientId& clientid) const = 0; + /// @brief Returns existing IPv4 lease for a given client identifier, + /// HW address and subnet identifier. + /// + /// @todo Consider whether this function is needed or not. In the basic + /// DHCPv4 server implementation it is not used by the allocation engine. + /// + /// @param client_id A client identifier. + /// @param hwaddr Hardware address. + /// @param subnet_id A subnet identifier. + /// + /// @return A pointer to the lease or NULL if the lease is not found. + virtual Lease4Ptr getLease4(const ClientId& clientid, const HWAddr& hwaddr, + SubnetID subnet_id) const = 0; + /// @brief Returns existing IPv4 lease for specified client-id /// /// There can be at most one lease for a given HW address in a single diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.cc b/src/lib/dhcpsrv/mysql_lease_mgr.cc index e69558414e..623440bf22 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.cc +++ b/src/lib/dhcpsrv/mysql_lease_mgr.cc @@ -1622,6 +1622,15 @@ MySqlLeaseMgr::getLease4(const ClientId& clientid) const { return (result); } +Lease4Ptr +MySqlLeaseMgr::getLease4(const ClientId&, const HWAddr&, SubnetID) const { + /// This function is currently not implemented because allocation engine + /// searches for the lease using HW address or client identifier. + /// It never uses both parameters in the same time. We need to + /// consider if this function is needed at all. + isc_throw(NotImplemented, "The MySqlLeaseMgr::getLease4 function was" + " called, but it is not implemented"); +} Lease4Ptr MySqlLeaseMgr::getLease4(const ClientId& clientid, SubnetID subnet_id) const { diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.h b/src/lib/dhcpsrv/mysql_lease_mgr.h index 6fae5e2b49..ef4dc47634 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.h +++ b/src/lib/dhcpsrv/mysql_lease_mgr.h @@ -1,4 +1,4 @@ -// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC") // // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above @@ -220,6 +220,19 @@ public: /// failed. virtual Lease4Collection getLease4(const ClientId& clientid) const; + /// @brief Returns IPv4 lease for the specified client identifier, HW + /// address and subnet identifier. + /// + /// @param client_id A client identifier. + /// @param hwaddr Hardware address. + /// @param subnet_id A subnet identifier. + /// + /// @return A pointer to the lease or NULL if the lease is not found. + /// @throw isc::NotImplemented On every call as this function is currently + /// not implemented for the MySQL backend. + virtual Lease4Ptr getLease4(const ClientId& client_id, const HWAddr& hwaddr, + SubnetID subnet_id) const; + /// @brief Returns existing IPv4 lease for specified client-id /// /// There can be at most one lease for a given HW address in a single diff --git a/src/lib/dhcpsrv/tests/lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/lease_mgr_unittest.cc index 776f593354..0d876219fd 100644 --- a/src/lib/dhcpsrv/tests/lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/lease_mgr_unittest.cc @@ -113,6 +113,19 @@ public: return (Lease4Collection()); } + /// @brief Returns existing IPv4 lease for specified client identifier, + /// HW address and subnet identifier. + /// + /// @param client_id Aclient identifier + /// @param hwaddr A HW address. + /// @param subnet_id A subnet identifier. + /// + /// @return A pointer to an existing lease or NULL if lease not found. + virtual Lease4Ptr + getLease4(const ClientId&, const HWAddr&, SubnetID) const { + return (Lease4Ptr()); + } + /// @brief Returns existing IPv4 lease for specified client-id /// /// There can be at most one lease for a given HW address in a single diff --git a/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc index 227c6e320d..85015f623d 100644 --- a/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc @@ -35,7 +35,14 @@ namespace { class MemfileLeaseMgrTest : public GenericLeaseMgrTest { public: MemfileLeaseMgrTest() { + const LeaseMgr::ParameterMap pmap; + lmptr_ = new Memfile_LeaseMgr(pmap); } + + virtual ~MemfileLeaseMgrTest() { + delete lmptr_; + } + }; // This test checks if the LeaseMgr can be instantiated and that it @@ -141,115 +148,22 @@ TEST_F(MemfileLeaseMgrTest, addGetDelete6) { // Simple test about lease4 retrieval through client id method TEST_F(MemfileLeaseMgrTest, getLease4ClientId) { - const LeaseMgr::ParameterMap pmap; - boost::scoped_ptr lease_mgr(new Memfile_LeaseMgr(pmap)); - // Let's initialize a specific lease ... - Lease4Ptr lease = initializeLease4(straddress4_[1]); - EXPECT_TRUE(lease_mgr->addLease(lease)); - Lease4Collection returned = lease_mgr->getLease4(*lease->client_id_); - - ASSERT_EQ(1, returned.size()); - // We should retrieve our lease... - detailCompareLease(lease, *returned.begin()); - lease = initializeLease4(straddress4_[2]); - returned = lease_mgr->getLease4(*lease->client_id_); - - ASSERT_EQ(0, returned.size()); + testGetLease4ClientId(); } // Checks that lease4 retrieval client id is null is working TEST_F(MemfileLeaseMgrTest, getLease4NullClientId) { - const LeaseMgr::ParameterMap pmap; - boost::scoped_ptr lease_mgr(new Memfile_LeaseMgr(pmap)); - // Let's initialize a specific lease ... But this time - // We keep its client id for further lookup and - // We clearly 'reset' it ... - Lease4Ptr leaseA = initializeLease4(straddress4_[4]); - ClientIdPtr client_id = leaseA->client_id_; - leaseA->client_id_ = ClientIdPtr(); - ASSERT_TRUE(lease_mgr->addLease(leaseA)); - - Lease4Collection returned = lease_mgr->getLease4(*client_id); - // Shouldn't have our previous lease ... - ASSERT_TRUE(returned.empty()); - - // Add another lease with the non-NULL client id, and make sure that the - // lookup will not break due to existence of both leases with non-NULL and - // NULL client ids. - Lease4Ptr leaseB = initializeLease4(straddress4_[0]); - // Shouldn't throw any null pointer exception - ASSERT_TRUE(lease_mgr->addLease(leaseB)); - // Try to get the lease. - returned = lease_mgr->getLease4(*client_id); - ASSERT_TRUE(returned.empty()); - - // Let's make it more interesting and add another lease with NULL client id. - Lease4Ptr leaseC = initializeLease4(straddress4_[5]); - leaseC->client_id_.reset(); - ASSERT_TRUE(lease_mgr->addLease(leaseC)); - returned = lease_mgr->getLease4(*client_id); - ASSERT_TRUE(returned.empty()); - - // But getting the lease with non-NULL client id should be successful. - returned = lease_mgr->getLease4(*leaseB->client_id_); - ASSERT_EQ(1, returned.size()); + testGetLease4NullClientId(); } // Checks lease4 retrieval through HWAddr TEST_F(MemfileLeaseMgrTest, getLease4HWAddr) { - const LeaseMgr::ParameterMap pmap; - boost::scoped_ptr lease_mgr(new Memfile_LeaseMgr(pmap)); - // Let's initialize two different leases 4 and just add the first ... - Lease4Ptr leaseA = initializeLease4(straddress4_[5]); - HWAddr hwaddrA(leaseA->hwaddr_, HTYPE_ETHER); - HWAddr hwaddrB(vector(6, 0x80), HTYPE_ETHER); - - EXPECT_TRUE(lease_mgr->addLease(leaseA)); - - // we should not have a lease, with this MAC Addr - Lease4Collection returned = lease_mgr->getLease4(hwaddrB); - ASSERT_EQ(0, returned.size()); - - // But with this one - returned = lease_mgr->getLease4(hwaddrA); - ASSERT_EQ(1, returned.size()); + testGetLease4HWAddr(); } // Checks lease4 retrieval with clientId, HWAddr and subnet_id TEST_F(MemfileLeaseMgrTest, getLease4ClientIdHWAddrSubnetId) { - const LeaseMgr::ParameterMap pmap; - boost::scoped_ptr lease_mgr(new Memfile_LeaseMgr(pmap)); - - Lease4Ptr leaseA = initializeLease4(straddress4_[4]); - Lease4Ptr leaseB = initializeLease4(straddress4_[5]); - Lease4Ptr leaseC = initializeLease4(straddress4_[6]); - // Set NULL client id for one of the leases. This is to make sure that such - // a lease may coexist with other leases with non NULL client id. - leaseC->client_id_.reset(); - - HWAddr hwaddrA(leaseA->hwaddr_, HTYPE_ETHER); - HWAddr hwaddrB(leaseB->hwaddr_, HTYPE_ETHER); - HWAddr hwaddrC(leaseC->hwaddr_, HTYPE_ETHER); - EXPECT_TRUE(lease_mgr->addLease(leaseA)); - EXPECT_TRUE(lease_mgr->addLease(leaseB)); - EXPECT_TRUE(lease_mgr->addLease(leaseC)); - // First case we should retrieve our lease - Lease4Ptr lease = lease_mgr->getLease4(*leaseA->client_id_, hwaddrA, leaseA->subnet_id_); - detailCompareLease(lease, leaseA); - // Retrieve the other lease. - lease = lease_mgr->getLease4(*leaseB->client_id_, hwaddrB, leaseB->subnet_id_); - detailCompareLease(lease, leaseB); - // The last lease has NULL client id so we will use a different getLease4 function - // which doesn't require client id (just a hwaddr and subnet id). - lease = lease_mgr->getLease4(hwaddrC, leaseC->subnet_id_); - detailCompareLease(lease, leaseC); - - // An attempt to retrieve the lease with non matching lease parameters should - // result in NULL pointer being returned. - lease = lease_mgr->getLease4(*leaseA->client_id_, hwaddrB, leaseA->subnet_id_); - EXPECT_FALSE(lease); - lease = lease_mgr->getLease4(*leaseA->client_id_, hwaddrA, leaseB->subnet_id_); - EXPECT_FALSE(lease); + testGetLease4ClientIdHWAddrSubnetId(); } }; // end of anonymous namespace diff --git a/src/lib/dhcpsrv/tests/test_utils.cc b/src/lib/dhcpsrv/tests/test_utils.cc index 12f474aa49..5018487379 100644 --- a/src/lib/dhcpsrv/tests/test_utils.cc +++ b/src/lib/dhcpsrv/tests/test_utils.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC") // // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above @@ -98,9 +98,8 @@ detailCompareLease(const Lease6Ptr& first, const Lease6Ptr& second) { EXPECT_EQ(first->hostname_, second->hostname_); } - GenericLeaseMgrTest::GenericLeaseMgrTest() - :lmptr_(NULL) { + : lmptr_(NULL) { // Initialize address strings and IOAddresses for (int i = 0; ADDRESS4[i] != NULL; ++i) { string addr(ADDRESS4[i]); @@ -121,6 +120,11 @@ GenericLeaseMgrTest::GenericLeaseMgrTest() } } +GenericLeaseMgrTest::~GenericLeaseMgrTest() { + // Does nothing. The derived classes are expected to clean up, i.e. + // remove the lmptr_ pointer. +} + /// @brief Initialize Lease4 Fields /// /// Returns a pointer to a Lease4 structure. Different values are put into @@ -469,6 +473,112 @@ GenericLeaseMgrTest::createLeases6() { return (leases); } +void +GenericLeaseMgrTest::testGetLease4ClientId() { + // Let's initialize a specific lease ... + Lease4Ptr lease = initializeLease4(straddress4_[1]); + EXPECT_TRUE(lmptr_->addLease(lease)); + Lease4Collection returned = lmptr_->getLease4(*lease->client_id_); + + ASSERT_EQ(1, returned.size()); + // We should retrieve our lease... + detailCompareLease(lease, *returned.begin()); + lease = initializeLease4(straddress4_[2]); + returned = lmptr_->getLease4(*lease->client_id_); + + ASSERT_EQ(0, returned.size()); +} + +void +GenericLeaseMgrTest::testGetLease4NullClientId() { + // Let's initialize a specific lease ... But this time + // We keep its client id for further lookup and + // We clearly 'reset' it ... + Lease4Ptr leaseA = initializeLease4(straddress4_[4]); + ClientIdPtr client_id = leaseA->client_id_; + leaseA->client_id_ = ClientIdPtr(); + ASSERT_TRUE(lmptr_->addLease(leaseA)); + + Lease4Collection returned = lmptr_->getLease4(*client_id); + // Shouldn't have our previous lease ... + ASSERT_TRUE(returned.empty()); + + // Add another lease with the non-NULL client id, and make sure that the + // lookup will not break due to existence of both leases with non-NULL and + // NULL client ids. + Lease4Ptr leaseB = initializeLease4(straddress4_[0]); + // Shouldn't throw any null pointer exception + ASSERT_TRUE(lmptr_->addLease(leaseB)); + // Try to get the lease. + returned = lmptr_->getLease4(*client_id); + ASSERT_TRUE(returned.empty()); + + // Let's make it more interesting and add another lease with NULL client id. + Lease4Ptr leaseC = initializeLease4(straddress4_[5]); + leaseC->client_id_.reset(); + ASSERT_TRUE(lmptr_->addLease(leaseC)); + returned = lmptr_->getLease4(*client_id); + ASSERT_TRUE(returned.empty()); + + // But getting the lease with non-NULL client id should be successful. + returned = lmptr_->getLease4(*leaseB->client_id_); + ASSERT_EQ(1, returned.size()); +} + +void +GenericLeaseMgrTest::testGetLease4HWAddr() { + const LeaseMgr::ParameterMap pmap; + // Let's initialize two different leases 4 and just add the first ... + Lease4Ptr leaseA = initializeLease4(straddress4_[5]); + HWAddr hwaddrA(leaseA->hwaddr_, HTYPE_ETHER); + HWAddr hwaddrB(vector(6, 0x80), HTYPE_ETHER); + + EXPECT_TRUE(lmptr_->addLease(leaseA)); + + // we should not have a lease, with this MAC Addr + Lease4Collection returned = lmptr_->getLease4(hwaddrB); + ASSERT_EQ(0, returned.size()); + + // But with this one + returned = lmptr_->getLease4(hwaddrA); + ASSERT_EQ(1, returned.size()); +} + +void +GenericLeaseMgrTest::testLease4ClientIdHWAddrSubnetId() { + Lease4Ptr leaseA = initializeLease4(straddress4_[4]); + Lease4Ptr leaseB = initializeLease4(straddress4_[5]); + Lease4Ptr leaseC = initializeLease4(straddress4_[6]); + // Set NULL client id for one of the leases. This is to make sure that such + // a lease may coexist with other leases with non NULL client id. + leaseC->client_id_.reset(); + + HWAddr hwaddrA(leaseA->hwaddr_, HTYPE_ETHER); + HWAddr hwaddrB(leaseB->hwaddr_, HTYPE_ETHER); + HWAddr hwaddrC(leaseC->hwaddr_, HTYPE_ETHER); + EXPECT_TRUE(lmptr_->addLease(leaseA)); + EXPECT_TRUE(lmptr_->addLease(leaseB)); + EXPECT_TRUE(lmptr_->addLease(leaseC)); + // First case we should retrieve our lease + Lease4Ptr lease = lmptr_->getLease4(*leaseA->client_id_, hwaddrA, leaseA->subnet_id_); + detailCompareLease(lease, leaseA); + // Retrieve the other lease. + lease = lmptr_->getLease4(*leaseB->client_id_, hwaddrB, leaseB->subnet_id_); + detailCompareLease(lease, leaseB); + // The last lease has NULL client id so we will use a different getLease4 function + // which doesn't require client id (just a hwaddr and subnet id). + lease = lmptr_->getLease4(hwaddrC, leaseC->subnet_id_); + detailCompareLease(lease, leaseC); + + // An attempt to retrieve the lease with non matching lease parameters should + // result in NULL pointer being returned. + lease = lmptr_->getLease4(*leaseA->client_id_, hwaddrB, leaseA->subnet_id_); + EXPECT_FALSE(lease); + lease = lmptr_->getLease4(*leaseA->client_id_, hwaddrA, leaseB->subnet_id_); + EXPECT_FALSE(lease); +} + + }; }; }; diff --git a/src/lib/dhcpsrv/tests/test_utils.h b/src/lib/dhcpsrv/tests/test_utils.h index af10a31e0c..45970db99c 100644 --- a/src/lib/dhcpsrv/tests/test_utils.h +++ b/src/lib/dhcpsrv/tests/test_utils.h @@ -1,4 +1,4 @@ -// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC") // // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above @@ -49,8 +49,13 @@ detailCompareLease(const Lease4Ptr& first, const Lease4Ptr& second); /// All concrete LeaseMgr test classes should be derived from it. class GenericLeaseMgrTest : public ::testing::Test { public: + + /// @brief Default constructor. GenericLeaseMgrTest(); + /// @brief Virtual destructor. + virtual ~GenericLeaseMgrTest(); + /// @brief Initialize Lease4 Fields /// /// Returns a pointer to a Lease4 structure. Different values are put into @@ -85,7 +90,7 @@ public: /// /// @param leases Vector of pointers to leases template - void checkLeasesDifferent(const std::vector& leases) const; + void checkLeasesDifferent(const std::vector& leases) const; /// @brief Creates leases for the test /// @@ -101,6 +106,18 @@ public: /// @return vector Vector of pointers to leases std::vector createLeases6(); + /// @brief Test lease retrieval using client id. + void testGetLease4ClientId(); + + /// @brief Test lease retrieval when leases with NULL client id are present. + void testGetLease4NullClientId(); + + /// @brief Test lease retrieval using HW address. + void testGetLease4HWAddr(); + + /// @brief Test lease retrieval using client id, HW address and subnet id. + void testLease4ClientIdHWAddrSubnetId(); + // Member variables std::vector straddress4_; ///< String forms of IPv4 addresses std::vector ioaddress4_; ///< IOAddress forms of IPv4 addresses @@ -108,7 +125,7 @@ public: std::vector leasetype6_; ///< Lease types std::vector ioaddress6_; ///< IOAddress forms of IPv6 addresses - LeaseMgr* lmptr_; ///< Pointer to the lease manager + LeaseMgr* lmptr_; ///< Pointer to the lease manager }; };