Browse Source

[5014_phase2] More tests and fixes

Francis Dupont 8 years ago
parent
commit
2508dd159f
2 changed files with 150 additions and 12 deletions
  1. 10 7
      src/bin/dhcp6/dhcp6_lexer.ll
  2. 140 5
      src/bin/dhcp6/tests/parser_unittest.cc

+ 10 - 7
src/bin/dhcp6/dhcp6_lexer.ll

@@ -110,7 +110,6 @@ JSONString                              \"{JSONStringCharacter}*\"
 
     if (start_token_flag) {
         start_token_flag = false;
-        BEGIN(0);
         switch (start_token_value) {
         case Parser6Context::PARSER_DHCP6:
             return isc::dhcp::Dhcp6Parser::make_TOPLEVEL_DHCP6(loc);
@@ -780,10 +779,6 @@ JSONString                              \"{JSONStringCharacter}*\"
     std::string tmp(yytext);
     double fp = 0.0;
     try {
-        // In substring we want to use negative values (e.g. -1).
-        // In enterprise-id we need to use values up to 0xffffffff.
-        // To cover both of those use cases, we need at least
-        // int64_t.
         fp = boost::lexical_cast<double>(tmp);
     } catch (const boost::bad_lexical_cast &) {
         driver.error(loc, "Failed to convert " + tmp + " to a floating point.");
@@ -825,6 +820,10 @@ using namespace isc::dhcp;
 void
 Parser6Context::scanStringBegin(const std::string& str, ParserType parser_type)
 {
+    locs.clear();
+    files.clear();
+    states.clear();
+    static_cast<void>(parser6_lex_destroy());
     start_token_flag = true;
     start_token_value = parser_type;
 
@@ -848,8 +847,12 @@ Parser6Context::scanStringEnd()
 void
 Parser6Context::scanFileBegin(FILE * f,
                               const std::string& filename,
-                              ParserType parser_type) {
-
+                              ParserType parser_type)
+{
+    locs.clear();
+    files.clear();
+    states.clear();
+    static_cast<void>(parser6_lex_destroy());
     start_token_flag = true;
     start_token_value = parser_type;
 

+ 140 - 5
src/bin/dhcp6/tests/parser_unittest.cc

@@ -264,8 +264,9 @@ void testError(const std::string& txt,
     }
 }
 
-// Check lexer errors
-TEST(ParserTest, lexerErrors) {
+// Check errors
+TEST(ParserTest, errors) {
+    // no input
     testError("",
               Parser6Context::PARSER_GENERIC_JSON,
               "<string>:1.1: syntax error, unexpected end of file, "
@@ -278,6 +279,8 @@ TEST(ParserTest, lexerErrors) {
               Parser6Context::PARSER_GENERIC_JSON,
               "<string>:2.1: syntax error, unexpected end of file, "
               "expecting {");
+
+    // comments
     testError("# nothing\n",
               Parser6Context::PARSER_GENERIC_JSON,
               "<string>:2.1: syntax error, unexpected end of file, "
@@ -305,10 +308,21 @@ TEST(ParserTest, lexerErrors) {
     testError("/* nothing\n",
               Parser6Context::PARSER_GENERIC_JSON,
               "Comment not closed. (/* in line 1");
-    fprintf(stderr, "got 124?\n");
-    testError("/**/\n\n\n/* nothing\n",
+    testError("\n\n\n/* nothing\n",
               Parser6Context::PARSER_GENERIC_JSON,
               "Comment not closed. (/* in line 4");
+    testError("{ /* */*/ }\n",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.3-8: Invalid character: *");
+    testError("{ /* // *// }\n",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.3-11: Invalid character: /");
+    testError("{ /* // *///  }\n",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:2.1: syntax error, unexpected end of file, "
+              "expecting }");
+
+    // includes
     testError("<?\n",
               Parser6Context::PARSER_GENERIC_JSON,
               "Directive not closed.");
@@ -316,10 +330,131 @@ TEST(ParserTest, lexerErrors) {
               Parser6Context::PARSER_GENERIC_JSON,
               "Directive not closed.");
     string file = string(CFG_EXAMPLES) + "/" + "stateless.json";
-    fprintf(stderr, "doing include\n");
     testError("<?include \"" + file + "\"\n",
               Parser6Context::PARSER_GENERIC_JSON,
               "Directive not closed.");
+    testError("<?include \"/foo/bar\" ?>/n",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "Can't open include file /foo/bar");
+
+    // numbers
+    testError("123",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.1-3: syntax error, unexpected integer, "
+              "expecting {");
+    testError("-456",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.1-4: syntax error, unexpected integer, "
+              "expecting {");
+    testError("-0001",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.1-5: syntax error, unexpected integer, "
+              "expecting {");
+    testError("1234567890123456789012345678901234567890",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.1-40: Failed to convert "
+              "1234567890123456789012345678901234567890"
+              " to an integer.");
+    testError("-3.14e+0",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.1-8: syntax error, unexpected floating point, "
+              "expecting {");
+    testError("1e50000",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.1-7: Failed to convert 1e50000 "
+              "to a floating point.");
+
+    // strings
+    testError("\"aabb\"",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.1-6: syntax error, unexpected constant string, "
+              "expecting {");
+    testError("{ \"aabb\"err",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.9: Invalid character: e");
+    testError("{ err\"aabb\"",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.3: Invalid character: e");
+    testError("\"a\n\tb\"",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.1-6: syntax error, unexpected constant string, "
+              "expecting {");
+    testError("\"a\\n\\tb\"",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.1-8: syntax error, unexpected constant string, "
+              "expecting {");
+    testError("\"a\\x01b\"",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.1: Invalid character: \"");
+    testError("\"a\\u0062\"",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.1-9: syntax error, unexpected constant string, "
+              "expecting {");
+    testError("\"a\\u062z\"",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.1: Invalid character: \"");
+
+    // want a map
+    testError("[]\n",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.1: syntax error, unexpected [, "
+              "expecting {");
+    testError("[]\n",
+              Parser6Context::PARSER_DHCP6,
+              "<string>:1.1: syntax error, unexpected [, "
+              "expecting {");
+    testError("{ 123 }\n",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.3-5: syntax error, unexpected integer, "
+              "expecting }");
+    testError("{ 123 }\n",
+              Parser6Context::PARSER_DHCP6,
+              "<string>:1.3-5: syntax error, unexpected integer, "
+              "expecting Dhcp6 or Logging");
+    testError("{ \"foo\" }\n",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.9: syntax error, unexpected }, "
+              "expecting :");
+    testError("{ \"foo\" }\n",
+              Parser6Context::PARSER_DHCP6,
+              "<string>:1.3-7: syntax error, unexpected constant string, "
+              "expecting Dhcp6 or Logging");
+    testError("{ \"Dhcp6\" }\n",
+              Parser6Context::PARSER_DHCP6,
+              "<string>:1.11: syntax error, unexpected }, "
+              "expecting :");
+    testError("{}{}",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.3: syntax error, unexpected {, "
+              "expecting end of file");
+
+    // bad commas
+    testError("{ , }\n",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.3: syntax error, unexpected \",\", "
+              "expecting }");
+    testError("{ , \"foo\":true }\n",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.3: syntax error, unexpected \",\", "
+              "expecting }");
+    testError("{ \"foo\":true, }\n",
+              Parser6Context::PARSER_GENERIC_JSON,
+              "<string>:1.15: syntax error, unexpected }, "
+              "expecting constant string");
+
+    // bad type
+    testError("{ \"Dhcp6\":{\n"
+              "  \"preferred-lifetime\":false }}\n",
+              Parser6Context::PARSER_DHCP6,
+              "<string>:2.24-28: syntax error, unexpected boolean, "
+              "expecting integer");
+
+    // unknown keyword
+    testError("{ \"Dhcp6\":{\n"
+              " \"preferred_lifetime\":600 }}\n",
+              Parser6Context::PARSER_DHCP6,
+              "<string>:2.2-21: got unexpected keyword "
+              "\"preferred_lifetime\" in Dhcp6 map.");
 }
 
 };