From: Francis Dupont Date: Tue, 20 Oct 2020 10:45:25 +0000 (+0200) Subject: [#1423] Addressed comments X-Git-Tag: Kea-1.9.1~64 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=03000f95fd563c13b59c4dac0995d7db887d0fa0;p=thirdparty%2Fkea.git [#1423] Addressed comments --- diff --git a/src/bin/agent/tests/parser_unittests.cc b/src/bin/agent/tests/parser_unittests.cc index 125d650de3..3d0df1e56a 100644 --- a/src/bin/agent/tests/parser_unittests.cc +++ b/src/bin/agent/tests/parser_unittests.cc @@ -686,6 +686,18 @@ TEST(ParserTest, unicodeSlash) { EXPECT_EQ("////", result->stringValue()); } +/// @brief Load a file into a JSON element. +/// +/// @param fname The name of the file to load. +/// @param list The JSON element list to add the parsing result to. +void loadFile(const string& fname, ElementPtr list) { + ParserContext ctx; + ElementPtr json; + EXPECT_NO_THROW(json = ctx.parseFile(fname, ParserContext::PARSER_AGENT)); + ASSERT_TRUE(json); + list->add(json); +} + // This test checks that all map entries are in the sample file. TEST(ParserTest, mapEntries) { // Type of keyword set. @@ -722,14 +734,11 @@ TEST(ParserTest, mapEntries) { } syntax_file.close(); - // Get keywords from the sample file - string sample_fname(CFG_EXAMPLES); - sample_fname += "/simple.json"; - ParserContext ctx; - ElementPtr sample_json; - EXPECT_NO_THROW(sample_json = - ctx.parseFile(sample_fname, ParserContext::PARSER_AGENT)); - ASSERT_TRUE(sample_json); + // Get keywords from the sample files + string sample_dir(CFG_EXAMPLES); + sample_dir += "/"; + ElementPtr sample_json = Element::createList(); + loadFile(sample_dir + "simple.json", sample_json); KeywordSet sample_keys; // Recursively extract keywords. static void (*extract)(ConstElementPtr, KeywordSet&) = @@ -799,8 +808,8 @@ TEST(ParserTest, duplicateMapEntries) { // Handle maps. for (auto elem : json->mapValue()) { // Skip entries with free content. - if ((elem.first != "user-context") && - (elem.first != "parameters")) { + if ((elem.first == "user-context") || + (elem.first == "parameters")) { continue; } diff --git a/src/bin/d2/tests/parser_unittest.cc b/src/bin/d2/tests/parser_unittest.cc index 70a372f5b9..d6b8c58ab4 100644 --- a/src/bin/d2/tests/parser_unittest.cc +++ b/src/bin/d2/tests/parser_unittest.cc @@ -631,8 +631,23 @@ TEST(ParserTest, unicodeSlash) { EXPECT_EQ("////", result->stringValue()); } +/// @brief Load a file into a JSON element. +/// +/// @param fname The name of the file to load. +/// @param list The JSON element list to add the parsing result to. +void loadFile(const string& fname, ElementPtr list) { + D2ParserContext ctx; + ElementPtr json; + EXPECT_NO_THROW(json = ctx.parseFile(fname, D2ParserContext::PARSER_DHCPDDNS)); + ASSERT_TRUE(json); + list->add(json); +} + // This test checks that all map entries are in the sample file. TEST(ParserTest, mapEntries) { + // Type of keyword set. + typedef set KeywordSet; + // Get keywords from the syntax file (d2_parser.yy). ifstream syntax_file(SYNTAX_FILE); EXPECT_TRUE(syntax_file.is_open()); @@ -665,13 +680,10 @@ TEST(ParserTest, mapEntries) { syntax_file.close(); // Get keywords from the sample file - string sample_fname(D2_TEST_DATA_DIR); - sample_fname += "/get_config.json"; - D2ParserContext ctx; - ElementPtr sample_json; - EXPECT_NO_THROW(sample_json = - ctx.parseFile(sample_fname, D2ParserContext::PARSER_DHCPDDNS)); - ASSERT_TRUE(sample_json); + string sample_dir(D2_TEST_DATA_DIR); + sample_dir += "/"; + ElementPtr sample_json = Element::createList(); + loadFile(sample_dir + "get_config.json", sample_json); KeywordSet sample_keys; // Recursively extract keywords. static void (*extract)(ConstElementPtr, KeywordSet&) = @@ -741,8 +753,8 @@ TEST(ParserTest, duplicateMapEntries) { // Handle maps. for (auto elem : json->mapValue()) { // Skip entries with free content. - if ((elem.first != "user-context") && - (elem.first != "parameters")) { + if ((elem.first == "user-context") || + (elem.first == "parameters")) { continue; } diff --git a/src/bin/d2/tests/parser_unittest.h b/src/bin/d2/tests/parser_unittest.h index dec228aa8b..7b22c9d070 100644 --- a/src/bin/d2/tests/parser_unittest.h +++ b/src/bin/d2/tests/parser_unittest.h @@ -30,9 +30,6 @@ parseJSON(const std::string& in) return (ctx.parseString(in, isc::d2::D2ParserContext::PARSER_JSON)); } -/// @brief Type of keyword set -typedef std::set KeywordSet; - } } } diff --git a/src/bin/dhcp4/tests/parser_unittest.cc b/src/bin/dhcp4/tests/parser_unittest.cc index b387652e67..e620fbbc7f 100644 --- a/src/bin/dhcp4/tests/parser_unittest.cc +++ b/src/bin/dhcp4/tests/parser_unittest.cc @@ -683,6 +683,18 @@ TEST(ParserTest, unicodeSlash) { EXPECT_EQ("////", result->stringValue()); } +/// @brief Load a file into a JSON element. +/// +/// @param fname The name of the file to load. +/// @param list The JSON element list to add the parsing result to. +void loadFile(const string& fname, ElementPtr list) { + Parser4Context ctx; + ElementPtr json; + EXPECT_NO_THROW(json = ctx.parseFile(fname, Parser4Context::PARSER_DHCP4)); + ASSERT_TRUE(json); + list->add(json); +} + // This test checks that all map entries are in the all-keys file. TEST(ParserTest, mapEntries) { // Type of keyword set. @@ -719,16 +731,13 @@ TEST(ParserTest, mapEntries) { } syntax_file.close(); - // Get keywords from the all keys file - string sample_fname(CFG_EXAMPLES); - sample_fname += "/all-keys.json"; - Parser4Context ctx; - ElementPtr sample_json; - EXPECT_NO_THROW(sample_json = - ctx.parseFile(sample_fname, Parser4Context::PARSER_DHCP4)); - ASSERT_TRUE(sample_json); + // Get keywords from the all-keys and reservations file + string sample_dir(CFG_EXAMPLES); + sample_dir += "/"; + ElementPtr sample_json = Element::createList(); + loadFile(sample_dir + "all-keys.json", sample_json); + loadFile(sample_dir + "reservations.json", sample_json); KeywordSet sample_keys = { - "hw-address", "duid", "client-id", "flex-id", "hosts-database" }; // Recursively extract keywords. @@ -815,8 +824,8 @@ TEST(ParserTest, duplicateMapEntries) { // Handle maps. for (auto elem : json->mapValue()) { // Skip entries with free content. - if ((elem.first != "user-context") && - (elem.first != "parameters")) { + if ((elem.first == "user-context") || + (elem.first == "parameters")) { continue; } diff --git a/src/bin/dhcp6/tests/parser_unittest.cc b/src/bin/dhcp6/tests/parser_unittest.cc index 750ba034d7..79b6f2766e 100644 --- a/src/bin/dhcp6/tests/parser_unittest.cc +++ b/src/bin/dhcp6/tests/parser_unittest.cc @@ -672,6 +672,18 @@ TEST(ParserTest, unicodeSlash) { EXPECT_EQ("////", result->stringValue()); } +/// @brief Load a file into a JSON element. +/// +/// @param fname The name of the file to load. +/// @param list The JSON element list to add the parsing result to. +void loadFile(const string& fname, ElementPtr list) { + Parser6Context ctx; + ElementPtr json; + EXPECT_NO_THROW(json = ctx.parseFile(fname, Parser6Context::PARSER_DHCP6)); + ASSERT_TRUE(json); + list->add(json); +} + // This test checks that all map entries are in the all-keys file. TEST(ParserTest, mapEntries) { // Type of keyword set. @@ -708,18 +720,16 @@ TEST(ParserTest, mapEntries) { } syntax_file.close(); - // Get keywords from the all keys file. - string sample_fname(CFG_EXAMPLES); - sample_fname += "/all-keys.json"; - Parser6Context ctx; - ElementPtr sample_json; - EXPECT_NO_THROW(sample_json = - ctx.parseFile(sample_fname, Parser6Context::PARSER_DHCP6)); - ASSERT_TRUE(sample_json); + // Get keywords from the example files. + string sample_dir(CFG_EXAMPLES); + sample_dir += "/"; + ElementPtr sample_json = Element::createList(); + loadFile(sample_dir + "advanced.json", sample_json); + loadFile(sample_dir + "all-keys.json", sample_json); + loadFile(sample_dir + "duid.json", sample_json); + loadFile(sample_dir + "reservations.json", sample_json); KeywordSet sample_keys = { - "hw-address", "flex-id", "hosts-database", - "htype", "ip-address", "time" }; // Recursively extract keywords. static void (*extract)(ConstElementPtr, KeywordSet&) = @@ -805,8 +815,8 @@ TEST(ParserTest, duplicateMapEntries) { // Handle maps. for (auto elem : json->mapValue()) { // Skip entries with free content. - if ((elem.first != "user-context") && - (elem.first != "parameters")) { + if ((elem.first == "user-context") || + (elem.first == "parameters")) { continue; } diff --git a/src/bin/netconf/tests/parser_unittests.cc b/src/bin/netconf/tests/parser_unittests.cc index 8cbf85de91..6cf0b216ee 100644 --- a/src/bin/netconf/tests/parser_unittests.cc +++ b/src/bin/netconf/tests/parser_unittests.cc @@ -742,6 +742,18 @@ TEST(ParserTest, unicodeSlash) { EXPECT_EQ("////", result->stringValue()); } +/// @brief Load a file into a JSON element. +/// +/// @param fname The name of the file to load. +/// @param list The JSON element list to add the parsing result to. +void loadFile(const string& fname, ElementPtr list) { + ParserContext ctx; + ElementPtr json; + EXPECT_NO_THROW(json = ctx.parseFile(fname, ParserContext::PARSER_NETCONF)); + ASSERT_TRUE(json); + list->add(json); +} + // This test checks that all map entries are in the sample file. TEST(ParserTest, mapEntries) { // Type of keyword set. @@ -779,15 +791,13 @@ TEST(ParserTest, mapEntries) { syntax_file.close(); // Get keywords from the sample file - string sample_fname(CFG_EXAMPLES); - sample_fname += "/simple-dhcp4.json"; - ParserContext ctx; - ElementPtr sample_json; - EXPECT_NO_THROW(sample_json = - ctx.parseFile(sample_fname, ParserContext::PARSER_NETCONF)); - ASSERT_TRUE(sample_json); + string sample_dir(CFG_EXAMPLES); + sample_dir += "/"; + ElementPtr sample_json = Element::createList(); + loadFile(sample_dir + "simple-dhcp4.json", sample_json); + loadFile(sample_dir + "simple-dhcp6.json", sample_json); KeywordSet sample_keys = { - "ca", "d2", "dhcp6", + "ca", "d2", "hooks-libraries", "library", "parameters", "socket-url" }; @@ -859,8 +869,8 @@ TEST(ParserTest, duplicateMapEntries) { // Handle maps. for (auto elem : json->mapValue()) { // Skip entries with free content. - if ((elem.first != "user-context") && - (elem.first != "parameters")) { + if ((elem.first == "user-context") || + (elem.first == "parameters")) { continue; }