]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#226] Factored new server UTs
authorFrancis Dupont <fdupont@isc.org>
Thu, 14 Aug 2025 09:06:29 +0000 (11:06 +0200)
committerFrancis Dupont <fdupont@isc.org>
Wed, 20 Aug 2025 15:39:16 +0000 (17:39 +0200)
src/bin/dhcp4/tests/flq_unittest.cc
src/bin/dhcp6/tests/flq_unittest.cc

index 48730258d0a77662429c655a68d9594eb3bc166e..294bed794c1dc2074dba39460075df2c40ce8ee6 100644 (file)
@@ -64,6 +64,64 @@ public:
         return (lease);
     }
 
+    /// @brief Check the response.
+    ///
+    /// @param resp the response.
+    /// @param expected the expected lifetime.
+    /// @param near use near comparision (when true) or equality (when false).
+    void checkResponse(Pkt4Ptr resp, uint32_t expected, bool near = false) {
+        ASSERT_TRUE(resp);
+
+        // Make sure that the server has responded with DHCPACK.
+        ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
+
+        // Make sure that the client has got the requested address.
+        EXPECT_EQ("10.0.0.14", resp->getYiaddr().toText());
+
+        // Verify the valid liftime.
+        OptionUint32Ptr opt = boost::dynamic_pointer_cast<
+            OptionUint32>(resp->getOption(DHO_DHCP_LEASE_TIME));
+        ASSERT_TRUE(opt);
+        if (near) {
+            EXPECT_NEAR(expected, opt->getValue(), 2);
+        } else {
+            EXPECT_EQ(expected, opt->getValue());
+        }
+    }
+
+    /// @brief Allocate all pool leases leaving the last one free.
+    void fill() {
+        auto& lease_mgr = LeaseMgrFactory::instance();
+        auto lease1 = createLease4(IOAddress("10.0.0.11"), 1);
+        EXPECT_TRUE(lease_mgr.addLease(lease1));
+        auto lease2 = createLease4(IOAddress("10.0.0.12"), 2);
+        EXPECT_TRUE(lease_mgr.addLease(lease2));
+        auto lease3 = createLease4(IOAddress("10.0.0.13"), 3);
+        EXPECT_TRUE(lease_mgr.addLease(lease3));
+    }
+
+    /// @brief Age and commit a lease.
+    ///
+    /// @param lease the lease.
+    /// @param delay the amount of time backward.
+    /// @param update when false add the lease, when true update the lease.
+    /// @param reclaim when true change the state.
+    void ageLease(Lease4Ptr lease, uint32_t delay, bool update,
+                  bool reclaim = false) {
+        ASSERT_TRUE(lease);
+        lease->cltt_ -= delay;
+        lease->current_cltt_ -= delay;
+        if (reclaim) {
+            lease->state_ = Lease::STATE_EXPIRED_RECLAIMED;
+        }
+        auto& lease_mgr = LeaseMgrFactory::instance();
+        if (update) {
+            EXPECT_NO_THROW(lease_mgr.updateLease4(lease));
+        } else {
+            EXPECT_TRUE(lease_mgr.addLease(lease));
+        }
+    }
+
     /// @brief Interface Manager's fake configuration control.
     IfaceMgrTestConfig iface_mgr_test_config_;
 };
@@ -79,21 +137,9 @@ TEST_F(FLQTest, empty) {
     boost::shared_ptr<IOAddress> hint(new IOAddress("10.0.0.14"));
     ASSERT_NO_THROW(client.doDORA(hint));
 
-    // Make sure that the server responded.
-    Pkt4Ptr resp = client.getContext().response_;
-    ASSERT_TRUE(resp);
-
-    // Make sure that the server has responded with DHCPACK.
-    ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
-
-    // Make sure that the client has got the requested address.
-    EXPECT_EQ(*hint, resp->getYiaddr());
-
     // Valid lifetime should be the valid-lifetime parameter value (200).
-    OptionUint32Ptr opt = boost::dynamic_pointer_cast<
-        OptionUint32>(resp->getOption(DHO_DHCP_LEASE_TIME));
-    ASSERT_TRUE(opt);
-    EXPECT_EQ(200, opt->getValue());
+    Pkt4Ptr resp = client.getContext().response_;
+    checkResponse(resp, 200);
 }
 
 // Test allocation with almost full pool.
@@ -104,32 +150,14 @@ TEST_F(FLQTest, last) {
     configure(FLQ_CONFIG, *client.getServer());
 
     // Create leases for the first addresses.
-    auto& lease_mgr = LeaseMgrFactory::instance();
-    auto lease1 = createLease4(IOAddress("10.0.0.11"), 1);
-    EXPECT_TRUE(lease_mgr.addLease(lease1));
-    auto lease2 = createLease4(IOAddress("10.0.0.12"), 2);
-    EXPECT_TRUE(lease_mgr.addLease(lease2));
-    auto lease3 = createLease4(IOAddress("10.0.0.13"), 3);
-    EXPECT_TRUE(lease_mgr.addLease(lease3));
+    fill();
 
     // Perform 4-way exchange with the server.
     ASSERT_NO_THROW(client.doDORA());
 
-    // Make sure that the server responded.
-    Pkt4Ptr resp = client.getContext().response_;
-    ASSERT_TRUE(resp);
-
-    // Make sure that the server has responded with DHCPACK.
-    ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
-
-    // Make sure that the client has got the last address.
-    EXPECT_EQ("10.0.0.14", resp->getYiaddr().toText());
-
     // Valid lifetime should be the min-valid-lifetime parameter value (100).
-    OptionUint32Ptr opt = boost::dynamic_pointer_cast<
-        OptionUint32>(resp->getOption(DHO_DHCP_LEASE_TIME));
-    ASSERT_TRUE(opt);
-    EXPECT_EQ(100, opt->getValue());
+    Pkt4Ptr resp = client.getContext().response_;
+    checkResponse(resp, 100);
 }
 
 // Test allocation with an expired lease.
