Browse Source

[5014] Unit-text fixed, all 12 unit-tests pass now.

Tomek Mrugalski 8 years ago
parent
commit
0b9ce9b5b2
3 changed files with 59 additions and 40 deletions
  1. 1 0
      src/bin/dhcp6/dhcp6_lexer.ll
  2. 20 11
      src/bin/dhcp6/dhcp6_parser.yy
  3. 38 29
      src/bin/dhcp6/tests/parser_unittest.cc

+ 1 - 0
src/bin/dhcp6/dhcp6_lexer.ll

@@ -100,6 +100,7 @@ JSONString                              \"{JSONStringCharacter}*\"
         case Parser6Context::PARSER_DHCP6:
             return isc::dhcp::Dhcp6Parser::make_TOPLEVEL_DHCP6(loc);
         case Parser6Context::PARSER_GENERIC_JSON:
+        default:
             return isc::dhcp::Dhcp6Parser::make_TOPLEVEL_GENERIC_JSON(loc);
         }
     }

+ 20 - 11
src/bin/dhcp6/dhcp6_parser.yy

@@ -140,7 +140,7 @@ using namespace std;
 %start start;
 
 start: TOPLEVEL_DHCP6 syntax_map
-| TOPLEVEL_GENERIC_JSON map;
+| TOPLEVEL_GENERIC_JSON map2;
 
 // ---- generic JSON parser ---------------------------------
 
@@ -150,11 +150,11 @@ value : INTEGER { $$ = ElementPtr(new IntElement($1)); }
      | BOOLEAN { $$ = ElementPtr(new BoolElement($1)); }
      | STRING { $$ = ElementPtr(new StringElement($1)); }
      | NULL_TYPE { $$ = ElementPtr(new NullElement()); }
-     | map { $$ = ctx.stack_.back(); ctx.stack_.pop_back(); }
-     | list { $$ = ctx.stack_.back(); ctx.stack_.pop_back(); }
+     | map2 { $$ = ctx.stack_.back(); ctx.stack_.pop_back(); }
+     | list_generic { $$ = ctx.stack_.back(); ctx.stack_.pop_back(); }
     ;
 
