summaryrefslogtreecommitdiffstats
path: root/src/lib/cc
diff options
context:
space:
mode:
authorTomek Mrugalski <tomasz@isc.org>2019-01-24 20:13:33 +0100
committerFrancis Dupont <fdupont@isc.org>2019-01-29 10:49:05 +0100
commita4e1c1e8090f5e3e51fc09d9f648abe5da2af09b (patch)
treef53b69646c1972be79fe54817507bb57a28c8d6e /src/lib/cc
parent[#313, !199] reservation-get-page description updated. (diff)
downloadkea-a4e1c1e8090f5e3e51fc09d9f648abe5da2af09b.tar.xz
kea-a4e1c1e8090f5e3e51fc09d9f648abe5da2af09b.zip
[#313, !199] implemented getInteger() with range checking
Diffstat (limited to 'src/lib/cc')
-rw-r--r--src/lib/cc/simple_parser.cc13
-rw-r--r--src/lib/cc/simple_parser.h16
-rw-r--r--src/lib/cc/tests/simple_parser_unittest.cc21
3 files changed, 50 insertions, 0 deletions
diff --git a/src/lib/cc/simple_parser.cc b/src/lib/cc/simple_parser.cc
index 6213837354..781086da94 100644
--- a/src/lib/cc/simple_parser.cc
+++ b/src/lib/cc/simple_parser.cc
@@ -89,6 +89,19 @@ SimpleParser::getInteger(ConstElementPtr scope, const std::string& name) {
return (x->intValue());
}
+int64_t
+SimpleParser::getInteger(isc::data::ConstElementPtr scope, const std::string& name,
+ int64_t min, int64_t max) {
+ int64_t tmp = getInteger(scope, name);
+ if (tmp < min || tmp > max) {
+ isc_throw(DhcpConfigError,
+ "The '" << name << "' value (" << tmp
+ << ") is not within expected range: (" << min << " - " << max
+ << ");");
+ }
+ return (tmp);
+}
+
bool
SimpleParser::getBoolean(ConstElementPtr scope, const std::string& name) {
ConstElementPtr x = scope->get(name);
diff --git a/src/lib/cc/simple_parser.h b/src/lib/cc/simple_parser.h
index 60b773beab..2eef2cecfc 100644
--- a/src/lib/cc/simple_parser.h
+++ b/src/lib/cc/simple_parser.h
@@ -170,6 +170,22 @@ class SimpleParser {
static int64_t getInteger(isc::data::ConstElementPtr scope,
const std::string& name);
+ /// @brief Returns an integer parameter from a scope and checks its range
+ ///
+ /// Unconditionally returns a parameter. Checks that the value specified
+ /// is in min =< X =< max range.
+ ///
+ /// @param scope specified parameter will be extracted from this scope
+ /// @param name name of the parameter
+ /// @param min minimum allowed value
+ /// @param max maximum allowed value
+ /// @return an integer value of the parameter
+ /// @throw DhcpConfigError if the parameter is not there or is not of
+ /// appropriate type or is out of range
+ static int64_t getInteger(isc::data::ConstElementPtr scope,
+ const std::string& name,
+ int64_t min, int64_t max);
+
/// @brief Returns a boolean parameter from a scope
///
/// Unconditionally returns a parameter.
diff --git a/src/lib/cc/tests/simple_parser_unittest.cc b/src/lib/cc/tests/simple_parser_unittest.cc
index 3c1d49cb1a..6642a59dde 100644
--- a/src/lib/cc/tests/simple_parser_unittest.cc
+++ b/src/lib/cc/tests/simple_parser_unittest.cc
@@ -242,6 +242,27 @@ TEST_F(SimpleParserTest, getIntType) {
EXPECT_EQ(100, val);
}
+// This test exercises the getInteger with range checking
+TEST_F(SimpleParserTest, getInteger) {
+
+ // The value specified is 100.
+ ElementPtr json = Element::fromJSON("{ \"bar\": 100 }");
+ int64_t x;
+
+ // Positive case: we expect value in range 0..200. All ok.
+ EXPECT_NO_THROW(x = SimpleParser::getInteger(json, "bar", 0, 200));
+ EXPECT_EQ(100, x);
+
+ // Border checks: 100 for 100..200 range is still ok.
+ EXPECT_NO_THROW(x = SimpleParser::getInteger(json, "bar", 100, 200));
+ // Border checks: 100 for 1..100 range is still ok.
+ EXPECT_NO_THROW(x = SimpleParser::getInteger(json, "bar", 1, 100));
+
+ // Out of expected range. Should throw.
+ EXPECT_THROW(x = SimpleParser::getInteger(json, "bar", 101, 200), DhcpConfigError);
+ EXPECT_THROW(x = SimpleParser::getInteger(json, "bar", 1, 99), DhcpConfigError);
+}
+
// This test exercises the getAndConvert template
TEST_F(SimpleParserTest, getAndConvert) {