Repository: nlohmann/json
File: tests/src/unit-bjdata.cpp (C++)

Code before:

                json _;
                // check if string is parsed correctly to "a"
                CHECK_THROWS_AS(_ = json::from_ubjson(s_u), json::parse_error&);
                CHECK_THROWS_WITH(_ = json::from_ubjson(s_u), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing UBJSON string: expected length type specification (U, i, I, l, L); last byte: 0x75");

                CHECK_THROWS_AS(_ = json::from_ubjson(s_m), json::parse_error&);
                CHECK_THROWS_WITH(_ = json::from_ubjson(s_m), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing UBJSON string: expected length type specification (U, i, I, l, L); last byte: 0x6D");

                CHECK_THROWS_AS(_ = json::from_ubjson(s_M), json::parse_error&);
                CHECK_THROWS_WITH(_ = json::from_ubjson(s_M), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing UBJSON string: expected length type specification (U, i, I, l, L); last byte: 0x4D");
            }
        }

        SECTION("array")
        {
            SECTION("optimized array: no size following type")
            {
                std::vector<uint8_t> v = {'[', '$', 'i', 2};
                json _;
                CHECK_THROWS_AS(_ = json::from_bjdata(v), json::parse_error&);
                CHECK_THROWS_WITH(_ = json::from_bjdata(v), "[json.exception.parse_error.112] parse error at byte 4: syntax error while parsing BJData size: expected '#' after type information; last byte: 0x02");
            }

            SECTION("optimized array: negative size")
            {
                std::vector<uint8_t> v1 = {'[', '#', 'i', 0xF1};
                std::vector<uint8_t> v2 = {'[', '$', 'I', '#', 'i', 0xF2};
                std::vector<uint8_t> v3 = {'[', '$', 'I', '#', '[', 'i', 0xF4, 'i', 0x02, ']'};
                std::vector<uint8_t> v4 = {'[', '$', 0xF6, '#', 'i', 0xF7};
                std::vector<uint8_t> v5 = {'[', '$', 'I', '#', '[', 'i', 0xF5, 'i', 0xF1, ']'};
                std::vector<uint8_t> v6 = {'[', '#', '[', 'i', 0xF3, 'i', 0x02, ']'};

                CHECK(json::from_bjdata(v1, true, false).is_discarded());
                CHECK(json::from_bjdata(v2, true, false).is_discarded());
                CHECK(json::from_bjdata(v3, true, false).is_discarded());
                CHECK(json::from_bjdata(v4, true, false).is_discarded());
                CHECK(json::from_bjdata(v5, true, false).is_discarded());
                CHECK(json::from_bjdata(v6, true, false).is_discarded());
            }

            SECTION("do not accept NTFZ markers in ndarray optimized type")
            {
                json _;
                std::vector<uint8_t> v_N = {'[', '$', 'N', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2};
                std::vector<uint8_t> v_T = {'[', '$', 'T', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2};
                std::vector<uint8_t> v_F = {'[', '$', 'F', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2};
                std::vector<uint8_t> v_Z = {'[', '$', 'Z', '#', '[', '#', 'i', 2, 'i', 1, 'i', 2};

                CHECK_THROWS_AS(_ = json::from_bjdata(v_N), json::parse_error&);
                CHECK_THROWS_WITH(_ = json::from_bjdata(v_N), "[json.exception.parse_error.112] parse error at byte 3: syntax error while parsing BJData type: marker 0x4E is not a permitted optimized array type");

Review comment (suggestion):
Since all these inputs would be throwing unless you set `allow_exceptions=false`, it would be better to check these exceptions explicitly via `CHECK_THROWS_WITH_AS`.

Code after:

        SECTION("array")
        {
            SECTION("optimized array: no size following type")
            {
                std::vector<uint8_t> v = {'[', '$', 'i', 2};
                json _;
                CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v), "[json.exception.parse_error.112] parse error at byte 4: syntax error while parsing BJData size: expected '#' after type information; last byte: 0x02", json::parse_error&);
            }

            SECTION("optimized array: negative size")
            {
                std::vector<uint8_t> v1 = {'[', '#', 'i', 0xF1};
                std::vector<uint8_t> v2 = {'[', '$', 'I', '#', 'i', 0xF2};
                std::vector<uint8_t> v3 = {'[', '$', 'I', '#', '[', 'i', 0xF4, 'i', 0x02, ']'};
                std::vector<uint8_t> v4 = {'[', '$', 0xF6, '#', 'i', 0xF7};
                std::vector<uint8_t> v5 = {'[', '$', 'I', '#', '[', 'i', 0xF5, 'i', 0xF1, ']'};
                std::vector<uint8_t> v6 = {'[', '#', '[', 'i', 0xF3, 'i', 0x02, ']'};

                std::vector<uint8_t> vI = {'[', '#', 'I', 0x00, 0xF1};
                std::vector<uint8_t> vl = {'[', '#', 'l', 0x00, 0x00, 0x00, 0xF2};
                std::vector<uint8_t> vL = {'[', '#', 'L', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF3};

                json _;
                CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v1), "[json.exception.parse_error.113] parse error at byte 4: syntax error while parsing BJData size: count in an optimized container must be positive", json::parse_error&);
                CHECK(json::from_bjdata(v1, true, false).is_discarded());

                CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v2), "[json.exception.parse_error.113] parse error at byte 6: syntax error while parsing BJData size: count in an optimized container must be positive", json::parse_error&);
                CHECK(json::from_bjdata(v2, true, false).is_discarded());

                CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v3), "[json.exception.parse_error.113] parse error at byte 7: syntax error while parsing BJData size: count in an optimized container must be positive", json::parse_error&);
                CHECK(json::from_bjdata(v3, true, false).is_discarded());

                CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v4), "[json.exception.parse_error.113] parse error at byte 6: syntax error while parsing BJData size: count in an optimized container must be positive", json::parse_error&);
                CHECK(json::from_bjdata(v4, true, false).is_discarded());

                CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v5), "[json.exception.parse_error.113] parse error at byte 7: syntax error while parsing BJData size: count in an optimized container must be positive", json::parse_error&);
                CHECK(json::from_bjdata(v5, true, false).is_discarded());

                CHECK_THROWS_WITH_AS(_ = json::from_bjdata(v6), "[json.exception.parse_error.113] parse error at byte 5: syntax error while parsing BJData size: count in an optimized container must be positive", json::parse_error&);
                CHECK(json::from_bjdata(v6, true, false).is_discarded());

                CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vI), "[json.exception.parse_error.113] parse error at byte 5: syntax error while parsing BJData size: count in an optimized container must be positive", json::parse_error&);
                CHECK(json::from_bjdata(vI, true, false).is_discarded());

                CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vl), "[json.exception.parse_error.113] parse error at byte 7: syntax error while parsing BJData size: count in an optimized container must be positive", json::parse_error&);
                CHECK(json::from_bjdata(vI, true, false).is_discarded());

                CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vL), "[json.exception.parse_error.113] parse error at byte 11: syntax error while parsing BJData size: count in an optimized container must be positive", json::parse_error&);
                CHECK(json::from_bjdata(vI, true, false).is_discarded());
            }