From: Tomek Mrugalski Date: Thu, 24 Jan 2019 19:13:33 +0000 (+0100) Subject: [#313, !199] implemented getInteger() with range checking X-Git-Tag: 429-Updated-StampedValue-to-support-reals_base~71 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a4e1c1e8090f5e3e51fc09d9f648abe5da2af09b;p=thirdparty%2Fkea.git [#313, !199] implemented getInteger() with range checking --- 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) {