MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
test_jsonutils.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Contributors to the Media eXchange Layer project.
2// SPDX-License-Identifier: Apache-2.0
3
4#include <cmath>
5#include <cstdint>
6#include <stdexcept>
7#include <string>
8#include <catch2/catch_test_macros.hpp>
10
14
15namespace
16{
17 std::string nestedArrays(std::size_t depth)
18 {
19 return std::string(depth, '[') + std::string(depth, ']');
20 }
21
22 std::string arrayOf(std::size_t count)
23 {
24 auto result = std::string{"["};
25 for (auto i = std::size_t{0}; i < count; ++i)
26 {
27 result += (i == 0U) ? "0" : ",0";
28 }
29 return result + "]";
30 }
31
32 std::string objectOf(std::size_t count)
33 {
34 auto result = std::string{"{"};
35 for (auto i = std::size_t{0}; i < count; ++i)
36 {
37 result += ((i == 0U) ? "\"k" : ",\"k") + std::to_string(i) + "\":0";
38 }
39 return result + "}";
40 }
41
42 picojson::value number(double value)
43 {
44 return picojson::value{value};
45 }
46}
47
48TEST_CASE("parseJson : accepts a valid document", "[json]")
49{
50 auto value = picojson::value{};
51 REQUIRE(parseJson(value,
52 R"( {"a": [1, "two", true, null], "b": {"c": 2.5}} )"
53 "\n\t")
54 .empty());
55 REQUIRE(value.is<picojson::object>());
56 auto const& root = value.get<picojson::object>();
57 REQUIRE(root.at("a").get<picojson::array>().size() == 4U);
58 REQUIRE(root.at("b").get<picojson::object>().at("c").get<double>() == 2.5);
59}
60
61TEST_CASE("parseJson : rejects syntax errors and trailing content", "[json]")
62{
63 auto value = picojson::value{};
64 REQUIRE_FALSE(parseJson(value, "{ this is not json").empty());
65 REQUIRE_FALSE(parseJson(value, "").empty());
66 REQUIRE_FALSE(parseJson(value, "{} {}").empty());
67 REQUIRE_FALSE(parseJson(value, "{\"a\":1} garbage").empty());
68}
69
70TEST_CASE("parseJson : limits the input size", "[json]")
71{
72 auto const limits = JsonLimits{.maxInputSize = 16U};
73 auto value = picojson::value{};
74 REQUIRE(parseJson(value, R"({"a":"12345678"})", limits).empty()); // 16 bytes
75 REQUIRE_FALSE(parseJson(value, R"({"a":"123456789"})", limits).empty());
76
77 // The default limit rejects the 4 MB description string accepted in the security audit.
78 auto const large = R"({"description":")" + std::string(4U * 1024U * 1024U, 'x') + R"("})";
79 REQUIRE_FALSE(parseJson(value, large).empty());
80}
81
82TEST_CASE("parseJson : limits the nesting depth", "[json]")
83{
84 auto const limits = JsonLimits{};
85 auto value = picojson::value{};
86 REQUIRE(parseJson(value, nestedArrays(limits.maxDepth)).empty());
87 REQUIRE_FALSE(parseJson(value, nestedArrays(limits.maxDepth + 1U)).empty());
88 REQUIRE_FALSE(parseJson(value, R"({"a":)" + nestedArrays(limits.maxDepth) + "}").empty());
89
90 // Deep enough to overflow the stack if picojson recursed into all of it.
91 auto const deep = JsonLimits{.maxInputSize = 1U << 22U};
92 REQUIRE_FALSE(parseJson(value, nestedArrays(1U << 20U), deep).empty());
93}
94
95TEST_CASE("parseJson : limits the container size", "[json]")
96{
97 auto const limits = JsonLimits{};
98 auto value = picojson::value{};
99 REQUIRE(parseJson(value, arrayOf(limits.maxContainerSize)).empty());
100 REQUIRE_FALSE(parseJson(value, arrayOf(limits.maxContainerSize + 1U)).empty());
101 REQUIRE(parseJson(value, objectOf(limits.maxContainerSize)).empty());
102 REQUIRE_FALSE(parseJson(value, objectOf(limits.maxContainerSize + 1U)).empty());
103}
104
105TEST_CASE("parseJson : rejects duplicate keys", "[json]")
106{
107 auto value = picojson::value{};
108 REQUIRE_FALSE(parseJson(value, R"({"a":1,"a":2})").empty());
109 REQUIRE_FALSE(parseJson(value, R"({"b":{"a":1,"a":1}})").empty());
110 REQUIRE(parseJson(value, R"({"a":{"a":1}})").empty());
111}
112
113TEST_CASE("parseJson : reports a number out of double range as an error", "[json]")
114{
115 auto value = picojson::value{};
116 REQUIRE_FALSE(parseJson(value, "[1e400]").empty());
117 REQUIRE_FALSE(parseJson(value, "[-1e400]").empty());
118}
119
120TEST_CASE("jsonToInteger : converts whole numbers in range", "[json]")
121{
122 REQUIRE(jsonToInteger<std::uint32_t>(number(0.0), "v") == 0U);
123 REQUIRE(jsonToInteger<std::uint32_t>(number(4294967295.0), "v") == UINT32_MAX);
124 REQUIRE(jsonToInteger<std::int64_t>(number(-9223372036854775808.0), "v") == INT64_MIN);
125 REQUIRE(jsonToInteger<std::int64_t>(number(-1001.0), "v") == -1001);
126 REQUIRE(jsonToInteger<std::uint64_t>(number(std::ldexp(1.0, 63)), "v") == (std::uint64_t{1} << 63U));
127}
128
129TEST_CASE("jsonToInteger : rejects values that do not convert exactly", "[json]")
130{
131 REQUIRE_THROWS_AS(jsonToInteger<std::uint32_t>(number(1.5), "v"), std::invalid_argument);
132 REQUIRE_THROWS_AS(jsonToInteger<std::uint32_t>(number(-1.0), "v"), std::invalid_argument);
133 REQUIRE_THROWS_AS(jsonToInteger<std::uint32_t>(number(4294967296.0), "v"), std::invalid_argument);
134 REQUIRE_THROWS_AS(jsonToInteger<std::size_t>(number(1e300), "v"), std::invalid_argument);
135 REQUIRE_THROWS_AS(jsonToInteger<std::uint64_t>(number(std::ldexp(1.0, 64)), "v"), std::invalid_argument);
136 REQUIRE_THROWS_AS(jsonToInteger<std::int64_t>(number(std::ldexp(1.0, 63)), "v"), std::invalid_argument);
137 REQUIRE_THROWS_AS(jsonToInteger<std::int64_t>(number(-std::ldexp(1.0, 64)), "v"), std::invalid_argument);
138 REQUIRE_THROWS_AS(jsonToInteger<std::uint32_t>(picojson::value{"12"}, "v"), std::invalid_argument);
139 REQUIRE_THROWS_AS(jsonToInteger<std::uint32_t>(picojson::value{}, "v"), std::invalid_argument);
140}
std::size_t count(uuids::uuid id) const noexcept
Returns the number of writers registered for flow id 'id'.
T jsonToInteger(picojson::value const &value, std::string_view name)
std::string parseJson(picojson::value &out, std::string_view text, JsonLimits const &limits={})
Limits applied by parseJson().
Definition JsonUtils.hpp:23
TEST_CASE("parseJson : accepts a valid document", "[json]")
T jsonToInteger(picojson::value const &value, std::string_view name)
std::string parseJson(picojson::value &out, std::string_view text, JsonLimits const &limits={})
REQUIRE(timeDelta< 33 '366 '667LL)