]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#805,!6-p] Addressed review comments
authorThomas Markwalder <tmark@isc.org>
Wed, 14 Aug 2019 14:18:26 +0000 (10:18 -0400)
committerThomas Markwalder <tmark@isc.org>
Fri, 16 Aug 2019 22:33:11 +0000 (18:33 -0400)
src/lib/dhcpsrv/csv_lease_file4.*
src/lib/dhcpsrv/tests/csv_lease_file4_unittest.cc
    - minor cleanup

src/lib/dhcpsrv/csv_lease_file6.*
    CSVLeaseFile6::append() - now throws if DUID is empty and
    state is not STATE_DECLINED

src/lib/dhcpsrv/tests/csv_lease_file6_unittest.cc
    Updated tests to verify duid/state logic

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

index 6649506579e81faccce343b1398f33d2c9e915d5..acf672362f91d67184fffb017efcf4928f337b74 100644 (file)
@@ -44,15 +44,21 @@ CSVLeaseFile4::append(const Lease4& lease) {
         ++write_errs_;
 
         isc_throw(BadValue, "Lease4: " << lease.addr_.toText() << ", state: "
-                  << lease.state_ << " has neither hardware address or client id");
+                  << Lease::basicStatesToText(lease.state_)
+                  << " has neither hardware address or client id");
+
+    }
+
+    // Hardware addr may be unset (NULL).
+    if (lease.hwaddr_) {
+        row.writeAt(getColumnIndex("hwaddr"), lease.hwaddr_->toText(false));
     }
 
-    row.writeAt(getColumnIndex("hwaddr"),
-                (!lease.hwaddr_  ? "" : lease.hwaddr_->toText(false)));
     // Client id may be unset (NULL).
     if (lease.client_id_) {
         row.writeAt(getColumnIndex("client_id"), lease.client_id_->toText());
     }
+
     row.writeAt(getColumnIndex("valid_lifetime"), lease.valid_lft_);
     row.writeAt(getColumnIndex("expire"), static_cast<uint64_t>(lease.cltt_ + lease.valid_lft_));
     row.writeAt(getColumnIndex("subnet_id"), lease.subnet_id_);
@@ -117,7 +123,8 @@ CSVLeaseFile4::next(Lease4Ptr& lease) {
         if ((hwaddr.hwaddr_.empty()) && (client_id_vec.empty()) &&
             (state != Lease::STATE_DECLINED)) {
             isc_throw(BadValue, "Lease4: " << addr.toText() << ", state: "
-                      << state << "has neither hardware address or client id");
+                      << Lease::basicStatesToText(state)
+                      << "has neither hardware address or client id");
         }
 
         // Get the user context (can be NULL).
index 950417c9e1e3a1d6e00eae2dc7cbb3e0cd626115..7f6b17f88f298f6b2b3ae8e77836618bfea9919c 100644 (file)
@@ -68,8 +68,8 @@ public:
     /// message using @c CSVFile::setReadMsg and returns false. The error
     /// string may be read using @c CSVFile::getReadMsg.
     ///
-    /// Treats rows that no hardware address, no client id and state is not
-    /// STATE_DECLINED as an error.
+    /// Treats rows without a hardware address or a client id when their
+    /// state is not STATE_DECLINED as an error.
     ///
     /// This function is exception safe.
     ///