@@ -140,38 +168,19 @@ TEST_F(FLQTest, expired) {
     configure(FLQ_CONFIG, *client.getServer());
 
     // Create leases for the first addresses.
-    auto& lease_mgr = LeaseMgrFactory::instance();
-    auto lease1 = createLease4(IOAddress("10.0.0.11"), 1);
-    EXPECT_TRUE(lease_mgr.addLease(lease1));
-    auto lease2 = createLease4(IOAddress("10.0.0.12"), 2);
-    EXPECT_TRUE(lease_mgr.addLease(lease2));
-    auto lease3 = createLease4(IOAddress("10.0.0.13"), 3);
-    EXPECT_TRUE(lease_mgr.addLease(lease3));
-    auto lease4 = createLease4(IOAddress("10.0.0.14"), 4);
-    // Expired lease.
-    lease4->cltt_ -= 1000;
-    lease4->current_cltt_ -= 1000;
-    ASSERT_TRUE(lease4->expired());
-    EXPECT_TRUE(lease_mgr.addLease(lease4));
+    fill();
+
+    // Create and expire last lease.
+    auto lease = createLease4(IOAddress("10.0.0.14"), 4);
+    ageLease(lease, 1000, false);
+    ASSERT_TRUE(lease->expired());
 
     // Perform 4-way exchange with the server.
     ASSERT_NO_THROW(client.doDORA());
 
-    // Make sure that the server responded.
-    Pkt4Ptr resp = client.getContext().response_;
-    ASSERT_TRUE(resp);
-
-    // Make sure that the server has responded with DHCPACK.
-    ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
-
-    // Make sure that the client has got the last address.
-    EXPECT_EQ("10.0.0.14", resp->getYiaddr().toText());
-
     // Valid lifetime should be the min-valid-lifetime parameter value (100).
-    OptionUint32Ptr opt = boost::dynamic_pointer_cast<
-        OptionUint32>(resp->getOption(DHO_DHCP_LEASE_TIME));
-    ASSERT_TRUE(opt);
-    EXPECT_EQ(100, opt->getValue());
+    Pkt4Ptr resp = client.getContext().response_;
+    checkResponse(resp, 100);
 }
 
 // Test allocation with a reclaimed lease.
@@ -182,38 +191,18 @@ TEST_F(FLQTest, reclaimed) {
     configure(FLQ_CONFIG, *client.getServer());
 
     // Create leases for the first addresses.
-    auto& lease_mgr = LeaseMgrFactory::instance();
-    auto lease1 = createLease4(IOAddress("10.0.0.11"), 1);
-    EXPECT_TRUE(lease_mgr.addLease(lease1));
-    auto lease2 = createLease4(IOAddress("10.0.0.12"), 2);
-    EXPECT_TRUE(lease_mgr.addLease(lease2));
-    auto lease3 = createLease4(IOAddress("10.0.0.13"), 3);
-    EXPECT_TRUE(lease_mgr.addLease(lease3));
-    auto lease4 = createLease4(IOAddress("10.0.0.14"), 4);
-    // Reclaimed lease.
-    lease4->cltt_ -= 1000;
-    lease4->current_cltt_ -= 1000;
-    lease4->state_ = Lease::STATE_EXPIRED_RECLAIMED;
-    EXPECT_TRUE(lease_mgr.addLease(lease4));
+    fill();
+
+    // Create and reclaim last lease.
+    auto lease = createLease4(IOAddress("10.0.0.14"), 4);
+    ageLease(lease, 1000, false, true);
 
     // Perform 4-way exchange with the server.
     ASSERT_NO_THROW(client.doDORA());
 
-    // Make sure that the server responded.
-    Pkt4Ptr resp = client.getContext().response_;
-    ASSERT_TRUE(resp);
-
-    // Make sure that the server has responded with DHCPACK.
-    ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
-
-    // Make sure that the client has got the last address.
-    EXPECT_EQ("10.0.0.14", resp->getYiaddr().toText());
-
     // Valid lifetime should be the min-valid-lifetime parameter value (100).
-    OptionUint32Ptr opt = boost::dynamic_pointer_cast<
-        OptionUint32>(resp->getOption(DHO_DHCP_LEASE_TIME));
-    ASSERT_TRUE(opt);
-    EXPECT_EQ(100, opt->getValue());
+    Pkt4Ptr resp = client.getContext().response_;
+    checkResponse(resp, 100);
 }
 
 // Test renewal with almost empty pool.
