]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[5682] Unit-tests written for lease file loader
authorTomek Mrugalski <tomasz@isc.org>
Wed, 25 Jul 2018 22:31:15 +0000 (00:31 +0200)
committerTomek Mrugalski <tomasz@isc.org>
Mon, 30 Jul 2018 17:03:18 +0000 (19:03 +0200)
src/lib/dhcpsrv/lease_file_loader.h
src/lib/dhcpsrv/tests/lease_file_loader_unittest.cc
src/lib/dhcpsrv/tests/sanity_checks_unittest.cc

index fee4321e5fa5af1bcce59c605f55b613484ec65f..3aef65fcca66619970f4c4635e3d3f60355f560a 100644 (file)
@@ -126,8 +126,8 @@ public:
                     .arg(lease->toText());
 
                 // Now see if we need to sanitize this lease. As lease file is
-                // loaded during the configuration, we have to use staging,
-                // rather than current config for this.
+                // loaded during the configuration, we have to use staging config,
+                // rather than current config for this (false = staging).
                 lease_checker.checkLease(lease, false);
                 if (!lease) {
                     continue;
index 1670f81087ee9eef2d48ac4b600820ae38f3b36f..f8d13ce23c001eda9ef755bd5c6de45dbfbeb736 100644 (file)
@@ -11,6 +11,8 @@
 #include <dhcpsrv/memfile_lease_storage.h>
 #include <dhcpsrv/lease_file_loader.h>
 #include <dhcpsrv/testutils/lease_file_io.h>
+#include <dhcpsrv/cfgmgr.h>
+#include <dhcpsrv/cfg_consistency.h>
 #include <boost/scoped_ptr.hpp>
 #include <gtest/gtest.h>
 #include <sstream>
@@ -140,6 +142,110 @@ public:
     std::string v4_hdr_; ///< String for the header of the v4 csv test file
     std::string v6_hdr_; ///< String for the header of the v6 csv test file
 
+    Lease4Storage storage4_; ///< Storage for IPv4 leases
+    Lease6Storage storage6_; ///< Storage for IPv4 leases
+
+    /// @brief Checks if IPv4 lease loaded from file is sanity checked.
+    ///
+    /// This method writes a simple lease file with one lease in it,
+    /// then sets sanity checks to tested level, then tries to load
+    /// the lease file and finally checks whether the lease was loaded
+    /// or not.
+    ///
+    /// @param lease address of the lease in text form
+    /// @param lease_id subnet-id to be used in a lease
+    /// @param sanity level of sanity checks
+    /// @param exp_present is the lease expected to be loaded (true = yes)
+    /// @param exp_id expected subnet-id of the loaded lease
+    void sanityChecks4(std::string lease, SubnetID lease_id,
+                       CfgConsistency::LeaseSanity sanity,
+                       bool exp_present, SubnetID exp_id) {
+
+        std::stringstream file_content;
+        file_content << v4_hdr_ << lease << ",dd:de:ba:0d:1b:2e,"
+                     << "0a:00:01:04,100,100," << static_cast<int>(lease_id)
+                     << ",0,0,,1,\n";
+
+        ASSERT_NO_THROW(CfgMgr::instance().getStagingCfg()->getConsistency()
+                    ->setLeaseSanityCheck(sanity));
+
+        io_.writeFile(file_content.str());
+
+        boost::scoped_ptr<CSVLeaseFile4> lf(new CSVLeaseFile4(filename_));
+        ASSERT_NO_THROW(lf->open());
+
+        // Load leases from the file.
+        ASSERT_NO_THROW(LeaseFileLoader::load<Lease4>(*lf, storage4_, 10));
+
+        {
+            SCOPED_TRACE("Read leases");
+            checkStats(*lf, 2, 1, 0, 0, 0, 0);
+        }
+
+        // Check how many leases were actually loaded.
+        ASSERT_EQ( (exp_present ? 1 : 0), storage4_.size());
+
+        Lease4Ptr l = getLease<Lease4Ptr>(lease, storage4_);
+
+        if (exp_present) {
+            ASSERT_TRUE(l) << "lease not found, but expected";
+            EXPECT_EQ(exp_id, l->subnet_id_);
+        } else {
+            EXPECT_FALSE(l) << "lease found, but was not expected";
+        }
+    }
+
+    /// @brief Checks if IPv4 lease loaded from file is sanity checked.
+    ///
+    /// This method writes a simple lease file with one lease in it,
+    /// then sets sanity checks to tested level, then tries to load
+    /// the lease file and finally checks whether the lease was loaded
+    /// or not.
+    ///
+    /// @param lease address of the lease in text form
+    /// @param lease_id subnet-id to be used in a lease
+    /// @param sanity level of sanity checks
+    /// @param exp_present is the lease expected to be loaded (true = yes)
+    /// @param exp_id expected subnet-id of the loaded lease
+    void sanityChecks6(std::string lease, SubnetID lease_id,
+                       CfgConsistency::LeaseSanity sanity,
+                       bool exp_present, SubnetID exp_id) {
+
+        std::stringstream file_content;
+
+        file_content << v6_hdr_ << lease << ",dd:de:ba:0d:1b:2e,"
+                     << "300,300," << static_cast<int>(lease_id)
+                     << ",150,0,8,0,0,0,,,1,\n";
+
+        ASSERT_NO_THROW(CfgMgr::instance().getStagingCfg()->getConsistency()
+                    ->setLeaseSanityCheck(sanity));
+
+        io_.writeFile(file_content.str());
+
+        boost::scoped_ptr<CSVLeaseFile6> lf(new CSVLeaseFile6(filename_));
+        ASSERT_NO_THROW(lf->open());
+
+        // Load leases from the file.
+        ASSERT_NO_THROW(LeaseFileLoader::load<Lease6>(*lf, storage6_, 10));
+
+        {
+            SCOPED_TRACE("Read leases");
+            checkStats(*lf, 2, 1, 0, 0, 0, 0);
+        }
+
+        // Check how many leases were actually loaded.
+        ASSERT_EQ( (exp_present ? 1 : 0), storage6_.size());
+
+        Lease6Ptr l = getLease<Lease6Ptr>(lease, storage6_);
+
+        if (exp_present) {
+            ASSERT_TRUE(l) << "lease not found, but expected";
+            EXPECT_EQ(exp_id, l->subnet_id_);
+        } else {
+            EXPECT_FALSE(l) << "lease found, but was not expected";
+        }
+    }
+
 protected:
     /// @brief Sets up the header strings
     virtual void SetUp() {
@@ -547,4 +653,31 @@ TEST_F(LeaseFileLoaderTest, loadWriteLeaseWithZeroLifetime) {
     checkStats(*lf, 0, 0, 0, 1, 1, 0);
     }
 }
+
+// This test checks if the lease can be loaded, even though there are no
+// subnets configured that it would match.
+TEST_F(LeaseFileLoaderTest, sanityChecker4NoSubnetsWarn) {
+    sanityChecks4("192.0.2.1", 1, CfgConsistency::LEASE_CHECK_WARN, true, 1);
+}
+
+// This test checks if the lease can be discarded.  Note we are
+// verifying whether the checks are conducted at all when loading a file.
+// Thorough tests were conducted in sanity_checks_unittest.cc.
+TEST_F(LeaseFileLoaderTest, sanityChecker4NoSubnetsDel) {
+    sanityChecks4("192.0.2.1", 1, CfgConsistency::LEASE_CHECK_DEL, false, 1);
+}
+
+// This test checks if the lease can be loaded, even though there are no
+// subnets configured that it would match.
+TEST_F(LeaseFileLoaderTest, sanityChecker6NoSubnetsWarn) {
+    sanityChecks6("2001::1", 1, CfgConsistency::LEASE_CHECK_WARN, true, 1);
+}
+
+// This test checks if the lease can be discarded.  Note we are
+// verifying whether the checks are conducted at all when loading a file.
+// Thorough tests were conducted in sanity_checks_unittest.cc.
+TEST_F(LeaseFileLoaderTest, sanityChecker6NoSubnetsDel) {
+    sanityChecks6("2001::1", 1, CfgConsistency::LEASE_CHECK_DEL, false, 1);
+}
+
 } // end of anonymous namespace
