]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[5491] Added a positive and a few negative user context / embedded comment tests...
authorFrancis Dupont <fdupont@isc.org>
Tue, 2 Jan 2018 15:52:25 +0000 (16:52 +0100)
committerFrancis Dupont <fdupont@isc.org>
Tue, 2 Jan 2018 15:52:25 +0000 (16:52 +0100)
src/bin/agent/tests/parser_unittests.cc
src/bin/d2/tests/parser_unittest.cc
src/bin/dhcp4/tests/parser_unittest.cc
src/bin/dhcp6/tests/parser_unittest.cc

index e777811463a173e169d3bdf540ce9463c70b8e4e..c7d8fb7c3b0ff80b0817b745d08052064418796e 100644 (file)
@@ -303,6 +303,22 @@ TEST(ParserTest, multilineComments) {
     testParser(txt, ParserContext::PARSER_AGENT, false);
 }
 
+// Tests if embedded comments are handled correctly.
+TEST(ParserTest, embbededComments) {
+    string txt= "{ \"Control-agent\": {"
+                "  \"comment\": \"a comment\","
+                "  \"http-host\": \"localhost\","
+                "  \"http-port\": 9000,\n"
+                "  \"control-sockets\": {\n"
+                "    \"dhcp4\": {\n"
+                "        \"user-context\": { \"comment\": \"indirect\" },\n"
+                "        \"socket-type\": \"unix\"\n"
+                "    } },\n"
+                "  \"user-context\": { \"compatible\": true }\n"
+                "} }";
+    testParser(txt, ParserContext::PARSER_AGENT, false);
+}
+
 /// @brief Loads specified example config file
 ///
 /// This test loads specified example file twice: first, using the legacy
@@ -596,6 +612,45 @@ TEST(ParserTest, errors) {
               ParserContext::PARSER_AGENT,
               "<string>:2.2-10: got unexpected keyword "
               "\"topping\" in Control-agent map.");
+
+    // user context and embedded comments
+    testError("{ \"Control-agent\":{\n"
+              "  \"comment\": true,\n"
+              "  \"http-port\": 9000 }}\n",
+              ParserContext::PARSER_AGENT,
+              "<string>:2.14-17: syntax error, unexpected boolean, "
+              "expecting constant string");
+
+    testError("{ \"Control-agent\":{\n"
+              "  \"user-context\": \"a comment\",\n"
+              "  \"http-port\": 9000 }}\n",
+              ParserContext::PARSER_AGENT,
+              "<string>:2.19-29: syntax error, unexpected constant string, "
+              "expecting {");
+
+    testError("{ \"Control-agent\":{\n"
+              "  \"comment\": \"a comment\",\n"
+              "  \"comment\": \"another one\",\n"
+              "  \"http-port\": 9000 }}\n",
+              ParserContext::PARSER_AGENT,
+              "<string>:3.3-11: duplicate user-context/comment entries "
+              "(previous at <string>:2:3)");
+
+    testError("{ \"Control-agent\":{\n"
+              "  \"user-context\": { \"version\": 1 },\n"
+              "  \"user-context\": { \"one\": \"only\" },\n"
+              "  \"http-port\": 9000 }}\n",
+              ParserContext::PARSER_AGENT,
+              "<string>:3.3-16: duplicate user-context entries "
+              "(previous at <string>:2:19)");
+
+    testError("{ \"Control-agent\":{\n"
+              "  \"user-context\": { \"comment\": \"indirect\" },\n"
+              "  \"comment\": \"a comment\",\n"
+              "  \"http-port\": 9000 }}\n",
+              ParserContext::PARSER_AGENT,
+              "<string>:3.3-11: duplicate user-context/comment entries "
+              "(previous at <string>:2:19)");
 }
 
 // Check unicode escapes
