From: Francis Dupont Date: Tue, 28 Apr 2020 12:28:30 +0000 (+0200) Subject: [#1170] Updated leaseX-add commands X-Git-Tag: Kea-1.7.8~131 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ea2bf81662231eace716679b62f358f2661963ef;p=thirdparty%2Fkea.git [#1170] Updated leaseX-add commands --- diff --git a/ChangeLog b/ChangeLog index 6c221c586b..1376af960e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +1746. [bug] fdupont + lease4-add and lease6-add commands now return an error when the + lease already exists. + (Gitlab #1170) + 1745. [bug] fdupont Made state model and external sockets thread safe. (Gitlab #1095) diff --git a/src/hooks/dhcp/lease_cmds/lease_cmds.cc b/src/hooks/dhcp/lease_cmds/lease_cmds.cc index eaf88bf90c..b54761ed86 100644 --- a/src/hooks/dhcp/lease_cmds/lease_cmds.cc +++ b/src/hooks/dhcp/lease_cmds/lease_cmds.cc @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -391,7 +392,9 @@ LeaseCmdsImpl::leaseAddHandler(CalloutHandle& handle) { lease4 = parser.parse(config, cmd_args_, force_create); if (lease4) { - LeaseMgrFactory::instance().addLease(lease4); + if (!LeaseMgrFactory::instance().addLease(lease4)) { + isc_throw(db::DuplicateEntry, "IPv4 lease already exists."); + } resp << "Lease for address " << lease4->addr_.toText() << ", subnet-id " << lease4->subnet_id_ << " added."; } @@ -401,7 +404,9 @@ LeaseCmdsImpl::leaseAddHandler(CalloutHandle& handle) { lease6 = parser.parse(config, cmd_args_, force_create); if (lease6) { - LeaseMgrFactory::instance().addLease(lease6); + if (!LeaseMgrFactory::instance().addLease(lease6)) { + isc_throw(db::DuplicateEntry, "IPv6 lease already exists."); + } if (lease6->type_ == Lease::TYPE_NA) { resp << "Lease for address " << lease6->addr_.toText() << ", subnet-id " << lease6->subnet_id_ << " added."; @@ -1814,5 +1819,5 @@ LeaseCmds::LeaseCmds() :impl_(new LeaseCmdsImpl()) { } -}; -}; +} // end of namespace lease_cmds +} // end of namespace isc diff --git a/src/hooks/dhcp/lease_cmds/tests/lease_cmds_unittest.cc b/src/hooks/dhcp/lease_cmds/tests/lease_cmds_unittest.cc index 5cb21b2edd..5e3178b1b5 100644 --- a/src/hooks/dhcp/lease_cmds/tests/lease_cmds_unittest.cc +++ b/src/hooks/dhcp/lease_cmds/tests/lease_cmds_unittest.cc @@ -847,6 +847,29 @@ TEST_F(LeaseCmdsTest, Lease4Add) { } +// Check that a lease4 is not added when it already exists. +TEST_F(LeaseCmdsTest, Lease4AddExisting) { + + // Initialize lease manager (false = v4, true = add leases) + initLeaseMgr(false, true); + + // Check that the lease manager pointer is there. + ASSERT_TRUE(lmptr_); + + // Now send the command. + string txt = + "{\n" + " \"command\": \"lease4-add\",\n" + " \"arguments\": {" + " \"subnet-id\": 44,\n" + " \"ip-address\": \"192.0.2.1\",\n" + " \"hw-address\": \"1a:1b:1c:1d:1e:1f\"\n" + " }\n" + "}"; + string exp_rsp = "IPv4 lease already exists."; + testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp); +} + // Check that subnet-id is optional. If not specified, Kea should select // it on its own. TEST_F(LeaseCmdsTest, Lease4AddSubnetIdMissing) { @@ -1291,6 +1314,30 @@ TEST_F(LeaseCmdsTest, Lease6Add) { EXPECT_FALSE(l->getContext()); } +// Check that a lease6 is not added when it already exists. +TEST_F(LeaseCmdsTest, Lease6AddExisting) { + + // Initialize lease manager (true = v6, true = add leases) + initLeaseMgr(true, true); + + // Check that the lease manager pointer is there. + ASSERT_TRUE(lmptr_); + + // Now send the command. + string txt = + "{\n" + " \"command\": \"lease6-add\",\n" + " \"arguments\": {" + " \"subnet-id\": 66,\n" + " \"ip-address\": \"2001:db8:1::1\",\n" + " \"duid\": \"1a:1b:1c:1d:1e:1f\",\n" + " \"iaid\": 1234\n" + " }\n" + "}"; + string exp_rsp = "IPv6 lease already exists."; + testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp); +} + // Check that subnet-id is optional. If not specified, Kea should select // it on its own. TEST_F(LeaseCmdsTest, Lease6AddSubnetIdMissing) {