Browse Source

[3947] Heavy changes to the Dhcp6Client configuration structures.

Also, fixed failing unit tests.
Marcin Siodelski 9 years ago
parent
commit
b3e1abee56

+ 2 - 2
src/bin/dhcp6/tests/confirm_unittest.cc

@@ -276,8 +276,8 @@ TEST_F(ConfirmTest, relayedClientNoSubnet) {
 
     // Set lifetimes to 0 so as the Confirm will ignore the specific address
     // and send an empty IA_NA.
-    client.config_.leases_[0].lease_.preferred_lft_ = 0;
-    client.config_.leases_[0].lease_.valid_lft_ = 0;
+    client.config_.leases_[0].preferred_lft_ = 0;
+    client.config_.leases_[0].valid_lft_ = 0;
     ASSERT_NO_THROW(client.doConfirm());
     EXPECT_FALSE(client.getContext().response_);
 

+ 127 - 75
src/bin/dhcp6/tests/dhcp6_client.cc

@@ -28,8 +28,52 @@
 #include <cstdlib>
 #include <time.h>
 
+using namespace isc::dhcp;
+using namespace isc::dhcp::test;
 using namespace isc::test;
 
+namespace {
+
+/// @brief Functor searching for the leases using a specified property.
+///
+/// @tparam BaseType Base type to which the property belongs: @c Lease or
+/// @c Lease6.
+/// @tparam PropertyType A type of the property, e.g. @c uint32_t for IAID.
+/// @tparam MemberPointer A pointer to the member, e.g. @c &Lease6::iaid_.
+template<typename BaseType, typename PropertyType,
+         PropertyType BaseType::*MemberPointer>
+struct getLeasesByPropertyFun {
+
+    /// @brief Returns leases matching the specified condition.
+    ///
+    /// @param config DHCP client configuration structure holding leases.
+    /// @param property A value of the lease property used to search the lease.
+    /// @param equals A flag which indicates if the operator should search for
+    /// the leases which property is equal to the value of @c property parameter
+    /// (if true), or unequal (if false).
+    /// @param [out] leases A vector in which the operator will store leases
+    /// found.
+    void operator()(const Dhcp6Client::Configuration& config,
+                    const PropertyType& property, const bool equals,
+                    std::vector<Lease6>& leases) {
+
+        // Iterate over the leases and match the property with a given lease
+        //field.
+        for (typename std::vector<Lease6>::const_iterator lease =
+                 config.leases_.begin(); lease != config.leases_.end();
+             ++lease) {
+            // Check if fulfils the condition.
+            if ((equals && ((*lease).*MemberPointer) == property) ||
+                (!equals && ((*lease).*MemberPointer) != property)) {
+                // Found the matching lease.
+                leases.push_back(*lease);
+            }
+        }
+    }
+};
+
+}; // end of anonymous namespace
+
 namespace isc {
 namespace dhcp {
 namespace test {
@@ -92,8 +136,9 @@ Dhcp6Client::applyRcvdConfiguration(const Pkt6Ptr& reply) {
         for (Opts::const_iterator iter_ia_opt = ia_opts.begin();
              iter_ia_opt != ia_opts.end(); ++iter_ia_opt) {
             OptionPtr ia_opt = iter_ia_opt->second;
-            LeaseInfo lease_info(ia->getType());
-            lease_info.lease_.iaid_ = ia->getIAID();
+            Lease6 lease;
+            lease.type_ = (ia->getType() == D6O_IA_NA ? Lease::TYPE_NA : Lease::TYPE_PD);
+            lease.iaid_ = ia->getIAID();
 
             switch (ia_opt->getType()) {
             case D6O_IAADDR:
@@ -101,23 +146,17 @@ Dhcp6Client::applyRcvdConfiguration(const Pkt6Ptr& reply) {
                     Option6IAAddrPtr iaaddr = boost::dynamic_pointer_cast<
                         Option6IAAddr>(ia_opt);
 
-                    if (!iaaddr) {
-                        // There is no address. This IA option may simply
-                        // contain a status code, so let's just reset the
-                        // lease and keep IAID around.
-                        lease_info.lease_ = Lease6();
-                        lease_info.lease_.type_ = Lease::TYPE_NA;
-                        break;
+                    if (iaaddr) {
+                        lease = Lease6(Lease::TYPE_NA,
+                                       iaaddr->getAddress(),
+                                       duid_, ia->getIAID(),
+                                       iaaddr->getPreferred(),
+                                       iaaddr->getValid(),
+                                       ia->getT1(), ia->getT2(), 0,
+                                       hwaddr);
+                        lease.cltt_ = time(NULL);
+                        applyLease(lease);
                     }
-
-                    lease_info.lease_ = Lease6(Lease::TYPE_NA,
-                                               iaaddr->getAddress(),
-                                               duid_, ia->getIAID(),
-                                               iaaddr->getPreferred(),
-                                               iaaddr->getValid(),
-                                               ia->getT1(), ia->getT2(), 0,
-                                               hwaddr);
-                    lease_info.lease_.cltt_ = time(NULL);
                 }
                 break;
 
@@ -125,23 +164,19 @@ Dhcp6Client::applyRcvdConfiguration(const Pkt6Ptr& reply) {
                 {
                     Option6IAPrefixPtr iaprefix = boost::dynamic_pointer_cast<
                         Option6IAPrefix>(ia_opt);
-                    if (!iaprefix) {
-                        // There is no prefix. This IA option may simply
-                        // contain a status code, so let's just reset the
-                        // lease and keep IAID around.
-                        lease_info.lease_ = Lease6();
-                        lease_info.lease_.type_ = Lease::TYPE_PD;
-                        break;
+
+                    if (iaprefix) {
+                        lease = Lease6(Lease::TYPE_PD,
+                                       iaprefix->getAddress(), duid_,
+                                       ia->getIAID(),
+                                       iaprefix->getPreferred(),
+                                       iaprefix->getValid(),
+                                       ia->getT1(), ia->getT2(), 0,
+                                       hwaddr,
+                                       iaprefix->getLength());
+                        lease.cltt_ = time(NULL);
+                        applyLease(lease);
                     }
-                    lease_info.lease_ = Lease6(Lease::TYPE_PD,
-                                               iaprefix->getAddress(), duid_,
-                                               ia->getIAID(),
-                                               iaprefix->getPreferred(),
-                                               iaprefix->getValid(),
-                                               ia->getT1(), ia->getT2(), 0,
-                                               hwaddr,
-                                               iaprefix->getLength());
-                    lease_info.lease_.cltt_ = time(NULL);
                 }
                 break;
 
@@ -151,8 +186,8 @@ Dhcp6Client::applyRcvdConfiguration(const Pkt6Ptr& reply) {
                     // code, assume the status code to be 0.
                     Option6StatusCodePtr status_code = boost::dynamic_pointer_cast<
                         Option6StatusCode>(ia->getOption(D6O_STATUS_CODE));
-                    lease_info.status_code_ =
-                        status_code ? status_code->getStatusCode() : 0;
+                    config_.status_codes_[ia->getIAID()] =
+                        (status_code ? status_code->getStatusCode() : 0);
                 }
                 break;
 
@@ -160,7 +195,6 @@ Dhcp6Client::applyRcvdConfiguration(const Pkt6Ptr& reply) {
                 ; // no-op
             }
 
-            applyLease(lease_info);
         }
     }
 
@@ -176,32 +210,25 @@ Dhcp6Client::applyRcvdConfiguration(const Pkt6Ptr& reply) {
 }
 
 void
-Dhcp6Client::applyLease(const LeaseInfo& lease_info) {
+Dhcp6Client::applyLease(const Lease6& lease) {
     // Go over existing leases and try to match the one that we have.
     for (size_t i = 0; i < config_.leases_.size(); ++i) {
-        Lease6 existing_lease = config_.leases_[i].lease_;
+        Lease6 existing_lease = config_.leases_[i];
         // If IAID is matching and there is an actual address assigned
         // replace the current lease. The default address is :: if the
         // server hasn't sent the IA option. In this case, there is no
         // lease assignment so we keep what we have.
-        if ((existing_lease.iaid_ == lease_info.lease_.iaid_)
-            && (existing_lease.type_ == lease_info.lease_.type_)
-            && (lease_info.lease_.addr_ != asiolink::IOAddress("::"))
-            && (existing_lease.addr_ == lease_info.lease_.addr_)) {
-            config_.leases_[i] = lease_info;
-            return;
-
-        } else if ((existing_lease.iaid_ == lease_info.lease_.iaid_) &&
-                   (lease_info.lease_.addr_ == asiolink::IOAddress("::"))) {
-            config_.leases_[i] = lease_info;
-            config_.leases_[i].status_code_ = lease_info.status_code_;
+        if ((existing_lease.iaid_ == lease.iaid_)
+            && (existing_lease.type_ == lease.type_)
+            && (lease.addr_ != asiolink::IOAddress("::"))
+            && (existing_lease.addr_ == lease.addr_)) {
+            config_.leases_[i] = lease;
             return;
-
         }
     }
 
     // It is a new lease. Add it.
-    config_.leases_.push_back(lease_info);
+    config_.leases_.push_back(lease);
 }
 
 void
@@ -321,9 +348,7 @@ Dhcp6Client::copyIAsFromLeases(const Pkt6Ptr& dest) const {
 
 void
 Dhcp6Client::createLease(const Lease6& lease) {
-    LeaseInfo info;
-    info.lease_ = lease;
-    applyLease(info);
+    applyLease(lease);
 }
 
 Pkt6Ptr
@@ -497,7 +522,7 @@ void
 Dhcp6Client::fastFwdTime(const uint32_t secs) {
     // Iterate over all leases and move their cltt backwards.
     for (size_t i = 0; i < config_.leases_.size(); ++i) {
-        config_.leases_[i].lease_.cltt_ -= secs;
+        config_.leases_[i].cltt_ -= secs;
     }
 }
 
@@ -533,10 +558,9 @@ Dhcp6Client::getClientId() const {
 std::set<uint32_t>
 Dhcp6Client::getIAIDs() const {
     std::set<uint32_t> iaids;
-    for (std::vector<LeaseInfo>::const_iterator lease_info =
-             config_.leases_.begin(); lease_info != config_.leases_.end();
-         ++lease_info) {
-        iaids.insert(lease_info->lease_.iaid_);
+    for (std::vector<Lease6>::const_iterator lease = config_.leases_.begin();
+         lease != config_.leases_.end(); ++lease) {
+        iaids.insert(lease->iaid_);
     }
     return (iaids);
 }
@@ -544,28 +568,56 @@ Dhcp6Client::getIAIDs() const {
 std::vector<Lease6>
 Dhcp6Client::getLeasesByIAID(const uint32_t iaid) const {
     std::vector<Lease6> leases;
-    for (std::vector<LeaseInfo>::const_iterator lease_info =
-             config_.leases_.begin(); lease_info != config_.leases_.end();
-         ++lease_info) {
-        if (lease_info->lease_.iaid_ == iaid) {
-            leases.push_back(lease_info->lease_);
-        }
-    }
+    getLeasesByProperty<Lease6, uint32_t, &Lease6::iaid_>(iaid, true, leases);
     return (leases);
 }
 
-std::vector<Dhcp6Client::LeaseInfo>
-Dhcp6Client::getLeasesByType(const Lease::Type& lease_type) const {
-    std::vector<Dhcp6Client::LeaseInfo> leases;
-    LeaseInfo lease_info;
-    BOOST_FOREACH(lease_info, config_.leases_) {
-        if (lease_info.lease_.type_ == lease_type) {
-            leases.push_back(lease_info);
+template<typename BaseType, typename PropertyType, PropertyType BaseType::*MemberPointer>
+void
+Dhcp6Client::getLeasesByProperty(const PropertyType& property, const bool equals,
+                                 std::vector<Lease6>& leases) const {
+    getLeasesByPropertyFun<BaseType, PropertyType, MemberPointer> fun;
+    fun(config_, property, equals, leases);
+}
+
+std::vector<Lease6>
+Dhcp6Client::getLeasesByType(const Lease6::Type& lease_type) const {
+    std::vector<Lease6> leases;
+    getLeasesByProperty<Lease6, Lease6::Type, &Lease6::type_>(lease_type, true, leases);
+    return (leases);
+}
+
+std::vector<Lease6>
+Dhcp6Client::getLeasesWithNonZeroLifetime() const {
+    std::vector<Lease6> leases;
+    getLeasesByProperty<Lease, uint32_t, &Lease::valid_lft_>(0, false, leases);
+    return (leases);
+}
+
+std::vector<Lease6>
+Dhcp6Client::getLeasesWithZeroLifetime() const {
+    std::vector<Lease6> leases;
+    getLeasesByProperty<Lease, uint32_t, &Lease::valid_lft_>(0, true, leases);
+    return (leases);
+}
+
+uint16_t
+Dhcp6Client::getStatusCode(const uint32_t iaid) const {
+    std::map<uint32_t, uint16_t>::const_iterator status_code =
+        config_.status_codes_.find(iaid);
+    if (status_code == config_.status_codes_.end()) {
+        if (!getLeasesByIAID(iaid).empty()) {
+            return (STATUS_Success);
         }
+
+    } else {
+        return (status_code->second);
     }
-    return (leases);
+
+    return (0xFFFF);
 }
 
+
 void
 Dhcp6Client::setDUID(const std::string& str) {
     DUID d = DUID::fromText(str);

+ 34 - 8
src/bin/dhcp6/tests/dhcp6_client.h

@@ -90,7 +90,10 @@ public:
     /// server-id and client-id.
     struct Configuration {
         /// @brief List of received leases
-        std::vector<LeaseInfo> leases_;
+        std::vector<Lease6> leases_;
+
+        /// @brief A map of IAID, status code tuples.
+        std::map<uint32_t, uint16_t> status_codes_;
 
         /// @brief List of received options
         OptionCollection options_;
@@ -110,6 +113,7 @@ public:
         /// @brief Clears configuration.
         void clear() {
             leases_.clear();
+            status_codes_.clear();
             resetGlobalStatusCode();
         }
 
@@ -311,7 +315,7 @@ public:
     /// @param at Index of the lease held by the client.
     /// @return A lease at the specified index.
     Lease6 getLease(const size_t at) const {
-        return (config_.leases_[at].lease_);
+        return (config_.leases_[at]);
     }
 
     /// @brief Returns collection of leases for specified IAID.
@@ -326,7 +330,13 @@ public:
     /// @param type Lease type: D6O_IA_NA or D6O_IA_PD.
     ///
     /// @return Vector containing leases of the specified type.
-    std::vector<LeaseInfo> getLeasesByType(const Lease::Type& lease_type) const;
+    std::vector<Lease6> getLeasesByType(const Lease::Type& lease_type) const;
+
+    /// @brief Returns leases with non-zero lifetimes.
+    std::vector<Lease6> getLeasesWithNonZeroLifetime() const;
+
+    /// @brief Returns leases with zero lifetimes.
+    std::vector<Lease6> getLeasesWithZeroLifetime() const;
 
     /// @brief Returns the value of the global status code for the last
     /// transaction.
@@ -334,7 +344,7 @@ public:
         return (config_.status_code_);
     }
 
-    /// @brief Returns status code set by the server for the lease.
+    /// @brief Returns status code set by the server for the IAID.
     ///
     /// @warning This method doesn't check if the specified index is out of
     /// range. The caller is responsible for using a correct offset by
@@ -342,9 +352,7 @@ public:
     ///
     /// @param at Index of the lease held by the client.
     /// @return A status code for the lease at the specified index.
-    uint16_t getStatusCode(const size_t at) const {
-        return (config_.leases_[at].status_code_);
-    }
+    uint16_t getStatusCode(const uint32_t iaid) const;
 
     /// @brief Returns number of acquired leases.
     size_t getLeaseNum() const {
@@ -543,7 +551,7 @@ private:
     /// each individual lease.
     ///
     /// @param lease_info Structure holding new lease information.
-    void applyLease(const LeaseInfo& lease_info);
+    void applyLease(const Lease6& lease);
 
     /// @brief Includes Client FQDN in the client's message.
     ///
@@ -612,6 +620,24 @@ private:
     /// @return Object encapsulating a DUID.
     DuidPtr generateDUID(DUID::DUIDType duid_type) const;
 
+    /// @brief Returns client's leases which match the specified condition.
+    ///
+    /// @param property A value of the lease property used to search the lease.
+    /// @param equals A flag which indicates if the operator should search
+    /// for the leases which property is equal to the value of @c property
+    /// parameter (if true), or unequal (if false).
+    /// @param [out] leases A vector in which the operator will store leases
+    /// found.
+    ///
+    /// @tparam BaseType Base type to which the property belongs: @c Lease or
+    /// @c Lease6.
+    /// @tparam PropertyType A type of the property, e.g. @c uint32_t for IAID.
+    /// @tparam MemberPointer A pointer to the member, e.g. @c &Lease6::iaid_.
+    template<typename BaseType, typename PropertyType,
+             PropertyType BaseType::*MemberPointer>
+    void getLeasesByProperty(const PropertyType& property, const bool equals,
+                             std::vector<Lease6>& leases) const;
+
     /// @brief Simulates reception of the message from the server.
     ///
     /// @return Received message.

+ 1 - 1
src/bin/dhcp6/tests/dhcp6_message_test.cc

@@ -81,7 +81,7 @@ Dhcpv6MessageTest::requestLease(const std::string& config,
     Lease6Ptr lease_server = checkLease(lease_client);
     ASSERT_TRUE(lease_server);
     // And that status code was OK.
-    ASSERT_EQ(STATUS_Success, client.getStatusCode(0));
+    ASSERT_EQ(STATUS_Success, client.getStatusCode(lease_client.iaid_));
 }
 
 }

+ 22 - 33
src/bin/dhcp6/tests/rebind_unittest.cc

@@ -288,7 +288,7 @@ TEST_F(RebindTest, directClientChangingSubnet) {
     Lease6Ptr lease_server2 = checkLease(lease_client2);
     EXPECT_TRUE(lease_server2);
     // Client should have received NoBinding status code.
-    EXPECT_EQ(STATUS_NoBinding, client.getStatusCode(0));
+    EXPECT_EQ(STATUS_NoBinding, client.getStatusCode(1234));
 
 }
 
@@ -304,7 +304,7 @@ TEST_F(RebindTest, directClientChangingIAID) {
     Lease6 lease_client = client.getLease(0);
     // Modify the IAID of the lease record that client stores. By adding
     // one to IAID we guarantee that the IAID will change.
-    ++client.config_.leases_[0].lease_.iaid_;
+    ++client.config_.leases_[0].iaid_;
     // Try to Rebind. Note that client will use a different IAID (which
     // is not matching IAID that server retains for the client). Server
     // should not find the lease that client is trying to extend and
@@ -315,7 +315,7 @@ TEST_F(RebindTest, directClientChangingIAID) {
     Lease6Ptr lease_server2 = checkLease(lease_client);
     EXPECT_TRUE(lease_server2);
     // The Status code returned to the client, should be NoBinding.
-    EXPECT_EQ(STATUS_NoBinding, client.getStatusCode(0));
+    EXPECT_EQ(STATUS_NoBinding, client.getStatusCode(1235));
 
 }
 
@@ -336,7 +336,7 @@ TEST_F(RebindTest, directClientLostLease) {
     // the server and the server should return NoBinding status code.
     ASSERT_NO_THROW(client.doRebind());
     ASSERT_EQ(1, client.getLeaseNum());
-    EXPECT_EQ(STATUS_NoBinding, client.getStatusCode(0));
+    EXPECT_EQ(STATUS_NoBinding, client.getStatusCode(1234));
 }
 
 /// @todo Extend tests for direct client changing address.
@@ -398,18 +398,9 @@ TEST_F(RebindTest, relayedClientChangingSubnet) {
     ASSERT_NO_THROW(client.doRebind());
     // We are expecting that the server didn't extend the lease because
     // the address that client is using doesn't match the new subnet.
-    // But, the client still has an old lease.
-    ASSERT_EQ(1, client.getLeaseNum());
-    Lease6 lease_client2 = client.getLease(0);
-    // The current lease should be exactly the same as old lease,
-    // because server shouldn't have extended.
-    EXPECT_TRUE(lease_client == lease_client2);
-    // Make sure, that the lease that client has, is matching the lease
-    // in the lease database.
-    Lease6Ptr lease_server2 = checkLease(lease_client2);
-    EXPECT_TRUE(lease_server2);
+    ASSERT_EQ(0, client.getLeaseNum());
     // Client should have received NoBinding status code.
-    EXPECT_EQ(STATUS_NoBinding, client.getStatusCode(0));
+    EXPECT_EQ(STATUS_NoBinding, client.getStatusCode(1234));
 
 }
 
@@ -429,7 +420,7 @@ TEST_F(RebindTest, relayedClientChangingIAID) {
     Lease6 lease_client = client.getLease(0);
     // Modify the IAID of the lease record that client stores. By adding
     // one to IAID we guarantee that the IAID will change.
-    ++client.config_.leases_[0].lease_.iaid_;
+    ++client.config_.leases_[0].iaid_;
     // Try to Rebind. Note that client will use a different IAID (which
     // is not matching IAID that server retains for the client). Server
     // should not find the lease that client is trying to extend and
@@ -440,7 +431,7 @@ TEST_F(RebindTest, relayedClientChangingIAID) {
     Lease6Ptr lease_server2 = checkLease(lease_client);
     EXPECT_TRUE(lease_server2);
     // The Status code returned to the client, should be NoBinding.
-    EXPECT_EQ(STATUS_NoBinding, client.getStatusCode(0));
+    EXPECT_EQ(STATUS_NoBinding, client.getStatusCode(1235));
 
 }
 
@@ -465,7 +456,7 @@ TEST_F(RebindTest, relayedClientLostLease) {
     // the server and the server should return NoBinding status code.
     ASSERT_NO_THROW(client.doRebind());
     ASSERT_EQ(1, client.getLeaseNum());
-    EXPECT_EQ(STATUS_NoBinding, client.getStatusCode(0));
+    EXPECT_EQ(STATUS_NoBinding, client.getStatusCode(1234));
 }
 
 // Check that relayed client receives the IA with lifetimes of 0, when
@@ -481,7 +472,7 @@ TEST_F(RebindTest, relayedClientChangingAddress) {
     // Modify the address of the lease record that client stores. The server
     // should check that the address is invalid (hasn't been allocated for
     // the particular IAID).
-    client.config_.leases_[0].lease_.addr_ = IOAddress("3000::100");
+    client.config_.leases_[0].addr_ = IOAddress("3000::100");
     // Try to Rebind. The client will use correct IAID but will specify a
     // wrong address. The server will discover that the client has a binding
     // but the address will not match.
@@ -599,7 +590,7 @@ TEST_F(RebindTest, directClientPDChangingIAID) {
     Lease6 lease_client = client.getLease(0);
     // Modify the IAID of the lease record that client stores. By adding
     // one to IAID we guarantee that the IAID will change.
-    ++client.config_.leases_[0].lease_.iaid_;
+    ++client.config_.leases_[0].iaid_;
     // Try to Rebind. Note that client will use a different IAID (which
     // is not matching IAID that server retains for the client). This is
     // a condition described in RFC3633, section 12.2 as the server finds
@@ -630,9 +621,9 @@ TEST_F(RebindTest, directClientPDChangingPrefix) {
     // Modify the Prefix of the lease record that client stores. The server
     // should check that the prefix is invalid (hasn't been allocated for
     // the particular IAID).
-    ASSERT_NE(client.config_.leases_[0].lease_.addr_,
+    ASSERT_NE(client.config_.leases_[0].addr_,
               IOAddress("2001:db8:1:10::"));
-    client.config_.leases_[0].lease_.addr_ = IOAddress("2001:db8:1:10::");
+    client.config_.leases_[0].addr_ = IOAddress("2001:db8:1:10::");
     // Try to Rebind. The client will use correct IAID but will specify a
     // wrong prefix. The server will discover that the client has a binding
     // but the prefix will not match. According to the RFC3633, section 12.2.
@@ -651,21 +642,19 @@ TEST_F(RebindTest, directClientPDChangingPrefix) {
     // Client should get two entries. One with the invalid address he requested
     // with zeroed lifetimes and a second one with the actual prefix he has
     // with non-zero lifetimes.
-    Lease6 lease_client1 = client.getLease(0);
-    Lease6 lease_client2 = client.getLease(1);
 
-    // The lifetimes should be set to 0, as an explicit notification to the
-    // client to stop using invalid prefix.
-    EXPECT_EQ(0, lease_client1.valid_lft_);
-    EXPECT_EQ(0, lease_client1.preferred_lft_);
+    // Get the lease with 0 lifetimes.
+    std::vector<Lease6> invalid_leases = client.getLeasesWithZeroLifetime();
+    ASSERT_EQ(1, invalid_leases.size());
+    EXPECT_EQ(0, invalid_leases[0].valid_lft_);
+    EXPECT_EQ(0, invalid_leases[0].preferred_lft_);
 
-    // The lifetimes should be set to 0, as an explicit notification to the
-    // client to stop using invalid prefix.
-    EXPECT_NE(0, lease_client2.valid_lft_);
-    EXPECT_NE(0, lease_client2.preferred_lft_);
+    // Get the valid lease with non-zero lifetime.
+    std::vector<Lease6> valid_leases = client.getLeasesWithNonZeroLifetime();
+    ASSERT_EQ(1, valid_leases.size());
 
     // Check that server still has the same lease.
-    Lease6Ptr lease_server = checkLease(lease_client);
+    Lease6Ptr lease_server = checkLease(valid_leases[0]);
     ASSERT_TRUE(lease_server);
     // Make sure that the lease in the data base hasn't been added.
     EXPECT_NE(0, lease_server->valid_lft_);

+ 17 - 22
src/bin/dhcp6/tests/renew_unittest.cc

@@ -131,15 +131,13 @@ TEST_F(RenewTest, requestPrefixInRenew) {
     client.fastFwdTime(1000);
 
     // Make sure that the client has acquired NA lease.
-    std::vector<Dhcp6Client::LeaseInfo> leases_client_na =
-        client.getLeasesByType(Lease::TYPE_NA);
+    std::vector<Lease6> leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
     ASSERT_EQ(1, leases_client_na.size());
 
     // The client should not acquire a PD lease.
-    std::vector<Dhcp6Client::LeaseInfo> leases_client_pd =
-        client.getLeasesByType(Lease::TYPE_PD);
-    ASSERT_EQ(1, leases_client_pd.size());
-    ASSERT_EQ(STATUS_NoPrefixAvail, leases_client_pd[0].status_code_);
+    std::vector<Lease6> leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
+    ASSERT_TRUE(leases_client_pd.empty());
+    ASSERT_EQ(STATUS_NoPrefixAvail, client.getStatusCode(5678));
 
     // Reconfigure the server to use both NA and PD pools.
     configure(RENEW_CONFIGS[2], *client.getServer());
@@ -148,19 +146,18 @@ TEST_F(RenewTest, requestPrefixInRenew) {
     ASSERT_NO_THROW(client.doRenew());
 
     // Make sure that the client has acquired NA lease.
-    std::vector<Dhcp6Client::LeaseInfo> leases_client_na_renewed =
+    std::vector<Lease6> leases_client_na_renewed =
         client.getLeasesByType(Lease::TYPE_NA);
     ASSERT_EQ(1, leases_client_na_renewed.size());
-    EXPECT_EQ(STATUS_Success, leases_client_na_renewed[0].status_code_);
+    EXPECT_EQ(STATUS_Success, client.getStatusCode(1234));
 
     // The lease should have been renewed.
-    EXPECT_EQ(1000, leases_client_na_renewed[0].lease_.cltt_ -
-              leases_client_na[0].lease_.cltt_);
+    EXPECT_EQ(1000, leases_client_na_renewed[0].cltt_ - leases_client_na[0].cltt_);
 
     // The client should now also acquire a PD lease.
     leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
     ASSERT_EQ(1, leases_client_pd.size());
-    EXPECT_EQ(STATUS_Success, leases_client_pd[0].status_code_);
+    EXPECT_EQ(STATUS_Success, client.getStatusCode(5678));
 }
 
 // This test verifies that the client can request the prefix delegation
@@ -182,16 +179,15 @@ TEST_F(RenewTest, requestAddressInRenew) {
     client.fastFwdTime(1000);
 
     // Make sure that the client has acquired PD lease.
-    std::vector<Dhcp6Client::LeaseInfo> leases_client_pd =
-        client.getLeasesByType(Lease::TYPE_PD);
+    std::vector<Lease6> leases_client_pd = client.getLeasesByType(Lease::TYPE_PD);
     ASSERT_EQ(1, leases_client_pd.size());
-    EXPECT_EQ(STATUS_Success, leases_client_pd[0].status_code_);
+    EXPECT_EQ(STATUS_Success, client.getStatusCode(5678));
 
     // The client should not acquire a NA lease.
-    std::vector<Dhcp6Client::LeaseInfo> leases_client_na =
+    std::vector<Lease6> leases_client_na =
         client.getLeasesByType(Lease::TYPE_NA);
-    ASSERT_EQ(1, leases_client_na.size());
-    ASSERT_EQ(STATUS_NoAddrsAvail, leases_client_na[0].status_code_);
+    ASSERT_EQ(0, leases_client_na.size());
+    ASSERT_EQ(STATUS_NoAddrsAvail, client.getStatusCode(1234));
 
     // Reconfigure the server to use both NA and PD pools.
     configure(RENEW_CONFIGS[2], *client.getServer());
@@ -200,17 +196,16 @@ TEST_F(RenewTest, requestAddressInRenew) {
     ASSERT_NO_THROW(client.doRenew());
 
     // Make sure that the client has renewed PD lease.
-    std::vector<Dhcp6Client::LeaseInfo> leases_client_pd_renewed =
+    std::vector<Lease6> leases_client_pd_renewed =
         client.getLeasesByType(Lease::TYPE_PD);
     ASSERT_EQ(1, leases_client_pd_renewed.size());
-    EXPECT_EQ(STATUS_Success, leases_client_pd_renewed[0].status_code_);
-    EXPECT_EQ(1000, leases_client_pd_renewed[0].lease_.cltt_ -
-              leases_client_pd[0].lease_.cltt_);
+    EXPECT_EQ(STATUS_Success, client.getStatusCode(5678));
+    EXPECT_EQ(1000, leases_client_pd_renewed[0].cltt_ - leases_client_pd[0].cltt_);
 
     // The client should now also acquire a NA lease.
     leases_client_na = client.getLeasesByType(Lease::TYPE_NA);
     ASSERT_EQ(1, leases_client_na.size());
-    EXPECT_EQ(STATUS_Success, leases_client_na[0].status_code_);
+    EXPECT_EQ(STATUS_Success, client.getStatusCode(1234));
 }