]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#313, !199] implemented getInteger() with range checking
authorTomek Mrugalski <tomasz@isc.org>
Thu, 24 Jan 2019 19:13:33 +0000 (20:13 +0100)
committerFrancis Dupont <fdupont@isc.org>
Tue, 29 Jan 2019 09:49:05 +0000 (04:49 -0500)
src/lib/cc/simple_parser.cc
src/lib/cc/simple_parser.h
src/lib/cc/tests/simple_parser_unittest.cc

index 621383735446cf5f073139918f6c9cd82216aa12..781086da94da0cc04fff7e431b33e62de8a2d476 100644 (file)
@@ -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);
index 60b773beab748251b9ab494ffe3495fe91fee3ff..2eef2cecfc4707ba24eff83bbe54ab5430747ac2 100644 (file)
@@ -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.
index 3c1d49cb1a868fdb70c6a6f101449481e9c4cf74..6642a59ddee14f59db0d6ff12ef59c7644ac54ce 100644 (file)
@@ -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) {