]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2260] Some reorg, e.g. scenario UTs
authorFrancis Dupont <fdupont@isc.org>
Fri, 10 Jul 2026 14:02:10 +0000 (16:02 +0200)
committerFrancis Dupont <fdupont@isc.org>
Fri, 10 Jul 2026 14:02:10 +0000 (16:02 +0200)
src/hooks/dhcp/lease_cmds/lease_cmds.cc
src/hooks/dhcp/lease_cmds/libloadtests/lease_cmds4_unittest.cc
src/hooks/dhcp/lease_cmds/libloadtests/lease_cmds6_unittest.cc

index 4b577addd21a984927188324cb8158ec62c981a2..a2c5530a11b7e7434beba7fe2e0e0920087a04af 100644 (file)
@@ -52,6 +52,13 @@ using namespace std;
 namespace isc {
 namespace lease_cmds {
 
+namespace {
+
+const int128_t uint32_min = numeric_limits<uint32_t>::min();
+const int128_t uint32_max = numeric_limits<uint32_t>::max();
+
+}
+
 /// @brief Wrapper class around reservation command handlers.
 class LeaseCmdsImpl : private CmdsImpl {
 public:
@@ -753,12 +760,11 @@ LeaseCmdsImpl::getParameters(bool v6, const ConstElementPtr& params) {
     if (tmp->getType() != Element::integer) {
         isc_throw(BadValue, "'subnet-id' parameter is not integer.");
     }
-    int64_t tmp64 = tmp->intValue();
-    if ((tmp64 < numeric_limits<uint32_t>::min()) ||
-        (tmp64 > numeric_limits<uint32_t>::max())) {
+    int128_t tmp128 = tmp->intValue();
+    if ((tmp128 < uint32_min) || (tmp128 > uint32_max)) {
         isc_throw(BadValue, "'subnet-id' parameter is not a 32 bit unsigned integer.");
     }
-    x.subnet_id = static_cast<SubnetID>(tmp64);
+    x.subnet_id = static_cast<SubnetID>(tmp128);
 
     if (params->contains("iaid")) {
         x.iaid = params->get("iaid")->intValue();
@@ -930,8 +936,7 @@ LeaseCmdsImpl::leaseGetAllHandler(CalloutHandle& handle) {
                     isc_throw(BadValue, "listed subnet identifiers must be numbers");
                 }
                 auto subnet_id_ = subnet_id->intValue();
-                if ((subnet_id_ < numeric_limits<uint32_t>::min()) ||
-                    (subnet_id_ > numeric_limits<uint32_t>::max())) {
+                if ((subnet_id_ < uint32_min) || (subnet_id_ > uint32_max)) {
                     isc_throw(BadValue, "out of range subnet identifier "
                               << subnet_id_);
                 }
@@ -1338,12 +1343,11 @@ LeaseCmdsImpl::leaseGetByStateHandler(CalloutHandle& handle) {
             if (subnet->getType() != Element::integer) {
                 isc_throw(BadValue, "'subnet-id' parameter must be a number");
             }
-            auto id64 = subnet->intValue();
-            if ((id64 < numeric_limits<uint32_t>::min()) ||
-                (id64 > numeric_limits<uint32_t>::max())) {
+            auto id128 = subnet->intValue();
+            if ((id128 < uint32_min) || (id128 > uint32_max)) {
                 isc_throw(BadValue, "'subnet-id' parameter must be a 32 bit unsigned integer");
             }
-            subnet_id_ = static_cast<SubnetID>(id64);
+            subnet_id_ = static_cast<SubnetID>(id128);
         }
 
         ElementPtr leases_json = Element::createList();
index 0c2d7ddd640c0510f0197a5cec5ec0354df85334..92d3cf01a61cfcfd46b1bbf26aa40be7be592459 100644 (file)
@@ -207,7 +207,7 @@ public:
     void testLease4GetMissingParams();
 
     /// @brief Check that lease4-get sanitizes its input.
-    void testLease4GetByAddrBadParam();
+    void testLease4GetBadParam();
 
     /// @brief Check that lease4-get can handle a situation when the query is
     /// correctly formed, but the lease is not there.
@@ -1203,39 +1203,6 @@ void Lease4CmdsTest::testLease4GetMissingParams() {
     string exp_rsp = "Mandatory 'subnet-id' parameter missing.";
     testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
 
-    // Reject not numeric subnet id.
-    cmd =
-        "{\n"
-        "    \"command\": \"lease4-get\",\n"
-        "    \"arguments\": {"
-        "        \"subnet-id\": \"foo\""
-        "    }\n"
-        "}";
-    exp_rsp = "'subnet-id' parameter is not integer.";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
-
-    // Reject negative subnet id.
-    cmd =
-        "{\n"
-        "    \"command\": \"lease4-get\",\n"
-        "    \"arguments\": {"
-        "        \"subnet-id\": -1"
-        "    }\n"
-        "}";
-    exp_rsp = "'subnet-id' parameter is not a 32 bit unsigned integer.";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
-
-    // Reject too large subnet id.
-    cmd =
-        "{\n"
-        "    \"command\": \"lease4-get\",\n"
-        "    \"arguments\": {"
-        "        \"subnet-id\": 4294967297"
-        "    }\n"
-        "}";
-    exp_rsp = "'subnet-id' parameter is not a 32 bit unsigned integer.";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
-
     // Just the subnet-id won't cut it, either.
     cmd =
         "{\n"
@@ -1299,31 +1266,56 @@ void Lease4CmdsTest::testLease4GetMissingParams() {
     testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
 }
 
-void Lease4CmdsTest::testLease4GetByAddrBadParam() {
-    // Initialize lease manager (false = v4, true = add leases)
-    initLeaseMgr(false, true);
+void Lease4CmdsTest::testLease4GetBadParam() {
+    // Structure detailing a test scenario.
+    struct Scenario {
+        string name_;                // name
+        string param_;               // parameter entry to test
+        string exp_rsp_;             // expected response
+    };
 
-    // Invalid family
-    string cmd =
-        "{\n"
-        "    \"command\": \"lease4-get\",\n"
-        "    \"arguments\": {"
-        "        \"ip-address\": \"2001:db8:1::1\""
-        "    }\n"
-        "}";
-    string exp_rsp = "Invalid IPv4 address specified: 2001:db8:1::1";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
+    // Test scenarios.
+    std::vector<Scenario> scenarios = {
+        {
+            "Invalid family",
+            "\"ip-address\": \"2001:db8:1::1\"",
+            "Invalid IPv4 address specified: 2001:db8:1::1"
+        },
+        {
+            "Bad address",
+            "\"ip-address\": \"221B Baker St.\"",
+            "Failed to convert string to address '221B Baker St.': Invalid argument"
+        },
+        {
+            "Not numeric subnet id",
+            "\"subnet-id\": \"foo\"",
+            "'subnet-id' parameter is not integer."
+        },
+        {
+            "Negative subnet id",
+            "\"subnet-id\": -1",
+            "'subnet-id' parameter is not a 32 bit unsigned integer."
+        },
+        {
+            "Too large subnet id",
+            "\"subnet-id\": 4294967297",
+            "'subnet-id' parameter is not a 32 bit unsigned integer."
+        }
+    };
 
-    // This is way off
-    cmd =
+    string prefix_cmd =
         "{\n"
         "    \"command\": \"lease4-get\",\n"
         "    \"arguments\": {"
-        "        \"ip-address\": \"221B Baker St.\""
+        "        ";
+    string end_cmd =
         "    }\n"
         "}";
-    exp_rsp = "Failed to convert string to address '221B Baker St.': Invalid argument";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
+    for (auto const& scenario : scenarios) {
+        SCOPED_TRACE(scenario.name_);
+        string cmd = prefix_cmd + scenario.param_ + end_cmd;
+        testCommand(cmd, CONTROL_RESULT_ERROR, scenario.exp_rsp_);
+    }
 }
 
 void Lease4CmdsTest::testLease4GetByAddrNotFound() {
@@ -2105,18 +2097,6 @@ void Lease4CmdsTest::testLease4GetByStateParams() {
         "}";
     exp_rsp = "'state' parameter value (foobar) is not recognized";
     testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
-
-    // subnet-id must be a number.
-    cmd =
-        "{\n"
-        "    \"command\": \"lease4-get-by-state\",\n"
-        "    \"arguments\": {"
-        "        \"state\": 1,\n"
-        "        \"subnet-id\": \"mynet\"\n"
-        "    }\n"
-        "}";
-    exp_rsp = "'subnet-id' parameter must be a number";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
 }
 
 void Lease4CmdsTest::testLease4GetByStateFind0() {
@@ -2241,41 +2221,47 @@ void Lease4CmdsTest::testLease4GetByStateFindN() {
 }
 
 void Lease4CmdsTest::testLease4GetByStateBadArgs() {
-    // Subnet id must be a number.
-    string cmd =
-        "{\n"
-        "    \"command\": \"lease4-get-by-state\",\n"
-        "    \"arguments\": {"
-        "        \"state\": 3,\n"
-        "        \"subnet-id\": \"foo\"\n"
-        "    }\n"
-        "}";
-    string exp_rsp = "'subnet-id' parameter must be a number";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
+    // Structure detailing a test scenario.
+    struct Scenario {
+        string name_;                // name
+        string param_;               // parameter entry to test
+        string exp_rsp_;             // expected response
+    };
 
-    // Subnet id must be positive.
-    cmd =
-        "{\n"
-        "    \"command\": \"lease4-get-by-state\",\n"
-        "    \"arguments\": {"
-        "        \"state\": 3,\n"
-        "        \"subnet-id\": -1\n"
-        "    }\n"
-        "}";
-    exp_rsp = "'subnet-id' parameter must be a 32 bit unsigned integer";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
+    // Test scenarios.
+    std::vector<Scenario> scenarios = {
+        {
+            "Subnet id must be a number",
+            "\"subnet-id\": \"foo\"",
+            "'subnet-id' parameter must be a number"
+        },
+        {
+            "Subnet id must be positive",
+            "\"subnet-id\": -1",
+            "'subnet-id' parameter must be a 32 bit unsigned integer"
+        },
+        {
+            "Subnet id must be fit into 32 bits",
+            "\"subnet-id\": 4294967297",
+            "'subnet-id' parameter must be a 32 bit unsigned integer"
+        }
+    };
 
-    // Subnet id must be fir into 32 bits.
-    cmd =
+    string prefix_cmd =
         "{\n"
         "    \"command\": \"lease4-get-by-state\",\n"
         "    \"arguments\": {"
         "        \"state\": 3,\n"
-        "        \"subnet-id\": 4294967297\n"
+        "        ";
+    string end_cmd =
+        "\n"
         "    }\n"
         "}";
-    exp_rsp = "'subnet-id' parameter must be a 32 bit unsigned integer";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
+    for (auto const& scenario : scenarios) {
+        SCOPED_TRACE(scenario.name_);
+        string cmd = prefix_cmd + scenario.param_ + end_cmd;
+        testCommand(cmd, CONTROL_RESULT_ERROR, scenario.exp_rsp_);
+    }
 }
 
 void Lease4CmdsTest::testLease4GetByHostnameParams() {
@@ -4176,13 +4162,13 @@ TEST_F(Lease4CmdsTest, lease4GetMissingParamsMultiThreading) {
     testLease4GetMissingParams();
 }
 
-TEST_F(Lease4CmdsTest, lease4GetByAddrBadParam) {
-    testLease4GetByAddrBadParam();
+TEST_F(Lease4CmdsTest, lease4GetBadParam) {
+    testLease4GetBadParam();
 }
 
-TEST_F(Lease4CmdsTest, lease4GetByAddrBadParamMultiThreading) {
+TEST_F(Lease4CmdsTest, lease4GetBadParamMultiThreading) {
     MultiThreadingTest mt(true);
-    testLease4GetByAddrBadParam();
+    testLease4GetBadParam();
 }
 
 TEST_F(Lease4CmdsTest, lease4GetByAddrNotFound) {
index a3f1c65211532b71d6ff492dd7180aacc90b909a..7ea368b38581c1c5269319f0ba8af8e7e3a8208b 100644 (file)
@@ -237,7 +237,7 @@ public:
     void testLease6GetMissingParams();
 
     /// @brief Check that lease6-get sanitizes its input.
-    void testLease6GetByAddrBadParam();
+    void testLease6GetBadParam();
 
     /// @brief Check that lease6-get can handle a situation when the query is
     /// correctly formed, but the lease is not there.
@@ -1456,39 +1456,6 @@ void Lease6CmdsTest::testLease6GetMissingParams() {
     string exp_rsp = "Mandatory 'subnet-id' parameter missing.";
     testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
 
-    // Reject not numeric subnet id.
-    cmd =
-        "{\n"
-        "    \"command\": \"lease6-get\",\n"
-        "    \"arguments\": {"
-        "        \"subnet-id\": \"foo\""
-        "    }\n"
-        "}";
-    exp_rsp = "'subnet-id' parameter is not integer.";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
-
-    // Reject negative subnet id.
-    cmd =
-        "{\n"
-        "    \"command\": \"lease6-get\",\n"
-        "    \"arguments\": {"
-        "        \"subnet-id\": -1"
-        "    }\n"
-        "}";
-    exp_rsp = "'subnet-id' parameter is not a 32 bit unsigned integer.";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
-
-    // Reject too large subnet id.
-    cmd =
-        "{\n"
-        "    \"command\": \"lease6-get\",\n"
-        "    \"arguments\": {"
-        "        \"subnet-id\": 4294967297"
-        "    }\n"
-        "}";
-    exp_rsp = "'subnet-id' parameter is not a 32 bit unsigned integer.";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
-
     // Just the subnet-id won't cut it, either.
     cmd =
         "{\n"
@@ -1552,31 +1519,56 @@ void Lease6CmdsTest::testLease6GetMissingParams() {
     testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
 }
 
-void Lease6CmdsTest::testLease6GetByAddrBadParam() {
-    // Initialize lease manager (true = v6, true = add leases)
-    initLeaseMgr(true, true);
+void Lease6CmdsTest::testLease6GetBadParam() {
+    // Structure detailing a test scenario.
+    struct Scenario {
+        string name_;                // name
+        string param_;               // parameter entry to test
+        string exp_rsp_;             // expected response
+    };
 
-    // Invalid family
-    string cmd =
-        "{\n"
-        "    \"command\": \"lease6-get\",\n"
-        "    \"arguments\": {"
-        "        \"ip-address\": \"192.0.2.1\""
-        "    }\n"
-        "}";
-    string exp_rsp = "Invalid IPv6 address specified: 192.0.2.1";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
+    // Test scenarios.
+    std::vector<Scenario> scenarios = {
+        {
+            "Invalid family",
+            "\"ip-address\": \"192.0.2.1\"",
+            "Invalid IPv6 address specified: 192.0.2.1"
+        },
+        {
+            "Bad address",
+            "\"ip-address\": \"221B Baker St.\"",
+            "Failed to convert string to address '221B Baker St.': Invalid argument"
+        },
+        {
+            "Not numeric subnet id",
+            "\"subnet-id\": \"foo\"",
+            "'subnet-id' parameter is not integer."
+        },
+        {
+            "Negative subnet id",
+            "\"subnet-id\": -1",
+            "'subnet-id' parameter is not a 32 bit unsigned integer."
+        },
+        {
+            "Too large subnet id",
+            "\"subnet-id\": 4294967297",
+            "'subnet-id' parameter is not a 32 bit unsigned integer."
+        }
+    };
 
-    // This is way off
-    cmd =
+    string prefix_cmd =
         "{\n"
         "    \"command\": \"lease6-get\",\n"
         "    \"arguments\": {"
-        "        \"ip-address\": \"221B Baker St.\""
+        "        ";
+    string end_cmd =
         "    }\n"
         "}";
-    exp_rsp = "Failed to convert string to address '221B Baker St.': Invalid argument";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
+    for (auto const& scenario : scenarios) {
+        SCOPED_TRACE(scenario.name_);
+        string cmd = prefix_cmd + scenario.param_ + end_cmd;
+        testCommand(cmd, CONTROL_RESULT_ERROR, scenario.exp_rsp_);
+    }
 }
 
 void Lease6CmdsTest::testLease6GetByAddrNotFound() {
@@ -2369,18 +2361,6 @@ void Lease6CmdsTest::testLease6GetByStateParams() {
         "}";
     exp_rsp = "'state' parameter value (foobar) is not recognized";
     testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
-
-    // subnet-id must be a number.
-    cmd =
-        "{\n"
-        "    \"command\": \"lease6-get-by-state\",\n"
-        "    \"arguments\": {"
-        "        \"state\": 1,\n"
-        "        \"subnet-id\": \"mynet\"\n"
-        "    }\n"
-        "}";
-    exp_rsp = "'subnet-id' parameter must be a number";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
 }
 
 void Lease6CmdsTest::testLease6GetByStateFind0() {
@@ -2511,41 +2491,47 @@ void Lease6CmdsTest::testLease6GetByStateFindN() {
 }
 
 void Lease6CmdsTest::testLease6GetByStateBadArgs() {
-    // Subnet id must be a number.
-    string cmd =
-        "{\n"
-        "    \"command\": \"lease6-get-by-state\",\n"
-        "    \"arguments\": {"
-        "        \"state\": 3,\n"
-        "        \"subnet-id\": \"foo\"\n"
-        "    }\n"
-        "}";
-    string exp_rsp = "'subnet-id' parameter must be a number";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
+    // Structure detailing a test scenario.
+    struct Scenario {
+        string name_;                // name
+        string param_;               // parameter entry to test
+        string exp_rsp_;             // expected response
+    };
 
-    // Subnet id must be positive.
-    cmd =
-        "{\n"
-        "    \"command\": \"lease6-get-by-state\",\n"
-        "    \"arguments\": {"
-        "        \"state\": 3,\n"
-        "        \"subnet-id\": -1\n"
-        "    }\n"
-        "}";
-    exp_rsp = "'subnet-id' parameter must be a 32 bit unsigned integer";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
+    // Test scenarios.
+    std::vector<Scenario> scenarios = {
+        {
+            "Subnet id must be a number",
+            "\"subnet-id\": \"foo\"",
+            "'subnet-id' parameter must be a number"
+        },
+        {
+            "Subnet id must be positive",
+            "\"subnet-id\": -1",
+            "'subnet-id' parameter must be a 32 bit unsigned integer"
+        },
+        {
+            "Subnet id must be fit into 32 bits",
+            "\"subnet-id\": 4294967297",
+            "'subnet-id' parameter must be a 32 bit unsigned integer"
+        }
+    };
 
-    // Subnet id must be fir into 32 bits.
-    cmd =
+    string prefix_cmd =
         "{\n"
         "    \"command\": \"lease6-get-by-state\",\n"
         "    \"arguments\": {"
         "        \"state\": 3,\n"
-        "        \"subnet-id\": 4294967297\n"
+        "        ";
+    string end_cmd =
+        "\n"
         "    }\n"
         "}";
-    exp_rsp = "'subnet-id' parameter must be a 32 bit unsigned integer";
-    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
+    for (auto const& scenario : scenarios) {
+        SCOPED_TRACE(scenario.name_);
+        string cmd = prefix_cmd + scenario.param_ + end_cmd;
+        testCommand(cmd, CONTROL_RESULT_ERROR, scenario.exp_rsp_);
+    }
 }
 
 void Lease6CmdsTest::testLease6GetByHostnameParams() {
@@ -5238,13 +5224,13 @@ TEST_F(Lease6CmdsTest, lease6GetMissingParamsMultiThreading) {
     testLease6GetMissingParams();
 }
 
-TEST_F(Lease6CmdsTest, lease6GetByAddrBadParam) {
-    testLease6GetByAddrBadParam();
+TEST_F(Lease6CmdsTest, lease6GetBadParam) {
+    testLease6GetBadParam();
 }
 
-TEST_F(Lease6CmdsTest, lease6GetByAddrBadParamMultiThreading) {
+TEST_F(Lease6CmdsTest, lease6GetBadParamMultiThreading) {
     MultiThreadingTest mt(true);
-    testLease6GetByAddrBadParam();
+    testLease6GetBadParam();
 }
 
 TEST_F(Lease6CmdsTest, lease6GetByAddrNotFound) {