-map: LCURLY_BRACKET {
+map2: LCURLY_BRACKET {
     // This code is executed when we're about to start parsing
     // the content of the map
     ElementPtr m(new MapElement());
@@ -177,10 +177,19 @@ map_content:  { /* do nothing, it's an empty map */ }
     }
     ;
 
-list: LSQUARE_BRACKET {
+list_generic: LSQUARE_BRACKET {
+    ElementPtr l(new ListElement());
+    ctx.stack_.push_back(l);
+ } list_content RSQUARE_BRACKET {
+
+ }
+
+// This one is used in syntax parser.
+list2: LSQUARE_BRACKET {
     // List parsing about to start
 } list_content RSQUARE_BRACKET {
     // list parsing complete. Put any sanity checking here
+    //ctx.stack_.pop_back();
 };
 
 list_content: { /* do nothing, it's an empty list */ }
@@ -291,7 +300,7 @@ interface_config_map: INTERFACES {
     ElementPtr l(new ListElement());
     ctx.stack_.back()->set("interfaces", l);
     ctx.stack_.push_back(l);
- } COLON list {
+ } COLON list2 {
      ctx.stack_.pop_back();
  }
 
@@ -386,7 +395,7 @@ relay_supplied_options: RELAY_SUPPLIED_OPTIONS {
     ElementPtr l(new ListElement());
     ctx.stack_.back()->set("relay-supplied-options", l);
     ctx.stack_.push_back(l);
-} COLON list {
+} COLON list2 {
     ctx.stack_.pop_back();
 };
 
@@ -398,7 +407,7 @@ hooks_libraries: HOOKS_LIBRARIES COLON {
     ctx.stack_.pop_back();
 };
 
-hooks_libraries_list: { } 
+hooks_libraries_list: { }
 | hooks_library
 | hooks_libraries_list COMMA hooks_library;
 
@@ -682,7 +691,7 @@ ip_addresses: IP_ADDRESSES COLON {
     ElementPtr l(new ListElement());
     ctx.stack_.back()->set("ip-addresses", l);
     ctx.stack_.push_back(l);
-} list {
+} list2 {
     ctx.stack_.pop_back();
 };
 
@@ -690,7 +699,7 @@ prefixes: PREFIXES COLON  {
     ElementPtr l(new ListElement());
     ctx.stack_.back()->set("prefixes", l);
     ctx.stack_.push_back(l);
-} list {
+} list2 {
     ctx.stack_.pop_back();
 };
 
@@ -710,7 +719,7 @@ reservation_client_classes: CLIENT_CLASSES COLON {
     ElementPtr c(new ListElement());
     ctx.stack_.back()->set("client-classes", c);
     ctx.stack_.push_back(c);
-} list {
+} list2 {
     ctx.stack_.pop_back();
   };
 

+ 38 - 29
src/bin/dhcp6/tests/parser_unittest.cc

@@ -17,9 +17,9 @@ void compareJSON(ConstElementPtr a, ConstElementPtr b, bool print = true) {
     ASSERT_TRUE(a);
     ASSERT_TRUE(b);
     if (print) {
-        std::cout << "JSON A: -----" << endl << a->str() << std::endl;
-        std::cout << "JSON B: -----" << endl << b->str() << std::endl;
-        cout << "---------" << endl << endl;
+        // std::cout << "JSON A: -----" << endl << a->str() << std::endl;
+        // std::cout << "JSON B: -----" << endl << b->str() << std::endl;
+        // cout << "---------" << endl << endl;
     }
     EXPECT_EQ(a->str(), b->str());
 }
@@ -28,10 +28,16 @@ void testParser(const std::string& txt, Parser6Context::ParserType parser_type)
     ElementPtr reference_json;
     ConstElementPtr test_json;
 
-    EXPECT_NO_THROW(reference_json = Element::fromJSON(txt, true));
-    EXPECT_NO_THROW({
+    ASSERT_NO_THROW(reference_json = Element::fromJSON(txt, true));
+    ASSERT_NO_THROW({
+            try {
         Parser6Context ctx;
         test_json = ctx.parseString(txt, parser_type);
+            } catch (const std::exception &e) {
+                cout << "EXCEPTION: " << e.what() << endl;
+                throw;
+            }
+
     });
 
     // Now compare if both representations are the same.
@@ -41,19 +47,24 @@ void testParser(const std::string& txt, Parser6Context::ParserType parser_type)
 void testParser2(const std::string& txt, Parser6Context::ParserType parser_type) {
     ConstElementPtr test_json;
 
-    EXPECT_NO_THROW({
+    ASSERT_NO_THROW({
+            try {
         Parser6Context ctx;
         test_json = ctx.parseString(txt, parser_type);
+            } catch (const std::exception &e) {
+                cout << "EXCEPTION: " << e.what() << endl;
+                throw;
+            }
     });
     /// @todo: Implement actual validation here. since the original
     /// Element::fromJSON does not support several comment types, we don't
     /// have anything to compare with.
-    std::cout << "Original text:" << txt << endl;
-    std::cout << "Parsed text  :" << test_json->str() << endl;
+    /// std::cout << "Original text:" << txt << endl;
+    /// std::cout << "Parsed text  :" << test_json->str() << endl;
 }
 
 TEST(ParserTest, mapInMap) {
-    string txt = "{ \"Dhcp6\": { \"foo\": 123, \"baz\": 456 } }";
+    string txt = "{ \"xyzzy\": { \"foo\": 123, \"baz\": 456 } }";
     testParser(txt, Parser6Context::PARSER_GENERIC_JSON);
 }
 
@@ -80,8 +91,8 @@ TEST(ParserTest, listsInMaps) {
 }
 
 TEST(ParserTest, mapsInLists) {
-    string txt = "{ \"solar-system\": [ { \"name\": \"earth\", \"gravity\": 1.0 },"
-                                      " { \"name\": \"mars\", \"gravity\": 0.376 } ] }";
+    string txt = "{ \"solar-system\": [ { \"body\": \"earth\", \"gravity\": 1.0 },"
+                                      " { \"body\": \"mars\", \"gravity\": 0.376 } ] }";
     testParser(txt, Parser6Context::PARSER_GENERIC_JSON);
 }
 
@@ -96,7 +107,7 @@ TEST(ParserTest, types) {
 }
 
 TEST(ParserTest, bashComments) {
-    string txt= "{ \"interfaces-config\": {"
+    string txt= "{ \"Dhcp6\": { \"interfaces-config\": {"
                 "  \"interfaces\": [ \"*\" ]"
                 "},\n"
                 "\"preferred-lifetime\": 3000,\n"
@@ -108,15 +119,14 @@ TEST(ParserTest, bashComments) {
                 "\"subnet6\": [ { "
                 "    \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
                 "    \"subnet\": \"2001:db8:1::/48\", "
-                "    \"interface-id\": \"\","
                 "    \"interface\": \"eth0\""
                 " } ],"
-                "\"valid-lifetime\": 4000 }";
-    testParser(txt, Parser6Context::PARSER_GENERIC_JSON);
+                "\"valid-lifetime\": 4000 } }";
+    testParser2(txt, Parser6Context::PARSER_DHCP6);
 }
 
 TEST(ParserTest, cComments) {
-    string txt= "{ \"interfaces-config\": {"
+    string txt= "{ \"Dhcp6\": { \"interfaces-config\": {"
                 "  \"interfaces\": [ \"*\" ]"
                 "},\n"
                 "\"preferred-lifetime\": 3000, // this is a comment \n"
@@ -125,15 +135,14 @@ TEST(ParserTest, cComments) {
                 "\"subnet6\": [ { "
                 "    \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
                 "    \"subnet\": \"2001:db8:1::/48\", "
-                "    \"interface-id\": \"\","
                 "    \"interface\": \"eth0\""
                 " } ],"
-                "\"valid-lifetime\": 4000 }";
-    testParser2(txt, Parser6Context::PARSER_GENERIC_JSON);
+                "\"valid-lifetime\": 4000 } }";
+    testParser2(txt, Parser6Context::PARSER_DHCP6);
 }
 
-TEST(ParserTest, bashComments2) {
-    string txt= "{ \"interfaces-config\": {"
+TEST(ParserTest, bashCommentsInline) {
+    string txt= "{ \"Dhcp6\": { \"interfaces-config\": {"
                 "  \"interfaces\": [ \"*\" ]"
                 "},\n"
                 "\"preferred-lifetime\": 3000, # this is a comment \n"
@@ -142,15 +151,14 @@ TEST(ParserTest, bashComments2) {
                 "\"subnet6\": [ { "
                 "    \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
                 "    \"subnet\": \"2001:db8:1::/48\", "
-                "    \"interface-id\": \"\","
                 "    \"interface\": \"eth0\""
                 " } ],"
-                "\"valid-lifetime\": 4000 }";
-    testParser2(txt, Parser6Context::PARSER_GENERIC_JSON);
+                "\"valid-lifetime\": 4000 } }";
+    testParser2(txt, Parser6Context::PARSER_DHCP6);
 }
 
 TEST(ParserTest, multilineComments) {
-    string txt= "{ \"interfaces-config\": {"
+    string txt= "{ \"Dhcp6\": { \"interfaces-config\": {"
                 "  \"interfaces\": [ \"*\" ]"
                 "},\n"
                 "\"preferred-lifetime\": 3000, /* this is a C style comment\n"
@@ -160,11 +168,10 @@ TEST(ParserTest, multilineComments) {
                 "\"subnet6\": [ { "
                 "    \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
                 "    \"subnet\": \"2001:db8:1::/48\", "
-                "    \"interface-id\": \"\","
                 "    \"interface\": \"eth0\""
                 " } ],"
-                "\"valid-lifetime\": 4000 }";
-    testParser2(txt, Parser6Context::PARSER_GENERIC_JSON);
+                "\"valid-lifetime\": 4000 } }";
+    testParser2(txt, Parser6Context::PARSER_DHCP6);
 }
 
 
@@ -176,12 +183,14 @@ void testFile(const std::string& fname, bool print) {
 
     EXPECT_NO_THROW(reference_json = Element::fromJSONFile(fname, true));
 
+    EXPECT_NO_THROW(
     try {
         Parser6Context ctx;
         test_json = ctx.parseFile(fname, Parser6Context::PARSER_DHCP6);
     } catch (const std::exception &x) {
         cout << "EXCEPTION: " << x.what() << endl;
-    }
+        throw;
+    });
 
     ASSERT_TRUE(reference_json);
     ASSERT_TRUE(test_json);