index 44198359c58c19d6ce5e4b9d5545cb81138b0532..9ecd6634510c73c13a6c0b28d0f0dac5217d856a 100644 (file)
@@ -188,73 +188,89 @@ TEST(ParserTest, Logging) {
 // Tests if bash (#) comments are supported. That's the only comment type that
 // was supported by the old parser.
 TEST(ParserTest, bashComments) {
-    string txt= "{ \"Dhcp6\": { \"interfaces-config\": {"
-                "  \"interfaces\": [ \"*\" ]"
-                "},\n"
-                "\"preferred-lifetime\": 3000,\n"
-                "# this is a comment\n"
-                "\"rebind-timer\": 2000, \n"
-                "# lots of comments here\n"
-                "# and here\n"
-                "\"renew-timer\": 1000, \n"
-                "\"subnet6\": [ { "
-                "    \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
-                "    \"subnet\": \"2001:db8:1::/48\", "
-                "    \"interface\": \"eth0\""
-                " } ],"
-                "\"valid-lifetime\": 4000 } }";
+    string txt =
+        "{ \"DhcpDdns\" : \n"
+           "{ \n"
+            " \"ip-address\": \"192.168.77.1\", \n"
+            "# this is a comment\n"
+            " \"port\": 777, \n "
+            " \"ncr-protocol\": \"UDP\", \n"
+            "# lots of comments here\n"
+            "# and here\n"
+            "\"tsig-keys\": [], \n"
+            "\"forward-ddns\" : {}, \n"
+            "\"reverse-ddns\" : {} \n"
+            "} \n"
+         "} \n";
     testParser(txt, D2ParserContext::PARSER_DHCPDDNS);
 }
 
 // Tests if C++ (//) comments can start anywhere, not just in the first line.
 TEST(ParserTest, cppComments) {
-    string txt= "{ \"Dhcp6\": { \"interfaces-config\": {"
-                "  \"interfaces\": [ \"*\" ]"
-                "},\n"
-                "\"preferred-lifetime\": 3000, // this is a comment \n"
-                "\"rebind-timer\": 2000, // everything after // is ignored\n"
-                "\"renew-timer\": 1000, // this will be ignored, too\n"
-                "\"subnet6\": [ { "
-                "    \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
-                "    \"subnet\": \"2001:db8:1::/48\", "
-                "    \"interface\": \"eth0\""
-                " } ],"
-                "\"valid-lifetime\": 4000 } }";
+    string txt =
+        "{ \"DhcpDdns\" : \n"
+           "{ \n"
+            " \"ip-address\": \"192.168.77.1\", \n"
+            " \"port\": 777, // this is a comment \n"
+            " \"ncr-protocol\": \"UDP\", // everything after // is ignored\n"
+            "\"tsig-keys\": [], // this will be ignored, too\n"
+            "\"forward-ddns\" : {}, \n"
+            "\"reverse-ddns\" : {} \n"
+            "} \n"
+         "} \n";
     testParser(txt, D2ParserContext::PARSER_DHCPDDNS, false);
 }
 
 // Tests if bash (#) comments can start anywhere, not just in the first line.
 TEST(ParserTest, bashCommentsInline) {
-    string txt= "{ \"Dhcp6\": { \"interfaces-config\": {"
-                "  \"interfaces\": [ \"*\" ]"
-                "},\n"
-                "\"preferred-lifetime\": 3000, # this is a comment \n"
-                "\"rebind-timer\": 2000, # everything after # is ignored\n"
-                "\"renew-timer\": 1000, # this will be ignored, too\n"
-                "\"subnet6\": [ { "
-                "    \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
-                "    \"subnet\": \"2001:db8:1::/48\", "
-                "    \"interface\": \"eth0\""
-                " } ],"
-                "\"valid-lifetime\": 4000 } }";
+    string txt =
+        "{ \"DhcpDdns\" : \n"
+           "{ \n"
+            " \"ip-address\": \"192.168.77.1\", \n"
+            " \"port\": 777, # this is a comment \n"
+            " \"ncr-protocol\": \"UDP\", # everything after # is ignored\n"
+            "\"tsig-keys\": [], # this will be ignored, too\n"
+            "\"forward-ddns\" : {}, \n"
+            "\"reverse-ddns\" : {} \n"
+            "} \n"
+         "} \n";
     testParser(txt, D2ParserContext::PARSER_DHCPDDNS, false);
 }
 
 // Tests if multi-line C style comments are handled correctly.
 TEST(ParserTest, multilineComments) {
-    string txt= "{ \"Dhcp6\": { \"interfaces-config\": {"
-                "  \"interfaces\": [ \"*\" ]"
-                "},\n"
-                "\"preferred-lifetime\": 3000, /* this is a C style comment\n"
-                "that\n can \n span \n multiple \n lines */ \n"
-                "\"rebind-timer\": 2000,\n"
-                "\"renew-timer\": 1000, \n"
-                "\"subnet6\": [ { "
-                "    \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
-                "    \"subnet\": \"2001:db8:1::/48\", "
-                "    \"interface\": \"eth0\""
-                " } ],"
-                "\"valid-lifetime\": 4000 } }";
+    string txt =
+        "{ \"DhcpDdns\" : \n"
+           "{ \n"
+            " \"ip-address\": \"192.168.77.1\", \n"
+            " \"port\": 777, /* this is a C style comment\n"
+            "that\n can \n span \n multiple \n lines */ \n"
+            " \"ncr-protocol\": \"UDP\", \n"
+            "\"tsig-keys\": [], \n"
+            "\"forward-ddns\" : {}, \n"
+            "\"reverse-ddns\" : {} \n"
+            "} \n"
+         "} \n";
+    testParser(txt, D2ParserContext::PARSER_DHCPDDNS, false);
+}
+
+// Tests if embedded comments are handled correctly.
+TEST(ParserTest, embbededComments) {
+    string txt =
+        "{ \"DhcpDdns\" : \n"
+           "{ \n"
+            "\"comment\": \"a comment\",\n"
+            " \"ip-address\": \"192.168.77.1\", \n"
+            " \"port\": 777, \n "
+            " \"ncr-protocol\": \"UDP\", \n"
+            "\"tsig-keys\" : [ { \n"
+            "    \"name\" : \"d2.md5.key\", \n"
+            "    \"user-context\" : { \"comment\" : \"indirect\" } } ], \n"
+            "\"forward-ddns\" : {}, \n"
+            "\"reverse-ddns\" : {}, \n"
+            "\"user-context\": { \"compatible\": true }"
+            "} \n"
+         "} \n";
     testParser(txt, D2ParserContext::PARSER_DHCPDDNS, false);
 }
 
@@ -551,6 +567,45 @@ TEST(ParserTest, errors) {
               D2ParserContext::PARSER_DHCPDDNS,
               "<string>:2.2-16: got unexpected keyword "
               "\"totally-bogus\" in DhcpDdns map.");
+
+    // user context and embedded comments
+    testError("{ \"DhcpDdns\":{\n"
+              "  \"comment\": true,\n"
+              "  \"dns-server-timeout\": 1000 }}\n",
+              D2ParserContext::PARSER_DHCPDDNS,
+              "<string>:2.14-17: syntax error, unexpected boolean, "
+              "expecting constant string");
+
+    testError("{ \"DhcpDdns\":{\n"
+              "  \"user-context\": \"a comment\",\n"
+              "  \"dns-server-timeout\": 1000 }}\n",
+              D2ParserContext::PARSER_DHCPDDNS,
+              "<string>:2.19-29: syntax error, unexpected constant string, "
+              "expecting {");
+
+    testError("{ \"DhcpDdns\":{\n"
+              "  \"comment\": \"a comment\",\n"
+              "  \"comment\": \"another one\",\n"
+              "  \"dns-server-timeout\": 1000 }}\n",
+              D2ParserContext::PARSER_DHCPDDNS,
+              "<string>:3.3-11: duplicate user-context/comment entries "
+              "(previous at <string>:2:3)");
+
+    testError("{ \"DhcpDdns\":{\n"
+              "  \"user-context\": { \"version\": 1 },\n"
+              "  \"user-context\": { \"one\": \"only\" },\n"
+              "  \"dns-server-timeout\": 1000 }}\n",
+              D2ParserContext::PARSER_DHCPDDNS,
+              "<string>:3.3-16: duplicate user-context entries "
+              "(previous at <string>:2:19)");
+
+    testError("{ \"DhcpDdns\":{\n"
+              "  \"user-context\": { \"comment\": \"indirect\" },\n"
+              "  \"comment\": \"a comment\",\n"
+              "  \"dns-server-timeout\": 1000 }}\n",
+              D2ParserContext::PARSER_DHCPDDNS,
+              "<string>:3.3-11: duplicate user-context/comment entries "
+              "(previous at <string>:2:19)");
 }
 
 // Check unicode escapes
