]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[5543] Changes after review
authorTomek Mrugalski <tomasz@isc.org>
Wed, 11 Apr 2018 21:04:15 +0000 (23:04 +0200)
committerTomek Mrugalski <tomasz@isc.org>
Wed, 11 Apr 2018 21:04:15 +0000 (23:04 +0200)
src/hooks/dhcp/lease_cmds/lease_cmds.cc
src/hooks/dhcp/lease_cmds/tests/lease_cmds_unittest.cc

index 6e4ae14f076a7b0f023dd85975f75bf971c72558..19f0c4794a89f9cfca754f0a9b4b66d33da38ee7 100644 (file)
@@ -759,21 +759,21 @@ LeaseCmdsImpl::lease4WipeHandler(CalloutHandle& handle) {
     try {
         extractCommand(handle);
 
-        // The subnet-id is a mandatory parameter.
-        if (!cmd_args_) {
-            isc_throw(isc::BadValue, "no parameters specified for lease4-wipe command");
-        }
-
         SimpleParser parser;
-        SubnetID id = parser.getUint32(cmd_args_, "subnet-id");
+        SubnetID id = 0;
 
         size_t num = 0; // number of leases deleted
         stringstream ids; // a text with subnet-ids being wiped
 
+        // The subnet-id parameter is now optional.
+        if (cmd_args_ && cmd_args_->contains("subnet-id")) {
+            id = parser.getUint32(cmd_args_, "subnet-id");
+        }
+
         if (id) {
             // Wipe a single subnet
             num = LeaseMgrFactory::instance().wipeLeases4(id);
-            ids << id;
+            ids << " " << id;
         } else {
             // Wipe them all!
             ConstSrvConfigPtr config = CfgMgr::instance().getCurrentCfg();
@@ -783,12 +783,12 @@ LeaseCmdsImpl::lease4WipeHandler(CalloutHandle& handle) {
             // Go over all subnets and wipe leases in each of them.
             for (auto sub : *subs) {
                 num += LeaseMgrFactory::instance().wipeLeases4(sub->getID());
-                ids << sub->getID() << " ";
+                ids << " " << sub->getID();
             }
         }
 
         stringstream tmp;
-        tmp << "Deleted " << num << " IPv4 lease(s) from subnet(s) " << ids.str();
+        tmp << "Deleted " << num << " IPv4 lease(s) from subnet(s)" << ids.str();
         ConstElementPtr response = createAnswer(num ? CONTROL_RESULT_SUCCESS
                                                     : CONTROL_RESULT_EMPTY, tmp.str());
         setResponse(handle, response);
@@ -805,21 +805,27 @@ LeaseCmdsImpl::lease6WipeHandler(CalloutHandle& handle) {
     try {
         extractCommand(handle);
 
-        // The subnet-id is a mandatory parameter.
-        if (!cmd_args_) {
-            isc_throw(isc::BadValue, "no parameters specified for lease6-wipe command");
-        }
-
         SimpleParser parser;
-        SubnetID id = parser.getUint32(cmd_args_, "subnet-id");
+        SubnetID id = 0;
 
         size_t num = 0; // number of leases deleted
         stringstream ids; // a text with subnet-ids being wiped
 
+        /// @todo: consider extending the code with wipe-leases:
+        /// - of specific type (v6)
+        /// - from specific shared network
+        /// - from specific pool
+        /// see https://kea.isc.org/ticket/5543#comment:6 for background.
+
+        // The subnet-id parameter is now optional.
+        if (cmd_args_ && cmd_args_->contains("subnet-id")) {
+            id = parser.getUint32(cmd_args_, "subnet-id");
+        }
+
         if (id) {
             // Wipe a single subnet.
             num = LeaseMgrFactory::instance().wipeLeases6(id);
-            ids << id;
+            ids << " " << id;
        } else {
             // Wipe them all!
             ConstSrvConfigPtr config = CfgMgr::instance().getCurrentCfg();
@@ -829,12 +835,12 @@ LeaseCmdsImpl::lease6WipeHandler(CalloutHandle& handle) {
             // Go over all subnets and wipe leases in each of them.
             for (auto sub : *subs) {
                 num += LeaseMgrFactory::instance().wipeLeases6(sub->getID());
-                ids << sub->getID() << " ";
+                ids << " " << sub->getID();
             }
         }
 
         stringstream tmp;
-        tmp << "Deleted " << num << " IPv6 lease(s) from subnet(s) " << ids.str();
+        tmp << "Deleted " << num << " IPv6 lease(s) from subnet(s)" << ids.str();
         ConstElementPtr response = createAnswer(num ? CONTROL_RESULT_SUCCESS
                                                     : CONTROL_RESULT_EMPTY, tmp.str());
         setResponse(handle, response);
index b408bb28de437ab1f649f816fff3c36165d427a0..535fc9803a12c4ac4163fa51ac548df5861bf54d 100644 (file)
@@ -2648,23 +2648,6 @@ TEST_F(LeaseCmdsTest, Lease6DelByDUID) {
     EXPECT_FALSE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::1")));
 }
 
-// Checks that lease4-wipe detects missing parmameter properly.
-TEST_F(LeaseCmdsTest, Lease4WipeMissingParam) {
-
-    // Initialize lease manager (false = v4, true = add a lease)
-    initLeaseMgr(false, true);
-
-    // Query for valid, existing lease.
-    string cmd =
-        "{\n"
-        "    \"command\": \"lease4-wipe\",\n"
-        "    \"arguments\": {"
-        "    }\n"
-        "}";
-    string exp_rsp = "missing parameter 'subnet-id' (<string>:3:19)";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
-}
-
 // Checks that lease4-wipe can remove leases.
 TEST_F(LeaseCmdsTest, Lease4Wipe) {
 
@@ -2706,7 +2689,31 @@ TEST_F(LeaseCmdsTest, Lease4WipeAll) {
         "        \"subnet-id\": 0"
         "    }\n"
         "}";
-    string exp_rsp = "Deleted 4 IPv4 lease(s) from subnet(s) 44 88 ";
+    string exp_rsp = "Deleted 4 IPv4 lease(s) from subnet(s) 44 88";
+    testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);
+
+    // Make sure the leases in subnet 44 are really gone.
+    EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.2.1")));
+    EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.2.2")));
+
+    // Make sure the leases from subnet 88 are gone, too.
+    EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.3.1")));
+    EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.3.2")));
+}
+
+// Checks that lease4-wipe can remove leases from all subnets
+// at once (when no parameters are specifed).
+TEST_F(LeaseCmdsTest, Lease4WipeAllNoArgs) {
+
+    // Initialize lease manager (false = v4, true = add a lease)
+    initLeaseMgr(false, true);
+
+    // Query for valid, existing lease.
+    string cmd =
+        "{\n"
+        "    \"command\": \"lease4-wipe\"\n"
+        "}";
+    string exp_rsp = "Deleted 4 IPv4 lease(s) from subnet(s) 44 88";
     testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);
 
     // Make sure the leases in subnet 44 are really gone.
@@ -2750,27 +2757,10 @@ TEST_F(LeaseCmdsTest, Lease4WipeNoLeasesAll) {
         "        \"subnet-id\": 0"
         "    }\n"
         "}";
-    string exp_rsp = "Deleted 0 IPv4 lease(s) from subnet(s) 44 88 ";
+    string exp_rsp = "Deleted 0 IPv4 lease(s) from subnet(s) 44 88";
     testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);
 }
 
-// Checks that lease4-wipe detects missing parmameter properly.
-TEST_F(LeaseCmdsTest, Lease6WipeMissingParam) {
-
-    // Initialize lease manager (true = v6, true = add a lease)
-    initLeaseMgr(true, true);
-
-    // Query for valid, existing lease.
-    string cmd =
-        "{\n"
-        "    \"command\": \"lease6-wipe\",\n"
-        "    \"arguments\": {"
-        "    }\n"
-        "}";
-    string exp_rsp = "missing parameter 'subnet-id' (<string>:3:19)";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
-}
-
 // Checks that lease4-wipe can remove leases.
 TEST_F(LeaseCmdsTest, Lease6Wipe) {
 
@@ -2811,7 +2801,32 @@ TEST_F(LeaseCmdsTest, Lease6WipeAll) {
         "        \"subnet-id\": 0\n"
         "    }\n"
         "}";
-    string exp_rsp = "Deleted 4 IPv6 lease(s) from subnet(s) 66 99 ";
+    string exp_rsp = "Deleted 4 IPv6 lease(s) from subnet(s) 66 99";
+
+    // The status expected is success. The lease should be deleted.
+    testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);
+
+    // Make sure the leases in subnet 44 are really gone.
+    EXPECT_FALSE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::1")));
+    EXPECT_FALSE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::2")));
+
+    // The leases in subnet 88 are supposed to be still there.
+    EXPECT_FALSE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:2::1")));
+    EXPECT_FALSE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:2::2")));
+}
+
+// Checks that lease4-wipe can remove leases from all subnets
+// (no arguments)
+TEST_F(LeaseCmdsTest, Lease6WipeAllNoArgs) {
+
+    initLeaseMgr(true, true); // (true = v6, true = create a lease)
+
+    // Now send the command.
+    string cmd =
+        "{\n"
+        "    \"command\": \"lease6-wipe\"\n"
+        "}";
+    string exp_rsp = "Deleted 4 IPv6 lease(s) from subnet(s) 66 99";
 
     // The status expected is success. The lease should be deleted.
     testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);
@@ -2857,7 +2872,7 @@ TEST_F(LeaseCmdsTest, Lease6WipeNoLeasesAll) {
         "        \"subnet-id\": 0"
         "    }\n"
         "}";
-    string exp_rsp = "Deleted 0 IPv6 lease(s) from subnet(s) 66 99 ";
+    string exp_rsp = "Deleted 0 IPv6 lease(s) from subnet(s) 66 99";
     testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);
 }