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);
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.
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) {