index d909e533e829f634792da1231febb9d90e759b9d..b3c822e24afb234ebd7fb87099242df015b63ae2 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2016-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -200,6 +200,24 @@ TEST(ParserTest, multilineComments) {
     testParser(txt, Parser4Context::PARSER_DHCP4, false);
 }
 
+// Tests if embedded comments are handled correctly.
+TEST(ParserTest, embbededComments) {
+    string txt= "{ \"Dhcp4\": { \"interfaces-config\": {"
+                "  \"interfaces\": [ \"*\" ]"
+                "},\n"
+                "\"comment\": \"a comment\",\n"
+                "\"rebind-timer\": 2000,\n"
+                "\"renew-timer\": 1000, \n"
+                "\"subnet4\": [ { "
+                "    \"user-context\": { \"comment\": \"indirect\" },"
+                "    \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
+                "    \"subnet\": \"192.0.2.0/24\", "
+                "    \"interface\": \"eth0\""
+                " } ],"
+                "\"user-context\": { \"compatible\": true },"
+                "\"valid-lifetime\": 4000 } }";
+    testParser(txt, Parser4Context::PARSER_DHCP4, false);
+}
 
 /// @brief Loads specified example config file
 ///
@@ -532,6 +550,45 @@ TEST(ParserTest, errors) {
               Parser4Context::PARSER_OPTION_DEF,
               "missing parameter 'type' (<string>:1:1) "
               "[option-def map between <string>:1:1 and <string>:2:15]");
