]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#303,!151] Solved a problem with reading leases having long lifetimes.
authorMarcin Siodelski <marcin@isc.org>
Mon, 3 Dec 2018 14:48:41 +0000 (15:48 +0100)
committerMarcin Siodelski <marcin@isc.org>
Wed, 5 Dec 2018 18:33:28 +0000 (13:33 -0500)
Reading leases having long, e.g. infinite, lifetimes from a lease file
used to fail due to invalid cast to uint32_t.

src/lib/dhcpsrv/csv_lease_file4.cc
src/lib/dhcpsrv/csv_lease_file6.cc
src/lib/dhcpsrv/tests/csv_lease_file4_unittest.cc
src/lib/dhcpsrv/tests/csv_lease_file6_unittest.cc

index e54189f144dee37c63c366bab6b64f453e280d19..e9ca5a76a616eb4ae019bba5af7c265655a9f9d4 100644 (file)
@@ -6,6 +6,7 @@
 
 #include <config.h>
 #include <dhcpsrv/csv_lease_file4.h>
+#include <ctime>
 
 using namespace isc::asiolink;
 using namespace isc::data;
@@ -47,7 +48,7 @@ CSVLeaseFile4::append(const Lease4& lease) {
         row.writeAt(getColumnIndex("client_id"), lease.client_id_->toText());
     }
     row.writeAt(getColumnIndex("valid_lifetime"), lease.valid_lft_);
-    row.writeAt(getColumnIndex("expire"), lease.cltt_ + lease.valid_lft_);
+    row.writeAt(getColumnIndex("expire"), static_cast<uint64_t>(lease.cltt_ + lease.valid_lft_));
     row.writeAt(getColumnIndex("subnet_id"), lease.subnet_id_);
     row.writeAt(getColumnIndex("fqdn_fwd"), lease.fqdn_fwd_);
     row.writeAt(getColumnIndex("fqdn_rev"), lease.fqdn_rev_);
