From: Francis Dupont Date: Sun, 7 Jul 2019 09:03:16 +0000 (+0200) Subject: [701-sanitizer-logic-is-wrong] Added unit tests X-Git-Tag: Kea-1.6.0-beta2~47 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2F701-sanitizer-logic-is-wrong;p=thirdparty%2Fkea.git [701-sanitizer-logic-is-wrong] Added unit tests --- diff --git a/src/lib/dhcpsrv/tests/sanity_checks_unittest.cc b/src/lib/dhcpsrv/tests/sanity_checks_unittest.cc index 662b6c8f09..a7231e1baa 100644 --- a/src/lib/dhcpsrv/tests/sanity_checks_unittest.cc +++ b/src/lib/dhcpsrv/tests/sanity_checks_unittest.cc @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -175,3 +176,203 @@ TEST_F(SanityChecksTest, leaseCheck) { parserCheck(cfg, bogus4, true, CfgConsistency::LEASE_CHECK_NONE); } +// Verify whether sanity checker works as expected (valid v4). +TEST_F(SanityChecksTest, valid4) { + // Create network and lease. + CfgMgr::instance().setFamily(AF_INET); + Subnet4Ptr subnet = createSubnet4("192.168.1.0/24", 1); + CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->add(subnet); + IOAddress addr("192.168.1.1"); + Lease4Ptr lease = newLease4(addr, 1); + + // Check the lease. + setLeaseCheck(CfgConsistency::LEASE_CHECK_FIX_DEL); + SanityChecker checker; + checker.checkLease(lease); + + // Verify the lease is still here in the same subnet. + ASSERT_TRUE(lease); + EXPECT_EQ(subnet->getID(), lease->subnet_id_); +} + +// Verify whether sanity checker works as expected (valid v6). +TEST_F(SanityChecksTest, valid6) { + // Create network and lease. + CfgMgr::instance().setFamily(AF_INET6); + Subnet6Ptr subnet = createSubnet6("2001:db8:1::/64", 1); + CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->add(subnet); + IOAddress addr("2001:db8:1::1"); + Lease6Ptr lease = newLease6(addr, 1); + + // Check the lease. + setLeaseCheck(CfgConsistency::LEASE_CHECK_FIX_DEL); + SanityChecker checker; + checker.checkLease(lease); + + // Verify the lease is still here in the same subnet. + ASSERT_TRUE(lease); + EXPECT_EQ(subnet->getID(), lease->subnet_id_); +} + +// Verify whether sanity checker works as expected (wrong subnet v4). +TEST_F(SanityChecksTest, wrongSubnet4) { + // Create networks and lease in the second and wrong subnet. + CfgMgr::instance().setFamily(AF_INET); + Subnet4Ptr subnet1 = createSubnet4("192.168.1.0/24", 1); + Subnet4Ptr subnet2 = createSubnet4("192.168.2.0/24", 2); + CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->add(subnet1); + CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->add(subnet2); + IOAddress addr("192.168.1.1"); + Lease4Ptr lease = newLease4(addr, 2); + + // Check the lease. + setLeaseCheck(CfgConsistency::LEASE_CHECK_FIX_DEL); + SanityChecker checker; + checker.checkLease(lease); + + // Verify the lease is still here but was moved to the first and right subnet. + ASSERT_TRUE(lease); + EXPECT_EQ(subnet1->getID(), lease->subnet_id_); +} + +// Verify whether sanity checker works as expected (wrong subnet v6). +TEST_F(SanityChecksTest, wrongSubnet6) { + // Create networks and lease in the second and wrong subnet. + CfgMgr::instance().setFamily(AF_INET6); + Subnet6Ptr subnet1 = createSubnet6("2001:db8:1::/64", 1); + Subnet6Ptr subnet2 = createSubnet6("2001:db8:2::/64", 2); + CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->add(subnet1); + CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->add(subnet2); + IOAddress addr("2001:db8:1::1"); + Lease6Ptr lease = newLease6(addr, 2); + + // Check the lease. + setLeaseCheck(CfgConsistency::LEASE_CHECK_FIX_DEL); + SanityChecker checker; + checker.checkLease(lease); + + // Verify the lease is still here but was moved to the first and right subnet. + ASSERT_TRUE(lease); + EXPECT_EQ(subnet1->getID(), lease->subnet_id_); +} + +// Verify whether sanity checker works as expected (no subnet v4). +TEST_F(SanityChecksTest, noSubnet4) { + // Create network and lease in a wrong subnet. + CfgMgr::instance().setFamily(AF_INET); + Subnet4Ptr subnet = createSubnet4("192.168.2.0/24", 1); + CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->add(subnet); + IOAddress addr("192.168.1.1"); + Lease4Ptr lease = newLease4(addr, 1); + + // Check the lease. + setLeaseCheck(CfgConsistency::LEASE_CHECK_FIX_DEL); + SanityChecker checker; + checker.checkLease(lease); + + // Verify the lease was removed because its subnet does not exist, + EXPECT_FALSE(lease); +} + +// Verify whether sanity checker works as expected (no subnet v6). +TEST_F(SanityChecksTest, noSubnet6) { + // Create network and lease in a wrong subnet. + CfgMgr::instance().setFamily(AF_INET6); + Subnet6Ptr subnet = createSubnet6("2001:db8:2::/64", 1); + CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->add(subnet); + IOAddress addr("2001:db8:1::1"); + Lease6Ptr lease = newLease6(addr, 1); + + // Check the lease. + setLeaseCheck(CfgConsistency::LEASE_CHECK_FIX_DEL); + SanityChecker checker; + checker.checkLease(lease); + + // Verify the lease was removed because its subnet does not exist, + EXPECT_FALSE(lease); +} + +// Verify whether sanity checker works as expected (guard v4). +TEST_F(SanityChecksTest, guard4) { + // Create networks and lease in the first and guarded subnet. + CfgMgr::instance().setFamily(AF_INET); + Subnet4Ptr subnet1 = createSubnet4("192.168.1.0/24", 1); + subnet1->allowClientClass("foo"); + Subnet4Ptr subnet2 = createSubnet4("192.168.1.100/24", 2); + CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->add(subnet1); + CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->add(subnet2); + IOAddress addr("192.168.1.1"); + Lease4Ptr lease = newLease4(addr, 1); + + // Check the lease. + setLeaseCheck(CfgConsistency::LEASE_CHECK_FIX_DEL); + SanityChecker checker; + checker.checkLease(lease); + + // Verify the lease is still here and in the guarded subnet. + ASSERT_TRUE(lease); + EXPECT_EQ(subnet1->getID(), lease->subnet_id_); +} + +// Verify whether sanity checker works as expected (guard v6). +TEST_F(SanityChecksTest, guard6) { + // Create networks and lease in the first and guarded subnet. + CfgMgr::instance().setFamily(AF_INET6); + Subnet6Ptr subnet1 = createSubnet6("2001:db8:1::/64", 1); + subnet1->allowClientClass("foo"); + Subnet6Ptr subnet2 = createSubnet6("2001:db8:2::100/64", 2); + CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->add(subnet1); + CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->add(subnet2); + IOAddress addr("2001:db8:1::1"); + Lease6Ptr lease = newLease6(addr, 1); + + // Check the lease. + setLeaseCheck(CfgConsistency::LEASE_CHECK_FIX_DEL); + SanityChecker checker; + checker.checkLease(lease); + + // Verify the lease is still here and in the guarded subnet. + ASSERT_TRUE(lease); + EXPECT_EQ(subnet1->getID(), lease->subnet_id_); +} + +// Verify whether sanity checker works as expected (guard only v4). +TEST_F(SanityChecksTest, guardOnly4) { + // Create guarded network and lease. + CfgMgr::instance().setFamily(AF_INET); + Subnet4Ptr subnet = createSubnet4("192.168.1.0/24", 1); + subnet->allowClientClass("foo"); + CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->add(subnet); + IOAddress addr("192.168.1.1"); + Lease4Ptr lease = newLease4(addr, 1); + + // Check the lease. + setLeaseCheck(CfgConsistency::LEASE_CHECK_FIX_DEL); + SanityChecker checker; + checker.checkLease(lease); + + // Verify the lease is still here in the same subnet. + ASSERT_TRUE(lease); + EXPECT_EQ(subnet->getID(), lease->subnet_id_); +} + +// Verify whether sanity checker works as expected (valid v6). +TEST_F(SanityChecksTest, guardOnly6) { + // Create guarded network and lease. + CfgMgr::instance().setFamily(AF_INET6); + Subnet6Ptr subnet = createSubnet6("2001:db8:1::/64", 1); + subnet->allowClientClass("foo"); + CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->add(subnet); + IOAddress addr("2001:db8:1::1"); + Lease6Ptr lease = newLease6(addr, 1); + + // Check the lease. + setLeaseCheck(CfgConsistency::LEASE_CHECK_FIX_DEL); + SanityChecker checker; + checker.checkLease(lease); + + // Verify the lease is still here in the same subnet. + ASSERT_TRUE(lease); + EXPECT_EQ(subnet->getID(), lease->subnet_id_); +} +