#include <cc/command_interpreter.h>
#include <cc/data.h>
#include <asiolink/io_address.h>
+#include <database/db_exceptions.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/dhcpsrv_exceptions.h>
#include <dhcpsrv/lease_mgr.h>
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.";
}
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.";
:impl_(new LeaseCmdsImpl()) {
}
-};
-};
+} // end of namespace lease_cmds
+} // end of namespace isc
}
+// 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) {
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) {