unit-unicode4.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 5517507 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 (4/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("UTF8-4 (xF1-F3 UTF8-tail UTF8-tail UTF8-tail)")
  160. {
  161. SECTION("well-formed")
  162. {
  163. for (int byte1 = 0xF1; byte1 <= 0xF3; ++byte1)
  164. {
  165. for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
  166. {
  167. for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
  168. {
  169. for (int byte4 = 0x80; byte4 <= 0xBF; ++byte4)
  170. {
  171. check_utf8string(true, byte1, byte2, byte3, byte4);
  172. check_utf8dump(true, byte1, byte2, byte3, byte4);
  173. }
  174. }
  175. }
  176. }
  177. }
  178. SECTION("ill-formed: missing second byte")
  179. {
  180. for (int byte1 = 0xF1; byte1 <= 0xF3; ++byte1)
  181. {
  182. check_utf8string(false, byte1);
  183. check_utf8dump(false, byte1);
  184. }
  185. }
  186. SECTION("ill-formed: missing third byte")
  187. {
  188. for (int byte1 = 0xF1; byte1 <= 0xF3; ++byte1)
  189. {
  190. for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
  191. {
  192. check_utf8string(false, byte1, byte2);
  193. check_utf8dump(false, byte1, byte2);
  194. }
  195. }
  196. }
  197. SECTION("ill-formed: missing fourth byte")
  198. {
  199. for (int byte1 = 0xF1; byte1 <= 0xF3; ++byte1)
  200. {
  201. for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
  202. {
  203. for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
  204. {
  205. check_utf8string(false, byte1, byte2, byte3);
  206. check_utf8dump(false, byte1, byte2, byte3);
  207. }
  208. }
  209. }
  210. }
  211. SECTION("ill-formed: wrong second byte")
  212. {
  213. for (int byte1 = 0xF1; byte1 <= 0xF3; ++byte1)
  214. {
  215. for (int byte2 = 0x00; byte2 <= 0xFF; ++byte2)
  216. {
  217. // skip correct second byte
  218. if (0x80 <= byte2 && byte2 <= 0xBF)
  219. {
  220. continue;
  221. }
  222. for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
  223. {
  224. for (int byte4 = 0x80; byte4 <= 0xBF; ++byte4)
  225. {
  226. check_utf8string(false, byte1, byte2, byte3, byte4);
  227. check_utf8dump(false, byte1, byte2, byte3, byte4);
  228. }
  229. }
  230. }
  231. }
  232. }
  233. SECTION("ill-formed: wrong third byte")
  234. {
  235. for (int byte1 = 0xF1; byte1 <= 0xF3; ++byte1)
  236. {
  237. for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
  238. {
  239. for (int byte3 = 0x00; byte3 <= 0xFF; ++byte3)
  240. {
  241. // skip correct third byte
  242. if (0x80 <= byte3 && byte3 <= 0xBF)
  243. {
  244. continue;
  245. }
  246. for (int byte4 = 0x80; byte4 <= 0xBF; ++byte4)
  247. {
  248. check_utf8string(false, byte1, byte2, byte3, byte4);
  249. check_utf8dump(false, byte1, byte2, byte3, byte4);
  250. }
  251. }
  252. }
  253. }
  254. }
  255. SECTION("ill-formed: wrong fourth byte")
  256. {
  257. for (int byte1 = 0xF1; byte1 <= 0xF3; ++byte1)
  258. {
  259. for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
  260. {
  261. for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
  262. {
  263. for (int byte4 = 0x00; byte4 <= 0xFF; ++byte4)
  264. {
  265. // skip correct fourth byte
  266. if (0x80 <= byte3 && byte3 <= 0xBF)
  267. {
  268. continue;
  269. }
  270. check_utf8string(false, byte1, byte2, byte3, byte4);
  271. check_utf8dump(false, byte1, byte2, byte3, byte4);
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. }
  279. }
  280. DOCTEST_CLANG_SUPPRESS_WARNING_POP