]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1423] Addressed comments
authorFrancis Dupont <fdupont@isc.org>
Tue, 20 Oct 2020 10:45:25 +0000 (12:45 +0200)
committerFrancis Dupont <fdupont@isc.org>
Tue, 20 Oct 2020 10:45:25 +0000 (12:45 +0200)
src/bin/agent/tests/parser_unittests.cc
src/bin/d2/tests/parser_unittest.cc
src/bin/d2/tests/parser_unittest.h
src/bin/dhcp4/tests/parser_unittest.cc
src/bin/dhcp6/tests/parser_unittest.cc
src/bin/netconf/tests/parser_unittests.cc

index 125d650de3f8e7bf3b447bba5fc8d8cf734e2141..3d0df1e56a56532c742a71460841f6a4a0efd5ce 100644 (file)
@@ -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;
                     }
 
index 70a372f5b91d93852d03a7d5c9b8822436434564..d6b8c58ab4c36bf031ddb9084a5cd29b3556ca91 100644 (file)
@@ -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<string> 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;
                     }
 
index dec228aa8b722752f659155b1982c06483cd283a..7b22c9d0703210ad7c3684dff0a251060eacaba2 100644 (file)
@@ -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<std::string> KeywordSet;
-
 }
 }
 }
index b387652e675f9de7ba3ad526a9934114b38a0f74..e620fbbc7ff4898e4c44a4e0fdfde0cda4703c83 100644 (file)
@@ -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;
                     }
 
index 750ba034d782df2575517f79b31766339cd15a8d..79b6f2766e09901b630333fd926b794464767f8e 100644 (file)
@@ -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;
                     }
 
index 8cbf85de91f8938a83f05acb1cd7346f27a11038..6cf0b216ee835497c0aa5f38e57ff492cb498f83 100644 (file)
@@ -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;
                     }