@@ -227,29 +216,15 @@ TEST_F(FLQTest, renew) {
     boost::shared_ptr<IOAddress> hint(new IOAddress("10.0.0.14"));
     ASSERT_NO_THROW(client.doDORA(hint));
 
-    // Make sure that the server responded.
-    Pkt4Ptr resp = client.getContext().response_;
-    ASSERT_TRUE(resp);
-
-    // Make sure that the server has responded with DHCPACK.
-    ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
-
-    // Make sure that the client has got the requested address.
-    EXPECT_EQ(*hint, resp->getYiaddr());
-
     // Valid lifetime should be the valid-lifetime parameter value (200).
-    OptionUint32Ptr opt = boost::dynamic_pointer_cast<
-        OptionUint32>(resp->getOption(DHO_DHCP_LEASE_TIME));
-    ASSERT_TRUE(opt);
-    EXPECT_EQ(200, opt->getValue());
+    Pkt4Ptr resp = client.getContext().response_;
+    checkResponse(resp, 200);
 
     // Age the lease.
     auto& lease_mgr = LeaseMgrFactory::instance();
     Lease4Ptr lease = lease_mgr.getLease4(*hint);
-    ASSERT_TRUE(lease);
-    lease->cltt_ -= 150;
-    lease->current_cltt_ -= 150;
-    EXPECT_NO_THROW(lease_mgr.updateLease4(lease));
+    ageLease(lease, 150, true);
+    ASSERT_FALSE(lease->expired());
 
     // Let's transition the client to Renewing state.
     client.setState(Dhcp4Client::RENEWING);
@@ -258,18 +233,9 @@ TEST_F(FLQTest, renew) {
     client.setDestAddress(IOAddress("10.0.0.1"));
     ASSERT_NO_THROW(client.doRequest());
 
-    // Make sure that renewal was ACKed.
-    resp = client.getContext().response_;
-    ASSERT_TRUE(resp);
-    ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
-
-    // Make sure that the client renewed the requested address.
-    EXPECT_EQ(*hint, resp->getYiaddr());
-
     // Valid lifetime should be the valid-lifetime parameter value (200).
-    opt = boost::dynamic_pointer_cast<OptionUint32>(resp->getOption(DHO_DHCP_LEASE_TIME));
-    ASSERT_TRUE(opt);
-    EXPECT_EQ(200, opt->getValue());
+    resp = client.getContext().response_;
+    checkResponse(resp, 200);
 }
 
 // Test renewal with full pool.
@@ -283,37 +249,18 @@ TEST_F(FLQTest, renewFull) {
     boost::shared_ptr<IOAddress> hint(new IOAddress("10.0.0.14"));
     ASSERT_NO_THROW(client.doDORA(hint));
 
-    // Make sure that the server responded.
+    // Valid lifetime should be the valid-lifetime parameter value (200).
     Pkt4Ptr resp = client.getContext().response_;
-    ASSERT_TRUE(resp);
-
-    // Make sure that the server has responded with DHCPACK.
-    ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
+    checkResponse(resp, 200);
 
-    // Make sure that the client has got the requested address.
-    EXPECT_EQ(*hint, resp->getYiaddr());
-
-    // Valid lifetime should be the valid-lifetime parameter value (200).
-    OptionUint32Ptr opt = boost::dynamic_pointer_cast<
-        OptionUint32>(resp->getOption(DHO_DHCP_LEASE_TIME));
-    ASSERT_TRUE(opt);
-    EXPECT_EQ(200, opt->getValue());
+    // Create leases for the first addresses.
+    fill();
 
     // Age the lease.
     auto& lease_mgr = LeaseMgrFactory::instance();
     Lease4Ptr lease = lease_mgr.getLease4(*hint);
-    ASSERT_TRUE(lease);
-    lease->cltt_ -= 150;
-    lease->current_cltt_ -= 150;
-    EXPECT_NO_THROW(lease_mgr.updateLease4(lease));
-
-    // Create leases for the first addresses.
-    auto lease1 = createLease4(IOAddress("10.0.0.11"), 1);
-    EXPECT_TRUE(lease_mgr.addLease(lease1));
-    auto lease2 = createLease4(IOAddress("10.0.0.12"), 2);
-    EXPECT_TRUE(lease_mgr.addLease(lease2));
-    auto lease3 = createLease4(IOAddress("10.0.0.13"), 3);
-    EXPECT_TRUE(lease_mgr.addLease(lease3));
+    ageLease(lease, 150, true);
+    ASSERT_FALSE(lease->expired());
 
     // Let's transition the client to Renewing state.
     client.setState(Dhcp4Client::RENEWING);
@@ -322,18 +269,9 @@ TEST_F(FLQTest, renewFull) {
     client.setDestAddress(IOAddress("10.0.0.1"));
     ASSERT_NO_THROW(client.doRequest());
 
-    // Make sure that renewal was ACKed.
-    resp = client.getContext().response_;
-    ASSERT_TRUE(resp);
-    ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
-
-    // Make sure that the client renewed the requested address.
-    EXPECT_EQ(*hint, resp->getYiaddr());
-
     // Valid lifetime should be the min-valid-lifetime parameter value (100).
-    opt = boost::dynamic_pointer_cast<OptionUint32>(resp->getOption(DHO_DHCP_LEASE_TIME));
-    ASSERT_TRUE(opt);
-    EXPECT_EQ(100, opt->getValue());
+    resp = client.getContext().response_;
+    checkResponse(resp, 100);
 }
 
 // Test renewal with full pool but remaining lifetime greater than minimal.
@@ -347,37 +285,18 @@ TEST_F(FLQTest, renewRemaining) {
     boost::shared_ptr<IOAddress> hint(new IOAddress("10.0.0.14"));
     ASSERT_NO_THROW(client.doDORA(hint));
 
-    // Make sure that the server responded.
+    // Valid lifetime should be the valid-lifetime parameter value (200).
     Pkt4Ptr resp = client.getContext().response_;
-    ASSERT_TRUE(resp);
-
-    // Make sure that the server has responded with DHCPACK.
-    ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
-
-    // Make sure that the client has got the requested address.
-    EXPECT_EQ(*hint, resp->getYiaddr());
+    checkResponse(resp, 200);
 
-    // Valid lifetime should be the valid-lifetime parameter value (200).
-    OptionUint32Ptr opt = boost::dynamic_pointer_cast<
-        OptionUint32>(resp->getOption(DHO_DHCP_LEASE_TIME));
-    ASSERT_TRUE(opt);
-    EXPECT_EQ(200, opt->getValue());
+    // Create leases for the first addresses.
+    fill();
 
     // Age the lease but only by 50 seconds.
     auto& lease_mgr = LeaseMgrFactory::instance();
     Lease4Ptr lease = lease_mgr.getLease4(*hint);
-    ASSERT_TRUE(lease);
-    lease->cltt_ -= 50;
-    lease->current_cltt_ -= 50;
-    EXPECT_NO_THROW(lease_mgr.updateLease4(lease));
-
-    // Create leases for the first addresses.
-    auto lease1 = createLease4(IOAddress("10.0.0.11"), 1);
-    EXPECT_TRUE(lease_mgr.addLease(lease1));
-    auto lease2 = createLease4(IOAddress("10.0.0.12"), 2);
-    EXPECT_TRUE(lease_mgr.addLease(lease2));
-    auto lease3 = createLease4(IOAddress("10.0.0.13"), 3);
-    EXPECT_TRUE(lease_mgr.addLease(lease3));
+    ageLease(lease, 50, true);
+    ASSERT_FALSE(lease->expired());
 
     // Let's transition the client to Renewing state.
     client.setState(Dhcp4Client::RENEWING);
@@ -386,18 +305,9 @@ TEST_F(FLQTest, renewRemaining) {
     client.setDestAddress(IOAddress("10.0.0.1"));
     ASSERT_NO_THROW(client.doRequest());
 
-    // Make sure that renewal was ACKed.
-    resp = client.getContext().response_;
-    ASSERT_TRUE(resp);
-    ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
-
-    // Make sure that the client renewed the requested address.
-    EXPECT_EQ(*hint, resp->getYiaddr());
-
     // Valid lifetime should be the remaining lifetime so ~150 seconds.
-    opt = boost::dynamic_pointer_cast<OptionUint32>(resp->getOption(DHO_DHCP_LEASE_TIME));
-    ASSERT_TRUE(opt);
-    EXPECT_NEAR(150, opt->getValue(), 2);
+    resp = client.getContext().response_;
+    checkResponse(resp, 150, true);
 }
 
 }
index 41ad2a7cb77e13e6185ea7275a49803e3ae88c5c..bdd8e4f756cfa02927f87d4ed94e8071be7647ff 100644 (file)
@@ -72,6 +72,78 @@ public:
         return (lease);
     }
 
+    /// @brief Check the returned lease.
+    ///
+    /// @param lease the lease.
+    /// @param exp_preferred the expected preferred lifetime.
+    /// @param exp_valid the expected valid lifetime
+    /// @param near_preferred use near comparision (when true) or equality
+    ///        when false) for the preferred lifetime.
+    /// @param near_valid use near comparision (when true) or equality
+    ///        when false) for the valid lifetime.
+    void checkResponse(Lease6Ptr lease, uint32_t exp_preferred,
+                       uint32_t exp_valid, bool near_preferred = false,
+                       bool near_valid = false) {
+        ASSERT_TRUE(lease);
+
+        // Make sure that the client has got the requested prefix.
+        EXPECT_EQ("2001:db8:1:e000::", lease->addr_.toText());
+        EXPECT_EQ(51, lease->prefixlen_);
+
+        if (near_preferred) {
+            EXPECT_NEAR(exp_preferred, lease->preferred_lft_, 2);
+        } else {
+            EXPECT_EQ(exp_preferred, lease->preferred_lft_);
+        }
+
+        if (near_valid) {
+            EXPECT_NEAR(exp_valid, lease->valid_lft_, 2);
+        } else {
+            EXPECT_EQ(exp_valid, lease->valid_lft_);
+        }
+    }
+
+    /// @brief Allocate all pool leases leaving the last one free.
+    void fill() {
+        auto& lease_mgr = LeaseMgrFactory::instance();
+        auto lease0 = createLease6(IOAddress("2001:db8:1::"), 1);
+        EXPECT_TRUE(lease_mgr.addLease(lease0));
+        auto lease1 = createLease6(IOAddress("2001:db8:1:2000::"), 2);
+        EXPECT_TRUE(lease_mgr.addLease(lease1));
+        auto lease2 = createLease6(IOAddress("2001:db8:1:4000::"), 3);
+        EXPECT_TRUE(lease_mgr.addLease(lease2));
+        auto lease3 = createLease6(IOAddress("2001:db8:1:6000::"), 4);
+        EXPECT_TRUE(lease_mgr.addLease(lease3));
+        auto lease4 = createLease6(IOAddress("2001:db8:1:8000::"), 5);
+        EXPECT_TRUE(lease_mgr.addLease(lease4));
+        auto lease5 = createLease6(IOAddress("2001:db8:1:a000::"), 6);
+        EXPECT_TRUE(lease_mgr.addLease(lease5));
+        auto lease6 = createLease6(IOAddress("2001:db8:1:c000::"), 7);
+        EXPECT_TRUE(lease_mgr.addLease(lease6));
+    }
+
+    /// @brief Age and commit a lease.
+    ///
+    /// @param lease the lease.
+    /// @param delay the amount of time backward.
+    /// @param update when false add the lease, when true update the lease.
+    /// @param reclaim when true change the state.
+    void ageLease(Lease6Ptr lease, uint32_t delay, bool update,
+                  bool reclaim = false) {
+        ASSERT_TRUE(lease);
+        lease->cltt_ -= delay;
+        lease->current_cltt_ -= delay;
+        if (reclaim) {
+            lease->state_ = Lease::STATE_EXPIRED_RECLAIMED;
+        }
+        auto& lease_mgr = LeaseMgrFactory::instance();
+        if (update) {
+            EXPECT_NO_THROW(lease_mgr.updateLease6(lease));
+        } else {
+            EXPECT_TRUE(lease_mgr.addLease(lease));
+        }
+    }
+
     /// @brief Interface Manager's fake configuration control.
     IfaceMgrTestConfig iface_mgr_test_config_;
 };