+
+    // user context and embedded comments
+    testError("{ \"Dhcp4\":{\n"
+              "  \"comment\": true,\n"
+              "  \"valid-lifetime\": 600 }}\n",
+              Parser4Context::PARSER_DHCP4,
+              "<string>:2.14-17: syntax error, unexpected boolean, "
+              "expecting constant string");
+
+    testError("{ \"Dhcp4\":{\n"
+              "  \"user-context\": \"a comment\",\n"
+              "  \"valid-lifetime\": 600 }}\n",
+              Parser4Context::PARSER_DHCP4,
+              "<string>:2.19-29: syntax error, unexpected constant string, "
+              "expecting {");
+
+    testError("{ \"Dhcp4\":{\n"
+              "  \"comment\": \"a comment\",\n"
+              "  \"comment\": \"another one\",\n"
+              "  \"valid-lifetime\": 600 }}\n",
+              Parser4Context::PARSER_DHCP4,
+              "<string>:3.3-11: duplicate user-context/comment entries "
+              "(previous at <string>:2:3)");
+
+    testError("{ \"Dhcp4\":{\n"
+              "  \"user-context\": { \"version\": 1 },\n"
+              "  \"user-context\": { \"one\": \"only\" },\n"
+              "  \"valid-lifetime\": 600 }}\n",
+              Parser4Context::PARSER_DHCP4,
+              "<string>:3.3-16: duplicate user-context entries "
+              "(previous at <string>:2:19)");
+
+    testError("{ \"Dhcp4\":{\n"
+              "  \"user-context\": { \"comment\": \"indirect\" },\n"
+              "  \"comment\": \"a comment\",\n"
+              "  \"valid-lifetime\": 600 }}\n",
+              Parser4Context::PARSER_DHCP4,
+              "<string>:3.3-11: duplicate user-context/comment entries "
+              "(previous at <string>:2:19)");
 }
 
 // Check unicode escapes
