#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>
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() {
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
// 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:
// 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);
}