@@ -90,17 +162,10 @@ TEST_F(FLQTest, empty) {
     // Server should have assigned a prefix.
     ASSERT_EQ(1, client.getLeaseNum());
     Lease6Ptr lease = checkLease(client.getLease(0));
-    ASSERT_TRUE(lease);
-
-    // Make sure that the client has got the requested prefix.
-    EXPECT_EQ("2001:db8:1:e000::", lease->addr_.toText());
-    EXPECT_EQ(51, lease->prefixlen_);
 
     // Preferred lifetime should be the config preferred-lifetime (200).
-    EXPECT_EQ(200, lease->preferred_lft_);
-
     // Valid lifetime should be the config valid-lifetime (600).
-    EXPECT_EQ(600, lease->valid_lft_);
+    checkResponse(lease, 200, 600);
 }
 
 // Test allocation with almost full pool.
@@ -111,21 +176,7 @@ TEST_F(FLQTest, last) {
     configure(FLQ_CONFIG, *client.getServer());
 
     // Create leases for the first prefixes.
-    auto& lease_mgr = LeaseMgrFactory::instance();
-    auto lease0 = createLease6(IOAddress("2001:db8:1::"), 1);
-    EXPECT_TRUE(lease_mgr.addLease(lease0));
-    auto lease1 = createLease6(IOAddress("2001:db8:1:2000::"), 2);
-    EXPECT_TRUE(lease_mgr.addLease(lease1));
-    auto lease2 = createLease6(IOAddress("2001:db8:1:4000::"), 3);
-    EXPECT_TRUE(lease_mgr.addLease(lease2));
-    auto lease3 = createLease6(IOAddress("2001:db8:1:6000::"), 4);
-    EXPECT_TRUE(lease_mgr.addLease(lease3));
-    auto lease4 = createLease6(IOAddress("2001:db8:1:8000::"), 5);
-    EXPECT_TRUE(lease_mgr.addLease(lease4));
-    auto lease5 = createLease6(IOAddress("2001:db8:1:a000::"), 6);
-    EXPECT_TRUE(lease_mgr.addLease(lease5));
-    auto lease6 = createLease6(IOAddress("2001:db8:1:c000::"), 7);
-    EXPECT_TRUE(lease_mgr.addLease(lease6));
+    fill();
 
     // Perform 4-way exchange with the server.
     client.requestPrefix();
@@ -134,17 +185,10 @@ TEST_F(FLQTest, last) {
     // Server should have assigned a prefix.
     ASSERT_EQ(1, client.getLeaseNum());
     Lease6Ptr lease = checkLease(client.getLease(0));
-    ASSERT_TRUE(lease);
-
-    // Make sure that the client has got the requested prefix.
-    EXPECT_EQ("2001:db8:1:e000::", lease->addr_.toText());
-    EXPECT_EQ(51, lease->prefixlen_);
 
     // Preferred lifetime should be the config min-preferred-lifetime (100).
-    EXPECT_EQ(100, lease->preferred_lft_);
-
     // Valid lifetime should be the config min-valid-lifetime (400).
-    EXPECT_EQ(400, lease->valid_lft_);
+    checkResponse(lease, 100, 400);
 }
 
 // Test allocation with an expired lease.
@@ -155,27 +199,12 @@ TEST_F(FLQTest, expired) {
     configure(FLQ_CONFIG, *client.getServer());
 
     // Create leases for the first prefixes.
-    auto& lease_mgr = LeaseMgrFactory::instance();
-    auto lease0 = createLease6(IOAddress("2001:db8:1::"), 1);
-    EXPECT_TRUE(lease_mgr.addLease(lease0));
-    auto lease1 = createLease6(IOAddress("2001:db8:1:2000::"), 2);
-    EXPECT_TRUE(lease_mgr.addLease(lease1));
-    auto lease2 = createLease6(IOAddress("2001:db8:1:4000::"), 3);
-    EXPECT_TRUE(lease_mgr.addLease(lease2));
-    auto lease3 = createLease6(IOAddress("2001:db8:1:6000::"), 4);
-    EXPECT_TRUE(lease_mgr.addLease(lease3));
-    auto lease4 = createLease6(IOAddress("2001:db8:1:8000::"), 5);
-    EXPECT_TRUE(lease_mgr.addLease(lease4));
-    auto lease5 = createLease6(IOAddress("2001:db8:1:a000::"), 6);
-    EXPECT_TRUE(lease_mgr.addLease(lease5));
-    auto lease6 = createLease6(IOAddress("2001:db8:1:c000::"), 7);
-    EXPECT_TRUE(lease_mgr.addLease(lease6));
+    fill();
+
+    // Create and expire last lease.
     auto lease7 = createLease6(IOAddress("2001:db8:1:e000::"), 8);
-    // Expired lease.
-    lease7->cltt_ -= 10000;
-    lease7->current_cltt_ -= 10000;
+    ageLease(lease7, 10000, false);
     ASSERT_TRUE(lease7->expired());
-    EXPECT_TRUE(lease_mgr.addLease(lease7));
 
     // Perform 4-way exchange with the server.
     client.requestPrefix();
@@ -184,17 +213,10 @@ TEST_F(FLQTest, expired) {
     // Server should have assigned a prefix.
     ASSERT_EQ(1, client.getLeaseNum());
     Lease6Ptr lease = checkLease(client.getLease(0));
-    ASSERT_TRUE(lease);
-
-    // Make sure that the client has got the requested prefix.
-    EXPECT_EQ("2001:db8:1:e000::", lease->addr_.toText());
-    EXPECT_EQ(51, lease->prefixlen_);
 
     // Preferred lifetime should be the config min-preferred-lifetime (100).
-    EXPECT_EQ(100, lease->preferred_lft_);
-
     // Valid lifetime should be the config min-valid-lifetime (400).
-    EXPECT_EQ(400, lease->valid_lft_);
+    checkResponse(lease, 100, 400);
 }
 
 // Test allocation with a reclaimed lease.
@@ -205,28 +227,12 @@ TEST_F(FLQTest, reclaimed) {
     configure(FLQ_CONFIG, *client.getServer());
 
     // Create leases for the first prefixes.
-    auto& lease_mgr = LeaseMgrFactory::instance();
-    auto lease0 = createLease6(IOAddress("2001:db8:1::"), 1);
-    EXPECT_TRUE(lease_mgr.addLease(lease0));
-    auto lease1 = createLease6(IOAddress("2001:db8:1:2000::"), 2);
-    EXPECT_TRUE(lease_mgr.addLease(lease1));
-    auto lease2 = createLease6(IOAddress("2001:db8:1:4000::"), 3);
-    EXPECT_TRUE(lease_mgr.addLease(lease2));
-    auto lease3 = createLease6(IOAddress("2001:db8:1:6000::"), 4);
-    EXPECT_TRUE(lease_mgr.addLease(lease3));
-    auto lease4 = createLease6(IOAddress("2001:db8:1:8000::"), 5);
-    EXPECT_TRUE(lease_mgr.addLease(lease4));
-    auto lease5 = createLease6(IOAddress("2001:db8:1:a000::"), 6);
-    EXPECT_TRUE(lease_mgr.addLease(lease5));
-    auto lease6 = createLease6(IOAddress("2001:db8:1:c000::"), 7);
-    EXPECT_TRUE(lease_mgr.addLease(lease6));
+    fill();
+
+    // Create and reclaim last lease.
     auto lease7 = createLease6(IOAddress("2001:db8:1:e000::"), 8);
-    // Reclaimed lease.
-    lease7->cltt_ -= 10000;
-    lease7->current_cltt_ -= 10000;
+    ageLease(lease7, 10000, false, true);
     ASSERT_TRUE(lease7->expired());
-    lease7->state_ = Lease::STATE_EXPIRED_RECLAIMED;
-    EXPECT_TRUE(lease_mgr.addLease(lease7));
 
     // Perform 4-way exchange with the server.
     client.requestPrefix();
@@ -235,17 +241,10 @@ TEST_F(FLQTest, reclaimed) {
     // Server should have assigned a prefix.
     ASSERT_EQ(1, client.getLeaseNum());
     Lease6Ptr lease = checkLease(client.getLease(0));
-    ASSERT_TRUE(lease);
-
-    // Make sure that the client has got the requested prefix.
-    EXPECT_EQ("2001:db8:1:e000::", lease->addr_.toText());
-    EXPECT_EQ(51, lease->prefixlen_);
 
     // Preferred lifetime should be the config min-preferred-lifetime (100).
-    EXPECT_EQ(100, lease->preferred_lft_);
-
     // Valid lifetime should be the config min-valid-lifetime (400).
-    EXPECT_EQ(400, lease->valid_lft_);
+    checkResponse(lease, 100, 400);
 }
 
 // Test renewal with almost empty pool.
@@ -262,23 +261,13 @@ TEST_F(FLQTest, renew) {
     // Server should have assigned a prefix.
     ASSERT_EQ(1, client.getLeaseNum());
     Lease6Ptr lease = checkLease(client.getLease(0));
-    ASSERT_TRUE(lease);
-
-    // Make sure that the client has got the requested prefix.
-    EXPECT_EQ("2001:db8:1:e000::", lease->addr_.toText());
-    EXPECT_EQ(51, lease->prefixlen_);
 
     // Preferred lifetime should be the config preferred-lifetime (200).
-    EXPECT_EQ(200, lease->preferred_lft_);
-
     // Valid lifetime should be the config valid-lifetime (600).
-    EXPECT_EQ(600, lease->valid_lft_);
+    checkResponse(lease, 200, 600);
 
     // Age the lease.
-    lease->cltt_ -= 1000;
-    lease->current_cltt_ -= 1000;
-    auto& lease_mgr = LeaseMgrFactory::instance();
-    EXPECT_NO_THROW(lease_mgr.updateLease6(lease));
+    ageLease(lease, 1000, true);
 
     // Send the renew message to the server.
     ASSERT_NO_THROW(client.doRenew());
@@ -286,20 +275,13 @@ TEST_F(FLQTest, renew) {
     // Server should have renewed the prefix.
     ASSERT_EQ(1, client.getLeaseNum());
     Lease6Ptr renewed = checkLease(client.getLease(0));
-    ASSERT_TRUE(renewed);
 
-    // Make sure that the client has got the same prefix.
-    EXPECT_EQ(lease->addr_, renewed->addr_);
-    EXPECT_EQ(lease->prefixlen_, renewed->prefixlen_);
+    // Preferred lifetime should be the config preferred-lifetime (200).
+    // Valid lifetime should be the config valid-lifetime (600).
+    checkResponse(renewed, 200, 600);
 
     // Check the lease was updated.
     EXPECT_NEAR(lease->cltt_ + 1000, renewed->cltt_, 2);
-
-    // Preferred lifetime should be the config preferred-lifetime (200).
-    EXPECT_EQ(200, renewed->preferred_lft_);
-
-    // Valid lifetime should be the config valid-lifetime (600).
-    EXPECT_EQ(600, renewed->valid_lft_);
 }
 
 // Test renewal with full pool.
@@ -316,40 +298,16 @@ TEST_F(FLQTest, renewFull) {
     // Server should have assigned a prefix.
     ASSERT_EQ(1, client.getLeaseNum());
     Lease6Ptr lease = checkLease(client.getLease(0));
-    ASSERT_TRUE(lease);
-
-    // Make sure that the client has got the requested prefix.
-    EXPECT_EQ("2001:db8:1:e000::", lease->addr_.toText());
-    EXPECT_EQ(51, lease->prefixlen_);
 
     // Preferred lifetime should be the config preferred-lifetime (200).
-    EXPECT_EQ(200, lease->preferred_lft_);
-
     // Valid lifetime should be the config valid-lifetime (600).
-    EXPECT_EQ(600, lease->valid_lft_);
+    checkResponse(lease, 200, 600);
 
     // Create leases for the first prefixes.
-    auto& lease_mgr = LeaseMgrFactory::instance();
-    auto lease0 = createLease6(IOAddress("2001:db8:1::"), 1);
-    EXPECT_TRUE(lease_mgr.addLease(lease0));
-    auto lease1 = createLease6(IOAddress("2001:db8:1:2000::"), 2);
-    EXPECT_TRUE(lease_mgr.addLease(lease1));
-    auto lease2 = createLease6(IOAddress("2001:db8:1:4000::"), 3);
-    EXPECT_TRUE(lease_mgr.addLease(lease2));
-    auto lease3 = createLease6(IOAddress("2001:db8:1:6000::"), 4);
-    EXPECT_TRUE(lease_mgr.addLease(lease3));
-    auto lease4 = createLease6(IOAddress("2001:db8:1:8000::"), 5);
-    EXPECT_TRUE(lease_mgr.addLease(lease4));
-    auto lease5 = createLease6(IOAddress("2001:db8:1:a000::"), 6);
-    EXPECT_TRUE(lease_mgr.addLease(lease5));
-    auto lease6 = createLease6(IOAddress("2001:db8:1:c000::"), 7);
-    EXPECT_TRUE(lease_mgr.addLease(lease6));
-    auto lease7 = createLease6(IOAddress("2001:db8:1:e000::"), 8);
+    fill();
 
     // Age the lease.
-    lease->cltt_ -= 1000;
-    lease->current_cltt_ -= 1000;
-    EXPECT_NO_THROW(lease_mgr.updateLease6(lease));
+    ageLease(lease, 1000, true);
 
     // Send the renew message to the server.
     ASSERT_NO_THROW(client.doRenew());
@@ -359,18 +317,12 @@ TEST_F(FLQTest, renewFull) {
     Lease6Ptr renewed = checkLease(client.getLease(0));
     ASSERT_TRUE(renewed);
 
-    // Make sure that the client has got the same prefix.
-    EXPECT_EQ(lease->addr_, renewed->addr_);
-    EXPECT_EQ(lease->prefixlen_, renewed->prefixlen_);
+    // Preferred lifetime should be the config min-preferred-lifetime (100).
+    // Valid lifetime should be the config min-valid-lifetime (400).
+    checkResponse(renewed, 100, 400);
 
     // Check the lease was updated.
     EXPECT_NEAR(lease->cltt_ + 1000, renewed->cltt_, 2);
-
-    // Preferred lifetime should be the config min-preferred-lifetime (100).
-    EXPECT_EQ(100, renewed->preferred_lft_);
-
-    // Valid lifetime should be the config min-valid-lifetime (400).
-    EXPECT_EQ(400, renewed->valid_lft_);
 }
 
 // Test renewal with full pool but remaining lifetimes greater than minimal.
@@ -387,40 +339,16 @@ TEST_F(FLQTest, renewRemaining) {
     // Server should have assigned a prefix.
     ASSERT_EQ(1, client.getLeaseNum());
     Lease6Ptr lease = checkLease(client.getLease(0));
-    ASSERT_TRUE(lease);
-
-    // Make sure that the client has got the requested prefix.
-    EXPECT_EQ("2001:db8:1:e000::", lease->addr_.toText());
-    EXPECT_EQ(51, lease->prefixlen_);
 
     // Preferred lifetime should be the config preferred-lifetime (200).
-    EXPECT_EQ(200, lease->preferred_lft_);
-
     // Valid lifetime should be the config valid-lifetime (600).
-    EXPECT_EQ(600, lease->valid_lft_);
+    checkResponse(lease, 200, 600);
 
     // Create leases for the first prefixes.
-    auto& lease_mgr = LeaseMgrFactory::instance();
-    auto lease0 = createLease6(IOAddress("2001:db8:1::"), 1);
-    EXPECT_TRUE(lease_mgr.addLease(lease0));
-    auto lease1 = createLease6(IOAddress("2001:db8:1:2000::"), 2);
-    EXPECT_TRUE(lease_mgr.addLease(lease1));
-    auto lease2 = createLease6(IOAddress("2001:db8:1:4000::"), 3);
-    EXPECT_TRUE(lease_mgr.addLease(lease2));
-    auto lease3 = createLease6(IOAddress("2001:db8:1:6000::"), 4);
-    EXPECT_TRUE(lease_mgr.addLease(lease3));
-    auto lease4 = createLease6(IOAddress("2001:db8:1:8000::"), 5);
-    EXPECT_TRUE(lease_mgr.addLease(lease4));
-    auto lease5 = createLease6(IOAddress("2001:db8:1:a000::"), 6);
-    EXPECT_TRUE(lease_mgr.addLease(lease5));
-    auto lease6 = createLease6(IOAddress("2001:db8:1:c000::"), 7);
-    EXPECT_TRUE(lease_mgr.addLease(lease6));
-    auto lease7 = createLease6(IOAddress("2001:db8:1:e000::"), 8);
+    fill();
 
     // Age the lease but only by 50 seconds.
-    lease->cltt_ -= 50;
-    lease->current_cltt_ -= 50;
-    EXPECT_NO_THROW(lease_mgr.updateLease6(lease));
+    ageLease(lease, 50, true);
 
     // Send the renew message to the server.
     ASSERT_NO_THROW(client.doRenew());
@@ -428,20 +356,13 @@ TEST_F(FLQTest, renewRemaining) {
     // Server should have renewed the prefix.
     ASSERT_EQ(1, client.getLeaseNum());
     Lease6Ptr renewed = checkLease(client.getLease(0));
-    ASSERT_TRUE(renewed);
 
-    // Make sure that the client has got the same prefix.
-    EXPECT_EQ(lease->addr_, renewed->addr_);
-    EXPECT_EQ(lease->prefixlen_, renewed->prefixlen_);
+    // Preferred lifetime should be the remaining lifetime so ~150.
+    // Valid lifetime should be the remaining lifetime so ~550
+    checkResponse(renewed, 150, 550, true, true);
 
     // Check the lease was updated.
     EXPECT_NEAR(lease->cltt_ + 50, renewed->cltt_, 2);
-
-    // Preferred lifetime should be the remaining lifetime so ~150.
-    EXPECT_NEAR(150, renewed->preferred_lft_, 2);
-
-    // Valid lifetime should be the remaining lifetime so ~550
-    EXPECT_NEAR(550, renewed->valid_lft_, 2);
 }
 
 // Test renewal with full pool but remaining valid lifetime only greater
@@ -459,40 +380,16 @@ TEST_F(FLQTest, renewRemainingValid) {
     // Server should have assigned a prefix.
     ASSERT_EQ(1, client.getLeaseNum());
     Lease6Ptr lease = checkLease(client.getLease(0));
-    ASSERT_TRUE(lease);
-
-    // Make sure that the client has got the requested prefix.
-    EXPECT_EQ("2001:db8:1:e000::", lease->addr_.toText());
-    EXPECT_EQ(51, lease->prefixlen_);
 
     // Preferred lifetime should be the config preferred-lifetime (200).
-    EXPECT_EQ(200, lease->preferred_lft_);
-
     // Valid lifetime should be the config valid-lifetime (600).
-    EXPECT_EQ(600, lease->valid_lft_);
+    checkResponse(lease, 200, 600);
 
     // Create leases for the first prefixes.
-    auto& lease_mgr = LeaseMgrFactory::instance();
-    auto lease0 = createLease6(IOAddress("2001:db8:1::"), 1);
-    EXPECT_TRUE(lease_mgr.addLease(lease0));
-    auto lease1 = createLease6(IOAddress("2001:db8:1:2000::"), 2);
-    EXPECT_TRUE(lease_mgr.addLease(lease1));
-    auto lease2 = createLease6(IOAddress("2001:db8:1:4000::"), 3);
-    EXPECT_TRUE(lease_mgr.addLease(lease2));
-    auto lease3 = createLease6(IOAddress("2001:db8:1:6000::"), 4);
-    EXPECT_TRUE(lease_mgr.addLease(lease3));
-    auto lease4 = createLease6(IOAddress("2001:db8:1:8000::"), 5);
-    EXPECT_TRUE(lease_mgr.addLease(lease4));
-    auto lease5 = createLease6(IOAddress("2001:db8:1:a000::"), 6);
-    EXPECT_TRUE(lease_mgr.addLease(lease5));
-    auto lease6 = createLease6(IOAddress("2001:db8:1:c000::"), 7);
-    EXPECT_TRUE(lease_mgr.addLease(lease6));
-    auto lease7 = createLease6(IOAddress("2001:db8:1:e000::"), 8);
+    fill();
 
     // Age the lease but only by 150 seconds.
-    lease->cltt_ -= 150;
-    lease->current_cltt_ -= 150;
-    EXPECT_NO_THROW(lease_mgr.updateLease6(lease));
+    ageLease(lease, 150, true);
 
     // Send the renew message to the server.
     ASSERT_NO_THROW(client.doRenew());
@@ -500,20 +397,13 @@ TEST_F(FLQTest, renewRemainingValid) {
     // Server should have renewed the prefix.
     ASSERT_EQ(1, client.getLeaseNum());
     Lease6Ptr renewed = checkLease(client.getLease(0));
-    ASSERT_TRUE(renewed);
 
-    // Make sure that the client has got the same prefix.
-    EXPECT_EQ(lease->addr_, renewed->addr_);
-    EXPECT_EQ(lease->prefixlen_, renewed->prefixlen_);
+    // Preferred lifetime should be the config min-preferred-lifetime (100).
+    // Valid lifetime should be the remaining lifetime so ~450
+    checkResponse(renewed, 100, 450, false, true);
 
     // Check the lease was updated.
     EXPECT_NEAR(lease->cltt_ + 150, renewed->cltt_, 2);
-
-    // Preferred lifetime should be the config min-preferred-lifetime (100).
-    EXPECT_EQ(100, renewed->preferred_lft_);
-
-    // Valid lifetime should be the remaining lifetime so ~450
-    EXPECT_NEAR(450, renewed->valid_lft_, 2);
 }
 
 }