index 3e70c7c6ecf84be32717911621bcfefcb2b70117..377411303250f26b071e9b5a656adcb59aece983 100644 (file)
@@ -35,6 +35,12 @@ CSVLeaseFile6::append(const Lease6& lease) {
     // Bump the number of write attempts
     ++writes_;
 
+    if ((*(lease.duid_) == DUID::EMPTY()) && (lease.state_ != Lease::STATE_DECLINED)) {
+        ++write_errs_;
+        isc_throw(BadValue, "Lease6: " << lease.addr_.toText() << ", state: "
+                  << Lease::basicStatesToText(lease.state_) << ", has no DUID");
+    }
+
     CSVRow row(getColumnCount());
     row.writeAt(getColumnIndex("address"), lease.addr_.toText());
     row.writeAt(getColumnIndex("duid"), lease.duid_->toText());
index 6ee380a2331e5ba164ef0bed9a1c9c7c404d71bc..84acbd0a85bdd4e078cffdfdf28b058ee2529f5b 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2018 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2019 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -57,6 +57,8 @@ public:
     /// error.
     ///
     /// @param lease Structure representing a DHCPv6 lease.
+    /// @throw BadValue if the lease to be written has an empty DUID and is
+    /// whose state is not STATE_DECLINED.
     void append(const Lease6& lease);
 
     /// @brief Reads next lease from the CSV file.
index ad65277a7185903734f2aba8092455c243ef06da..fe5b668a1b78b4392e946b69dbf95365a1b2a16b 100644 (file)
@@ -514,7 +514,7 @@ TEST_F(CSVLeaseFile4Test, emptyHWAddrDefaultStateOnly) {
 
     hwaddr.reset(new HWAddr());
 
-    //Create lease with empty hdaddr and default state
+    // Create lease with empty hwaddr and default state
     Lease4Ptr lease_empty_hwaddr(new Lease4(IOAddress("192.0.3.2"),
                                  hwaddr,
                                  NULL, 0,
index 49ae14bf9ac87cb27234765254d49b8da8ac27d8..bfc79791c0a7dfac1eac704639b07e09dd35172d 100644 (file)
@@ -109,7 +109,9 @@ CSVLeaseFile6Test::writeSampleFile() const {
                   "2001:db8:2::10,01:01:01:01:0a:01:02:03:04:05,300,300,6,150,"
                   "0,8,0,0,0,,,1,\n"
                   "3000:1::,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,0,200,8,0,2,"
-                  "16,64,0,0,,,1,{ \"foobar\": true }\n");
+                  "16,64,0,0,,,1,{ \"foobar\": true }\n"
+                  "2001:db8:1::2,00,200,200,8,100,0,7,0,1,1,host.example.com,,0,\n"
+                  "2001:db8:1::3,00,200,200,8,100,0,7,0,1,1,host.example.com,,1,\n");
 }
 
 // This test checks the capability to read and parse leases from the file.
@@ -153,7 +155,7 @@ TEST_F(CSVLeaseFile6Test, parse) {
     EXPECT_FALSE(lease->getContext());
     }
 
-    // Second lease is malformed - DUID is empty.
+    // Second lease is malformed - DUID is blank (i.e. ",,")
     {
     SCOPED_TRACE("Second lease malformed");
     EXPECT_FALSE(lf.next(lease));
@@ -212,21 +214,43 @@ TEST_F(CSVLeaseFile6Test, parse) {
     EXPECT_EQ("{ \"foobar\": true }", lease->getContext()->str());
     }
 
+
+    // Fifth lease is invalid - DUID is empty, state is not DECLINED
+    {
+    SCOPED_TRACE("Fifth lease invalid");
+    EXPECT_FALSE(lf.next(lease));
+    checkStats(lf, 5, 3, 2, 0, 0, 0);
+    }
+
+    // Reading the sixth lease should be successful.
+    {
+    SCOPED_TRACE("sixth lease valid");
+    EXPECT_TRUE(lf.next(lease));
+    ASSERT_TRUE(lease);
+    checkStats(lf, 6, 4, 2, 0, 0, 0);
+
+    // Verify that the lease is correct.
+    EXPECT_EQ("2001:db8:1::3", lease->addr_.toText());
+    ASSERT_TRUE(lease->duid_);
+    EXPECT_EQ("00", lease->duid_->toText());
+    EXPECT_EQ(Lease::STATE_DECLINED, lease->state_);
+    }
+
     // There are no more leases. Reading should cause no error, but the returned
     // lease pointer should be NULL.
     {
-    SCOPED_TRACE("Fifth read empty");
+    SCOPED_TRACE("Sixth read empty");
     EXPECT_TRUE(lf.next(lease));
     EXPECT_FALSE(lease);
-    checkStats(lf, 5, 3, 1, 0, 0, 0);
+    checkStats(lf, 7, 4, 2, 0, 0, 0);
     }
 
     // We should be able to do it again.
     {
-    SCOPED_TRACE("Sixth read empty");
+    SCOPED_TRACE("Seventh read empty");
     EXPECT_TRUE(lf.next(lease));
     EXPECT_FALSE(lease);
-    checkStats(lf, 6, 3, 1, 0, 0, 0);
+    checkStats(lf, 8, 4, 2, 0, 0, 0);
     }
 }
 
@@ -276,6 +300,25 @@ TEST_F(CSVLeaseFile6Test, recreate) {
     checkStats(lf, 0, 0, 0, 3, 3, 0);
     }
 
+    DuidPtr empty(new DUID(DUID::EMPTY()));
+    lease.reset(new Lease6(Lease::TYPE_NA, IOAddress("2001:db8:2::10"),
+                           empty, 8, 150, 300, 6, false, false,
+                           "", HWAddrPtr(), 128));
+    lease->cltt_ = 0;
+    {
+    SCOPED_TRACE("Fourth write - invalid, no DUID, not declined");
+    ASSERT_THROW(lf.append(*lease), BadValue);
+    checkStats(lf, 0, 0, 0, 4, 3, 1);
+    }
+
+    {
+    SCOPED_TRACE("Fifth write - valid, no DUID, declined");
+    lease->state_ = Lease::STATE_DECLINED;
+    ASSERT_NO_THROW(lf.append(*lease));
+    checkStats(lf, 0, 0, 0, 5, 4, 1);
+    }
+
+
     EXPECT_EQ("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
               "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
               "state,user_context\n"
@@ -284,7 +327,8 @@ TEST_F(CSVLeaseFile6Test, recreate) {
               "2001:db8:2::10,01:01:01:01:0a:01:02:03:04:05"
               ",300,300,6,150,0,8,128,0,0,,,0,\n"
               "3000:1:1::,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,"
-              "300,300,10,150,2,7,64,0,0,,,0,{ \"foobar\": true }\n",
+              "300,300,10,150,2,7,64,0,0,,,0,{ \"foobar\": true }\n"
+              "2001:db8:2::10,00,300,300,6,150,0,8,128,0,0,,,1,\n",
               io_.readFile());
 }