123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610 |
- // __ _____ _____ _____
- // __| | __| | | | JSON for Modern C++ (supporting code)
- // | | |__ | | | | | | version 3.11.2
- // |_____|_____|_____|_|___| https://github.com/nlohmann/json
- //
- // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
- // SPDX-License-Identifier: MIT
- #include "doctest_compatibility.h"
- // for some reason including this after the json header leads to linker errors with VS 2017...
- #include <locale>
- #include <nlohmann/json.hpp>
- using nlohmann::json;
- #include <fstream>
- #include <sstream>
- #include <iostream>
- #include <iomanip>
- #include "make_test_data_available.hpp"
- // this test suite uses static variables with non-trivial destructors
- DOCTEST_CLANG_SUPPRESS_WARNING_PUSH
- DOCTEST_CLANG_SUPPRESS_WARNING("-Wexit-time-destructors")
- namespace
- {
- extern size_t calls;
- size_t calls = 0;
- void check_utf8dump(bool success_expected, int byte1, int byte2, int byte3, int byte4);
- void check_utf8dump(bool success_expected, int byte1, int byte2 = -1, int byte3 = -1, int byte4 = -1)
- {
- static std::string json_string;
- json_string.clear();
- CAPTURE(byte1)
- CAPTURE(byte2)
- CAPTURE(byte3)
- CAPTURE(byte4)
- json_string += std::string(1, static_cast<char>(byte1));
- if (byte2 != -1)
- {
- json_string += std::string(1, static_cast<char>(byte2));
- }
- if (byte3 != -1)
- {
- json_string += std::string(1, static_cast<char>(byte3));
- }
- if (byte4 != -1)
- {
- json_string += std::string(1, static_cast<char>(byte4));
- }
- CAPTURE(json_string)
- // store the string in a JSON value
- static json j;
- static json j2;
- j = json_string;
- j2 = "abc" + json_string + "xyz";
- static std::string s_ignored;
- static std::string s_ignored2;
- static std::string s_ignored_ascii;
- static std::string s_ignored2_ascii;
- static std::string s_replaced;
- static std::string s_replaced2;
- static std::string s_replaced_ascii;
- static std::string s_replaced2_ascii;
- // dumping with ignore/replace must not throw in any case
- s_ignored = j.dump(-1, ' ', false, json::error_handler_t::ignore);
- s_ignored2 = j2.dump(-1, ' ', false, json::error_handler_t::ignore);
- s_ignored_ascii = j.dump(-1, ' ', true, json::error_handler_t::ignore);
- s_ignored2_ascii = j2.dump(-1, ' ', true, json::error_handler_t::ignore);
- s_replaced = j.dump(-1, ' ', false, json::error_handler_t::replace);
- s_replaced2 = j2.dump(-1, ' ', false, json::error_handler_t::replace);
- s_replaced_ascii = j.dump(-1, ' ', true, json::error_handler_t::replace);
- s_replaced2_ascii = j2.dump(-1, ' ', true, json::error_handler_t::replace);
- if (success_expected)
- {
- static std::string s_strict;
- // strict mode must not throw if success is expected
- s_strict = j.dump();
- // all dumps should agree on the string
- CHECK(s_strict == s_ignored);
- CHECK(s_strict == s_replaced);
- }
- else
- {
- // strict mode must throw if success is not expected
- CHECK_THROWS_AS(j.dump(), json::type_error&);
- // ignore and replace must create different dumps
- CHECK(s_ignored != s_replaced);
- // check that replace string contains a replacement character
- CHECK(s_replaced.find("\xEF\xBF\xBD") != std::string::npos);
- }
- // check that prefix and suffix are preserved
- CHECK(s_ignored2.substr(1, 3) == "abc");
- CHECK(s_ignored2.substr(s_ignored2.size() - 4, 3) == "xyz");
- CHECK(s_ignored2_ascii.substr(1, 3) == "abc");
- CHECK(s_ignored2_ascii.substr(s_ignored2_ascii.size() - 4, 3) == "xyz");
- CHECK(s_replaced2.substr(1, 3) == "abc");
- CHECK(s_replaced2.substr(s_replaced2.size() - 4, 3) == "xyz");
- CHECK(s_replaced2_ascii.substr(1, 3) == "abc");
- CHECK(s_replaced2_ascii.substr(s_replaced2_ascii.size() - 4, 3) == "xyz");
- }
- void check_utf8string(bool success_expected, int byte1, int byte2, int byte3, int byte4);
- // create and check a JSON string with up to four UTF-8 bytes
- void check_utf8string(bool success_expected, int byte1, int byte2 = -1, int byte3 = -1, int byte4 = -1)
- {
- if (++calls % 100000 == 0)
- {
- std::cout << calls << " of 455355 UTF-8 strings checked" << std::endl;
- }
- static std::string json_string;
- json_string = "\"";
- CAPTURE(byte1)
- json_string += std::string(1, static_cast<char>(byte1));
- if (byte2 != -1)
- {
- CAPTURE(byte2)
- json_string += std::string(1, static_cast<char>(byte2));
- }
- if (byte3 != -1)
- {
- CAPTURE(byte3)
- json_string += std::string(1, static_cast<char>(byte3));
- }
- if (byte4 != -1)
- {
- CAPTURE(byte4)
- json_string += std::string(1, static_cast<char>(byte4));
- }
- json_string += "\"";
- CAPTURE(json_string)
- json _;
- if (success_expected)
- {
- CHECK_NOTHROW(_ = json::parse(json_string));
- }
- else
- {
- CHECK_THROWS_AS(_ = json::parse(json_string), json::parse_error&);
- }
- }
- } // namespace
- TEST_CASE("Unicode (2/5)" * doctest::skip())
- {
- SECTION("RFC 3629")
- {
- /*
- RFC 3629 describes in Sect. 4 the syntax of UTF-8 byte sequences as
- follows:
- A UTF-8 string is a sequence of octets representing a sequence of UCS
- characters. An octet sequence is valid UTF-8 only if it matches the
- following syntax, which is derived from the rules for encoding UTF-8
- and is expressed in the ABNF of [RFC2234].
- UTF8-octets = *( UTF8-char )
- UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
- UTF8-1 = %x00-7F
- UTF8-2 = %xC2-DF UTF8-tail
- UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
- %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
- UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
- %xF4 %x80-8F 2( UTF8-tail )
- UTF8-tail = %x80-BF
- */
- SECTION("ill-formed first byte")
- {
- for (int byte1 = 0x80; byte1 <= 0xC1; ++byte1)
- {
- check_utf8string(false, byte1);
- check_utf8dump(false, byte1);
- }
- for (int byte1 = 0xF5; byte1 <= 0xFF; ++byte1)
- {
- check_utf8string(false, byte1);
- check_utf8dump(false, byte1);
- }
- }
- SECTION("UTF8-1 (x00-x7F)")
- {
- SECTION("well-formed")
- {
- for (int byte1 = 0x00; byte1 <= 0x7F; ++byte1)
- {
- // unescaped control characters are parse errors in JSON
- if (0x00 <= byte1 && byte1 <= 0x1F)
- {
- check_utf8string(false, byte1);
- continue;
- }
- // a single quote is a parse error in JSON
- if (byte1 == 0x22)
- {
- check_utf8string(false, byte1);
- continue;
- }
- // a single backslash is a parse error in JSON
- if (byte1 == 0x5C)
- {
- check_utf8string(false, byte1);
- continue;
- }
- // all other characters are OK
- check_utf8string(true, byte1);
- check_utf8dump(true, byte1);
- }
- }
- }
- SECTION("UTF8-2 (xC2-xDF UTF8-tail)")
- {
- SECTION("well-formed")
- {
- for (int byte1 = 0xC2; byte1 <= 0xDF; ++byte1)
- {
- for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
- {
- check_utf8string(true, byte1, byte2);
- check_utf8dump(true, byte1, byte2);
- }
- }
- }
- SECTION("ill-formed: missing second byte")
- {
- for (int byte1 = 0xC2; byte1 <= 0xDF; ++byte1)
- {
- check_utf8string(false, byte1);
- check_utf8dump(false, byte1);
- }
- }
- SECTION("ill-formed: wrong second byte")
- {
- for (int byte1 = 0xC2; byte1 <= 0xDF; ++byte1)
- {
- for (int byte2 = 0x00; byte2 <= 0xFF; ++byte2)
- {
- // skip correct second byte
- if (0x80 <= byte2 && byte2 <= 0xBF)
- {
- continue;
- }
- check_utf8string(false, byte1, byte2);
- check_utf8dump(false, byte1, byte2);
- }
- }
- }
- }
- SECTION("UTF8-3 (xE0 xA0-BF UTF8-tail)")
- {
- SECTION("well-formed")
- {
- for (int byte1 = 0xE0; byte1 <= 0xE0; ++byte1)
- {
- for (int byte2 = 0xA0; byte2 <= 0xBF; ++byte2)
- {
- for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
- {
- check_utf8string(true, byte1, byte2, byte3);
- check_utf8dump(true, byte1, byte2, byte3);
- }
- }
- }
- }
- SECTION("ill-formed: missing second byte")
- {
- for (int byte1 = 0xE0; byte1 <= 0xE0; ++byte1)
- {
- check_utf8string(false, byte1);
- check_utf8dump(false, byte1);
- }
- }
- SECTION("ill-formed: missing third byte")
- {
- for (int byte1 = 0xE0; byte1 <= 0xE0; ++byte1)
- {
- for (int byte2 = 0xA0; byte2 <= 0xBF; ++byte2)
- {
- check_utf8string(false, byte1, byte2);
- check_utf8dump(false, byte1, byte2);
- }
- }
- }
- SECTION("ill-formed: wrong second byte")
- {
- for (int byte1 = 0xE0; byte1 <= 0xE0; ++byte1)
- {
- for (int byte2 = 0x00; byte2 <= 0xFF; ++byte2)
- {
- // skip correct second byte
- if (0xA0 <= byte2 && byte2 <= 0xBF)
- {
- continue;
- }
- for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
- {
- check_utf8string(false, byte1, byte2, byte3);
- check_utf8dump(false, byte1, byte2, byte3);
- }
- }
- }
- }
- SECTION("ill-formed: wrong third byte")
- {
- for (int byte1 = 0xE0; byte1 <= 0xE0; ++byte1)
- {
- for (int byte2 = 0xA0; byte2 <= 0xBF; ++byte2)
- {
- for (int byte3 = 0x00; byte3 <= 0xFF; ++byte3)
- {
- // skip correct third byte
- if (0x80 <= byte3 && byte3 <= 0xBF)
- {
- continue;
- }
- check_utf8string(false, byte1, byte2, byte3);
- check_utf8dump(false, byte1, byte2, byte3);
- }
- }
- }
- }
- }
- SECTION("UTF8-3 (xE1-xEC UTF8-tail UTF8-tail)")
- {
- SECTION("well-formed")
- {
- for (int byte1 = 0xE1; byte1 <= 0xEC; ++byte1)
- {
- for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
- {
- for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
- {
- check_utf8string(true, byte1, byte2, byte3);
- check_utf8dump(true, byte1, byte2, byte3);
- }
- }
- }
- }
- SECTION("ill-formed: missing second byte")
- {
- for (int byte1 = 0xE1; byte1 <= 0xEC; ++byte1)
- {
- check_utf8string(false, byte1);
- check_utf8dump(false, byte1);
- }
- }
- SECTION("ill-formed: missing third byte")
- {
- for (int byte1 = 0xE1; byte1 <= 0xEC; ++byte1)
- {
- for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
- {
- check_utf8string(false, byte1, byte2);
- check_utf8dump(false, byte1, byte2);
- }
- }
- }
- SECTION("ill-formed: wrong second byte")
- {
- for (int byte1 = 0xE1; byte1 <= 0xEC; ++byte1)
- {
- for (int byte2 = 0x00; byte2 <= 0xFF; ++byte2)
- {
- // skip correct second byte
- if (0x80 <= byte2 && byte2 <= 0xBF)
- {
- continue;
- }
- for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
- {
- check_utf8string(false, byte1, byte2, byte3);
- check_utf8dump(false, byte1, byte2, byte3);
- }
- }
- }
- }
- SECTION("ill-formed: wrong third byte")
- {
- for (int byte1 = 0xE1; byte1 <= 0xEC; ++byte1)
- {
- for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
- {
- for (int byte3 = 0x00; byte3 <= 0xFF; ++byte3)
- {
- // skip correct third byte
- if (0x80 <= byte3 && byte3 <= 0xBF)
- {
- continue;
- }
- check_utf8string(false, byte1, byte2, byte3);
- check_utf8dump(false, byte1, byte2, byte3);
- }
- }
- }
- }
- }
- SECTION("UTF8-3 (xED x80-9F UTF8-tail)")
- {
- SECTION("well-formed")
- {
- for (int byte1 = 0xED; byte1 <= 0xED; ++byte1)
- {
- for (int byte2 = 0x80; byte2 <= 0x9F; ++byte2)
- {
- for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
- {
- check_utf8string(true, byte1, byte2, byte3);
- check_utf8dump(true, byte1, byte2, byte3);
- }
- }
- }
- }
- SECTION("ill-formed: missing second byte")
- {
- for (int byte1 = 0xED; byte1 <= 0xED; ++byte1)
- {
- check_utf8string(false, byte1);
- check_utf8dump(false, byte1);
- }
- }
- SECTION("ill-formed: missing third byte")
- {
- for (int byte1 = 0xED; byte1 <= 0xED; ++byte1)
- {
- for (int byte2 = 0x80; byte2 <= 0x9F; ++byte2)
- {
- check_utf8string(false, byte1, byte2);
- check_utf8dump(false, byte1, byte2);
- }
- }
- }
- SECTION("ill-formed: wrong second byte")
- {
- for (int byte1 = 0xED; byte1 <= 0xED; ++byte1)
- {
- for (int byte2 = 0x00; byte2 <= 0xFF; ++byte2)
- {
- // skip correct second byte
- if (0x80 <= byte2 && byte2 <= 0x9F)
- {
- continue;
- }
- for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
- {
- check_utf8string(false, byte1, byte2, byte3);
- check_utf8dump(false, byte1, byte2, byte3);
- }
- }
- }
- }
- SECTION("ill-formed: wrong third byte")
- {
- for (int byte1 = 0xED; byte1 <= 0xED; ++byte1)
- {
- for (int byte2 = 0x80; byte2 <= 0x9F; ++byte2)
- {
- for (int byte3 = 0x00; byte3 <= 0xFF; ++byte3)
- {
- // skip correct third byte
- if (0x80 <= byte3 && byte3 <= 0xBF)
- {
- continue;
- }
- check_utf8string(false, byte1, byte2, byte3);
- check_utf8dump(false, byte1, byte2, byte3);
- }
- }
- }
- }
- }
- SECTION("UTF8-3 (xEE-xEF UTF8-tail UTF8-tail)")
- {
- SECTION("well-formed")
- {
- for (int byte1 = 0xEE; byte1 <= 0xEF; ++byte1)
- {
- for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
- {
- for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
- {
- check_utf8string(true, byte1, byte2, byte3);
- check_utf8dump(true, byte1, byte2, byte3);
- }
- }
- }
- }
- SECTION("ill-formed: missing second byte")
- {
- for (int byte1 = 0xEE; byte1 <= 0xEF; ++byte1)
- {
- check_utf8string(false, byte1);
- check_utf8dump(false, byte1);
- }
- }
- SECTION("ill-formed: missing third byte")
- {
- for (int byte1 = 0xEE; byte1 <= 0xEF; ++byte1)
- {
- for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
- {
- check_utf8string(false, byte1, byte2);
- check_utf8dump(false, byte1, byte2);
- }
- }
- }
- SECTION("ill-formed: wrong second byte")
- {
- for (int byte1 = 0xEE; byte1 <= 0xEF; ++byte1)
- {
- for (int byte2 = 0x00; byte2 <= 0xFF; ++byte2)
- {
- // skip correct second byte
- if (0x80 <= byte2 && byte2 <= 0xBF)
- {
- continue;
- }
- for (int byte3 = 0x80; byte3 <= 0xBF; ++byte3)
- {
- check_utf8string(false, byte1, byte2, byte3);
- check_utf8dump(false, byte1, byte2, byte3);
- }
- }
- }
- }
- SECTION("ill-formed: wrong third byte")
- {
- for (int byte1 = 0xEE; byte1 <= 0xEF; ++byte1)
- {
- for (int byte2 = 0x80; byte2 <= 0xBF; ++byte2)
- {
- for (int byte3 = 0x00; byte3 <= 0xFF; ++byte3)
- {
- // skip correct third byte
- if (0x80 <= byte3 && byte3 <= 0xBF)
- {
- continue;
- }
- check_utf8string(false, byte1, byte2, byte3);
- check_utf8dump(false, byte1, byte2, byte3);
- }
- }
- }
- }
- }
- }
- }
- DOCTEST_CLANG_SUPPRESS_WARNING_POP
|