From: Marcin Siodelski Date: Mon, 3 Dec 2018 14:48:41 +0000 (+0100) Subject: [#303,!151] Solved a problem with reading leases having long lifetimes. X-Git-Tag: 186-add-kea-netconf-daemon-to-keactrl_base~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2408adc54888165c7aaae2a7d1b6b507a517893c;p=thirdparty%2Fkea.git [#303,!151] Solved a problem with reading leases having long lifetimes. Reading leases having long, e.g. infinite, lifetimes from a lease file used to fail due to invalid cast to uint32_t. --- diff --git a/src/lib/dhcpsrv/csv_lease_file4.cc b/src/lib/dhcpsrv/csv_lease_file4.cc index e54189f144..e9ca5a76a6 100644 --- a/src/lib/dhcpsrv/csv_lease_file4.cc +++ b/src/lib/dhcpsrv/csv_lease_file4.cc @@ -6,6 +6,7 @@ #include #include +#include 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(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(getColumnIndex("expire")) - - readValid(row); + time_t cltt = + static_cast(row.readAndConvertAt(getColumnIndex("expire")) + - readValid(row)); return (cltt); } diff --git a/src/lib/dhcpsrv/csv_lease_file6.cc b/src/lib/dhcpsrv/csv_lease_file6.cc index ea1d5f2454..e3b4ff435e 100644 --- a/src/lib/dhcpsrv/csv_lease_file6.cc +++ b/src/lib/dhcpsrv/csv_lease_file6.cc @@ -7,6 +7,7 @@ #include #include #include +#include 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(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(getColumnIndex("expire")) - - readValid(row); + time_t cltt = + static_cast(row.readAndConvertAt(getColumnIndex("expire")) + - readValid(row)); return (cltt); } diff --git a/src/lib/dhcpsrv/tests/csv_lease_file4_unittest.cc b/src/lib/dhcpsrv/tests/csv_lease_file4_unittest.cc index 00c3718a08..4055a04a86 100644 --- a/src/lib/dhcpsrv/tests/csv_lease_file4_unittest.cc +++ b/src/lib/dhcpsrv/tests/csv_lease_file4_unittest.cc @@ -11,6 +11,7 @@ #include #include #include +#include #include 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 diff --git a/src/lib/dhcpsrv/tests/csv_lease_file6_unittest.cc b/src/lib/dhcpsrv/tests/csv_lease_file6_unittest.cc index b483c57194..fd9a03491b 100644 --- a/src/lib/dhcpsrv/tests/csv_lease_file6_unittest.cc +++ b/src/lib/dhcpsrv/tests/csv_lease_file6_unittest.cc @@ -11,6 +11,7 @@ #include #include #include +#include #include 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