index a8c246f2755c2b1eda39e6ac6dcb8afbe01072c1..05400fc713a595a364550f751a9a207b13aed522 100644 (file)
@@ -282,63 +282,72 @@ TEST_F(SanityChecksTest, leaseCheck) {
 // no subnets configured, sanity-check is set to none
 // Expected behavior: lease added as is
 TEST_F(SanityChecksTest, memfileAdd4NoSubnetNone) {
-    leaseAddCheck4("192.0.2.1", 1, "", 0, CfgConsistency::LEASE_CHECK_NONE, true, 1);
+    leaseAddCheck4("192.0.2.1", 1, "", 0, CfgConsistency::LEASE_CHECK_NONE,
+                   true, 1);
 }
 
 // This test checks how the code behaves when there is:
 // no subnets configured, sanity-check is set to warn
 // Expected behavior: lease added as is
 TEST_F(SanityChecksTest, memfileAdd4NoSubnetWarn) {
-    leaseAddCheck4("192.0.2.1", 1, "", 0, CfgConsistency::LEASE_CHECK_WARN, true, 1);
+    leaseAddCheck4("192.0.2.1", 1, "", 0, CfgConsistency::LEASE_CHECK_WARN,
+                   true, 1);
 }
 
 // This test checks how the code behaves when there is:
 // no subnets configured, sanity-check is set to fix
 // Expected behavior: lease added as is
 TEST_F(SanityChecksTest, memfileAdd4NoSubnetFix) {
-    leaseAddCheck4("192.0.2.1", 1, "", 0, CfgConsistency::LEASE_CHECK_FIX, true, 1);
+    leaseAddCheck4("192.0.2.1", 1, "", 0, CfgConsistency::LEASE_CHECK_FIX,
+                   true, 1);
 }
 
 // This test checks how the code behaves when there is:
 // no subnets configured, sanity-check is set to fix-del
 // Expected behavior: lease not added
 TEST_F(SanityChecksTest, memfileAdd4NoSubnetFixDel) {
-    leaseAddCheck4("192.0.2.1", 1, "", 0, CfgConsistency::LEASE_CHECK_FIX_DEL, false, 1);
+    leaseAddCheck4("192.0.2.1", 1, "", 0, CfgConsistency::LEASE_CHECK_FIX_DEL,
+                   false, 1);
 }
 
 // This test checks how the code behaves when there is:
 // no subnets configured, sanity-check is set to del
 // Expected behavior: lease not added
 TEST_F(SanityChecksTest, memfileAdd4NoSubnetDel) {
-    leaseAddCheck4("192.0.2.1", 1, "", 0, CfgConsistency::LEASE_CHECK_DEL, false, 1);
+    leaseAddCheck4("192.0.2.1", 1, "", 0, CfgConsistency::LEASE_CHECK_DEL,
+                   false, 1);
 }
 
 // This test checks how the code behaves when there is:
 // one subnet configured, sanity-check is set to none
 // Expected behavior: lease added as is
 TEST_F(SanityChecksTest, memfileAdd4checksNone) {
-    leaseAddCheck4("192.0.2.1", 1, "192.0.2.0/24", 2, CfgConsistency::LEASE_CHECK_NONE, true, 1);
+    leaseAddCheck4("192.0.2.1", 1, "192.0.2.0/24", 2,
+                   CfgConsistency::LEASE_CHECK_NONE, true, 1);
 }
 
 // This test checks how the code behaves when there is:
 // one subnet configured, sanity-check is set to warn
 // Expected behavior: lease added as is
 TEST_F(SanityChecksTest, memfileAdd4checksWarn) {
-    leaseAddCheck4("192.0.2.1", 1, "192.0.2.0/24", 2, CfgConsistency::LEASE_CHECK_WARN, true, 1);
+    leaseAddCheck4("192.0.2.1", 1, "192.0.2.0/24", 2,
+                   CfgConsistency::LEASE_CHECK_WARN, true, 1);
 }
 
 // This test checks how the code behaves when there is:
 // one subnet configured, sanity-check is set to fix
 // Expected behavior: lease added with corrected subnet-id
 TEST_F(SanityChecksTest, memfileAdd4checksFix) {
-    leaseAddCheck4("192.0.2.1", 1, "192.0.2.0/24", 2, CfgConsistency::LEASE_CHECK_FIX, true, 2);
+    leaseAddCheck4("192.0.2.1", 1, "192.0.2.0/24", 2,
+                   CfgConsistency::LEASE_CHECK_FIX, true, 2);
 }
 
 // This test checks how the code behaves when there is:
 // one subnet configured, sanity-check is set to fix-del
 // Expected behavior: lease added with corrected subnet-id
 TEST_F(SanityChecksTest, memfileAdd4checksFixdel1) {
-    leaseAddCheck4("192.0.2.1", 1, "192.0.2.0/24", 2, CfgConsistency::LEASE_CHECK_FIX_DEL, true, 2);
+    leaseAddCheck4("192.0.2.1", 1, "192.0.2.0/24", 2,
+                   CfgConsistency::LEASE_CHECK_FIX_DEL, true, 2);
 }
 
 // This test checks how the code behaves when there is:
@@ -346,68 +355,103 @@ TEST_F(SanityChecksTest, memfileAdd4checksFixdel1) {
 // sanity-check is set to fix-del
 // Expected behavior: lease not added
 TEST_F(SanityChecksTest, memfileAdd4checksFixdel2) {
-    leaseAddCheck4("192.0.2.1", 1, "192.0.3.0/24", 2, CfgConsistency::LEASE_CHECK_FIX_DEL, false, 0);
+    leaseAddCheck4("192.0.2.1", 1, "192.0.3.0/24", 2,
+                   CfgConsistency::LEASE_CHECK_FIX_DEL, false, 0);
 }
 
 // This test checks how the code behaves when there is:
-// one subnet configured, sanity-check is set to fix-del
+// one subnet configured, sanity-check is set to del
 // Expected behavior: lease not added
 TEST_F(SanityChecksTest, memfileAdd4checksDel) {
-    leaseAddCheck4("192.0.2.1", 1, "192.0.2.0/24", 2, CfgConsistency::LEASE_CHECK_DEL, false, 0);
+    leaseAddCheck4("192.0.2.1", 1, "192.0.2.0/24", 2,
+                   CfgConsistency::LEASE_CHECK_DEL, false, 0);
 }
 
 // This test checks how the code behaves when there is:
 // no subnets configured, sanity-check is set to none
 // Expected behavior: lease added as is
 TEST_F(SanityChecksTest, memfileAdd6NoSubnetNone) {
-    leaseAddCheck6("2001::1", 1, "", 0, CfgConsistency::LEASE_CHECK_NONE, true, 1);
+    leaseAddCheck6("2001::1", 1, "", 0,
+                   CfgConsistency::LEASE_CHECK_NONE, true, 1);
 }
 
 // This test checks how the code behaves when there is:
 // no subnets configured, sanity-check is set to warn
 // Expected behavior: lease added as is
 TEST_F(SanityChecksTest, memfileAdd6NoSubnetWarn) {
-    leaseAddCheck6("2000::1", 1, "", 0, CfgConsistency::LEASE_CHECK_WARN, true, 1);
+    leaseAddCheck6("2000::1", 1, "", 0,
+                   CfgConsistency::LEASE_CHECK_WARN, true, 1);
 }
 
 // This test checks how the code behaves when there is:
 // no subnets configured, sanity-check is set to fix
 // Expected behavior: lease added as is
 TEST_F(SanityChecksTest, memfileAdd6NoSubnetFix) {
-    leaseAddCheck6("2000::1", 1, "", 0, CfgConsistency::LEASE_CHECK_FIX, true, 1);
+    leaseAddCheck6("2000::1", 1, "", 0,
+                   CfgConsistency::LEASE_CHECK_FIX, true, 1);
 }
 
 // This test checks how the code behaves when there is:
 // no subnets configured, sanity-check is set to fix-del
 // Expected behavior: lease not added
 TEST_F(SanityChecksTest, memfileAdd6NoSubnetFixDel) {
-    leaseAddCheck6("2000::1", 1, "", 0, CfgConsistency::LEASE_CHECK_FIX_DEL, false, 1);
+    leaseAddCheck6("2000::1", 1, "", 0,
+                   CfgConsistency::LEASE_CHECK_FIX_DEL, false, 1);
 }
 
+// This test checks how the code behaves when there is:
+// no subnets configured, sanity-check is set to del
+// Expected behavior: lease not added
 TEST_F(SanityChecksTest, memfileAdd6NoSubnetDel) {
-    leaseAddCheck6("2000::1", 1, "", 0, CfgConsistency::LEASE_CHECK_DEL, false, 1);
+    leaseAddCheck6("2000::1", 1, "", 0,
+                   CfgConsistency::LEASE_CHECK_DEL, false, 1);
 }
 
+// This test checks how the code behaves when there is:
+// one subnet configured, sanity-check is set to none
+// Expected behavior: lease added as is
 TEST_F(SanityChecksTest, memfileAdd6checksNone) {
-    leaseAddCheck6("2000::1", 1, "2000::/16", 2, CfgConsistency::LEASE_CHECK_NONE, true, 1);
+    leaseAddCheck6("2000::1", 1, "2000::/16", 2,
+                   CfgConsistency::LEASE_CHECK_NONE, true, 1);
 }
 
+// This test checks how the code behaves when there is:
+// one subnet configured, sanity-check is set to warn
+// Expected behavior: lease added as is
 TEST_F(SanityChecksTest, memfileAdd6checksWarn) {
-    leaseAddCheck6("2000::1", 1, "2000::/16", 2, CfgConsistency::LEASE_CHECK_WARN, true, 1);
+    leaseAddCheck6("2000::1", 1, "2000::/16", 2,
+                   CfgConsistency::LEASE_CHECK_WARN, true, 1);
 }
 
+// This test checks how the code behaves when there is:
+// one subnet configured, sanity-check is set to fix
+// Expected behavior: lease added with corrected subnet-id
 TEST_F(SanityChecksTest, memfileAdd6checksFix) {
-    leaseAddCheck6("2000::1", 1, "2000::/16", 2, CfgConsistency::LEASE_CHECK_FIX, true, 2);
+    leaseAddCheck6("2000::1", 1, "2000::/16", 2,
+                   CfgConsistency::LEASE_CHECK_FIX, true, 2);
 }
 
+// This test checks how the code behaves when there is:
+// one subnet configured, sanity-check is set to fix-del
+// Expected behavior: lease added with corrected subnet-id
 TEST_F(SanityChecksTest, memfileAdd6checksFixdel1) {
-    leaseAddCheck6("2000::1", 1, "2000::/16", 2, CfgConsistency::LEASE_CHECK_FIX_DEL, true, 2);
+    leaseAddCheck6("2000::1", 1, "2000::/16", 2,
+                   CfgConsistency::LEASE_CHECK_FIX_DEL, true, 2);
 }
 
+// This test checks how the code behaves when there is:
+// one subnet configured (but the lease does not belong to it),
+// sanity-check is set to fix-del
+// Expected behavior: lease not added
 TEST_F(SanityChecksTest, memfileAdd6checksFixdel2) {
-    leaseAddCheck6("2000::1", 1, "3000::/16", 2, CfgConsistency::LEASE_CHECK_FIX_DEL, false, 0);
+    leaseAddCheck6("2000::1", 1, "3000::/16", 2,
+                   CfgConsistency::LEASE_CHECK_FIX_DEL, false, 0);
 }
 
+// This test checks how the code behaves when there is:
+// one subnet configured, sanity-check is set to del
+// Expected behavior: lease not added
 TEST_F(SanityChecksTest, memfileAdd6checksDel) {
-    leaseAddCheck6("2000::1", 1, "2000::/16", 2, CfgConsistency::LEASE_CHECK_DEL, false, 0);
+    leaseAddCheck6("2000::1", 1, "2000::/16", 2,
+                   CfgConsistency::LEASE_CHECK_DEL, false, 0);
 }