index 64e3fdc4199ffad21c3efbdd5186130ee21db16e..9a884542065cb49f6e321b842f4f2a472a1cb074 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2016-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -204,6 +204,26 @@ TEST(ParserTest, multilineComments) {
     testParser(txt, Parser6Context::PARSER_DHCP6, false);
 }
 
+// Tests if embedded comments are handled correctly.
+TEST(ParserTest, embbededComments) {
+    string txt= "{ \"Dhcp6\": { \"interfaces-config\": {"
+                "  \"interfaces\": [ \"*\" ]"
+                "},\n"
+                "\"comment\": \"a comment\",\n"
+                "\"preferred-lifetime\": 3000,\n"
+                "\"rebind-timer\": 2000,\n"
+                "\"renew-timer\": 1000, \n"
+                "\"subnet6\": [ { "
+                "    \"user-context\": { \"comment\": \"indirect\" },"
+                "    \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
+                "    \"subnet\": \"2001:db8:1::/48\", "
+                "    \"interface\": \"eth0\""
+                " } ],"
+                "\"user-context\": { \"compatible\": true },"
+                "\"valid-lifetime\": 4000 } }";
+    testParser(txt, Parser6Context::PARSER_DHCP6, false);
+}
+
 /// @brief Loads specified example config file
 ///
 /// This test loads specified example file twice: first, using the legacy
@@ -521,6 +541,45 @@ TEST(ParserTest, errors) {
               Parser6Context::PARSER_OPTION_DEF,
               "missing parameter 'type' (<string>:1:1) "
               "[option-def map between <string>:1:1 and <string>:2:15]");
+
+    // user context and embedded comments
+    testError("{ \"Dhcp6\":{\n"
+              "  \"comment\": true,\n"
+              "  \"preferred-lifetime\": 600 }}\n",
+              Parser6Context::PARSER_DHCP6,
+              "<string>:2.14-17: syntax error, unexpected boolean, "
+              "expecting constant string");
+
+    testError("{ \"Dhcp6\":{\n"
+              "  \"user-context\": \"a comment\",\n"
+              "  \"preferred-lifetime\": 600 }}\n",
+              Parser6Context::PARSER_DHCP6,
+              "<string>:2.19-29: syntax error, unexpected constant string, "
+              "expecting {");
+
+    testError("{ \"Dhcp6\":{\n"
+              "  \"comment\": \"a comment\",\n"
+              "  \"comment\": \"another one\",\n"
+              "  \"preferred-lifetime\": 600 }}\n",
+              Parser6Context::PARSER_DHCP6,
+              "<string>:3.3-11: duplicate user-context/comment entries "
+              "(previous at <string>:2:3)");
+
+    testError("{ \"Dhcp6\":{\n"
+              "  \"user-context\": { \"version\": 1 },\n"
+              "  \"user-context\": { \"one\": \"only\" },\n"
+              "  \"preferred-lifetime\": 600 }}\n",
+              Parser6Context::PARSER_DHCP6,
+              "<string>:3.3-16: duplicate user-context entries "
+              "(previous at <string>:2:19)");
+
+    testError("{ \"Dhcp6\":{\n"
+              "  \"user-context\": { \"comment\": \"indirect\" },\n"
+              "  \"comment\": \"a comment\",\n"
+              "  \"preferred-lifetime\": 600 }}\n",
+              Parser6Context::PARSER_DHCP6,
+              "<string>:3.3-11: duplicate user-context/comment entries "
+              "(previous at <string>:2:19)");
 }
 
 // Check unicode escapes