From: Francis Dupont Date: Sat, 11 Apr 2020 18:21:13 +0000 (+0200) Subject: [#1100] Got rid of comment extraction X-Git-Tag: Kea-1.7.9~77 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c2e71eef0554fd30b30a0a2187a2accf151480ca;p=thirdparty%2Fkea.git [#1100] Got rid of comment extraction --- diff --git a/doc/sphinx/arm/config.rst b/doc/sphinx/arm/config.rst index db869be014..762e4f66c5 100644 --- a/doc/sphinx/arm/config.rst +++ b/doc/sphinx/arm/config.rst @@ -176,9 +176,9 @@ In a sense the user context mechanism has superseeded the JSON comment capabilities. ISC would like to encourage people to use user-context in favor of the older mechanisms and we hope to deprecate JSON comments one day in the future. To promote this way of storing comments, Kea code is able to understand -JSON comments, but converts them to user context on the fly. For the time being, -the comments entries in user-context are converted back to JSON comments to keep -backward compatibility, but that conversion is expected to go away soon. +JSON comments, but converts them to user context on the fly. The +comments entries in user-context were converted back to JSON comments to +keep backward compatibility, but that conversion went away in version 1.7.x. The is one side effect, however. If your configuration uses the old JSON comment, the `config-get` command will return a slightly modified diff --git a/doc/sphinx/arm/hooks.rst b/doc/sphinx/arm/hooks.rst index a555430cb2..1ab531af58 100644 --- a/doc/sphinx/arm/hooks.rst +++ b/doc/sphinx/arm/hooks.rst @@ -2838,8 +2838,8 @@ operations, for example. If user context is supported in a given context, the parser translates "comment" entries into user context with a "comment" entry. The pretty -print of a configuration does the opposite operation and puts "comment" -entries at the beginning of maps, as that seems to be the common usage. +print of a configuration did the opposite operation and put "comment" +entries at the beginning of maps, but this was withdrawn in 1.7.x. As of Kea 1.3, the structures that allow user contexts are pools of all types (addresses and prefixes) and subnets. Kea 1.4 extended user diff --git a/src/bin/agent/tests/testdata/get_config.json b/src/bin/agent/tests/testdata/get_config.json index 6f9b5b3209..e1de227316 100644 --- a/src/bin/agent/tests/testdata/get_config.json +++ b/src/bin/agent/tests/testdata/get_config.json @@ -9,9 +9,11 @@ } }, "dhcp4": { - "comment": "socket to DHCP4 server", "socket-name": "/tmp/kea4-ctrl-socket", - "socket-type": "unix" + "socket-type": "unix", + "user-context": { + "comment": "socket to DHCP4 server" + } }, "dhcp6": { "socket-name": "/tmp/kea6-ctrl-socket", @@ -20,7 +22,7 @@ }, "hooks-libraries": [ { - "library": "/opt/local/control-agent-commands.so", + "library": "/tmp/k1100/src/bin/agent/tests/.libs/libbasic.so", "parameters": { "param1": "foo" } diff --git a/src/bin/d2/tests/testdata/get_config.json b/src/bin/d2/tests/testdata/get_config.json index d49f359fd3..5fc47d3f8e 100644 --- a/src/bin/d2/tests/testdata/get_config.json +++ b/src/bin/d2/tests/testdata/get_config.json @@ -8,7 +8,6 @@ "forward-ddns": { "ddns-domains": [ { - "comment": "DdnsDomain example", "dns-servers": [ { "hostname": "", @@ -17,7 +16,10 @@ } ], "key-name": "d2.md5.key", - "name": "four.example.com." + "name": "four.example.com.", + "user-context": { + "comment": "DdnsDomain example" + } }, { "dns-servers": [ diff --git a/src/bin/dhcp4/tests/get_config_unittest.cc b/src/bin/dhcp4/tests/get_config_unittest.cc index 1604fb200c..cac614e0f3 100644 --- a/src/bin/dhcp4/tests/get_config_unittest.cc +++ b/src/bin/dhcp4/tests/get_config_unittest.cc @@ -1937,88 +1937,112 @@ const char* EXTRACTED_CONFIGS[] = { " }\n", // CONFIGURATION 67 "{\n" -" \"comment\": \"A DHCPv4 server\",\n" " \"client-classes\": [\n" " {\n" -" \"comment\": \"match all\",\n" " \"name\": \"all\",\n" -" \"test\": \"'' == ''\"\n" +" \"test\": \"'' == ''\",\n" +" \"user-context\": {\n" +" \"comment\": \"match all\"\n" +" }\n" " },\n" " {\n" " \"name\": \"none\"\n" " },\n" " {\n" -" \"comment\": \"a comment\",\n" " \"name\": \"both\",\n" " \"user-context\": {\n" +" \"comment\": \"a comment\",\n" " \"version\": 1\n" " }\n" " }\n" " ],\n" " \"control-socket\": {\n" -" \"comment\": \"Indirect comment\",\n" " \"socket-name\": \"/tmp/kea4-ctrl-socket\",\n" -" \"socket-type\": \"unix\"\n" +" \"socket-type\": \"unix\",\n" +" \"user-context\": {\n" +" \"comment\": \"Indirect comment\"\n" +" }\n" " },\n" " \"dhcp-ddns\": {\n" -" \"comment\": \"No dynamic DNS\",\n" -" \"enable-updates\": false\n" +" \"enable-updates\": false,\n" +" \"user-context\": {\n" +" \"comment\": \"No dynamic DNS\"\n" +" }\n" " },\n" " \"interfaces-config\": {\n" -" \"comment\": \"Use wildcard\",\n" " \"interfaces\": [ \"*\" ],\n" -" \"re-detect\": false\n" +" \"re-detect\": false,\n" +" \"user-context\": {\n" +" \"comment\": \"Use wildcard\"\n" +" }\n" " },\n" " \"option-data\": [\n" " {\n" -" \"comment\": \"Set option value\",\n" " \"csv-format\": false,\n" " \"data\": \"ABCDEF0105\",\n" -" \"name\": \"dhcp-message\"\n" +" \"name\": \"dhcp-message\",\n" +" \"user-context\": {\n" +" \"comment\": \"Set option value\"\n" +" }\n" " }\n" " ],\n" " \"option-def\": [\n" " {\n" -" \"comment\": \"An option definition\",\n" " \"code\": 100,\n" " \"name\": \"foo\",\n" " \"space\": \"isc\",\n" -" \"type\": \"ipv4-address\"\n" +" \"type\": \"ipv4-address\",\n" +" \"user-context\": {\n" +" \"comment\": \"An option definition\"\n" +" }\n" " }\n" " ],\n" " \"shared-networks\": [\n" " {\n" -" \"comment\": \"A shared network\",\n" " \"name\": \"foo\",\n" " \"subnet4\": [\n" " {\n" -" \"comment\": \"A subnet\",\n" " \"id\": 100,\n" " \"pools\": [\n" " {\n" -" \"comment\": \"A pool\",\n" -" \"pool\": \"192.0.1.1-192.0.1.10\"\n" +" \"pool\": \"192.0.1.1-192.0.1.10\",\n" +" \"user-context\": {\n" +" \"comment\": \"A pool\"\n" +" }\n" " }\n" " ],\n" " \"reservations\": [\n" " {\n" -" \"comment\": \"A host reservation\",\n" " \"hostname\": \"foo.example.com\",\n" " \"hw-address\": \"AA:BB:CC:DD:EE:FF\",\n" " \"option-data\": [\n" " {\n" -" \"comment\": \"An option in a reservation\",\n" " \"data\": \"example.com\",\n" -" \"name\": \"domain-name\"\n" +" \"name\": \"domain-name\",\n" +" \"user-context\": {\n" +" \"comment\": \"An option in a reservation\"\n" +" }\n" " }\n" -" ]\n" +" ],\n" +" \"user-context\": {\n" +" \"comment\": \"A host reservation\"\n" +" }\n" " }\n" " ],\n" -" \"subnet\": \"192.0.1.0/24\"\n" +" \"subnet\": \"192.0.1.0/24\",\n" +" \"user-context\": {\n" +" \"comment\": \"A subnet\"\n" +" }\n" " }\n" -" ]\n" +" ],\n" +" \"user-context\": {\n" +" \"comment\": \"A shared network\"\n" +" }\n" " }\n" -" ]\n" +" ],\n" +" \"user-context\": {\n" +" \"comment\": \"A DHCPv4 server\"\n" +" }\n" " }\n", // CONFIGURATION 68 "{\n" @@ -9665,20 +9689,21 @@ const char* UNPARSED_CONFIGS[] = { " }\n", // CONFIGURATION 67 "{\n" -" \"comment\": \"A DHCPv4 server\",\n" " \"authoritative\": false,\n" " \"boot-file-name\": \"\",\n" " \"calculate-tee-times\": false,\n" " \"client-classes\": [\n" " {\n" -" \"comment\": \"match all\",\n" " \"boot-file-name\": \"\",\n" " \"name\": \"all\",\n" " \"next-server\": \"0.0.0.0\",\n" " \"option-data\": [ ],\n" " \"option-def\": [ ],\n" " \"server-hostname\": \"\",\n" -" \"test\": \"'' == ''\"\n" +" \"test\": \"'' == ''\",\n" +" \"user-context\": {\n" +" \"comment\": \"match all\"\n" +" }\n" " },\n" " {\n" " \"boot-file-name\": \"\",\n" @@ -9689,7 +9714,6 @@ const char* UNPARSED_CONFIGS[] = { " \"server-hostname\": \"\"\n" " },\n" " {\n" -" \"comment\": \"a comment\",\n" " \"boot-file-name\": \"\",\n" " \"name\": \"both\",\n" " \"next-server\": \"0.0.0.0\",\n" @@ -9697,14 +9721,17 @@ const char* UNPARSED_CONFIGS[] = { " \"option-def\": [ ],\n" " \"server-hostname\": \"\",\n" " \"user-context\": {\n" +" \"comment\": \"a comment\",\n" " \"version\": 1\n" " }\n" " }\n" " ],\n" " \"control-socket\": {\n" -" \"comment\": \"Indirect comment\",\n" " \"socket-name\": \"/tmp/kea4-ctrl-socket\",\n" -" \"socket-type\": \"unix\"\n" +" \"socket-type\": \"unix\",\n" +" \"user-context\": {\n" +" \"comment\": \"Indirect comment\"\n" +" }\n" " },\n" " \"ddns-generated-prefix\": \"myhost\",\n" " \"ddns-override-client-update\": false,\n" @@ -9714,7 +9741,6 @@ const char* UNPARSED_CONFIGS[] = { " \"ddns-send-updates\": true,\n" " \"decline-probation-period\": 86400,\n" " \"dhcp-ddns\": {\n" -" \"comment\": \"No dynamic DNS\",\n" " \"enable-updates\": false,\n" " \"max-queue-size\": 1024,\n" " \"ncr-format\": \"JSON\",\n" @@ -9722,7 +9748,10 @@ const char* UNPARSED_CONFIGS[] = { " \"sender-ip\": \"0.0.0.0\",\n" " \"sender-port\": 0,\n" " \"server-ip\": \"127.0.0.1\",\n" -" \"server-port\": 53001\n" +" \"server-port\": 53001,\n" +" \"user-context\": {\n" +" \"comment\": \"No dynamic DNS\"\n" +" }\n" " },\n" " \"dhcp-queue-control\": {\n" " \"capacity\": 500,\n" @@ -9744,9 +9773,11 @@ const char* UNPARSED_CONFIGS[] = { " \"hostname-char-replacement\": \"\",\n" " \"hostname-char-set\": \"[^A-Za-z0-9.-]\",\n" " \"interfaces-config\": {\n" -" \"comment\": \"Use wildcard\",\n" " \"interfaces\": [ \"*\" ],\n" -" \"re-detect\": false\n" +" \"re-detect\": false,\n" +" \"user-context\": {\n" +" \"comment\": \"Use wildcard\"\n" +" }\n" " },\n" " \"lease-database\": {\n" " \"type\": \"memfile\"\n" @@ -9760,25 +9791,29 @@ const char* UNPARSED_CONFIGS[] = { " \"next-server\": \"0.0.0.0\",\n" " \"option-data\": [\n" " {\n" -" \"comment\": \"Set option value\",\n" " \"always-send\": false,\n" " \"code\": 56,\n" " \"csv-format\": false,\n" " \"data\": \"ABCDEF0105\",\n" " \"name\": \"dhcp-message\",\n" -" \"space\": \"dhcp4\"\n" +" \"space\": \"dhcp4\",\n" +" \"user-context\": {\n" +" \"comment\": \"Set option value\"\n" +" }\n" " }\n" " ],\n" " \"option-def\": [\n" " {\n" -" \"comment\": \"An option definition\",\n" " \"array\": false,\n" " \"code\": 100,\n" " \"encapsulate\": \"\",\n" " \"name\": \"foo\",\n" " \"record-types\": \"\",\n" " \"space\": \"isc\",\n" -" \"type\": \"ipv4-address\"\n" +" \"type\": \"ipv4-address\",\n" +" \"user-context\": {\n" +" \"comment\": \"An option definition\"\n" +" }\n" " }\n" " ],\n" " \"reservation-mode\": \"all\",\n" @@ -9789,7 +9824,6 @@ const char* UNPARSED_CONFIGS[] = { " \"server-tag\": \"\",\n" " \"shared-networks\": [\n" " {\n" -" \"comment\": \"A shared network\",\n" " \"calculate-tee-times\": false,\n" " \"name\": \"foo\",\n" " \"option-data\": [ ],\n" @@ -9799,7 +9833,6 @@ const char* UNPARSED_CONFIGS[] = { " \"store-extended-info\": false,\n" " \"subnet4\": [\n" " {\n" -" \"comment\": \"A subnet\",\n" " \"4o6-interface\": \"\",\n" " \"4o6-interface-id\": \"\",\n" " \"4o6-subnet\": \"\",\n" @@ -9808,9 +9841,11 @@ const char* UNPARSED_CONFIGS[] = { " \"option-data\": [ ],\n" " \"pools\": [\n" " {\n" -" \"comment\": \"A pool\",\n" " \"option-data\": [ ],\n" -" \"pool\": \"192.0.1.1-192.0.1.10\"\n" +" \"pool\": \"192.0.1.1-192.0.1.10\",\n" +" \"user-context\": {\n" +" \"comment\": \"A pool\"\n" +" }\n" " }\n" " ],\n" " \"relay\": {\n" @@ -9818,7 +9853,6 @@ const char* UNPARSED_CONFIGS[] = { " },\n" " \"reservations\": [\n" " {\n" -" \"comment\": \"A host reservation\",\n" " \"boot-file-name\": \"\",\n" " \"client-classes\": [ ],\n" " \"hostname\": \"foo.example.com\",\n" @@ -9826,27 +9860,38 @@ const char* UNPARSED_CONFIGS[] = { " \"next-server\": \"0.0.0.0\",\n" " \"option-data\": [\n" " {\n" -" \"comment\": \"An option in a reservation\",\n" " \"always-send\": false,\n" " \"code\": 15,\n" " \"csv-format\": true,\n" " \"data\": \"example.com\",\n" " \"name\": \"domain-name\",\n" -" \"space\": \"dhcp4\"\n" +" \"space\": \"dhcp4\",\n" +" \"user-context\": {\n" +" \"comment\": \"An option in a reservation\"\n" +" }\n" " }\n" " ],\n" -" \"server-hostname\": \"\"\n" +" \"server-hostname\": \"\",\n" +" \"user-context\": {\n" +" \"comment\": \"A host reservation\"\n" +" }\n" " }\n" " ],\n" " \"store-extended-info\": false,\n" " \"subnet\": \"192.0.1.0/24\",\n" " \"t1-percent\": 0.5,\n" " \"t2-percent\": 0.875,\n" +" \"user-context\": {\n" +" \"comment\": \"A subnet\"\n" +" },\n" " \"valid-lifetime\": 7200\n" " }\n" " ],\n" " \"t1-percent\": 0.5,\n" " \"t2-percent\": 0.875,\n" +" \"user-context\": {\n" +" \"comment\": \"A shared network\"\n" +" },\n" " \"valid-lifetime\": 7200\n" " }\n" " ],\n" @@ -9856,6 +9901,9 @@ const char* UNPARSED_CONFIGS[] = { " \"subnet4\": [ ],\n" " \"t1-percent\": 0.5,\n" " \"t2-percent\": 0.875,\n" +" \"user-context\": {\n" +" \"comment\": \"A DHCPv4 server\"\n" +" },\n" " \"valid-lifetime\": 7200\n" " }\n", // CONFIGURATION 68 @@ -10693,7 +10741,7 @@ static_assert(max_config_counter == sizeof(UNPARSED_CONFIGS) / sizeof(char*), void outputFormatted(const std::string& config) { // pretty print it - ConstElementPtr json = extractComments(parseDHCP4(config)); + ConstElementPtr json = parseDHCP4(config); std::string prettier = prettyPrint(json, 4, 4); // get it as a line array std::list lines; diff --git a/src/bin/dhcp4/tests/get_config_unittest.cc.skel b/src/bin/dhcp4/tests/get_config_unittest.cc.skel index ebfd146b82..4ffc908e41 100644 --- a/src/bin/dhcp4/tests/get_config_unittest.cc.skel +++ b/src/bin/dhcp4/tests/get_config_unittest.cc.skel @@ -1,4 +1,4 @@ -// Copyright (C) 2017-2019 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2017-2020 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 @@ -104,7 +104,7 @@ static_assert(max_config_counter == sizeof(UNPARSED_CONFIGS) / sizeof(char*), void outputFormatted(const std::string& config) { // pretty print it - ConstElementPtr json = extractComments(parseDHCP4(config)); + ConstElementPtr json = parseDHCP4(config); std::string prettier = prettyPrint(json, 4, 4); // get it as a line array std::list lines; diff --git a/src/bin/dhcp6/tests/get_config_unittest.cc b/src/bin/dhcp6/tests/get_config_unittest.cc index c0ca287fef..f61123ad19 100644 --- a/src/bin/dhcp6/tests/get_config_unittest.cc +++ b/src/bin/dhcp6/tests/get_config_unittest.cc @@ -1759,100 +1759,128 @@ const char* EXTRACTED_CONFIGS[] = { " }\n", // CONFIGURATION 58 "{\n" -" \"comment\": \"A DHCPv6 server\",\n" " \"client-classes\": [\n" " {\n" -" \"comment\": \"match all\",\n" " \"name\": \"all\",\n" -" \"test\": \"'' == ''\"\n" +" \"test\": \"'' == ''\",\n" +" \"user-context\": {\n" +" \"comment\": \"match all\"\n" +" }\n" " },\n" " {\n" " \"name\": \"none\"\n" " },\n" " {\n" -" \"comment\": \"a comment\",\n" " \"name\": \"both\",\n" " \"user-context\": {\n" +" \"comment\": \"a comment\",\n" " \"version\": 1\n" " }\n" " }\n" " ],\n" " \"control-socket\": {\n" -" \"comment\": \"Indirect comment\",\n" " \"socket-name\": \"/tmp/kea6-ctrl-socket\",\n" -" \"socket-type\": \"unix\"\n" +" \"socket-type\": \"unix\",\n" +" \"user-context\": {\n" +" \"comment\": \"Indirect comment\"\n" +" }\n" " },\n" " \"dhcp-ddns\": {\n" -" \"comment\": \"No dynamic DNS\",\n" -" \"enable-updates\": false\n" +" \"enable-updates\": false,\n" +" \"user-context\": {\n" +" \"comment\": \"No dynamic DNS\"\n" +" }\n" " },\n" " \"interfaces-config\": {\n" -" \"comment\": \"Use wildcard\",\n" " \"interfaces\": [ \"*\" ],\n" -" \"re-detect\": false\n" +" \"re-detect\": false,\n" +" \"user-context\": {\n" +" \"comment\": \"Use wildcard\"\n" +" }\n" " },\n" " \"option-data\": [\n" " {\n" -" \"comment\": \"Set option value\",\n" " \"csv-format\": false,\n" " \"data\": \"ABCDEF0105\",\n" -" \"name\": \"subscriber-id\"\n" +" \"name\": \"subscriber-id\",\n" +" \"user-context\": {\n" +" \"comment\": \"Set option value\"\n" +" }\n" " }\n" " ],\n" " \"option-def\": [\n" " {\n" -" \"comment\": \"An option definition\",\n" " \"code\": 100,\n" " \"name\": \"foo\",\n" " \"space\": \"isc\",\n" -" \"type\": \"ipv6-address\"\n" +" \"type\": \"ipv6-address\",\n" +" \"user-context\": {\n" +" \"comment\": \"An option definition\"\n" +" }\n" " }\n" " ],\n" " \"server-id\": {\n" -" \"comment\": \"DHCPv6 specific\",\n" -" \"type\": \"LL\"\n" +" \"type\": \"LL\",\n" +" \"user-context\": {\n" +" \"comment\": \"DHCPv6 specific\"\n" +" }\n" " },\n" " \"shared-networks\": [\n" " {\n" -" \"comment\": \"A shared network\",\n" " \"name\": \"foo\",\n" " \"subnet6\": [\n" " {\n" -" \"comment\": \"A subnet\",\n" " \"id\": 100,\n" " \"pd-pools\": [\n" " {\n" -" \"comment\": \"A prefix pool\",\n" " \"delegated-len\": 64,\n" " \"prefix\": \"2001:db2::\",\n" -" \"prefix-len\": 48\n" +" \"prefix-len\": 48,\n" +" \"user-context\": {\n" +" \"comment\": \"A prefix pool\"\n" +" }\n" " }\n" " ],\n" " \"pools\": [\n" " {\n" -" \"comment\": \"A pool\",\n" -" \"pool\": \"2001:db1::/64\"\n" +" \"pool\": \"2001:db1::/64\",\n" +" \"user-context\": {\n" +" \"comment\": \"A pool\"\n" +" }\n" " }\n" " ],\n" " \"reservations\": [\n" " {\n" -" \"comment\": \"A host reservation\",\n" " \"hostname\": \"foo.example.com\",\n" " \"hw-address\": \"AA:BB:CC:DD:EE:FF\",\n" " \"option-data\": [\n" " {\n" -" \"comment\": \"An option in a reservation\",\n" " \"data\": \"example.com\",\n" -" \"name\": \"domain-search\"\n" +" \"name\": \"domain-search\",\n" +" \"user-context\": {\n" +" \"comment\": \"An option in a reservation\"\n" +" }\n" " }\n" -" ]\n" +" ],\n" +" \"user-context\": {\n" +" \"comment\": \"A host reservation\"\n" +" }\n" " }\n" " ],\n" -" \"subnet\": \"2001:db1::/48\"\n" +" \"subnet\": \"2001:db1::/48\",\n" +" \"user-context\": {\n" +" \"comment\": \"A subnet\"\n" +" }\n" " }\n" -" ]\n" +" ],\n" +" \"user-context\": {\n" +" \"comment\": \"A shared network\"\n" +" }\n" " }\n" -" ]\n" +" ],\n" +" \"user-context\": {\n" +" \"comment\": \"A DHCPv6 server\"\n" +" }\n" " }\n", // CONFIGURATION 59 "{\n" @@ -8679,32 +8707,35 @@ const char* UNPARSED_CONFIGS[] = { " }\n", // CONFIGURATION 58 "{\n" -" \"comment\": \"A DHCPv6 server\",\n" " \"calculate-tee-times\": true,\n" " \"client-classes\": [\n" " {\n" -" \"comment\": \"match all\",\n" " \"name\": \"all\",\n" " \"option-data\": [ ],\n" -" \"test\": \"'' == ''\"\n" +" \"test\": \"'' == ''\",\n" +" \"user-context\": {\n" +" \"comment\": \"match all\"\n" +" }\n" " },\n" " {\n" " \"name\": \"none\",\n" " \"option-data\": [ ]\n" " },\n" " {\n" -" \"comment\": \"a comment\",\n" " \"name\": \"both\",\n" " \"option-data\": [ ],\n" " \"user-context\": {\n" +" \"comment\": \"a comment\",\n" " \"version\": 1\n" " }\n" " }\n" " ],\n" " \"control-socket\": {\n" -" \"comment\": \"Indirect comment\",\n" " \"socket-name\": \"/tmp/kea6-ctrl-socket\",\n" -" \"socket-type\": \"unix\"\n" +" \"socket-type\": \"unix\",\n" +" \"user-context\": {\n" +" \"comment\": \"Indirect comment\"\n" +" }\n" " },\n" " \"ddns-generated-prefix\": \"myhost\",\n" " \"ddns-override-client-update\": false,\n" @@ -8714,7 +8745,6 @@ const char* UNPARSED_CONFIGS[] = { " \"ddns-send-updates\": true,\n" " \"decline-probation-period\": 86400,\n" " \"dhcp-ddns\": {\n" -" \"comment\": \"No dynamic DNS\",\n" " \"enable-updates\": false,\n" " \"max-queue-size\": 1024,\n" " \"ncr-format\": \"JSON\",\n" @@ -8722,7 +8752,10 @@ const char* UNPARSED_CONFIGS[] = { " \"sender-ip\": \"0.0.0.0\",\n" " \"sender-port\": 0,\n" " \"server-ip\": \"127.0.0.1\",\n" -" \"server-port\": 53001\n" +" \"server-port\": 53001,\n" +" \"user-context\": {\n" +" \"comment\": \"No dynamic DNS\"\n" +" }\n" " },\n" " \"dhcp-queue-control\": {\n" " \"capacity\": 500,\n" @@ -8743,9 +8776,11 @@ const char* UNPARSED_CONFIGS[] = { " \"hostname-char-replacement\": \"\",\n" " \"hostname-char-set\": \"[^A-Za-z0-9.-]\",\n" " \"interfaces-config\": {\n" -" \"comment\": \"Use wildcard\",\n" " \"interfaces\": [ \"*\" ],\n" -" \"re-detect\": false\n" +" \"re-detect\": false,\n" +" \"user-context\": {\n" +" \"comment\": \"Use wildcard\"\n" +" }\n" " },\n" " \"lease-database\": {\n" " \"type\": \"memfile\"\n" @@ -8758,25 +8793,29 @@ const char* UNPARSED_CONFIGS[] = { " },\n" " \"option-data\": [\n" " {\n" -" \"comment\": \"Set option value\",\n" " \"always-send\": false,\n" " \"code\": 38,\n" " \"csv-format\": false,\n" " \"data\": \"ABCDEF0105\",\n" " \"name\": \"subscriber-id\",\n" -" \"space\": \"dhcp6\"\n" +" \"space\": \"dhcp6\",\n" +" \"user-context\": {\n" +" \"comment\": \"Set option value\"\n" +" }\n" " }\n" " ],\n" " \"option-def\": [\n" " {\n" -" \"comment\": \"An option definition\",\n" " \"array\": false,\n" " \"code\": 100,\n" " \"encapsulate\": \"\",\n" " \"name\": \"foo\",\n" " \"record-types\": \"\",\n" " \"space\": \"isc\",\n" -" \"type\": \"ipv6-address\"\n" +" \"type\": \"ipv6-address\",\n" +" \"user-context\": {\n" +" \"comment\": \"An option definition\"\n" +" }\n" " }\n" " ],\n" " \"preferred-lifetime\": 3600,\n" @@ -8786,18 +8825,19 @@ const char* UNPARSED_CONFIGS[] = { " \"lease-checks\": \"warn\"\n" " },\n" " \"server-id\": {\n" -" \"comment\": \"DHCPv6 specific\",\n" " \"enterprise-id\": 0,\n" " \"htype\": 0,\n" " \"identifier\": \"\",\n" " \"persist\": true,\n" " \"time\": 0,\n" -" \"type\": \"LL\"\n" +" \"type\": \"LL\",\n" +" \"user-context\": {\n" +" \"comment\": \"DHCPv6 specific\"\n" +" }\n" " },\n" " \"server-tag\": \"\",\n" " \"shared-networks\": [\n" " {\n" -" \"comment\": \"A shared network\",\n" " \"calculate-tee-times\": true,\n" " \"name\": \"foo\",\n" " \"option-data\": [ ],\n" @@ -8809,24 +8849,27 @@ const char* UNPARSED_CONFIGS[] = { " \"store-extended-info\": false,\n" " \"subnet6\": [\n" " {\n" -" \"comment\": \"A subnet\",\n" " \"calculate-tee-times\": true,\n" " \"id\": 100,\n" " \"option-data\": [ ],\n" " \"pd-pools\": [\n" " {\n" -" \"comment\": \"A prefix pool\",\n" " \"delegated-len\": 64,\n" " \"option-data\": [ ],\n" " \"prefix\": \"2001:db2::\",\n" -" \"prefix-len\": 48\n" +" \"prefix-len\": 48,\n" +" \"user-context\": {\n" +" \"comment\": \"A prefix pool\"\n" +" }\n" " }\n" " ],\n" " \"pools\": [\n" " {\n" -" \"comment\": \"A pool\",\n" " \"option-data\": [ ],\n" -" \"pool\": \"2001:db1::/64\"\n" +" \"pool\": \"2001:db1::/64\",\n" +" \"user-context\": {\n" +" \"comment\": \"A pool\"\n" +" }\n" " }\n" " ],\n" " \"preferred-lifetime\": 3600,\n" @@ -8835,34 +8878,44 @@ const char* UNPARSED_CONFIGS[] = { " },\n" " \"reservations\": [\n" " {\n" -" \"comment\": \"A host reservation\",\n" " \"client-classes\": [ ],\n" " \"hostname\": \"foo.example.com\",\n" " \"hw-address\": \"aa:bb:cc:dd:ee:ff\",\n" " \"ip-addresses\": [ ],\n" " \"option-data\": [\n" " {\n" -" \"comment\": \"An option in a reservation\",\n" " \"always-send\": false,\n" " \"code\": 24,\n" " \"csv-format\": true,\n" " \"data\": \"example.com\",\n" " \"name\": \"domain-search\",\n" -" \"space\": \"dhcp6\"\n" +" \"space\": \"dhcp6\",\n" +" \"user-context\": {\n" +" \"comment\": \"An option in a reservation\"\n" +" }\n" " }\n" " ],\n" -" \"prefixes\": [ ]\n" +" \"prefixes\": [ ],\n" +" \"user-context\": {\n" +" \"comment\": \"A host reservation\"\n" +" }\n" " }\n" " ],\n" " \"store-extended-info\": false,\n" " \"subnet\": \"2001:db1::/48\",\n" " \"t1-percent\": 0.5,\n" " \"t2-percent\": 0.8,\n" +" \"user-context\": {\n" +" \"comment\": \"A subnet\"\n" +" },\n" " \"valid-lifetime\": 7200\n" " }\n" " ],\n" " \"t1-percent\": 0.5,\n" " \"t2-percent\": 0.8,\n" +" \"user-context\": {\n" +" \"comment\": \"A shared network\"\n" +" },\n" " \"valid-lifetime\": 7200\n" " }\n" " ],\n" @@ -8872,6 +8925,9 @@ const char* UNPARSED_CONFIGS[] = { " \"subnet6\": [ ],\n" " \"t1-percent\": 0.5,\n" " \"t2-percent\": 0.8,\n" +" \"user-context\": {\n" +" \"comment\": \"A DHCPv6 server\"\n" +" },\n" " \"valid-lifetime\": 7200\n" " }\n", // CONFIGURATION 59 @@ -9334,7 +9390,7 @@ static_assert(max_config_counter == sizeof(UNPARSED_CONFIGS) / sizeof(char*), void outputFormatted(const std::string& config) { // pretty print it - ConstElementPtr json = extractComments(parseDHCP6(config)); + ConstElementPtr json = parseDHCP6(config); std::string prettier = prettyPrint(json, 4, 4); // get it as a line array std::list lines; diff --git a/src/bin/dhcp6/tests/get_config_unittest.cc.skel b/src/bin/dhcp6/tests/get_config_unittest.cc.skel index 2030cb925c..5c227dbc53 100644 --- a/src/bin/dhcp6/tests/get_config_unittest.cc.skel +++ b/src/bin/dhcp6/tests/get_config_unittest.cc.skel @@ -1,4 +1,4 @@ -// Copyright (C) 2017-2019 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2017-2020 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 @@ -105,7 +105,7 @@ static_assert(max_config_counter == sizeof(UNPARSED_CONFIGS) / sizeof(char*), void outputFormatted(const std::string& config) { // pretty print it - ConstElementPtr json = extractComments(parseDHCP6(config)); + ConstElementPtr json = parseDHCP6(config); std::string prettier = prettyPrint(json, 4, 4); // get it as a line array std::list lines; diff --git a/src/bin/netconf/tests/testdata/get_config.json b/src/bin/netconf/tests/testdata/get_config.json index 38c6bceb7d..d08aed20c0 100644 --- a/src/bin/netconf/tests/testdata/get_config.json +++ b/src/bin/netconf/tests/testdata/get_config.json @@ -4,7 +4,6 @@ "hooks-libraries": [ ], "managed-servers": { "dhcp4": { - "comment": "Kea DHCP4 server serving network on floor 13", "boot-update": true, "control-socket": { "socket-name": "/tmp/kea4-ctrl-socket", @@ -13,6 +12,9 @@ }, "model": "kea-dhcp4-server", "subscribe-changes": true, + "user-context": { + "comment": "Kea DHCP4 server serving network on floor 13" + }, "validate-changes": false } }, diff --git a/src/lib/cc/data.cc b/src/lib/cc/data.cc index 882f34abbf..d644c75cfa 100644 --- a/src/lib/cc/data.cc +++ b/src/lib/cc/data.cc @@ -1279,26 +1279,10 @@ prettyPrint(ConstElementPtr element, std::ostream& out, // open the map out << "{\n"; - bool first = true; - // output comment first - if (element->contains("comment")) { - // add indentation - out << std::string(indent + step, ' '); - // add keyword: - out << "\"comment\": "; - // recursive call - prettyPrint(element->get("comment"), out, indent + step, step); - // it was the first - first = false; - } - // iterate on keyword: value const auto& m = element->mapValue(); + bool first = true; for (auto it = m.begin(); it != m.end(); ++it) { - // skip comment - if (it->first == "comment") { - continue; - } // add the separator if not the first item if (first) { first = false; diff --git a/src/lib/cc/tests/data_unittests.cc b/src/lib/cc/tests/data_unittests.cc index a347a2469f..41b20d4603 100644 --- a/src/lib/cc/tests/data_unittests.cc +++ b/src/lib/cc/tests/data_unittests.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2009-2019 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2009-2020 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 @@ -1147,8 +1147,8 @@ TEST(Element, prettyPrint) { // default step is 2, order is alphabetic, no \n at the end string text = "{\n" - " \"comment\": \"this is an exception\",\n" " \"boolean\": true,\n" + " \"comment\": \"this is an exception\",\n" " \"empty-list\": [ ],\n" " \"empty-map\": { },\n" " \"integer\": 1,\n" diff --git a/src/lib/cc/tests/user_context_unittests.cc b/src/lib/cc/tests/user_context_unittests.cc index 3035e99ade..e88b421fec 100644 --- a/src/lib/cc/tests/user_context_unittests.cc +++ b/src/lib/cc/tests/user_context_unittests.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2018-2020 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 @@ -74,7 +74,7 @@ TEST(UserContext, onlyComment) { ElementPtr map = gen(); parent.contextToElement(map); ElementPtr expected = gen(); - merge(expected, ctx); + expected->set("user-context", ctx); EXPECT_EQ(*expected, *map); } @@ -86,8 +86,7 @@ TEST(UserContext, both) { ElementPtr map = gen(); parent.contextToElement(map); ElementPtr expected = gen(); - expected->set("comment", Element::create("foobar")); - expected->set("user-context", Element::fromJSON("{ \"version\": 1 }")); + expected->set("user-context", ctx); EXPECT_EQ(*expected, *map); } @@ -118,9 +117,7 @@ TEST(toElement, onlyComment) { ConstElementPtr ctx = Element::fromJSON("{ \"comment\": \"foobar\" }"); map->set("user-context", ctx); ConstElementPtr result = UserContext::toElement(map); - ElementPtr expected = gen(); - merge(expected, ctx); - EXPECT_EQ(*expected, *result); + EXPECT_EQ(*result, *map); } TEST(toElement, both) { @@ -129,10 +126,7 @@ TEST(toElement, both) { Element::fromJSON("{ \"comment\": \"foobar\", \"version\": 1 }"); map->set("user-context", ctx); ConstElementPtr result = UserContext::toElement(map); - ElementPtr expected = gen(); - expected->set("comment", Element::create("foobar")); - expected->set("user-context", Element::fromJSON("{ \"version\": 1 }")); - EXPECT_EQ(*expected, *result); + EXPECT_EQ(*result, *map); } } diff --git a/src/lib/cc/user_context.cc b/src/lib/cc/user_context.cc index 3194fcd6ae..2ff1984cc9 100644 --- a/src/lib/cc/user_context.cc +++ b/src/lib/cc/user_context.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2017-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2017-2020 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 @@ -15,43 +15,14 @@ void UserContext::contextToElement(ElementPtr map) const { // Set user-context extracting comment ConstElementPtr context = getContext(); - if (!isNull(context)) { - if ((context->getType() == Element::map) && - context->contains("comment")) { - ElementPtr copied = isc::data::copy(context); - map->set("comment", copied->get("comment")); - copied->remove("comment"); - if (copied->size() > 0) { - map->set("user-context", copied); - } - } else { - map->set("user-context", context); - } + if (context) { + map->set("user-context", context); } } ElementPtr UserContext::toElement(ConstElementPtr map) { ElementPtr result = isc::data::copy(map); - // Protect against argument not map - if (result->getType() != Element::map) { - return (result); - } - ConstElementPtr ctx = result->get("user-context"); - // Protect against user context not map - if (!ctx || (ctx->getType() != Element::map)) { - return (result); - } - // Extract comment - if (ctx->contains("comment")) { - ElementPtr ctx_copy = isc::data::copy(ctx); - result->set("comment", ctx_copy->get("comment")); - ctx_copy->remove("comment"); - result->remove("user-context"); - if (ctx_copy->size() > 0) { - result->set("user-context", ctx_copy); - } - } return (result); } diff --git a/src/lib/cc/user_context.h b/src/lib/cc/user_context.h index ae795b6753..2804c7ef44 100644 --- a/src/lib/cc/user_context.h +++ b/src/lib/cc/user_context.h @@ -1,4 +1,4 @@ -// Copyright (C) 2017-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2017-2020 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 @@ -34,15 +34,16 @@ struct UserContext { /// @brief Merge unparse a user_context object. /// /// Add user-context to map, but only if defined. Omit if it was not. - /// Extract comment so it will be pretty-printed first. /// /// @param map A pointer to map where the user context will be unparsed. void contextToElement(data::ElementPtr map) const; - /// @brief Copy extracting comments an Element map + /// @brief Copy an Element map + /// + /// A previous version of this extracted comments. /// /// @param map A pointer to map. - /// @return a copy of map where comment is extracted. + /// @return a copy of map static data::ElementPtr toElement(data::ConstElementPtr map); protected: diff --git a/src/lib/dhcpsrv/tests/cfg_duid_unittest.cc b/src/lib/dhcpsrv/tests/cfg_duid_unittest.cc index d15cc5567b..73c6af1781 100644 --- a/src/lib/dhcpsrv/tests/cfg_duid_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_duid_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2015,2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2015-2020 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 @@ -130,14 +130,14 @@ TEST_F(CfgDUIDTest, setValues) { EXPECT_EQ(user_context, cfg_duid.getContext()->str()); std::string expected = "{\n" - " \"comment\": \"bar\",\n" " \"type\": \"EN\",\n" " \"identifier\": \"ABCDEF\",\n" " \"htype\": 100,\n" " \"time\": 32100,\n" " \"enterprise-id\": 10,\n" " \"persist\": false,\n" - " \"user-context\": { \"foo\": 1 }\n" + " \"user-context\": { \"foo\": 1,\n" + " \"comment\": \"bar\" }\n" "}"; runToElementTest(expected, cfg_duid); } diff --git a/src/lib/dhcpsrv/tests/cfg_iface_unittest.cc b/src/lib/dhcpsrv/tests/cfg_iface_unittest.cc index a6aa610f19..770004bcf2 100644 --- a/src/lib/dhcpsrv/tests/cfg_iface_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_iface_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2019 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2020 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 @@ -486,10 +486,10 @@ TEST_F(CfgIfaceTest, unparse) { // Check unparse std::string expected = - "{ \"comment\": \"foo\", " + "{ " "\"interfaces\": [ \"*\", \"eth0\", \"eth1/192.0.2.3\" ], " "\"re-detect\": false, " - "\"user-context\": { \"bar\": 1 } }"; + "\"user-context\": { \"comment\": \"foo\", \"bar\": 1 } }"; runToElementTest(expected, cfg4); // Now check IPv6 @@ -501,10 +501,10 @@ TEST_F(CfgIfaceTest, unparse) { EXPECT_NO_THROW(cfg6.setContext(Element::fromJSON(comment))); expected = - "{ \"comment\": \"bar\", " + "{ " "\"interfaces\": [ \"*\", \"eth1\", \"eth0/2001:db8:1::1\" ], " "\"re-detect\": false, " - "\"user-context\": { \"foo\": 2 } }"; + "\"user-context\": { \"comment\": \"bar\", \"foo\": 2 } }"; runToElementTest(expected, cfg6); } diff --git a/src/lib/dhcpsrv/tests/cfg_option_def_unittest.cc b/src/lib/dhcpsrv/tests/cfg_option_def_unittest.cc index aa5b1ca537..1aa9d96d52 100644 --- a/src/lib/dhcpsrv/tests/cfg_option_def_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_option_def_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2019 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2020 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 @@ -318,13 +318,12 @@ TEST(CfgOptionDefTest, unparse) { "},{\n" " \"name\": \"option-rec\",\n" " \"code\": 6,\n" - " \"comment\": \"foo\",\n" " \"type\": \"record\",\n" " \"array\": false,\n" " \"record-types\": \"uint16, uint16\",\n" " \"encapsulate\": \"\",\n" " \"space\": \"dns\",\n" - " \"user-context\": { \"bar\": 1 }\n" + " \"user-context\": { \"comment\": \"foo\", \"bar\": 1 }\n" "},{\n" " \"name\": \"option-foo\",\n" " \"code\": 5,\n" diff --git a/src/lib/dhcpsrv/tests/cfg_option_unittest.cc b/src/lib/dhcpsrv/tests/cfg_option_unittest.cc index f2f8dc72be..10f128b06c 100644 --- a/src/lib/dhcpsrv/tests/cfg_option_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_option_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2019 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2020 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 @@ -1097,13 +1097,12 @@ TEST_F(CfgOptionTest, unparse) { " \"data\": \"12121212\",\n" " \"always-send\": false\n" "},{\n" - " \"comment\": \"foo\",\n" " \"code\": 101,\n" " \"space\": \"dns\",\n" " \"csv-format\": true,\n" " \"data\": \"12, 12, 12, 12\",\n" " \"always-send\": false,\n" - " \"user-context\": { \"bar\": 1 }\n" + " \"user-context\": { \"comment\": \"foo\", \"bar\": 1 }\n" "},{\n" " \"code\": 13,\n" " \"name\": \"status-code\",\n" diff --git a/src/lib/dhcpsrv/tests/cfg_subnets4_unittest.cc b/src/lib/dhcpsrv/tests/cfg_subnets4_unittest.cc index 7b15ddd4d8..6b064a4298 100644 --- a/src/lib/dhcpsrv/tests/cfg_subnets4_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_subnets4_unittest.cc @@ -1057,7 +1057,6 @@ TEST(CfgSubnets4Test, unparseSubnet) { // Unparse std::string expected = "[\n" "{\n" - " \"comment\": \"foo\",\n" " \"id\": 123,\n" " \"subnet\": \"192.0.2.0/26\",\n" " \"t1-percent\": 0.45," @@ -1071,7 +1070,8 @@ TEST(CfgSubnets4Test, unparseSubnet) { " \"4o6-interface-id\": \"\",\n" " \"4o6-subnet\": \"\",\n" " \"option-data\": [ ],\n" - " \"pools\": [ ]\n" + " \"pools\": [ ],\n" + " \"user-context\": { \"comment\": \"foo\" }\n" "},{\n" " \"id\": 124,\n" " \"subnet\": \"192.0.2.64/26\",\n" @@ -1161,10 +1161,10 @@ TEST(CfgSubnets4Test, unparsePool) { " \"option-data\": [],\n" " \"pools\": [\n" " {\n" - " \"comment\": \"foo\",\n" " \"option-data\": [ ],\n" " \"pool\": \"192.0.2.1-192.0.2.10\",\n" - " \"user-context\": { \"version\": 1 }\n" + " \"user-context\": { \"comment\": \"foo\",\n" + " \"version\": 1 }\n" " },{\n" " \"option-data\": [ ],\n" " \"pool\": \"192.0.2.64/26\",\n" diff --git a/src/lib/dhcpsrv/tests/cfg_subnets6_unittest.cc b/src/lib/dhcpsrv/tests/cfg_subnets6_unittest.cc index a934f6a749..bc2979a598 100644 --- a/src/lib/dhcpsrv/tests/cfg_subnets6_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_subnets6_unittest.cc @@ -657,7 +657,6 @@ TEST(CfgSubnets6Test, unparseSubnet) { // Unparse std::string expected = "[\n" "{\n" - " \"comment\": \"foo\",\n" " \"id\": 123,\n" " \"subnet\": \"2001:db8:1::/48\",\n" " \"t1-percent\": 0.45," @@ -671,7 +670,8 @@ TEST(CfgSubnets6Test, unparseSubnet) { " \"client-class\": \"foo\",\n" " \"pools\": [ ],\n" " \"pd-pools\": [ ],\n" - " \"option-data\": [ ]\n" + " \"option-data\": [ ],\n" + " \"user-context\": { \"comment\": \"foo\" }\n" "},{\n" " \"id\": 124,\n" " \"subnet\": \"2001:db8:2::/48\",\n" @@ -757,9 +757,9 @@ TEST(CfgSubnets6Test, unparsePool) { " \"valid-lifetime\": 4,\n" " \"pools\": [\n" " {\n" - " \"comment\": \"foo\",\n" " \"pool\": \"2001:db8:1::100-2001:db8:1::199\",\n" - " \"user-context\": { \"version\": 1 },\n" + " \"user-context\": { \"version\": 1,\n" + " \"comment\": \"foo\" },\n" " \"option-data\": [ ]\n" " },{\n" " \"pool\": \"2001:db8:1:1::/64\",\n" diff --git a/src/lib/dhcpsrv/tests/client_class_def_unittest.cc b/src/lib/dhcpsrv/tests/client_class_def_unittest.cc index 37fa7b5f19..1c862b8c35 100644 --- a/src/lib/dhcpsrv/tests/client_class_def_unittest.cc +++ b/src/lib/dhcpsrv/tests/client_class_def_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2015-2020 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 @@ -504,7 +504,6 @@ TEST(ClientClassDef, unparseDef) { // Unparse it std::string expected = "{\n" - "\"comment\": \"" + comment + "\",\n" "\"name\": \"" + name + "\",\n" "\"test\": \"" + test + "\",\n" "\"only-if-required\": true,\n" @@ -512,7 +511,8 @@ TEST(ClientClassDef, unparseDef) { "\"server-hostname\": \"" + sname + "\",\n" "\"boot-file-name\": \"" + filename + "\",\n" "\"option-data\": [ ],\n" - "\"user-context\": { \"bar\": 1 } }\n"; + "\"user-context\": { \"bar\": 1,\n" + "\"comment\": \"" + comment + "\" } }\n"; runToElementTest(expected, *cclass); } diff --git a/src/lib/dhcpsrv/tests/d2_client_unittest.cc b/src/lib/dhcpsrv/tests/d2_client_unittest.cc index 46126ba4d3..5fc2a2b80e 100644 --- a/src/lib/dhcpsrv/tests/d2_client_unittest.cc +++ b/src/lib/dhcpsrv/tests/d2_client_unittest.cc @@ -118,7 +118,6 @@ TEST(D2ClientConfigTest, constructorsAndAccessors) { // Verify what toElement returns. std::string expected = "{\n" - "\"comment\": \"bar\",\n" "\"enable-updates\": true,\n" "\"server-ip\": \"127.0.0.1\",\n" "\"server-port\": 477,\n" @@ -127,7 +126,7 @@ TEST(D2ClientConfigTest, constructorsAndAccessors) { "\"max-queue-size\": 2048,\n" "\"ncr-protocol\": \"UDP\",\n" "\"ncr-format\": \"JSON\",\n" - "\"user-context\": { \"foo\": 1 }\n" + "\"user-context\": { \"foo\": 1, \"comment\": \"bar\" }\n" "}\n"; runToElementTest(expected, *d2_client_config); diff --git a/src/lib/dhcpsrv/tests/host_unittest.cc b/src/lib/dhcpsrv/tests/host_unittest.cc index 3c4c448e69..34ffb4fa05 100644 --- a/src/lib/dhcpsrv/tests/host_unittest.cc +++ b/src/lib/dhcpsrv/tests/host_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2019 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2020 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 @@ -1093,24 +1093,24 @@ TEST_F(HostTest, unparse) { EXPECT_EQ("{ " "\"boot-file-name\": \"\", " "\"client-classes\": [ ], " - "\"comment\": \"a host reservation\", " "\"hostname\": \"myhost.example.com\", " "\"hw-address\": \"01:02:03:04:05:06\", " "\"ip-address\": \"192.0.2.3\", " "\"next-server\": \"0.0.0.0\", " "\"option-data\": [ ], " - "\"server-hostname\": \"\" " + "\"server-hostname\": \"\", " + "\"user-context\": { \"comment\": \"a host reservation\" } " "}", host->toElement4()->str()); EXPECT_EQ("{ " "\"client-classes\": [ ], " - "\"comment\": \"a host reservation\", " "\"hostname\": \"myhost.example.com\", " "\"hw-address\": \"01:02:03:04:05:06\", " "\"ip-addresses\": [ \"2001:db8:1::cafe\", \"2001:db8:1::1\" ], " "\"option-data\": [ ], " - "\"prefixes\": [ \"2001:db8:1:1::/64\", \"2001:db8:1:2::/64\" ] " + "\"prefixes\": [ \"2001:db8:1:1::/64\", \"2001:db8:1:2::/64\" ], " + "\"user-context\": { \"comment\": \"a host reservation\" } " "}", host->toElement6()->str()); @@ -1122,23 +1122,23 @@ TEST_F(HostTest, unparse) { EXPECT_EQ("{ " "\"boot-file-name\": \"\", " "\"client-classes\": [ ], " - "\"comment\": \"a host reservation\", " "\"hostname\": \"\", " "\"hw-address\": \"01:02:03:04:05:06\", " "\"next-server\": \"0.0.0.0\", " "\"option-data\": [ ], " - "\"server-hostname\": \"\" " + "\"server-hostname\": \"\", " + "\"user-context\": { \"comment\": \"a host reservation\" } " "}", host->toElement4()->str()); EXPECT_EQ("{ " "\"client-classes\": [ ], " - "\"comment\": \"a host reservation\", " "\"hostname\": \"\", " "\"hw-address\": \"01:02:03:04:05:06\", " "\"ip-addresses\": [ \"2001:db8:1::cafe\", \"2001:db8:1::1\" ], " "\"option-data\": [ ], " - "\"prefixes\": [ \"2001:db8:1:1::/64\", \"2001:db8:1:2::/64\" ] " + "\"prefixes\": [ \"2001:db8:1:1::/64\", \"2001:db8:1:2::/64\" ], " + "\"user-context\": { \"comment\": \"a host reservation\" } " "}", host->toElement6()->str()); diff --git a/src/lib/dhcpsrv/tests/lease_unittest.cc b/src/lib/dhcpsrv/tests/lease_unittest.cc index 0aa9411746..4b490ec96d 100644 --- a/src/lib/dhcpsrv/tests/lease_unittest.cc +++ b/src/lib/dhcpsrv/tests/lease_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2013-2019 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2013-2020 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 @@ -535,7 +535,7 @@ TEST_F(Lease4Test, toElement) { expected = "{" "\"cltt\": 12345678," - "\"comment\": \"a comment\"," + "\"user-context\": { \"comment\": \"a comment\" }," "\"fqdn-fwd\": true," "\"fqdn-rev\": true," "\"hostname\": \"urania.example.org\"," @@ -1119,7 +1119,7 @@ TEST(Lease6Test, toElementAddress) { expected = "{" "\"cltt\": 12345678," - "\"comment\": \"a comment\"," + "\"user-context\": { \"comment\": \"a comment\" }," "\"duid\": \"00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f\"," "\"fqdn-fwd\": false," "\"fqdn-rev\": false," @@ -1207,15 +1207,17 @@ TEST(Lease6Test, toElementPrefix) { l = lease.toElement(); EXPECT_FALSE(l->contains("hw-address")); EXPECT_FALSE(l->contains("user-context")); - EXPECT_FALSE(l->contains("comment")); // And to finish try with a comment. lease.setContext(Element::fromJSON("{ \"comment\": \"a comment\" }")); l = lease.toElement(); EXPECT_FALSE(l->contains("hw-address")); - EXPECT_FALSE(l->contains("user-context")); - ASSERT_TRUE(l->contains("comment")); - EXPECT_EQ("a comment", l->get("comment")->stringValue()); + ConstElementPtr ctx = l->get("user-context"); + ASSERT_TRUE(ctx); + ASSERT_EQ(Element::map, ctx->getType()); + EXPECT_EQ(1, ctx->size()); + ASSERT_TRUE(ctx->contains("comment")); + EXPECT_EQ("a comment", ctx->get("comment")->stringValue()); } // Verify that the IA_NA can be created from JSON. diff --git a/src/lib/dhcpsrv/tests/shared_network_unittest.cc b/src/lib/dhcpsrv/tests/shared_network_unittest.cc index ec93caddd8..ee83c601de 100644 --- a/src/lib/dhcpsrv/tests/shared_network_unittest.cc +++ b/src/lib/dhcpsrv/tests/shared_network_unittest.cc @@ -648,7 +648,6 @@ TEST(SharedNetwork4Test, unparse) { network->add(subnet2); std::string expected = "{\n" - " \"comment\": \"bar\",\n" " \"authoritative\": false,\n" " \"interface\": \"eth1\",\n" " \"match-client-id\": false,\n" @@ -693,7 +692,7 @@ TEST(SharedNetwork4Test, unparse) { " \"valid-lifetime\": 30\n" " }\n" " ],\n" - " \"user-context\": { \"foo\": 1 },\n" + " \"user-context\": { \"comment\": \"bar\", \"foo\": 1 },\n" " \"valid-lifetime\": 200\n" "}\n"; diff --git a/src/lib/process/tests/logging_info_unittests.cc b/src/lib/process/tests/logging_info_unittests.cc index 9040a5d3fb..7a8d1f113f 100644 --- a/src/lib/process/tests/logging_info_unittests.cc +++ b/src/lib/process/tests/logging_info_unittests.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2019 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2020 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 @@ -96,11 +96,14 @@ TEST_F(LoggingInfoTest, defaults) { // Add a user context std::string comment = "\"comment\": \"foo\""; std::string user_context = "{ " + comment + " }"; + std::string user_context_nl = "{\n" + comment + "\n}"; EXPECT_FALSE(info_non_verbose.getContext()); info_non_verbose.setContext(Element::fromJSON(user_context)); ASSERT_TRUE(info_non_verbose.getContext()); EXPECT_EQ(user_context, info_non_verbose.getContext()->str()); - expected = header + comment + ",\n" + begin + "INFO" + dbglvl + "0" + trailer; + expected = header; + expected += "\"user-context\": " + user_context_nl + ",\n"; + expected += begin + "INFO" + dbglvl + "0" + trailer; runToElementTest(expected, info_non_verbose); Daemon::setVerbose(true); @@ -123,7 +126,9 @@ TEST_F(LoggingInfoTest, defaults) { info_verbose.setContext(Element::fromJSON(user_context)); ASSERT_TRUE(info_verbose.getContext()); EXPECT_EQ(user_context, info_verbose.getContext()->str()); - expected = header + comment + ",\n" + begin + "DEBUG" + dbglvl + "99" + trailer; + expected = header; + expected += "\"user-context\": " + user_context_nl + ",\n"; + expected += begin + "DEBUG" + dbglvl + "99" + trailer; runToElementTest(expected, info_verbose); } diff --git a/src/lib/testutils/user_context_utils.cc b/src/lib/testutils/user_context_utils.cc index 33799f39b1..bad26bc87c 100644 --- a/src/lib/testutils/user_context_utils.cc +++ b/src/lib/testutils/user_context_utils.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2017-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2017-2020 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 @@ -133,113 +133,5 @@ ConstElementPtr moveComments(ConstElementPtr element) { return (result.get()); } -}; // end of isc::test namespace -}; // end of isc namespace - -namespace { - -/// @brief Recursive helper -/// -/// Instead of returning a unmodified copy a @ref UnModified exception -/// is raised with the unmodified value -/// -/// @tparam EP ElementPtr or ConstElementPtr (compiler will infer which one) -/// @param element the element to traverse -/// @return a modified copy where comment entries were moved from user-context -/// or the unmodified original argument encapsulated into a Value -template -Value extractComments1(EP element) { - bool modified = false; - - // On lists recurse on items - if (element->getType() == Element::list) { - ElementPtr result = ElementPtr(new ListElement()); - typedef std::vector ListType; - const ListType& list = element->listValue(); - for (ListType::const_iterator it = list.cbegin(); - it != list.cend(); ++it) { - Value item = extractComments1(*it); - result->add(item.get()); - if (!item.isShared()) { - modified = true; - } - } - if (!modified) { - return (Value::mkShare(element)); - } else { - return (Value::mkCopy(result)); - } - } else if (element->getType() != Element::map) { - return (Value::mkShare(element)); - } - - // Process maps: recurse on items - ElementPtr result = ElementPtr(new MapElement()); - bool has_comment = false; - typedef std::map map_type; - const map_type& map = element->mapValue(); - for (map_type::const_iterator it = map.cbegin(); it != map.cend(); ++it) { - if (it->first == "comment") { - // Do not traverse comment entries - result->set("comment", it->second); - } else if (it->first == "user-context") { - if (it->second->contains("comment")) { - // Note there is a entry to move - has_comment = true; - } else { - // Do not traverse user-context entries - result->set("user-context", it->second); - } - } else { - // Not comment or user-context - Value item = extractComments1(it->second); - result->set(it->first, item.get()); - if (!item.isShared()) { - modified = true; - } - } - } - // Check if the value should be not modified - if (!has_comment && !modified) { - return (Value::mkShare(element)); - } - - if (has_comment) { - // Move the comment entry - const map_type& uc_map = element->get("user-context")->mapValue(); - ElementPtr user_context = Element::createMap(); - ConstElementPtr comment; - for (map_type::const_iterator it = uc_map.cbegin(); - it != uc_map.cend(); ++it) { - if (it->first == "comment") { - comment = it->second; - } else { - user_context->set(it->first, it->second); - } - } - if (user_context->size() > 0) { - result->set("user-context", user_context); - } - result->set("comment", comment); - } - - return (Value::mkCopy(result)); -} - -} // anonymous namespace - -namespace isc { -namespace test { - -ElementPtr extractComments(ElementPtr element) { - Value result = extractComments1(element); - return (result.get()); -} - -ConstElementPtr extractComments(ConstElementPtr element) { - Value result = extractComments1(element); - return (result.get()); -} - -}; // end of isc::test namespace -}; // end of isc namespace +} // end of isc::test namespace +} // end of isc namespace diff --git a/src/lib/testutils/user_context_utils.h b/src/lib/testutils/user_context_utils.h index 498efe4805..3a04f41b0b 100644 --- a/src/lib/testutils/user_context_utils.h +++ b/src/lib/testutils/user_context_utils.h @@ -1,4 +1,4 @@ -// Copyright (C) 2017-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2017-2020 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 @@ -29,23 +29,7 @@ isc::data::ElementPtr moveComments(isc::data::ElementPtr element); /// @return a processed copy of element or unmodified element isc::data::ConstElementPtr moveComments(isc::data::ConstElementPtr element); -/// @brief Extract comment entries from user-context -/// -/// Process an element looking for user-context entries carrying -/// comments and moving these comments at the same level than -/// user-contexts. As the common case is no comment and this routine -/// tries to maximize sharing the standard behavior is just to return -/// the argument unchanged. -/// -/// @param element -/// @return a processed copy of element or unmodified element -isc::data::ElementPtr extractComments(isc::data::ElementPtr element); - -/// @brief Extract comment entries from user-context (const variant) -/// -/// @param element -/// @return a processed copy of element or unmodified element -isc::data::ConstElementPtr extractComments(isc::data::ConstElementPtr element); +/// extractComments was removed. }; // end of isc::test namespace }; // end of isc namespace