parser_unittest.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. // Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <gtest/gtest.h>
  7. #include <cc/data.h>
  8. #include <dhcp4/parser_context.h>
  9. #include <testutils/io_utils.h>
  10. using namespace isc::data;
  11. using namespace isc::test;
  12. using namespace std;
  13. namespace isc {
  14. namespace dhcp {
  15. namespace test {
  16. /// @brief compares two JSON trees
  17. ///
  18. /// If differences are discovered, gtest failure is reported (using EXPECT_EQ)
  19. ///
  20. /// @param a first to be compared
  21. /// @param b second to be compared
  22. void compareJSON(ConstElementPtr a, ConstElementPtr b) {
  23. ASSERT_TRUE(a);
  24. ASSERT_TRUE(b);
  25. EXPECT_EQ(a->str(), b->str());
  26. }
  27. /// @brief Tests if the input string can be parsed with specific parser
  28. ///
  29. /// The input text will be passed to bison parser of specified type.
  30. /// Then the same input text is passed to legacy JSON parser and outputs
  31. /// from both parsers are compared. The legacy comparison can be disabled,
  32. /// if the feature tested is not supported by the old parser (e.g.
  33. /// new comment styles)
  34. ///
  35. /// @param txt text to be compared
  36. /// @param parser_type bison parser type to be instantiated
  37. /// @param compare whether to compare the output with legacy JSON parser
  38. void testParser(const std::string& txt, Parser4Context::ParserType parser_type,
  39. bool compare = true) {
  40. ConstElementPtr test_json;
  41. ASSERT_NO_THROW({
  42. try {
  43. Parser4Context ctx;
  44. test_json = ctx.parseString(txt, parser_type);
  45. } catch (const std::exception &e) {
  46. cout << "EXCEPTION: " << e.what() << endl;
  47. throw;
  48. }
  49. });
  50. if (!compare) {
  51. return;
  52. };
  53. // Now compare if both representations are the same.
  54. ElementPtr reference_json;
  55. ASSERT_NO_THROW(reference_json = Element::fromJSON(txt, true));
  56. compareJSON(reference_json, test_json);
  57. }
  58. TEST(ParserTest, mapInMap) {
  59. string txt = "{ \"xyzzy\": { \"foo\": 123, \"baz\": 456 } }";
  60. testParser(txt, Parser4Context::PARSER_JSON);
  61. }
  62. TEST(ParserTest, listInList) {
  63. string txt = "[ [ \"Britain\", \"Wales\", \"Scotland\" ], "
  64. "[ \"Pomorze\", \"Wielkopolska\", \"Tatry\"] ]";
  65. testParser(txt, Parser4Context::PARSER_JSON);
  66. }
  67. TEST(ParserTest, nestedMaps) {
  68. string txt = "{ \"europe\": { \"UK\": { \"London\": { \"street\": \"221B Baker\" }}}}";
  69. testParser(txt, Parser4Context::PARSER_JSON);
  70. }
  71. TEST(ParserTest, nestedLists) {
  72. string txt = "[ \"half\", [ \"quarter\", [ \"eighth\", [ \"sixteenth\" ]]]]";
  73. testParser(txt, Parser4Context::PARSER_JSON);
  74. }
  75. TEST(ParserTest, listsInMaps) {
  76. string txt = "{ \"constellations\": { \"orion\": [ \"rigel\", \"betelguese\" ], "
  77. "\"cygnus\": [ \"deneb\", \"albireo\"] } }";
  78. testParser(txt, Parser4Context::PARSER_JSON);
  79. }
  80. TEST(ParserTest, mapsInLists) {
  81. string txt = "[ { \"body\": \"earth\", \"gravity\": 1.0 },"
  82. " { \"body\": \"mars\", \"gravity\": 0.376 } ]";
  83. testParser(txt, Parser4Context::PARSER_JSON);
  84. }
  85. TEST(ParserTest, types) {
  86. string txt = "{ \"string\": \"foo\","
  87. "\"integer\": 42,"
  88. "\"boolean\": true,"
  89. "\"map\": { \"foo\": \"bar\" },"
  90. "\"list\": [ 1, 2, 3 ],"
  91. "\"null\": null }";
  92. testParser(txt, Parser4Context::PARSER_JSON);
  93. }
  94. TEST(ParserTest, keywordJSON) {
  95. string txt = "{ \"name\": \"user\","
  96. "\"type\": \"password\","
  97. "\"user\": \"name\","
  98. "\"password\": \"type\" }";
  99. testParser(txt, Parser4Context::PARSER_JSON);
  100. }
  101. TEST(ParserTest, keywordDhcp4) {
  102. string txt = "{ \"Dhcp4\": { \"interfaces-config\": {"
  103. " \"interfaces\": [ \"type\", \"htype\" ] },\n"
  104. "\"rebind-timer\": 2000, \n"
  105. "\"renew-timer\": 1000, \n"
  106. "\"subnet4\": [ { "
  107. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  108. " \"subnet\": \"192.0.2.0/24\", "
  109. " \"interface\": \"test\" } ],\n"
  110. "\"valid-lifetime\": 4000 } }";
  111. testParser(txt, Parser4Context::PARSER_DHCP4);
  112. }
  113. // Tests if bash (#) comments are supported. That's the only comment type that
  114. // was supported by the old parser.
  115. TEST(ParserTest, bashComments) {
  116. string txt= "{ \"Dhcp4\": { \"interfaces-config\": {"
  117. " \"interfaces\": [ \"*\" ]"
  118. "},\n"
  119. "# this is a comment\n"
  120. "\"rebind-timer\": 2000, \n"
  121. "# lots of comments here\n"
  122. "# and here\n"
  123. "\"renew-timer\": 1000, \n"
  124. "\"subnet4\": [ { "
  125. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  126. " \"subnet\": \"192.0.2.0/24\", "
  127. " \"interface\": \"eth0\""
  128. " } ],"
  129. "\"valid-lifetime\": 4000 } }";
  130. testParser(txt, Parser4Context::PARSER_DHCP4, false);
  131. }
  132. // Tests if C++ (//) comments can start anywhere, not just in the first line.
  133. TEST(ParserTest, cppComments) {
  134. string txt= "{ \"Dhcp4\": { \"interfaces-config\": {"
  135. " \"interfaces\": [ \"*\" ]"
  136. "},\n"
  137. "\"rebind-timer\": 2000, // everything after // is ignored\n"
  138. "\"renew-timer\": 1000, // this will be ignored, too\n"
  139. "\"subnet4\": [ { "
  140. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  141. " \"subnet\": \"192.0.2.0/24\", "
  142. " \"interface\": \"eth0\""
  143. " } ],"
  144. "\"valid-lifetime\": 4000 } }";
  145. testParser(txt, Parser4Context::PARSER_DHCP4, false);
  146. }
  147. // Tests if bash (#) comments can start anywhere, not just in the first line.
  148. TEST(ParserTest, bashCommentsInline) {
  149. string txt= "{ \"Dhcp4\": { \"interfaces-config\": {"
  150. " \"interfaces\": [ \"*\" ]"
  151. "},\n"
  152. "\"rebind-timer\": 2000, # everything after # is ignored\n"
  153. "\"renew-timer\": 1000, # this will be ignored, too\n"
  154. "\"subnet4\": [ { "
  155. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  156. " \"subnet\": \"192.0.2.0/24\", "
  157. " \"interface\": \"eth0\""
  158. " } ],"
  159. "\"valid-lifetime\": 4000 } }";
  160. testParser(txt, Parser4Context::PARSER_DHCP4, false);
  161. }
  162. // Tests if multi-line C style comments are handled correctly.
  163. TEST(ParserTest, multilineComments) {
  164. string txt= "{ \"Dhcp4\": { \"interfaces-config\": {"
  165. " \"interfaces\": [ \"*\" ]"
  166. "},\n"
  167. " /* this is a C style comment\n"
  168. "that\n can \n span \n multiple \n lines */ \n"
  169. "\"rebind-timer\": 2000,\n"
  170. "\"renew-timer\": 1000, \n"
  171. "\"subnet4\": [ { "
  172. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ],"
  173. " \"subnet\": \"192.0.2.0/24\", "
  174. " \"interface\": \"eth0\""
  175. " } ],"
  176. "\"valid-lifetime\": 4000 } }";
  177. testParser(txt, Parser4Context::PARSER_DHCP4, false);
  178. }
  179. /// @brief Loads specified example config file
  180. ///
  181. /// This test loads specified example file twice: first, using the legacy
  182. /// JSON file and then second time using bison parser. Two created Element
  183. /// trees are then compared. The input is decommented before it is passed
  184. /// to legacy parser (as legacy support for comments is very limited).
  185. ///
  186. /// @param fname name of the file to be loaded
  187. void testFile(const std::string& fname) {
  188. ElementPtr reference_json;
  189. ConstElementPtr test_json;
  190. string decommented = decommentJSONfile(fname);
  191. cout << "Parsing file " << fname << " (" << decommented << ")" << endl;
  192. EXPECT_NO_THROW(reference_json = Element::fromJSONFile(decommented, true));
  193. // remove the temporary file
  194. EXPECT_NO_THROW(::remove(decommented.c_str()));
  195. EXPECT_NO_THROW(
  196. try {
  197. Parser4Context ctx;
  198. test_json = ctx.parseFile(fname, Parser4Context::PARSER_DHCP4);
  199. } catch (const std::exception &x) {
  200. cout << "EXCEPTION: " << x.what() << endl;
  201. throw;
  202. });
  203. ASSERT_TRUE(reference_json);
  204. ASSERT_TRUE(test_json);
  205. compareJSON(reference_json, test_json);
  206. }
  207. // This test loads all available existing files. Each config is loaded
  208. // twice: first with the existing Element::fromJSONFile() and then
  209. // the second time with Parser4. Both JSON trees are then compared.
  210. TEST(ParserTest, file) {
  211. vector<string> configs = { "advanced.json" ,
  212. "backends.json",
  213. "classify.json",
  214. "dhcpv4-over-dhcpv6.json",
  215. "hooks.json",
  216. "leases-expiration.json",
  217. "multiple-options.json",
  218. "mysql-reservations.json",
  219. "pgsql-reservations.json",
  220. "reservations.json",
  221. "several-subnets.json",
  222. "single-subnet.json",
  223. "with-ddns.json" };
  224. for (int i = 0; i<configs.size(); i++) {
  225. testFile(string(CFG_EXAMPLES) + "/" + configs[i]);
  226. }
  227. }
  228. /// @brief Tests error conditions in Dhcp4Parser
  229. ///
  230. /// @param txt text to be parsed
  231. /// @param parser_type type of the parser to be used in the test
  232. /// @param msg expected content of the exception
  233. void testError(const std::string& txt,
  234. Parser4Context::ParserType parser_type,
  235. const std::string& msg)
  236. {
  237. try {
  238. Parser4Context ctx;
  239. ConstElementPtr parsed = ctx.parseString(txt, parser_type);
  240. FAIL() << "Expected Dhcp4ParseError but nothing was raised (expected: "
  241. << msg << ")";
  242. }
  243. catch (const Dhcp4ParseError& ex) {
  244. EXPECT_EQ(msg, ex.what());
  245. }
  246. catch (...) {
  247. FAIL() << "Expected Dhcp4ParseError but something else was raised";
  248. }
  249. }
  250. // Verify that error conditions are handled correctly.
  251. TEST(ParserTest, errors) {
  252. // no input
  253. testError("", Parser4Context::PARSER_JSON,
  254. "<string>:1.1: syntax error, unexpected end of file");
  255. testError(" ", Parser4Context::PARSER_JSON,
  256. "<string>:1.2: syntax error, unexpected end of file");
  257. testError("\n", Parser4Context::PARSER_JSON,
  258. "<string>:2.1: syntax error, unexpected end of file");
  259. testError("\t", Parser4Context::PARSER_JSON,
  260. "<string>:1.2: syntax error, unexpected end of file");
  261. testError("\r", Parser4Context::PARSER_JSON,
  262. "<string>:1.2: syntax error, unexpected end of file");
  263. // comments
  264. testError("# nothing\n",
  265. Parser4Context::PARSER_JSON,
  266. "<string>:2.1: syntax error, unexpected end of file");
  267. testError(" #\n",
  268. Parser4Context::PARSER_JSON,
  269. "<string>:2.1: syntax error, unexpected end of file");
  270. testError("// nothing\n",
  271. Parser4Context::PARSER_JSON,
  272. "<string>:2.1: syntax error, unexpected end of file");
  273. testError("/* nothing */\n",
  274. Parser4Context::PARSER_JSON,
  275. "<string>:2.1: syntax error, unexpected end of file");
  276. testError("/* no\nthing */\n",
  277. Parser4Context::PARSER_JSON,
  278. "<string>:3.1: syntax error, unexpected end of file");
  279. testError("/* no\nthing */\n\n",
  280. Parser4Context::PARSER_JSON,
  281. "<string>:4.1: syntax error, unexpected end of file");
  282. testError("/* nothing\n",
  283. Parser4Context::PARSER_JSON,
  284. "Comment not closed. (/* in line 1");
  285. testError("\n\n\n/* nothing\n",
  286. Parser4Context::PARSER_JSON,
  287. "Comment not closed. (/* in line 4");
  288. testError("{ /* */*/ }\n",
  289. Parser4Context::PARSER_JSON,
  290. "<string>:1.3-8: Invalid character: *");
  291. testError("{ /* // *// }\n",
  292. Parser4Context::PARSER_JSON,
  293. "<string>:1.3-11: Invalid character: /");
  294. testError("{ /* // */// }\n",
  295. Parser4Context::PARSER_JSON,
  296. "<string>:2.1: syntax error, unexpected end of file, "
  297. "expecting }");
  298. // includes
  299. testError("<?\n",
  300. Parser4Context::PARSER_JSON,
  301. "Directive not closed.");
  302. testError("<?include\n",
  303. Parser4Context::PARSER_JSON,
  304. "Directive not closed.");
  305. string file = string(CFG_EXAMPLES) + "/" + "single-subnet.json";
  306. testError("<?include \"" + file + "\"\n",
  307. Parser4Context::PARSER_JSON,
  308. "Directive not closed.");
  309. testError("<?include \"/foo/bar\" ?>/n",
  310. Parser4Context::PARSER_JSON,
  311. "Can't open include file /foo/bar");
  312. // JSON keywords
  313. testError("{ \"foo\": True }",
  314. Parser4Context::PARSER_JSON,
  315. "<string>:1.10-13: JSON true reserved keyword is lower case only");
  316. testError("{ \"foo\": False }",
  317. Parser4Context::PARSER_JSON,
  318. "<string>:1.10-14: JSON false reserved keyword is lower case only");
  319. testError("{ \"foo\": NULL }",
  320. Parser4Context::PARSER_JSON,
  321. "<string>:1.10-13: JSON null reserved keyword is lower case only");
  322. testError("{ \"foo\": Tru }",
  323. Parser4Context::PARSER_JSON,
  324. "<string>:1.10: Invalid character: T");
  325. testError("{ \"foo\": nul }",
  326. Parser4Context::PARSER_JSON,
  327. "<string>:1.10: Invalid character: n");
  328. // numbers
  329. testError("123",
  330. Parser4Context::PARSER_DHCP4,
  331. "<string>:1.1-3: syntax error, unexpected integer, "
  332. "expecting {");
  333. testError("-456",
  334. Parser4Context::PARSER_DHCP4,
  335. "<string>:1.1-4: syntax error, unexpected integer, "
  336. "expecting {");
  337. testError("-0001",
  338. Parser4Context::PARSER_DHCP4,
  339. "<string>:1.1-5: syntax error, unexpected integer, "
  340. "expecting {");
  341. testError("1234567890123456789012345678901234567890",
  342. Parser4Context::PARSER_JSON,
  343. "<string>:1.1-40: Failed to convert "
  344. "1234567890123456789012345678901234567890"
  345. " to an integer.");
  346. testError("-3.14e+0",
  347. Parser4Context::PARSER_DHCP4,
  348. "<string>:1.1-8: syntax error, unexpected floating point, "
  349. "expecting {");
  350. testError("1e50000",
  351. Parser4Context::PARSER_JSON,
  352. "<string>:1.1-7: Failed to convert 1e50000 "
  353. "to a floating point.");
  354. // strings
  355. testError("\"aabb\"",
  356. Parser4Context::PARSER_DHCP4,
  357. "<string>:1.1-6: syntax error, unexpected constant string, "
  358. "expecting {");
  359. testError("{ \"aabb\"err",
  360. Parser4Context::PARSER_JSON,
  361. "<string>:1.9: Invalid character: e");
  362. testError("{ err\"aabb\"",
  363. Parser4Context::PARSER_JSON,
  364. "<string>:1.3: Invalid character: e");
  365. testError("\"a\n\tb\"",
  366. Parser4Context::PARSER_JSON,
  367. "<string>:1.1-6: Invalid control in \"a\n\tb\"");
  368. testError("\"a\\n\\tb\"",
  369. Parser4Context::PARSER_DHCP4,
  370. "<string>:1.1-8: syntax error, unexpected constant string, "
  371. "expecting {");
  372. testError("\"a\\x01b\"",
  373. Parser4Context::PARSER_JSON,
  374. "<string>:1.1-8: Bad escape in \"a\\x01b\"");
  375. testError("\"a\\u0162\"",
  376. Parser4Context::PARSER_JSON,
  377. "<string>:1.1-9: Unsupported unicode escape in \"a\\u0162\"");
  378. testError("\"a\\u062z\"",
  379. Parser4Context::PARSER_JSON,
  380. "<string>:1.1-9: Bad escape in \"a\\u062z\"");
  381. testError("\"abc\\\"",
  382. Parser4Context::PARSER_JSON,
  383. "<string>:1.1-6: Overflow escape in \"abc\\\"");
  384. // from data_unittest.c
  385. testError("\\a",
  386. Parser4Context::PARSER_JSON,
  387. "<string>:1.1: Invalid character: \\");
  388. testError("\\",
  389. Parser4Context::PARSER_JSON,
  390. "<string>:1.1: Invalid character: \\");
  391. testError("\\\"\\\"",
  392. Parser4Context::PARSER_JSON,
  393. "<string>:1.1: Invalid character: \\");
  394. // want a map
  395. testError("[]\n",
  396. Parser4Context::PARSER_DHCP4,
  397. "<string>:1.1: syntax error, unexpected [, "
  398. "expecting {");
  399. testError("[]\n",
  400. Parser4Context::PARSER_DHCP4,
  401. "<string>:1.1: syntax error, unexpected [, "
  402. "expecting {");
  403. testError("{ 123 }\n",
  404. Parser4Context::PARSER_JSON,
  405. "<string>:1.3-5: syntax error, unexpected integer, "
  406. "expecting }");
  407. testError("{ 123 }\n",
  408. Parser4Context::PARSER_DHCP4,
  409. "<string>:1.3-5: syntax error, unexpected integer");
  410. testError("{ \"foo\" }\n",
  411. Parser4Context::PARSER_JSON,
  412. "<string>:1.9: syntax error, unexpected }, "
  413. "expecting :");
  414. testError("{ \"foo\" }\n",
  415. Parser4Context::PARSER_DHCP4,
  416. "<string>:1.9: syntax error, unexpected }, expecting :");
  417. testError("{ \"foo\":null }\n",
  418. Parser4Context::PARSER_DHCP4,
  419. "<string>:1.3-7: got unexpected keyword "
  420. "\"foo\" in toplevel map.");
  421. testError("{ \"Dhcp4\" }\n",
  422. Parser4Context::PARSER_DHCP4,
  423. "<string>:1.11: syntax error, unexpected }, "
  424. "expecting :");
  425. testError("{ \"Dhcp6\":[]\n",
  426. Parser4Context::PARSER_DHCP4,
  427. "<string>:2.1: syntax error, unexpected end of file, "
  428. "expecting \",\" or }");
  429. testError("{}{}\n",
  430. Parser4Context::PARSER_JSON,
  431. "<string>:1.3: syntax error, unexpected {, "
  432. "expecting end of file");
  433. // bad commas
  434. testError("{ , }\n",
  435. Parser4Context::PARSER_JSON,
  436. "<string>:1.3: syntax error, unexpected \",\", "
  437. "expecting }");
  438. testError("{ , \"foo\":true }\n",
  439. Parser4Context::PARSER_JSON,
  440. "<string>:1.3: syntax error, unexpected \",\", "
  441. "expecting }");
  442. testError("{ \"foo\":true, }\n",
  443. Parser4Context::PARSER_JSON,
  444. "<string>:1.15: syntax error, unexpected }, "
  445. "expecting constant string");
  446. // bad type
  447. testError("{ \"Dhcp4\":{\n"
  448. " \"valid-lifetime\":false }}\n",
  449. Parser4Context::PARSER_DHCP4,
  450. "<string>:2.20-24: syntax error, unexpected boolean, "
  451. "expecting integer");
  452. // unknown keyword
  453. testError("{ \"Dhcp4\":{\n"
  454. " \"valid_lifetime\":600 }}\n",
  455. Parser4Context::PARSER_DHCP4,
  456. "<string>:2.2-17: got unexpected keyword "
  457. "\"valid_lifetime\" in Dhcp4 map.");
  458. }
  459. // Check unicode escapes
  460. TEST(ParserTest, unicodeEscapes) {
  461. ConstElementPtr result;
  462. string json;
  463. // check we can reread output
  464. for (char c = -128; c < 127; ++c) {
  465. string ins(" ");
  466. ins[1] = c;
  467. ConstElementPtr e(new StringElement(ins));
  468. json = e->str();
  469. ASSERT_NO_THROW(
  470. try {
  471. Parser4Context ctx;
  472. result = ctx.parseString(json, Parser4Context::PARSER_JSON);
  473. } catch (const std::exception &x) {
  474. cout << "EXCEPTION: " << x.what() << endl;
  475. throw;
  476. });
  477. ASSERT_EQ(Element::string, result->getType());
  478. EXPECT_EQ(ins, result->stringValue());
  479. }
  480. }
  481. // This test checks that all representations of a slash are recognized properly.
  482. TEST(ParserTest, unicodeSlash) {
  483. // check the 4 possible encodings of solidus '/'
  484. ConstElementPtr result;
  485. string json = "\"/\\/\\u002f\\u002F\"";
  486. ASSERT_NO_THROW(
  487. try {
  488. Parser4Context ctx;
  489. result = ctx.parseString(json, Parser4Context::PARSER_JSON);
  490. } catch (const std::exception &x) {
  491. cout << "EXCEPTION: " << x.what() << endl;
  492. throw;
  493. });
  494. ASSERT_EQ(Element::string, result->getType());
  495. EXPECT_EQ("////", result->stringValue());
  496. }
  497. };
  498. };
  499. };