@@ -194,8 +195,9 @@ CSVLeaseFile4::readValid(const CSVRow& row) {
 
 time_t
 CSVLeaseFile4::readCltt(const CSVRow& row) {
-    uint32_t cltt = row.readAndConvertAt<uint32_t>(getColumnIndex("expire"))
-        - readValid(row);
+    time_t cltt =
+        static_cast<time_t>(row.readAndConvertAt<uint64_t>(getColumnIndex("expire"))
+                            - readValid(row));
     return (cltt);
 }
 
index ea1d5f24549c0cb7bfb7a4cae7ff137d5d5f6b58..e3b4ff435eec20e4d6cf8f068811e164dd65a79c 100644 (file)
@@ -7,6 +7,7 @@
 #include <config.h>
 #include <dhcpsrv/dhcpsrv_log.h>
 #include <dhcpsrv/csv_lease_file6.h>
+#include <ctime>
 
 using namespace isc::asiolink;
 using namespace isc::data;
@@ -38,7 +39,7 @@ CSVLeaseFile6::append(const Lease6& lease) {
     row.writeAt(getColumnIndex("address"), lease.addr_.toText());
     row.writeAt(getColumnIndex("duid"), lease.duid_->toText());
     row.writeAt(getColumnIndex("valid_lifetime"), lease.valid_lft_);
-    row.writeAt(getColumnIndex("expire"), lease.cltt_ + lease.valid_lft_);
+    row.writeAt(getColumnIndex("expire"), static_cast<uint64_t>(lease.cltt_ + lease.valid_lft_));
     row.writeAt(getColumnIndex("subnet_id"), lease.subnet_id_);
     row.writeAt(getColumnIndex("pref_lifetime"), lease.preferred_lft_);
     row.writeAt(getColumnIndex("lease_type"), lease.type_);
@@ -186,8 +187,9 @@ CSVLeaseFile6::readValid(const CSVRow& row) {
 
 uint32_t
 CSVLeaseFile6::readCltt(const CSVRow& row) {
-    uint32_t cltt = row.readAndConvertAt<uint32_t>(getColumnIndex("expire"))
-        - readValid(row);
+    time_t cltt =
+        static_cast<time_t>(row.readAndConvertAt<uint64_t>(getColumnIndex("expire"))
+                            - readValid(row));
     return (cltt);
 }
 
index 00c3718a08ee193a44862b69ef07910815c2f3d3..4055a04a86db0ada0b0343023155a4e048bcbc40 100644 (file)
@@ -11,6 +11,7 @@
 #include <dhcpsrv/lease.h>
 #include <dhcpsrv/testutils/lease_file_io.h>
 #include <gtest/gtest.h>
+#include <ctime>
 #include <sstream>
 
 using namespace isc;
@@ -426,6 +427,41 @@ TEST_F(CSVLeaseFile4Test, declinedLeaseTest) {
     }
 }
 
+// Verifies that it is possible to output a lease with very high valid
+// lifetime (infinite in RFC2131 terms) and current time, and then read
+// back this lease.
+TEST_F(CSVLeaseFile4Test, highLeaseLifetime) {
+    CSVLeaseFile4 lf(filename_);
+    ASSERT_NO_THROW(lf.recreate());
+    ASSERT_TRUE(io_.exists());
+
+    // Write lease with very high lease lifetime and current time.
+    Lease4Ptr lease(new Lease4(IOAddress("192.0.3.2"),
+                               hwaddr0_,
+                               NULL, 0,
+                               0xFFFFFFFF, 50, 80, time(NULL),
+                               8, true, true,
+                               "host.example.com"));
+    // Write this lease out to the lease file.
+    ASSERT_NO_THROW(lf.append(*lease));
+
+    // Close the lease file.
+    lf.close();
+
+    Lease4Ptr lease_read;
+
+    // Re-open the file for reading.
+    ASSERT_NO_THROW(lf.open());
+
+    // Read the lease and make sure it is successful.
+    EXPECT_TRUE(lf.next(lease_read));
+    ASSERT_TRUE(lease_read);
+
+    // The valid lifetime and the cltt should match with the original lease.
+    EXPECT_EQ(lease->valid_lft_, lease_read->valid_lft_);
+    EXPECT_EQ(lease->cltt_, lease_read->cltt_);
+}
+
 /// @todo Currently we don't check invalid lease attributes, such as invalid
 /// lease type, invalid preferred lifetime vs valid lifetime etc. The Lease6
 /// should be extended with the function that validates lease attributes. Once
index b483c5719456a5f0e86f65e7725264eaa31ad893..fd9a03491ba7d218fac26aed16d9a3cb6b59e152 100644 (file)
@@ -11,6 +11,7 @@
 #include <dhcpsrv/lease.h>
 #include <dhcpsrv/testutils/lease_file_io.h>
 #include <gtest/gtest.h>
+#include <ctime>
 #include <sstream>
 
 using namespace isc;
@@ -539,6 +540,40 @@ TEST_F(CSVLeaseFile6Test, declinedLeaseTest) {
 }
 
 
+// Verifies that it is possible to output a lease with very high valid
+// lifetime (infinite in RFC2131 terms) and current time, and then read
+// back this lease.
+TEST_F(CSVLeaseFile6Test, highLeaseLifetime) {
+    CSVLeaseFile6 lf(filename_);
+    ASSERT_NO_THROW(lf.recreate());
+    ASSERT_TRUE(io_.exists());
+
+    // Write lease with very high lease lifetime and current time.
+    Lease6Ptr lease(new Lease6(Lease::TYPE_NA, IOAddress("2001:db8:1::1"),
+                               makeDUID(DUID0, sizeof(DUID0)),
+                               7, 100, 0xFFFFFFFF, 50, 80, 8, true, true,
+                               "host.example.com"));
+
+    // Write this lease out to the lease file.
+    ASSERT_NO_THROW(lf.append(*lease));
+
+    // Close the lease file.
+    lf.close();
+
+    Lease6Ptr lease_read;
+
+    // Re-open the file for reading.
+    ASSERT_NO_THROW(lf.open());
+
+    // Read the lease and make sure it is successful.
+    EXPECT_TRUE(lf.next(lease_read));
+    ASSERT_TRUE(lease_read);
+
+    // The valid lifetime and the cltt should match with the original lease.
+    EXPECT_EQ(lease->valid_lft_, lease_read->valid_lft_);
+    EXPECT_EQ(lease->cltt_, lease_read->cltt_);
+}
+
 /// @todo Currently we don't check invalid lease attributes, such as invalid
 /// lease type, invalid preferred lifetime vs valid lifetime etc. The Lease6
 /// should be extended with the function that validates lease attributes. Once