Commit b9c2085a authored by Tomek Mrugalski's avatar Tomek Mrugalski 🛰
Browse files

[5032] empty "relay" structure is no longer allowed.

parent bc730569
......@@ -3203,6 +3203,9 @@ src/lib/dhcpsrv/cfg_host_operations.cc -->
</screen>
</para>
<para>If "relay" is specified, the "ip-address" parameter within
it is mandatory.</para>
</section>
<section id="dhcp4-srv-example-client-class-relay">
......
......@@ -3410,6 +3410,9 @@ If not specified, the default value is:
</screen>
</para>
<para>If "relay" is specified, the "ip-address" parameter within
it is mandatory.</para>
</section>
<section id="dhcp6-client-class-relay">
......
......@@ -809,12 +809,16 @@ RelayInfoParser::parse(const isc::dhcp::Subnet::RelayInfoPtr& cfg,
}
// Now create the default value.
isc::asiolink::IOAddress ip(family_ == Option::V4 ? "0.0.0.0" : "::");
isc::asiolink::IOAddress ip(family_ == Option::V4 ? IOAddress::IPV4_ZERO_ADDRESS()
: IOAddress::IPV6_ZERO_ADDRESS());
// Now iterate over all parameters. Currently there's only one supported
// parameter, so it should be an easy thing to check.
bool ip_address_specified = false;
BOOST_FOREACH(ConfigPair param, relay_info->mapValue()) {
if (param.first == "ip-address") {
ip_address_specified = true;
try {
ip = asiolink::IOAddress(param.second->stringValue());
} catch (...) {
......@@ -838,6 +842,11 @@ RelayInfoParser::parse(const isc::dhcp::Subnet::RelayInfoPtr& cfg,
}
}
if (!ip_address_specified) {
isc_throw(DhcpConfigError, "'relay' specified, but mandatory 'ip-address' "
"paramter in it is missing");
}
// Ok, we're done with parsing. Let's store the result in the structure
// we were given as configuration storage.
*cfg = isc::dhcp::Subnet::RelayInfo(ip);
......
......@@ -2463,6 +2463,19 @@ TEST_F(ParseConfigTest, validRelayInfo4) {
" }";
ElementPtr json = Element::fromJSON(config_str);
// We need to set the default ip-address to something.
Subnet::RelayInfoPtr result(new Subnet::RelayInfo(asiolink::IOAddress("0.0.0.0")));
RelayInfoParser parser(Option::V4);
// Subnet4 parser will pass 0.0.0.0 to the RelayInfoParser
EXPECT_NO_THROW(parser.parse(result, json));
EXPECT_EQ("192.0.2.1", result->addr_.toText());
}
/// @brief Checks that a bogus relay info structure for IPv4 is rejected.
TEST_F(ParseConfigTest, bogusRelayInfo4) {
// Invalid config (wrong family type of the ip-address field)
std::string config_str_bogus1 =
" {"
......@@ -2477,19 +2490,24 @@ TEST_F(ParseConfigTest, validRelayInfo4) {
" }";
ElementPtr json_bogus2 = Element::fromJSON(config_str_bogus2);
// Invalid config (ip-address is mandatory)
std::string config_str_bogus3 =
" {"
" }";
ElementPtr json_bogus3 = Element::fromJSON(config_str_bogus3);
// We need to set the default ip-address to something.
Subnet::RelayInfoPtr result(new Subnet::RelayInfo(asiolink::IOAddress("0.0.0.0")));
Subnet::RelayInfoPtr result(new Subnet::RelayInfo(IOAddress::IPV4_ZERO_ADDRESS()));
RelayInfoParser parser(Option::V4);
// Subnet4 parser will pass 0.0.0.0 to the RelayInfoParser
EXPECT_NO_THROW(parser.parse(result, json));
EXPECT_EQ("192.0.2.1", result->addr_.toText());
// Let's check negative scenario (wrong family type)
// wrong family type
EXPECT_THROW(parser.parse(result, json_bogus1), DhcpConfigError);
// Let's check negative scenario (too large byte values in pseudo-IPv4 addr)
// Too large byte values in pseudo-IPv4 addr
EXPECT_THROW(parser.parse(result, json_bogus2), DhcpConfigError);
// Mandatory ip-address is missing. What a pity.
EXPECT_THROW(parser.parse(result, json_bogus2), DhcpConfigError);
}
......@@ -2503,6 +2521,18 @@ TEST_F(ParseConfigTest, validRelayInfo6) {
" }";
ElementPtr json = Element::fromJSON(config_str);
// We need to set the default ip-address to something.
Subnet::RelayInfoPtr result(new Subnet::RelayInfo(asiolink::IOAddress("::")));
RelayInfoParser parser(Option::V6);
// Subnet4 parser will pass :: to the RelayInfoParser
EXPECT_NO_THROW(parser.parse(result, json));
EXPECT_EQ("2001:db8::1", result->addr_.toText());
}
/// @brief Checks that a valid relay info structure for IPv6 can be handled
TEST_F(ParseConfigTest, bogusRelayInfo6) {
// Invalid config (wrong family type of the ip-address field
std::string config_str_bogus1 =
" {"
......@@ -2517,19 +2547,25 @@ TEST_F(ParseConfigTest, validRelayInfo6) {
" }";
ElementPtr json_bogus2 = Element::fromJSON(config_str_bogus2);
// Missing mandatory ip-address field.
std::string config_str_bogus3 =
" {"
" }";
ElementPtr json_bogus3 = Element::fromJSON(config_str_bogus3);
// We need to set the default ip-address to something.
Subnet::RelayInfoPtr result(new Subnet::RelayInfo(asiolink::IOAddress("::")));
RelayInfoParser parser(Option::V6);
// Subnet4 parser will pass :: to the RelayInfoParser
EXPECT_NO_THROW(parser.parse(result, json));
EXPECT_EQ("2001:db8::1", result->addr_.toText());
// Let's check negative scenario (wrong family type)
// Negative scenario (wrong family type)
EXPECT_THROW(parser.parse(result, json_bogus1), DhcpConfigError);
// Unparseable text that looks like IPv6 address, but has too many colons
// Looks like IPv6 address, but has too many colons
EXPECT_THROW(parser.parse(result, json_bogus2), DhcpConfigError);
// Mandatory ip-address is missing. What a pity.
EXPECT_THROW(parser.parse(result, json_bogus3), DhcpConfigError);
}
// There's no test for ControlSocketParser, as it is tested in the DHCPv4 code
......
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