From: Tomek Mrugalski Date: Wed, 25 Jul 2018 22:31:15 +0000 (+0200) Subject: [5682] Unit-tests written for lease file loader X-Git-Tag: ha_phase2~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a42a26af8d5be3c17b0a01b4797b2f4057bb83c;p=thirdparty%2Fkea.git [5682] Unit-tests written for lease file loader --- diff --git a/src/lib/dhcpsrv/lease_file_loader.h b/src/lib/dhcpsrv/lease_file_loader.h index fee4321e5f..3aef65fcca 100644 --- a/src/lib/dhcpsrv/lease_file_loader.h +++ b/src/lib/dhcpsrv/lease_file_loader.h @@ -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; diff --git a/src/lib/dhcpsrv/tests/lease_file_loader_unittest.cc b/src/lib/dhcpsrv/tests/lease_file_loader_unittest.cc index 1670f81087..f8d13ce23c 100644 --- a/src/lib/dhcpsrv/tests/lease_file_loader_unittest.cc +++ b/src/lib/dhcpsrv/tests/lease_file_loader_unittest.cc @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include #include #include @@ -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(lease_id) + << ",0,0,,1,\n"; + + ASSERT_NO_THROW(CfgMgr::instance().getStagingCfg()->getConsistency() + ->setLeaseSanityCheck(sanity)); + + io_.writeFile(file_content.str()); + + boost::scoped_ptr lf(new CSVLeaseFile4(filename_)); + ASSERT_NO_THROW(lf->open()); + + // Load leases from the file. + ASSERT_NO_THROW(LeaseFileLoader::load(*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(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(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 lf(new CSVLeaseFile6(filename_)); + ASSERT_NO_THROW(lf->open()); + + // Load leases from the file. + ASSERT_NO_THROW(LeaseFileLoader::load(*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(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 diff --git a/src/lib/dhcpsrv/tests/sanity_checks_unittest.cc b/src/lib/dhcpsrv/tests/sanity_checks_unittest.cc index a8c246f275..05400fc713 100644 --- a/src/lib/dhcpsrv/tests/sanity_checks_unittest.cc +++ b/src/lib/dhcpsrv/tests/sanity_checks_unittest.cc @@ -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); }