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:
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();
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_);
}
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();
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.
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"
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() {
"}";
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() {
}
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() {
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) {
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.
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"
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() {
"}";
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() {
}
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() {
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) {