Commit 2408adc5 authored by Marcin Siodelski's avatar Marcin Siodelski

[#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.
parent 067c78a8
......@@ -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);
}
......
......@@ -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);
}
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment