unit-unicode2.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++ (supporting code)
  3. // | | |__ | | | | | | version 3.11.2
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  7. // SPDX-License-Identifier: MIT
  8. #include "doctest_compatibility.h"
  9. // for some reason including this after the json header leads to linker errors with VS 2017...
  10. #include <locale>
  11. #include <nlohmann/json.hpp>
  12. using nlohmann::json;
  13. #include <fstream>
  14. #include <sstream>
  15. #include <iostream>
  16. #include <iomanip>
  17. #include "make_test_data_available.hpp"
  18. // this test suite uses static variables with non-trivial destructors
  19. DOCTEST_CLANG_SUPPRESS_WARNING_PUSH
  20. DOCTEST_CLANG_SUPPRESS_WARNING("-Wexit-time-destructors")
  21. namespace
  22. {
  23. extern size_t calls;
  24. size_t calls = 0;
  25. void check_utf8dump(bool success_expected, int byte1, int byte2, int byte3, int byte4);
  26. void check_utf8dump(bool success_expected, int byte1, int byte2 = -1, int byte3 = -1, int byte4 = -1)
  27. {
  28. static std::string json_string;
  29. json_string.clear();
  30. CAPTURE(byte1)
  31. CAPTURE(byte2)
  32. CAPTURE(byte3)
  33. CAPTURE(byte4)
  34. json_string += std::string(1, static_cast<char>(byte1));
  35. if (byte2 != -1)
  36. {
  37. json_string += std::string(1, static_cast<char>(byte2));
  38. }
  39. if (byte3 != -1)
  40. {
  41. json_string += std::string(1, static_cast<char>(byte3));
  42. }
  43. if (byte4 != -1)
  44. {
  45. json_string += std::string(1, static_cast<char>(byte4));
  46. }
  47. CAPTURE(json_string)
  48. // store the string in a JSON value
  49. static json j;
  50. static json j2;
  51. j = json_string;
  52. j2 = "abc" + json_string + "xyz";
  53. static std::string s_ignored;
  54. static std::string s_ignored2;
  55. static std::string s_ignored_ascii;
  56. static std::string s_ignored2_ascii;
  57. static std::string s_replaced;
  58. static std::string s_replaced2;
  59. static std::string s_replaced_ascii;
  60. static std::string s_replaced2_ascii;
  61. // dumping with ignore/replace must not throw in any case
  62. s_ignored = j.dump(-1, ' ', false, json::error_handler_t::ignore);
  63. s_ignored2 = j2.dump(-1, ' ', false, json::error_handler_t::ignore);
  64. s_ignored_ascii = j.dump(-1, ' ', true, json::error_handler_t::ignore);
  65. s_ignored2_ascii = j2.dump(-1, ' ', true, json::error_handler_t::ignore);
  66. s_replaced = j.dump(-1, ' ', false, json::error_handler_t::replace);
  67. s_replaced2 = j2.dump(-1, ' ', false, json::error_handler_t::replace);
  68. s_replaced_ascii = j.dump(-1, ' ', true, json::error_handler_t::replace);
  69. s_replaced2_ascii = j2.dump(-1, ' ', true, json::error_handler_t::replace);
  70. if (success_expected)
  71. {
  72. static std::string s_strict;
  73. // strict mode must not throw if success is expected
  74. s_strict = j.dump();
  75. // all dumps should agree on the string
  76. CHECK(s_strict == s_ignored);
  77. CHECK(s_strict == s_replaced);
  78. }
  79. else
  80. {
  81. // strict mode must throw if success is not expected
  82. CHECK_THROWS_AS(j.dump(), json::type_error&);
  83. // ignore and replace must create different dumps
  84. CHECK(s_ignored != s_replaced);
  85. // check that replace string contains a replacement character
  86. CHECK(s_replaced.find("\xEF\xBF\xBD") != std::string::npos);
  87. }
  88. // check that prefix and suffix are preserved
  89. CHECK(s_ignored2.substr(1, 3) == "abc");
  90. CHECK(s_ignored2.substr(s_ignored2.size() - 4, 3) == "xyz");
  91. CHECK(s_ignored2_ascii.substr(1, 3) == "abc");
  92. CHECK(s_ignored2_ascii.substr(s_ignored2_ascii.size() - 4, 3) == "xyz");
  93. CHECK(s_replaced2.substr(1, 3) == "abc");
  94. CHECK(s_replaced2.substr(s_replaced2.size() - 4, 3) == "xyz");
  95. CHECK(s_replaced2_ascii.substr(1, 3) == "abc");
  96. CHECK(s_replaced2_ascii.substr(s_replaced2_ascii.size() - 4, 3) == "xyz");
  97. }
  98. void check_utf8string(bool success_expected, int byte1, int byte2, int byte3, int byte4);
  99. // create and check a JSON string with up to four UTF-8 bytes
  100. void check_utf8string(bool success_expected, int byte1, int byte2 = -1, int byte3 = -1, int byte4 = -1)
  101. {
  102. if (++calls % 100000 == 0)
  103. {
  104. std::cout << calls << " of 455355 UTF-8 strings checked" << std::endl;
  105. }
  106. static std::string json_string;
  107. json_string = "\"";
  108. CAPTURE(byte1)
  109. json_string += std::string(1, static_cast<char>(byte1));
  110. if (byte2 != -1)
  111. {
  112. CAPTURE(byte2)
  113. json_string += std::string(1, static_cast<char>(byte2));
  114. }
  115. if (byte3 != -1)
  116. {
  117. CAPTURE(byte3)
  118. json_string += std::string(1, static_cast<char>(byte3));
  119. }
  120. if (byte4 != -1)
  121. {
  122. CAPTURE(byte4)
  123. json_string += std::string(1, static_cast<char>(byte4));
  124. }
  125. json_string += "\"";
  126. CAPTURE(json_string)
  127. json _;
  128. if (success_expected)
  129. {
  130. CHECK_NOTHROW(_ = json::parse(json_string));
  131. }
  132. else
  133. {
  134. CHECK_THROWS_AS(_ = json::parse(json_string), json::parse_error&);
  135. }
  136. }
  137. } // namespace
  138. TEST_CASE("Unicode (2/5)" * doctest::skip())
  139. {
  140. SECTION("RFC 3629")
  141. {
  142. /*
  143. RFC 3629 describes in Sect. 4 the syntax of UTF-8 byte sequences as
  144. follows:
  145. A UTF-8 string is a sequence of octets representing a sequence of UCS
  146. characters. An octet sequence is valid UTF-8 only if it matches the
  147. following syntax, which is derived from the rules for encoding UTF-8
  148. and is expressed in the ABNF of [RFC2234].
  149. UTF8-octets = *( UTF8-char )
  150. UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
  151. UTF8-1 = %x00-7F
  152. UTF8-2 = %xC2-DF UTF8-tail
  153. UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
  154. %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
  155. UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
  156. %xF4 %x80-8F 2( UTF8-tail )
  157. UTF8-tail = %x80-BF
  158. */
  159. SECTION("ill-formed first byte")
  160. {
  161. for (int byte1 = 0x80; byte1 <= 0xC1; ++byte1)
  162. {
  163. check_utf8string(false, byte1);
  164. check_utf8dump(false, byte1);
  165. }
  166. for (int byte1 = 0xF5; byte1 <= 0xFF; ++byte1)
  167. {
  168. check_utf8string(false, byte1);
  169. check_utf8dump(false, byte1);
  170. }
  171. }
  172. SECTION("UTF8-1 (x00-x7F)")
  173. {
  174. SECTION("well-formed")
  175. {
  176. for (int byte1 = 0x00; byte1 <= 0x7F; ++byte1)
  177. {
  178. // unescaped control characters are parse errors in JSON
  179. if (0x00 <= byte1 && byte1 <= 0x1F)
  180. {
  181. check_utf8string(false, byte1);
  182. continue;
  183. }
  184. // a single quote is a parse error in JSON
  185. if (byte1 == 0x22)
  186. {
  187. check_utf8string(false, byte1);
  188. continue;
  189. }
  190. // a single backslash is a parse error in JSON
  191. if (byte1 == 0x5C)
  192. {
  193. check_utf8string(false, byte1);
  194. continue;
  195. }
  196. // all other characters are OK
  197. check_utf8string(true, byte1);
  198. check_utf8dump(true, byte1);
  199. }
  200. }
  201. }
  202. SECTION("UTF8-2 (xC2-xDF UTF8-tail)")
  203. {
  204. SECTION("well-formed")
  205. {
  206. for (int byte1 = 0xC2; byte1 <= 0xDF; ++byte1)
  207. {
  208. for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
  209. {
  210. check_utf8string(true, byte1, byte2);
  211. check_utf8dump(true, byte1, byte2);
  212. }
  213. }
  214. }
  215. SECTION("ill-formed: missing second byte")
  216. {
  217. for (int byte1 = 0xC2; byte1 <= 0xDF; ++byte1)
  218. {
  219. check_utf8string(false, byte1);
  220. check_utf8dump(false, byte1);
  221. }
  222. }
  223. SECTION("ill-formed: wrong second byte")
  224. {
  225. for (int byte1 = 0xC2; byte1 <= 0xDF; ++byte1)
  226. {
  227. for (int byte2 = 0x00; byte2 <= 0xFF; ++byte2)
  228. {
  229. // skip correct second byte
  230. if (0x80 <= byte2 && byte2 <= 0xBF)
  231. {
  232. continue;
  233. }
  234. check_utf8string(false, byte1, byte2);
  235. check_utf8dump(false, byte1, byte2);
  236. }
  237. }
  238. }
  239. }
  240. SECTION("UTF8-3 (xE0 xA0-BF UTF8-tail)")
  241. {
  242. SECTION("well-formed")
  243. {
  244. for (int byte1 = 0xE0; byte1 <= 0xE0; ++byte1)
  245. {
  246. for (int byte2 = 0xA0; byte2 <= 0xBF; ++byte2)
  247. {
  248. for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
  249. {
  250. check_utf8string(true, byte1, byte2, byte3);
  251. check_utf8dump(true, byte1, byte2, byte3);
  252. }
  253. }
  254. }
  255. }
  256. SECTION("ill-formed: missing second byte")
  257. {
  258. for (int byte1 = 0xE0; byte1 <= 0xE0; ++byte1)
  259. {
  260. check_utf8string(false, byte1);
  261. check_utf8dump(false, byte1);
  262. }
  263. }
  264. SECTION("ill-formed: missing third byte")
  265. {
  266. for (int byte1 = 0xE0; byte1 <= 0xE0; ++byte1)
  267. {
  268. for (int byte2 = 0xA0; byte2 <= 0xBF; ++byte2)
  269. {
  270. check_utf8string(false, byte1, byte2);
  271. check_utf8dump(false, byte1, byte2);
  272. }
  273. }
  274. }
  275. SECTION("ill-formed: wrong second byte")
  276. {
  277. for (int byte1 = 0xE0; byte1 <= 0xE0; ++byte1)
  278. {
  279. for (int byte2 = 0x00; byte2 <= 0xFF; ++byte2)
  280. {
  281. // skip correct second byte
  282. if (0xA0 <= byte2 && byte2 <= 0xBF)
  283. {
  284. continue;
  285. }
  286. for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
  287. {
  288. check_utf8string(false, byte1, byte2, byte3);
  289. check_utf8dump(false, byte1, byte2, byte3);
  290. }
  291. }
  292. }
  293. }
  294. SECTION("ill-formed: wrong third byte")
  295. {
  296. for (int byte1 = 0xE0; byte1 <= 0xE0; ++byte1)
  297. {
  298. for (int byte2 = 0xA0; byte2 <= 0xBF; ++byte2)
  299. {
  300. for (int byte3 = 0x00; byte3 <= 0xFF; ++byte3)
  301. {
  302. // skip correct third byte
  303. if (0x80 <= byte3 && byte3 <= 0xBF)
  304. {
  305. continue;
  306. }
  307. check_utf8string(false, byte1, byte2, byte3);
  308. check_utf8dump(false, byte1, byte2, byte3);
  309. }
  310. }
  311. }
  312. }
  313. }
  314. SECTION("UTF8-3 (xE1-xEC UTF8-tail UTF8-tail)")
  315. {
  316. SECTION("well-formed")
  317. {
  318. for (int byte1 = 0xE1; byte1 <= 0xEC; ++byte1)
  319. {
  320. for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
  321. {
  322. for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
  323. {
  324. check_utf8string(true, byte1, byte2, byte3);
  325. check_utf8dump(true, byte1, byte2, byte3);
  326. }
  327. }
  328. }
  329. }
  330. SECTION("ill-formed: missing second byte")
  331. {
  332. for (int byte1 = 0xE1; byte1 <= 0xEC; ++byte1)
  333. {
  334. check_utf8string(false, byte1);
  335. check_utf8dump(false, byte1);
  336. }
  337. }
  338. SECTION("ill-formed: missing third byte")
  339. {
  340. for (int byte1 = 0xE1; byte1 <= 0xEC; ++byte1)
  341. {
  342. for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
  343. {
  344. check_utf8string(false, byte1, byte2);
  345. check_utf8dump(false, byte1, byte2);
  346. }
  347. }
  348. }
  349. SECTION("ill-formed: wrong second byte")
  350. {
  351. for (int byte1 = 0xE1; byte1 <= 0xEC; ++byte1)
  352. {
  353. for (int byte2 = 0x00; byte2 <= 0xFF; ++byte2)
  354. {
  355. // skip correct second byte
  356. if (0x80 <= byte2 && byte2 <= 0xBF)
  357. {
  358. continue;
  359. }
  360. for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
  361. {
  362. check_utf8string(false, byte1, byte2, byte3);
  363. check_utf8dump(false, byte1, byte2, byte3);
  364. }
  365. }
  366. }
  367. }
  368. SECTION("ill-formed: wrong third byte")
  369. {
  370. for (int byte1 = 0xE1; byte1 <= 0xEC; ++byte1)
  371. {
  372. for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
  373. {
  374. for (int byte3 = 0x00; byte3 <= 0xFF; ++byte3)
  375. {
  376. // skip correct third byte
  377. if (0x80 <= byte3 && byte3 <= 0xBF)
  378. {
  379. continue;
  380. }
  381. check_utf8string(false, byte1, byte2, byte3);
  382. check_utf8dump(false, byte1, byte2, byte3);
  383. }
  384. }
  385. }
  386. }
  387. }
  388. SECTION("UTF8-3 (xED x80-9F UTF8-tail)")
  389. {
  390. SECTION("well-formed")
  391. {
  392. for (int byte1 = 0xED; byte1 <= 0xED; ++byte1)
  393. {
  394. for (int byte2 = 0x80; byte2 <= 0x9F; ++byte2)
  395. {
  396. for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
  397. {
  398. check_utf8string(true, byte1, byte2, byte3);
  399. check_utf8dump(true, byte1, byte2, byte3);
  400. }
  401. }
  402. }
  403. }
  404. SECTION("ill-formed: missing second byte")
  405. {
  406. for (int byte1 = 0xED; byte1 <= 0xED; ++byte1)
  407. {
  408. check_utf8string(false, byte1);
  409. check_utf8dump(false, byte1);
  410. }
  411. }
  412. SECTION("ill-formed: missing third byte")
  413. {
  414. for (int byte1 = 0xED; byte1 <= 0xED; ++byte1)
  415. {
  416. for (int byte2 = 0x80; byte2 <= 0x9F; ++byte2)
  417. {
  418. check_utf8string(false, byte1, byte2);
  419. check_utf8dump(false, byte1, byte2);
  420. }
  421. }
  422. }
  423. SECTION("ill-formed: wrong second byte")
  424. {
  425. for (int byte1 = 0xED; byte1 <= 0xED; ++byte1)
  426. {
  427. for (int byte2 = 0x00; byte2 <= 0xFF; ++byte2)
  428. {
  429. // skip correct second byte
  430. if (0x80 <= byte2 && byte2 <= 0x9F)
  431. {
  432. continue;
  433. }
  434. for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
  435. {
  436. check_utf8string(false, byte1, byte2, byte3);
  437. check_utf8dump(false, byte1, byte2, byte3);
  438. }
  439. }
  440. }
  441. }
  442. SECTION("ill-formed: wrong third byte")
  443. {
  444. for (int byte1 = 0xED; byte1 <= 0xED; ++byte1)
  445. {
  446. for (int byte2 = 0x80; byte2 <= 0x9F; ++byte2)
  447. {
  448. for (int byte3 = 0x00; byte3 <= 0xFF; ++byte3)
  449. {
  450. // skip correct third byte
  451. if (0x80 <= byte3 && byte3 <= 0xBF)
  452. {
  453. continue;
  454. }
  455. check_utf8string(false, byte1, byte2, byte3);
  456. check_utf8dump(false, byte1, byte2, byte3);
  457. }
  458. }
  459. }
  460. }
  461. }
  462. SECTION("UTF8-3 (xEE-xEF UTF8-tail UTF8-tail)")
  463. {
  464. SECTION("well-formed")
  465. {
  466. for (int byte1 = 0xEE; byte1 <= 0xEF; ++byte1)
  467. {
  468. for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
  469. {
  470. for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
  471. {
  472. check_utf8string(true, byte1, byte2, byte3);
  473. check_utf8dump(true, byte1, byte2, byte3);
  474. }
  475. }
  476. }
  477. }
  478. SECTION("ill-formed: missing second byte")
  479. {
  480. for (int byte1 = 0xEE; byte1 <= 0xEF; ++byte1)
  481. {
  482. check_utf8string(false, byte1);
  483. check_utf8dump(false, byte1);
  484. }
  485. }
  486. SECTION("ill-formed: missing third byte")
  487. {
  488. for (int byte1 = 0xEE; byte1 <= 0xEF; ++byte1)
  489. {
  490. for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
  491. {
  492. check_utf8string(false, byte1, byte2);
  493. check_utf8dump(false, byte1, byte2);
  494. }
  495. }
  496. }
  497. SECTION("ill-formed: wrong second byte")
  498. {
  499. for (int byte1 = 0xEE; byte1 <= 0xEF; ++byte1)
  500. {
  501. for (int byte2 = 0x00; byte2 <= 0xFF; ++byte2)
  502. {
  503. // skip correct second byte
  504. if (0x80 <= byte2 && byte2 <= 0xBF)
  505. {
  506. continue;
  507. }
  508. for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
  509. {
  510. check_utf8string(false, byte1, byte2, byte3);
  511. check_utf8dump(false, byte1, byte2, byte3);
  512. }
  513. }
  514. }
  515. }
  516. SECTION("ill-formed: wrong third byte")
  517. {
  518. for (int byte1 = 0xEE; byte1 <= 0xEF; ++byte1)
  519. {
  520. for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
  521. {
  522. for (int byte3 = 0x00; byte3 <= 0xFF; ++byte3)
  523. {
  524. // skip correct third byte
  525. if (0x80 <= byte3 && byte3 <= 0xBF)
  526. {
  527. continue;
  528. }
  529. check_utf8string(false, byte1, byte2, byte3);
  530. check_utf8dump(false, byte1, byte2, byte3);
  531. }
  532. }
  533. }
  534. }
  535. }
  536. }
  537. }
  538. DOCTEST_CLANG_SUPPRESS_WARNING_POP