#include "doctest_compatibility.h"
#include <nlohmann/json.hpp>
using nlohmann::json;
#include <cstdint>
#include <fstream>
#include <limits>
#include <sstream>
#include <vector>
#include "make_test_data_available.hpp"
#include "test_utils.hpp"
namespace
{
class huge_binary_t : public std::vector<std::uint8_t>
{
public:
using std::vector<std::uint8_t>::vector;
size_type size() const noexcept
{
return static_cast<size_type>((std::numeric_limits<std::int32_t>::max)()) + 1;
}
};
using huge_binary_json = nlohmann::basic_json <
std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t,
double, std::allocator, nlohmann::adl_serializer, huge_binary_t, void >;
class huge_string_t : public std::string
{
public:
using std::string::string;
huge_string_t(const std::string& s) : std::string(s) {}
static huge_string_t as_huge(const std::string& s)
{
huge_string_t result(s);
result.pretend_huge = true;
return result;
}
size_type size() const noexcept
{
if (pretend_huge)
{
return static_cast<size_type>((std::numeric_limits<std::int32_t>::max)()) + 1;
}
return std::string::size();
}
private:
bool pretend_huge = false;
};
using huge_string_json = nlohmann::basic_json <
std::map, std::vector, huge_string_t, bool, std::int64_t, std::uint64_t,
double, std::allocator, nlohmann::adl_serializer, std::vector<std::uint8_t>, void >;
}
TEST_CASE("BSON")
{
SECTION("individual values not supported")
{
SECTION("null")
{
json const j = nullptr;
CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is null", json::type_error&);
}
SECTION("boolean")
{
SECTION("true")
{
json const j = true;
CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is boolean", json::type_error&);
}
SECTION("false")
{
json const j = false;
CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is boolean", json::type_error&);
}
}
SECTION("number")
{
json const j = 42;
CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is number", json::type_error&);
}
SECTION("float")
{
json const j = 4.2;
CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is number", json::type_error&);
}
SECTION("string")
{
json const j = "not supported";
CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is string", json::type_error&);
}
SECTION("array")
{
json const j = std::vector<int> {1, 2, 3, 4, 5, 6, 7};
CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is array", json::type_error&);
}
}
SECTION("keys containing code-point U+0000 cannot be serialized to BSON")
{
json const j =
{
{ std::string("en\0try", 6), true }
};
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.out_of_range.409] (/en) BSON key cannot contain code point U+0000 (at byte 2)", json::out_of_range&);
#else
CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.out_of_range.409] BSON key cannot contain code point U+0000 (at byte 2)", json::out_of_range&);
#endif
}
SECTION("lengths exceeding INT32_MAX cannot be serialized to BSON")
{
SECTION("binary")
{
huge_binary_json j;
j["b"] = huge_binary_json::binary(huge_binary_t{});
CHECK_THROWS_WITH_AS(huge_binary_json::to_bson(j), "[json.exception.out_of_range.412] BSON length 2147483661 exceeds maximum of 2147483647", huge_binary_json::out_of_range&);
}
SECTION("string")
{
huge_string_json j;
j["s"] = huge_string_t::as_huge("value");
CHECK_THROWS_WITH_AS(huge_string_json::to_bson(j), "[json.exception.out_of_range.412] BSON length 2147483661 exceeds maximum of 2147483647", huge_string_json::out_of_range&);
}
SECTION("document")
{
huge_string_json nested;
nested["s"] = huge_string_t::as_huge("value");
huge_string_json j;
j["nested"] = nested;
CHECK_THROWS_WITH_AS(huge_string_json::to_bson(j), "[json.exception.out_of_range.412] BSON length 2147483674 exceeds maximum of 2147483647", huge_string_json::out_of_range&);
}
}
SECTION("string length must be at least 1")
{
std::vector<std::uint8_t> const v =
{
0x20, 0x20, 0x20, 0x20,
0x02,
0x00,
0x00, 0x00, 0x00, 0x80
};
json _;
CHECK_THROWS_WITH_AS(_ = json::from_bson(v), "[json.exception.parse_error.112] parse error at byte 10: syntax error while parsing BSON string: string length must be at least 1, is -2147483648", json::parse_error&);
}
SECTION("objects")
{
SECTION("empty object")
{
json const j = json::object();
std::vector<std::uint8_t> const expected =
{
0x05, 0x00, 0x00, 0x00,
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("non-empty object with bool")
{
json const j =
{
{ "entry", true }
};
std::vector<std::uint8_t> const expected =
{
0x0D, 0x00, 0x00, 0x00,
0x08,
'e', 'n', 't', 'r', 'y', '\x00',
0x01,
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("non-empty object with bool")
{
json const j =
{
{ "entry", false }
};
std::vector<std::uint8_t> const expected =
{
0x0D, 0x00, 0x00, 0x00,
0x08,
'e', 'n', 't', 'r', 'y', '\x00',
0x00,
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("non-empty object with bool from a non-0/1 byte (lenient parsing)")
{
std::vector<std::uint8_t> const input =
{
0x0D, 0x00, 0x00, 0x00,
0x08,
'e', 'n', 't', 'r', 'y', '\x00',
0x02,
0x00
};
const json expected = { { "entry", true } };
CHECK(json::from_bson(input) == expected);
}
SECTION("non-empty object with double")
{
json const j =
{
{ "entry", 4.2 }
};
std::vector<std::uint8_t> const expected =
{
0x14, 0x00, 0x00, 0x00,
0x01,
'e', 'n', 't', 'r', 'y', '\x00',
0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x10, 0x40,
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("non-empty object with string")
{
json const j =
{
{ "entry", "bsonstr" }
};
std::vector<std::uint8_t> const expected =
{
0x18, 0x00, 0x00, 0x00,
0x02,
'e', 'n', 't', 'r', 'y', '\x00',
0x08, 0x00, 0x00, 0x00, 'b', 's', 'o', 'n', 's', 't', 'r', '\x00',
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("non-empty object with null member")
{
json const j =
{
{ "entry", nullptr }
};
std::vector<std::uint8_t> const expected =
{
0x0C, 0x00, 0x00, 0x00,
0x0A,
'e', 'n', 't', 'r', 'y', '\x00',
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("non-empty object with integer (32-bit) member")
{
json const j =
{
{ "entry", std::int32_t{0x12345678} }
};
std::vector<std::uint8_t> const expected =
{
0x10, 0x00, 0x00, 0x00,
0x10,
'e', 'n', 't', 'r', 'y', '\x00',
0x78, 0x56, 0x34, 0x12,
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("non-empty object with integer (64-bit) member")
{
json const j =
{
{ "entry", std::int64_t{0x1234567804030201} }
};
std::vector<std::uint8_t> const expected =
{
0x14, 0x00, 0x00, 0x00,
0x12,
'e', 'n', 't', 'r', 'y', '\x00',
0x01, 0x02, 0x03, 0x04, 0x78, 0x56, 0x34, 0x12,
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("non-empty object with negative integer (32-bit) member")
{
json const j =
{
{ "entry", std::int32_t{-1} }
};
std::vector<std::uint8_t> const expected =
{
0x10, 0x00, 0x00, 0x00,
0x10,
'e', 'n', 't', 'r', 'y', '\x00',
0xFF, 0xFF, 0xFF, 0xFF,
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("non-empty object with negative integer (64-bit) member")
{
json const j =
{
{ "entry", std::int64_t{-1} }
};
std::vector<std::uint8_t> const expected =
{
0x10, 0x00, 0x00, 0x00,
0x10,
'e', 'n', 't', 'r', 'y', '\x00',
0xFF, 0xFF, 0xFF, 0xFF,
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("non-empty object with unsigned integer (64-bit) member")
{
json const j =
{
{ "entry", std::uint64_t{0x1234567804030201} }
};
std::vector<std::uint8_t> const expected =
{
0x14, 0x00, 0x00, 0x00,
0x12,
'e', 'n', 't', 'r', 'y', '\x00',
0x01, 0x02, 0x03, 0x04, 0x78, 0x56, 0x34, 0x12,
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("non-empty object with small unsigned integer member")
{
json const j =
{
{ "entry", std::uint64_t{0x42} }
};
std::vector<std::uint8_t> const expected =
{
0x10, 0x00, 0x00, 0x00,
0x10,
'e', 'n', 't', 'r', 'y', '\x00',
0x42, 0x00, 0x00, 0x00,
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("non-empty object with object member")
{
json const j =
{
{ "entry", json::object() }
};
std::vector<std::uint8_t> const expected =
{
0x11, 0x00, 0x00, 0x00,
0x03,
'e', 'n', 't', 'r', 'y', '\x00',
0x05, 0x00, 0x00, 0x00,
0x00,
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("non-empty object with array member")
{
json const j =
{
{ "entry", json::array() }
};
std::vector<std::uint8_t> const expected =
{
0x11, 0x00, 0x00, 0x00,
0x04,
'e', 'n', 't', 'r', 'y', '\x00',
0x05, 0x00, 0x00, 0x00,
0x00,
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("non-empty object with non-empty array member")
{
json const j =
{
{ "entry", json::array({1, 2, 3, 4, 5, 6, 7, 8}) }
};
std::vector<std::uint8_t> const expected =
{
0x49, 0x00, 0x00, 0x00,
0x04,
'e', 'n', 't', 'r', 'y', '\x00',
0x3D, 0x00, 0x00, 0x00,
0x10, '0', 0x00, 0x01, 0x00, 0x00, 0x00,
0x10, '1', 0x00, 0x02, 0x00, 0x00, 0x00,
0x10, '2', 0x00, 0x03, 0x00, 0x00, 0x00,
0x10, '3', 0x00, 0x04, 0x00, 0x00, 0x00,
0x10, '4', 0x00, 0x05, 0x00, 0x00, 0x00,
0x10, '5', 0x00, 0x06, 0x00, 0x00, 0x00,
0x10, '6', 0x00, 0x07, 0x00, 0x00, 0x00,
0x10, '7', 0x00, 0x08, 0x00, 0x00, 0x00,
0x00,
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("array elements with non-conforming keys (lenient parsing)")
{
std::vector<std::uint8_t> const input =
{
0x26, 0x00, 0x00, 0x00,
0x04, 'e', 'n', 't', 'r', 'y', '\x00',
0x1A, 0x00, 0x00, 0x00,
0x10, '5', 0x00, 0x0A, 0x00, 0x00, 0x00,
0x10, 'x', 0x00, 0x14, 0x00, 0x00, 0x00,
0x10, '1', 0x00, 0x1E, 0x00, 0x00, 0x00,
0x00,
0x00
};
const json expected = { { "entry", json::array({10, 20, 30}) } };
CHECK(json::from_bson(input) == expected);
}
SECTION("non-empty object with binary member")
{
const size_t N = 10;
const auto s = std::vector<std::uint8_t>(N, 'x');
json const j =
{
{ "entry", json::binary(s, 0) }
};
std::vector<std::uint8_t> const expected =
{
0x1B, 0x00, 0x00, 0x00,
0x05,
'e', 'n', 't', 'r', 'y', '\x00',
0x0A, 0x00, 0x00, 0x00,
0x00,
0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("non-empty object with binary member without subtype")
{
const size_t N = 10;
const auto s = std::vector<std::uint8_t>(N, 'x');
json const j =
{
{ "entry", json::binary(s) }
};
CHECK(!j.at("entry").get_binary().has_subtype());
std::vector<std::uint8_t> const expected =
{
0x1B, 0x00, 0x00, 0x00,
0x05,
'e', 'n', 't', 'r', 'y', '\x00',
0x0A, 0x00, 0x00, 0x00,
0x00,
0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
const auto roundtrip = json::from_bson(result);
CHECK(roundtrip != j);
CHECK(roundtrip.at("entry").get_binary().has_subtype());
CHECK(roundtrip.at("entry").get_binary().subtype() == 0);
CHECK(json::from_bson(result, true, false) == roundtrip);
}
SECTION("non-empty object with binary member with subtype")
{
const std::vector<std::uint8_t> md5hash = {0xd7, 0x7e, 0x27, 0x54, 0xbe, 0x12, 0x37, 0xfe, 0xd6, 0x0c, 0x33, 0x98, 0x30, 0x3b, 0x8d, 0xc4};
json const j =
{
{ "entry", json::binary(md5hash, 5) }
};
std::vector<std::uint8_t> const expected =
{
0x21, 0x00, 0x00, 0x00,
0x05,
'e', 'n', 't', 'r', 'y', '\x00',
0x10, 0x00, 0x00, 0x00,
0x05,
0xd7, 0x7e, 0x27, 0x54, 0xbe, 0x12, 0x37, 0xfe, 0xd6, 0x0c, 0x33, 0x98, 0x30, 0x3b, 0x8d, 0xc4,
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("binary member with subtype 0x02 (old binary) keeps its inner length prefix (lenient parsing)")
{
std::vector<std::uint8_t> const input =
{
0x17, 0x00, 0x00, 0x00,
0x05, 'e', 'n', 't', 'r', 'y', '\x00',
0x06, 0x00, 0x00, 0x00,
0x02,
0x02, 0x00, 0x00, 0x00,
0x68, 0x69,
0x00
};
const std::vector<std::uint8_t> expected_payload = {0x02, 0x00, 0x00, 0x00, 0x68, 0x69};
const json expected = { { "entry", json::binary(expected_payload, 0x02) } };
CHECK(json::from_bson(input) == expected);
}
SECTION("Some more complex document")
{
json const j =
{
{"double", 42.5},
{"entry", 4.2},
{"number", 12345},
{"object", {{ "string", "value" }}}
};
std::vector<std::uint8_t> const expected =
{
0x4f, 0x00, 0x00, 0x00,
0x01, 'd', 'o', 'u', 'b', 'l', 'e', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x45, 0x40,
0x01, 'e', 'n', 't', 'r', 'y', 0x00, 0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x10, 0x40,
0x10, 'n', 'u', 'm', 'b', 'e', 'r', 0x00, 0x39, 0x30, 0x00, 0x00,
0x03, 'o', 'b', 'j', 'e', 'c', 't', 0x00,
0x17, 0x00, 0x00, 0x00,
0x02, 's', 't', 'r', 'i', 'n', 'g', 0x00, 0x06, 0x00, 0x00, 0x00, 'v', 'a', 'l', 'u', 'e', 0,
0x00,
0x00
};
const auto result = json::to_bson(j);
CHECK(result == expected);
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
}
SECTION("Examples from https://bsonspec.org/faq.html")
{
SECTION("Example 1")
{
std::vector<std::uint8_t> input = {0x16, 0x00, 0x00, 0x00, 0x02, 'h', 'e', 'l', 'l', 'o', 0x00, 0x06, 0x00, 0x00, 0x00, 'w', 'o', 'r', 'l', 'd', 0x00, 0x00};
const json parsed = json::from_bson(input);
const json expected = {{"hello", "world"}};
CHECK(parsed == expected);
const auto dumped = json::to_bson(parsed);
CHECK(dumped == input);
CHECK(json::from_bson(dumped) == expected);
}
SECTION("Example 2")
{
std::vector<std::uint8_t> input = {0x31, 0x00, 0x00, 0x00, 0x04, 'B', 'S', 'O', 'N', 0x00, 0x26, 0x00, 0x00, 0x00, 0x02, 0x30, 0x00, 0x08, 0x00, 0x00, 0x00, 'a', 'w', 'e', 's', 'o', 'm', 'e', 0x00, 0x01, 0x31, 0x00, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x14, 0x40, 0x10, 0x32, 0x00, 0xc2, 0x07, 0x00, 0x00, 0x00, 0x00};
const json parsed = json::from_bson(input);
const json expected = {{"BSON", {"awesome", 5.05, 1986}}};
CHECK(parsed == expected);
const auto dumped = json::to_bson(parsed);
CHECK(dumped == input);
CHECK(json::from_bson(dumped) == expected);
}
}
}
TEST_CASE("BSON input/output_adapters")
{
const json json_representation =
{
{"double", 42.5},
{"entry", 4.2},
{"number", 12345},
{"object", {{ "string", "value" }}}
};
const std::vector<std::uint8_t> bson_representation =
{
0x4f, 0x00, 0x00, 0x00,
0x01, 'd', 'o', 'u', 'b', 'l', 'e', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x45, 0x40,
0x01, 'e', 'n', 't', 'r', 'y', 0x00, 0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x10, 0x40,
0x10, 'n', 'u', 'm', 'b', 'e', 'r', 0x00, 0x39, 0x30, 0x00, 0x00,
0x03, 'o', 'b', 'j', 'e', 'c', 't', 0x00,
0x17, 0x00, 0x00, 0x00,
0x02, 's', 't', 'r', 'i', 'n', 'g', 0x00, 0x06, 0x00, 0x00, 0x00, 'v', 'a', 'l', 'u', 'e', 0,
0x00,
0x00
};
json j2;
CHECK_NOTHROW(j2 = json::from_bson(bson_representation));
CHECK(json_representation == j2);
SECTION("roundtrips")
{
SECTION("std::ostringstream")
{
std::basic_ostringstream<char> ss;
json::to_bson(json_representation, ss);
const json j3 = json::from_bson(ss.str());
CHECK(json_representation == j3);
}
SECTION("std::string")
{
std::string s;
json::to_bson(json_representation, s);
const json j3 = json::from_bson(s);
CHECK(json_representation == j3);
}
SECTION("std::vector")
{
std::vector<std::uint8_t> v;
json::to_bson(json_representation, v);
const json j3 = json::from_bson(v);
CHECK(json_representation == j3);
}
}
}
namespace
{
class SaxCountdown
{
public:
explicit SaxCountdown(const int count) : events_left(count)
{}
bool null()
{
return events_left-- > 0;
}
bool boolean(bool )
{
return events_left-- > 0;
}
bool number_integer(json::number_integer_t )
{
return events_left-- > 0;
}
bool number_unsigned(json::number_unsigned_t )
{
return events_left-- > 0;
}
bool number_float(json::number_float_t , const std::string& )
{
return events_left-- > 0;
}
bool string(std::string& )
{
return events_left-- > 0;
}
bool binary(std::vector<std::uint8_t>& )
{
return events_left-- > 0;
}
bool start_object(std::size_t )
{
return events_left-- > 0;
}
bool key(std::string& )
{
return events_left-- > 0;
}
bool end_object()
{
return events_left-- > 0;
}
bool start_array(std::size_t )
{
return events_left-- > 0;
}
bool end_array()
{
return events_left-- > 0;
}
bool parse_error(std::size_t , const std::string& , const json::exception& )
{
return false;
}
private:
int events_left = 0;
};
}
TEST_CASE("Incomplete BSON Input")
{
SECTION("Incomplete BSON Input 1")
{
std::vector<std::uint8_t> const incomplete_bson =
{
0x0D, 0x00, 0x00, 0x00,
0x08,
'e', 'n', 't'
};
json _;
CHECK_THROWS_WITH_AS(_ = json::from_bson(incomplete_bson), "[json.exception.parse_error.110] parse error at byte 9: syntax error while parsing BSON cstring: unexpected end of input", json::parse_error&);
CHECK(json::from_bson(incomplete_bson, true, false).is_discarded());
SaxCountdown scp(0);
CHECK(!json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
}
SECTION("Incomplete BSON Input 2")
{
std::vector<std::uint8_t> const incomplete_bson =
{
0x0D, 0x00, 0x00, 0x00,
0x08,
};
json _;
CHECK_THROWS_WITH_AS(_ = json::from_bson(incomplete_bson), "[json.exception.parse_error.110] parse error at byte 6: syntax error while parsing BSON cstring: unexpected end of input", json::parse_error&);
CHECK(json::from_bson(incomplete_bson, true, false).is_discarded());
SaxCountdown scp(0);
CHECK(!json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
}
SECTION("Incomplete BSON Input 3")
{
std::vector<std::uint8_t> const incomplete_bson =
{
0x41, 0x00, 0x00, 0x00,
0x04,
'e', 'n', 't', 'r', 'y', '\x00',
0x35, 0x00, 0x00, 0x00,
0x10, 0x00, 0x01, 0x00, 0x00, 0x00,
0x10, 0x00, 0x02, 0x00, 0x00, 0x00
};
json _;
CHECK_THROWS_WITH_AS(_ = json::from_bson(incomplete_bson), "[json.exception.parse_error.110] parse error at byte 28: syntax error while parsing BSON element list: unexpected end of input", json::parse_error&);
CHECK(json::from_bson(incomplete_bson, true, false).is_discarded());
SaxCountdown scp(1);
CHECK(!json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
}
SECTION("Incomplete BSON Input 4")
{
std::vector<std::uint8_t> const incomplete_bson =
{
0x0D, 0x00,
};
json _;
CHECK_THROWS_WITH_AS(_ = json::from_bson(incomplete_bson), "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing BSON number: unexpected end of input", json::parse_error&);
CHECK(json::from_bson(incomplete_bson, true, false).is_discarded());
SaxCountdown scp(0);
CHECK(!json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
}
SECTION("Incomplete BSON Input 5")
{
std::vector<std::uint8_t> const incomplete_bson =
{
0x09, 0x00, 0x00, 0x00,
0x08,
'b', '\x00'
};
json _;
CHECK_THROWS_WITH_AS(_ = json::from_bson(incomplete_bson), "[json.exception.parse_error.110] parse error at byte 8: syntax error while parsing BSON number: unexpected end of input", json::parse_error&);
CHECK(json::from_bson(incomplete_bson, true, false).is_discarded());
SaxCountdown scp(0);
CHECK(!json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
}
SECTION("Incomplete BSON Input 6")
{
std::vector<std::uint8_t> const incomplete_bson =
{
0x0F, 0x00, 0x00, 0x00,
0x05,
'b', '\x00',
0x00, 0x00, 0x00, 0x00
};
json _;
CHECK_THROWS_WITH_AS(_ = json::from_bson(incomplete_bson), "[json.exception.parse_error.110] parse error at byte 12: syntax error while parsing BSON number: unexpected end of input", json::parse_error&);
CHECK(json::from_bson(incomplete_bson, true, false).is_discarded());
SaxCountdown scp(0);
CHECK(!json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
}
SECTION("Improve coverage")
{
SECTION("key")
{
json const j = {{"key", "value"}};
auto bson_vec = json::to_bson(j);
SaxCountdown scp(2);
CHECK(!json::sax_parse(bson_vec, &scp, json::input_format_t::bson));
}
SECTION("array")
{
json const j =
{
{ "entry", json::array() }
};
auto bson_vec = json::to_bson(j);
SaxCountdown scp(2);
CHECK(!json::sax_parse(bson_vec, &scp, json::input_format_t::bson));
}
}
}
TEST_CASE("Negative size of binary value")
{
std::vector<std::uint8_t> const input =
{
0x21, 0x00, 0x00, 0x00,
0x05,
'e', 'n', 't', 'r', 'y', '\x00',
0xFF, 0xFF, 0xFF, 0xFF,
0x05,
0xd7, 0x7e, 0x27, 0x54, 0xbe, 0x12, 0x37, 0xfe, 0xd6, 0x0c, 0x33, 0x98, 0x30, 0x3b, 0x8d, 0xc4,
0x00
};
json _;
CHECK_THROWS_WITH_AS(_ = json::from_bson(input), "[json.exception.parse_error.112] parse error at byte 15: syntax error while parsing BSON binary: byte array length cannot be negative, is -1", json::parse_error);
}
TEST_CASE("Unsupported BSON input")
{
std::vector<std::uint8_t> const bson =
{
0x0C, 0x00, 0x00, 0x00,
0xFF,
'e', 'n', 't', 'r', 'y', '\x00',
0x00
};
json _;
CHECK_THROWS_WITH_AS(_ = json::from_bson(bson), "[json.exception.parse_error.114] parse error at byte 5: Unsupported BSON record type 0xFF", json::parse_error&);
CHECK(json::from_bson(bson, true, false).is_discarded());
SaxCountdown scp(0);
CHECK(!json::sax_parse(bson, &scp, json::input_format_t::bson));
}
TEST_CASE("BSON document size mismatch")
{
json _;
SECTION("top-level document declaring more bytes than it contains")
{
std::vector<std::uint8_t> const input = {0x06, 0x00, 0x00, 0x00, 0x00};
CHECK_THROWS_WITH_AS(_ = json::from_bson(input), "[json.exception.parse_error.112] parse error at byte 5: syntax error while parsing BSON document: document size 6 does not match the number of bytes read (5)", json::parse_error&);
CHECK(json::from_bson(input, true, false).is_discarded());
}
SECTION("top-level document with a negative size")
{
std::vector<std::uint8_t> const input = {0xFF, 0xFF, 0xFF, 0xFF, 0x00};
CHECK_THROWS_WITH_AS(_ = json::from_bson(input), "[json.exception.parse_error.112] parse error at byte 5: syntax error while parsing BSON document: document size -1 does not match the number of bytes read (5)", json::parse_error&);
CHECK(json::from_bson(input, true, false).is_discarded());
}
SECTION("embedded document whose size disagrees with its terminator")
{
std::vector<std::uint8_t> const input =
{
0x00, 0x00, 0x00, 0x00,
0x03, 'd', 0x00,
0xFF, 0xFF, 0xFF, 0x7F,
0x0A, 'a', 0x00,
0x00,
0x08, 'h', 0x00, 0x01,
0x00
};
CHECK_THROWS_WITH_AS(_ = json::from_bson(input), "[json.exception.parse_error.112] parse error at byte 15: syntax error while parsing BSON document: document size 2147483647 does not match the number of bytes read (8)", json::parse_error&);
CHECK(json::from_bson(input, true, false).is_discarded());
}
SECTION("embedded array whose size disagrees with its terminator")
{
std::vector<std::uint8_t> const input =
{
0x00, 0x00, 0x00, 0x00,
0x04, 'a', 0x00,
0x0D, 0x00, 0x00, 0x00,
0x10, '0', 0x00, 0x2A, 0x00, 0x00, 0x00,
0x00,
0x00
};
CHECK_THROWS_WITH_AS(_ = json::from_bson(input), "[json.exception.parse_error.112] parse error at byte 19: syntax error while parsing BSON document: document size 13 does not match the number of bytes read (12)", json::parse_error&);
CHECK(json::from_bson(input, true, false).is_discarded());
}
}
TEST_CASE("BSON numerical data")
{
SECTION("number")
{
SECTION("signed")
{
SECTION("std::int64_t: INT64_MIN .. INT32_MIN-1")
{
std::vector<int64_t> const numbers
{
(std::numeric_limits<int64_t>::min)(),
-1000000000000000000LL,
-100000000000000000LL,
-10000000000000000LL,
-1000000000000000LL,
-100000000000000LL,
-10000000000000LL,
-1000000000000LL,
-100000000000LL,
-10000000000LL,
static_cast<std::int64_t>((std::numeric_limits<std::int32_t>::min)()) - 1,
};
for (const auto i : numbers)
{
CAPTURE(i)
json const j =
{
{ "entry", i }
};
CHECK(j.at("entry").is_number_integer());
std::uint64_t const iu = *reinterpret_cast<const std::uint64_t*>(&i);
std::vector<std::uint8_t> const expected_bson =
{
0x14u, 0x00u, 0x00u, 0x00u,
0x12u,
'e', 'n', 't', 'r', 'y', '\x00',
static_cast<std::uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 1u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 2u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 3u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 4u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 5u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 6u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 7u)) & 0xffu),
0x00u
};
const auto bson = json::to_bson(j);
CHECK(bson == expected_bson);
auto j_roundtrip = json::from_bson(bson);
CHECK(j_roundtrip.at("entry").is_number_integer());
CHECK(j_roundtrip == j);
CHECK(json::from_bson(bson, true, false) == j);
}
}
SECTION("signed std::int32_t: INT32_MIN .. INT32_MAX")
{
std::vector<int32_t> const numbers
{
(std::numeric_limits<int32_t>::min)(),
-2147483647L,
-1000000000L,
-100000000L,
-10000000L,
-1000000L,
-100000L,
-10000L,
-1000L,
-100L,
-10L,
-1L,
0L,
1L,
10L,
100L,
1000L,
10000L,
100000L,
1000000L,
10000000L,
100000000L,
1000000000L,
2147483646L,
(std::numeric_limits<int32_t>::max)()
};
for (const auto i : numbers)
{
CAPTURE(i)
json const j =
{
{ "entry", i }
};
CHECK(j.at("entry").is_number_integer());
std::uint32_t const iu = *reinterpret_cast<const std::uint32_t*>(&i);
std::vector<std::uint8_t> const expected_bson =
{
0x10u, 0x00u, 0x00u, 0x00u,
0x10u,
'e', 'n', 't', 'r', 'y', '\x00',
static_cast<std::uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 1u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 2u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 3u)) & 0xffu),
0x00u
};
const auto bson = json::to_bson(j);
CHECK(bson == expected_bson);
auto j_roundtrip = json::from_bson(bson);
CHECK(j_roundtrip.at("entry").is_number_integer());
CHECK(j_roundtrip == j);
CHECK(json::from_bson(bson, true, false) == j);
}
}
SECTION("signed std::int64_t: INT32_MAX+1 .. INT64_MAX")
{
std::vector<int64_t> const numbers
{
(std::numeric_limits<int64_t>::max)(),
1000000000000000000LL,
100000000000000000LL,
10000000000000000LL,
1000000000000000LL,
100000000000000LL,
10000000000000LL,
1000000000000LL,
100000000000LL,
10000000000LL,
static_cast<std::int64_t>((std::numeric_limits<int32_t>::max)()) + 1,
};
for (const auto i : numbers)
{
CAPTURE(i)
json const j =
{
{ "entry", i }
};
CHECK(j.at("entry").is_number_integer());
std::uint64_t const iu = *reinterpret_cast<const std::uint64_t*>(&i);
std::vector<std::uint8_t> const expected_bson =
{
0x14u, 0x00u, 0x00u, 0x00u,
0x12u,
'e', 'n', 't', 'r', 'y', '\x00',
static_cast<std::uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 1u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 2u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 3u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 4u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 5u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 6u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 7u)) & 0xffu),
0x00u
};
const auto bson = json::to_bson(j);
CHECK(bson == expected_bson);
auto j_roundtrip = json::from_bson(bson);
CHECK(j_roundtrip.at("entry").is_number_integer());
CHECK(j_roundtrip == j);
CHECK(json::from_bson(bson, true, false) == j);
}
}
}
SECTION("unsigned")
{
SECTION("unsigned std::uint64_t: 0 .. INT32_MAX")
{
std::vector<std::uint64_t> const numbers
{
0ULL,
1ULL,
10ULL,
100ULL,
1000ULL,
10000ULL,
100000ULL,
1000000ULL,
10000000ULL,
100000000ULL,
1000000000ULL,
2147483646ULL,
static_cast<std::uint64_t>((std::numeric_limits<int32_t>::max)())
};
for (const auto i : numbers)
{
CAPTURE(i)
json const j =
{
{ "entry", i }
};
auto iu = i;
std::vector<std::uint8_t> const expected_bson =
{
0x10u, 0x00u, 0x00u, 0x00u,
0x10u,
'e', 'n', 't', 'r', 'y', '\x00',
static_cast<std::uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 1u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 2u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 3u)) & 0xffu),
0x00u
};
const auto bson = json::to_bson(j);
CHECK(bson == expected_bson);
auto j_roundtrip = json::from_bson(bson);
CHECK(j.at("entry").is_number_unsigned());
CHECK(j_roundtrip.at("entry").is_number_integer());
CHECK(j_roundtrip == j);
CHECK(json::from_bson(bson, true, false) == j);
}
}
SECTION("unsigned std::uint64_t: INT32_MAX+1 .. INT64_MAX")
{
std::vector<std::uint64_t> const numbers
{
static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()) + 1,
4000000000ULL,
static_cast<std::uint64_t>((std::numeric_limits<std::uint32_t>::max)()),
10000000000ULL,
100000000000ULL,
1000000000000ULL,
10000000000000ULL,
100000000000000ULL,
1000000000000000ULL,
10000000000000000ULL,
100000000000000000ULL,
1000000000000000000ULL,
static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()),
};
for (const auto i : numbers)
{
CAPTURE(i)
json const j =
{
{ "entry", i }
};
auto iu = i;
std::vector<std::uint8_t> const expected_bson =
{
0x14u, 0x00u, 0x00u, 0x00u,
0x12u,
'e', 'n', 't', 'r', 'y', '\x00',
static_cast<std::uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 1u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 2u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 3u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 4u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 5u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 6u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 7u)) & 0xffu),
0x00u
};
const auto bson = json::to_bson(j);
CHECK(bson == expected_bson);
auto j_roundtrip = json::from_bson(bson);
CHECK(j.at("entry").is_number_unsigned());
CHECK(j_roundtrip.at("entry").is_number_integer());
CHECK(j_roundtrip == j);
CHECK(json::from_bson(bson, true, false) == j);
}
}
SECTION("unsigned std::uint64_t: INT64_MAX+1 .. UINT64_MAX")
{
std::vector<std::uint64_t> const numbers
{
static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()) + 1ULL,
0xffffffffffffffff,
};
for (const auto i : numbers)
{
CAPTURE(i)
json const j =
{
{ "entry", i }
};
auto iu = i;
std::vector<std::uint8_t> const expected_bson =
{
0x14u, 0x00u, 0x00u, 0x00u,
0x11u,
'e', 'n', 't', 'r', 'y', '\x00',
static_cast<std::uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 1u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 2u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 3u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 4u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 5u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 6u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 7u)) & 0xffu),
0x00u
};
const auto bson = json::to_bson(j);
CHECK(bson == expected_bson);
auto j_roundtrip = json::from_bson(bson);
CHECK(j.at("entry").is_number_unsigned());
CHECK(j_roundtrip.at("entry").is_number_unsigned());
CHECK(j_roundtrip == j);
CHECK(json::from_bson(bson, true, false) == j);
}
}
}
}
}
TEST_CASE("Parse BSON directly from a file using iterator and sentinel")
{
std::string const filename = TEST_DATA_DIRECTORY "/json.org/1.json";
std::ifstream f_json(filename);
const json expected = json::parse(f_json);
std::ifstream file(filename + ".bson", std::ios::binary);
const std::istreambuf_iterator<char> first(file);
const json parsed = json::from_bson(first, utils::istreambuf_sentinel{});
CHECK(parsed == expected);
}
TEST_CASE("BSON roundtrips" * doctest::skip())
{
SECTION("reference files")
{
for (const std::string filename :
{
TEST_DATA_DIRECTORY "/json.org/1.json",
TEST_DATA_DIRECTORY "/json.org/2.json",
TEST_DATA_DIRECTORY "/json.org/3.json",
TEST_DATA_DIRECTORY "/json.org/4.json",
TEST_DATA_DIRECTORY "/json.org/5.json"
})
{
CAPTURE(filename)
{
INFO_WITH_TEMP(filename + ": std::vector<std::uint8_t>");
std::ifstream f_json(filename);
const json j1 = json::parse(f_json);
auto packed = utils::read_binary_file(filename + ".bson");
json j2;
CHECK_NOTHROW(j2 = json::from_bson(packed));
CHECK(j1 == j2);
}
{
INFO_WITH_TEMP(filename + ": std::ifstream");
std::ifstream f_json(filename);
const json j1 = json::parse(f_json);
std::ifstream f_bson(filename + ".bson", std::ios::binary);
json j2;
CHECK_NOTHROW(j2 = json::from_bson(f_bson));
CHECK(j1 == j2);
}
{
INFO_WITH_TEMP(filename + ": uint8_t* and size");
std::ifstream f_json(filename);
const json j1 = json::parse(f_json);
auto packed = utils::read_binary_file(filename + ".bson");
json j2;
CHECK_NOTHROW(j2 = json::from_bson({packed.data(), packed.size()}));
CHECK(j1 == j2);
}
{
INFO_WITH_TEMP(filename + ": output to output adapters");
std::ifstream f_json(filename);
json const j1 = json::parse(f_json);
auto packed = utils::read_binary_file(filename + ".bson");
{
INFO_WITH_TEMP(filename + ": output adapters: std::vector<std::uint8_t>");
std::vector<std::uint8_t> vec;
json::to_bson(j1, vec);
if (vec != packed)
{
CHECK(json::from_bson(vec) == json::from_bson(packed));
}
}
}
}
}
}