Browse Source

[4105] config parser now uses getOptionalParam() methods.

Tomek Mrugalski 9 years ago
parent
commit
115a407148
2 changed files with 63 additions and 12 deletions
  1. 6 12
      src/bin/dhcp4/json_config_parser.cc
  2. 57 0
      src/bin/dhcp4/tests/config_parser_unittest.cc

+ 6 - 12
src/bin/dhcp4/json_config_parser.cc

@@ -312,17 +312,15 @@ protected:
         }
 
         // Try 4o6 specific parameter: 4o6-interface
-        try {
-            string iface4o6 = string_values_->getParam("4o6-interface");
+        string iface4o6 = string_values_->getOptionalParam("4o6-interface", "");
+        if (!iface4o6.empty()) {
             subnet4->get4o6().setIface4o6(iface4o6);
             subnet4->get4o6().enabled(true);
-        } catch (const DhcpConfigError&) {
-            // Don't care. 4o6-subnet is optional.
         }
 
         // Try 4o6 specific parameter: 4o6-subnet
-        try {
-            string subnet4o6 = string_values_->getParam("4o6-subnet");
+        string subnet4o6 = string_values_->getOptionalParam("4o6-subnet", "");
+        if (!subnet4o6.empty()) {
             size_t slash = subnet4o6.find("/");
             if (slash == std::string::npos) {
                 isc_throw(DhcpConfigError, "Missing / in the 4o6-subnet parameter:"
@@ -340,19 +338,15 @@ protected:
             }
             subnet4->get4o6().setSubnet4o6(IOAddress(prefix), len);
             subnet4->get4o6().enabled(true);
-        } catch (const DhcpConfigError&) {
-            // Don't care. 4o6-subnet is optional.
         }
 
         // Try 4o6 specific paramter: 4o6-interface-id
-        try {
-            std::string ifaceid = string_values_->getParam("4o6-interface-id");
+        std::string ifaceid = string_values_->getOptionalParam("4o6-interface-id", "");
+        if (!ifaceid.empty()) {
             OptionBuffer tmp(ifaceid.begin(), ifaceid.end());
             OptionPtr opt(new Option(Option::V6, D6O_INTERFACE_ID, tmp));
             subnet4->get4o6().setInterfaceId(opt);
             subnet4->get4o6().enabled(true);
-        } catch (const DhcpConfigError&) {
-
         }
 
         // Try setting up client class (if specified)

+ 57 - 0
src/bin/dhcp4/tests/config_parser_unittest.cc

@@ -3850,6 +3850,63 @@ TEST_F(Dhcp4ParserTest, 4o6subnet) {
     EXPECT_EQ(45, dhcp4o6.getSubnet4o6().second);
 }
 
+// Checks if the DHCPv4 is able to parse the configuration with 4o6 subnet
+// defined.
+TEST_F(Dhcp4ParserTest, 4o6subnetBogus) {
+
+    ConstElementPtr status;
+
+    // Just a plain v4 config (no 4o6 parameters)
+    string config[] = {
+        // Bogus configuration 1: missing / in subnet
+        "{ " + genIfaceConfig() + "," +
+        "\"rebind-timer\": 2000, "
+        "\"renew-timer\": 1000, "
+        "\"subnet4\": [ { "
+        "    \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
+        "    \"subnet\": \"192.0.2.0/24\","
+        "    \"4o6-subnet\": \"2001:db8::123\" } ],"
+        "\"valid-lifetime\": 4000 }",
+
+        // Bogus configuration 2: incorrect address
+                "{ " + genIfaceConfig() + "," +
+        "\"rebind-timer\": 2000, "
+        "\"renew-timer\": 1000, "
+        "\"subnet4\": [ { "
+        "    \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
+        "    \"subnet\": \"192.0.2.0/24\","
+        "    \"4o6-subnet\": \"2001:db8:bogus/45\" } ],"
+        "\"valid-lifetime\": 4000 }",
+
+        // Bogus configuration 3: incorrect prefix lenght
+        "{ " + genIfaceConfig() + "," +
+        "\"rebind-timer\": 2000, "
+        "\"renew-timer\": 1000, "
+        "\"subnet4\": [ { "
+        "    \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
+        "    \"subnet\": \"192.0.2.0/24\","
+        "    \"4o6-subnet\": \"2001:db8::123/200\" } ],"
+        "\"valid-lifetime\": 4000 }"
+    };
+
+    ElementPtr json1 = Element::fromJSON(config[0]);
+    ElementPtr json2 = Element::fromJSON(config[0]);
+    ElementPtr json3 = Element::fromJSON(config[0]);
+
+    // Check that the first config is rejected.
+    EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json1));
+    checkResult(status, 1);
+
+    // Check that the second config is rejected.
+    EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json2));
+    checkResult(status, 1);
+
+    // Check that the third config is rejected.
+    EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json3));
+    checkResult(status, 1);
+}
+
+
 // Checks if the DHCPv4 is able to parse the configuration with 4o6 network
 // interface defined.
 TEST_F(Dhcp4ParserTest, 4o6iface) {