Commit 0f5c5c76 authored by Marcin Siodelski's avatar Marcin Siodelski
Browse files

[3810] Host reservation parser checks for unsupported parameters.

parent c5430032
......@@ -23,6 +23,46 @@
using namespace isc::asiolink;
using namespace isc::data;
namespace {
/// @brief Returns set of the supported parameters for DHCPv4.
///
/// This function returns the set of supported parameters for
/// host reservation in DHCPv4.
const std::set<std::string>& getSupportedParams4() {
static const char* params[] = {
"duid", "hw-address", "hostname", "ip-address"
};
static const size_t params_num = static_cast<const size_t>(sizeof(params) / sizeof(char*));
static std::set<std::string> params_set;
if (params_set.empty()) {
for (int i = 0; i < params_num; ++i) {
params_set.insert(std::string(params[i]));
}
}
return (params_set);
}
/// @brief Returns set of the supported parameters for DHCPv4.
///
/// This function returns the set of supported parameters for
/// host reservation in DHCPv6.
const std::set<std::string>& getSupportedParams6() {
static const char* params[] = {
"duid", "hw-address", "hostname", "ip-addresses", "prefixes"
};
static const size_t params_num = static_cast<const size_t>(sizeof(params) / sizeof(char*));
static std::set<std::string> params_set;
if (params_set.empty()) {
for (int i = 0; i < params_num; ++i) {
params_set.insert(std::string(params[i]));
}
}
return (params_set);
}
}
namespace isc {
namespace dhcp {
......@@ -40,6 +80,12 @@ HostReservationParser::build(isc::data::ConstElementPtr reservation_data) {
// reservations.
BOOST_FOREACH(ConfigPair element, reservation_data->mapValue()) {
try {
// Check if we support this parameter.
if (!isSupportedParameter(element.first)) {
isc_throw(DhcpConfigError, "unsupported configuration"
" parameter '" << element.first << "'");
}
if (element.first == "hw-address" || element.first == "duid") {
if (!identifier_name.empty()) {
isc_throw(DhcpConfigError, "the 'hw-address' and 'duid'"
......@@ -116,6 +162,11 @@ HostReservationParser4::build(isc::data::ConstElementPtr reservation_data) {
addHost(reservation_data);
}
bool
HostReservationParser4::isSupportedParameter(const std::string& param_name) const {
return (getSupportedParams4().count(param_name) > 0);
}
HostReservationParser6::HostReservationParser6(const SubnetID& subnet_id)
: HostReservationParser(subnet_id) {
}
......@@ -197,5 +248,10 @@ HostReservationParser6::build(isc::data::ConstElementPtr reservation_data) {
addHost(reservation_data);
}
bool
HostReservationParser6::isSupportedParameter(const std::string& param_name) const {
return (getSupportedParams6().count(param_name) > 0);
}
} // end of namespace isc::dhcp
} // end of namespace isc
......@@ -56,6 +56,13 @@ protected:
/// @throw DhcpConfigError When operation to add a configured host fails.
void addHost(isc::data::ConstElementPtr reservation_data);
/// @brief Checks if the specified parameter is supported by the parser.
///
/// @param param_name Parameter name.
///
/// @return true if the parameter is supported, false otherwise.
virtual bool isSupportedParameter(const std::string& param_name) const = 0;
/// @brief Identifier of the subnet that the host is connected to.
SubnetID subnet_id_;
......@@ -82,6 +89,15 @@ public:
///
/// @throw DhcpConfigError If the configuration is invalid.
virtual void build(isc::data::ConstElementPtr reservation_data);
protected:
/// @brief Checks if the specified parameter is supported by the parser.
///
/// @param param_name Parameter name.
///
/// @return true if the parameter is supported, false otherwise.
virtual bool isSupportedParameter(const std::string& param_name) const;
};
/// @brief Parser for a single host reservation for DHCPv6.
......@@ -101,6 +117,15 @@ public:
///
/// @throw DhcpConfigError If the configuration is invalid.
virtual void build(isc::data::ConstElementPtr reservation_data);
protected:
/// @brief Checks if the specified parameter is supported by the parser.
///
/// @param param_name Parameter name.
///
/// @return true if the parameter is supported, false otherwise.
virtual bool isSupportedParameter(const std::string& param_name) const;
};
......
......@@ -273,6 +273,19 @@ TEST_F(HostReservationParserTest, bcastAddress) {
EXPECT_THROW(parser.build(config_element), DhcpConfigError);
}
// This test verifies that the configuration parser for host reservations
// throws an exception when unsupported parameter is specified.
TEST_F(HostReservationParserTest, invalidParameterName) {
std::string config = "{ \"hw-address\": \"01:02:03:04:05:06\","
"\"hostname\": \"foo.bar.isc.org\","
"\"ip-addresses\": \"192.0.2.3\" }";
ElementPtr config_element = Element::fromJSON(config);
HostReservationParser4 parser(SubnetID(10));
EXPECT_THROW(parser.build(config_element), DhcpConfigError);
}
// This test verfies that the parser can parse the IPv6 reservation entry for
// which hw-address is a host identifier.
TEST_F(HostReservationParserTest, dhcp6HWaddr) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment