🚧 some changes to the BSON code
- added fuzz testers - added some reference files - made an exception text more clear
This commit is contained in:
parent
bba159121f
commit
e2c5913a50
13 changed files with 218 additions and 32 deletions
|
@ -10,6 +10,7 @@ SOURCES = src/unit.cpp \
|
|||
src/unit-algorithms.cpp \
|
||||
src/unit-allocator.cpp \
|
||||
src/unit-alt-string.cpp \
|
||||
src/unit-bson.cpp \
|
||||
src/unit-capacity.cpp \
|
||||
src/unit-cbor.cpp \
|
||||
src/unit-class_const_iterator.cpp \
|
||||
|
@ -90,12 +91,15 @@ check: $(OBJECTS) $(TESTCASES)
|
|||
##############################################################################
|
||||
|
||||
FUZZER_ENGINE = src/fuzzer-driver_afl.cpp
|
||||
FUZZERS = parse_afl_fuzzer parse_cbor_fuzzer parse_msgpack_fuzzer parse_ubjson_fuzzer
|
||||
FUZZERS = parse_afl_fuzzer parse_bson_fuzzer parse_cbor_fuzzer parse_msgpack_fuzzer parse_ubjson_fuzzer
|
||||
fuzzers: $(FUZZERS)
|
||||
|
||||
parse_afl_fuzzer:
|
||||
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(FUZZER_ENGINE) src/fuzzer-parse_json.cpp -o $@
|
||||
|
||||
parse_bson_fuzzer:
|
||||
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(FUZZER_ENGINE) src/fuzzer-parse_bson.cpp -o $@
|
||||
|
||||
parse_cbor_fuzzer:
|
||||
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(FUZZER_ENGINE) src/fuzzer-parse_cbor.cpp -o $@
|
||||
|
||||
|
|
BIN
test/data/json.org/1.json.bson
Normal file
BIN
test/data/json.org/1.json.bson
Normal file
Binary file not shown.
BIN
test/data/json.org/2.json.bson
Normal file
BIN
test/data/json.org/2.json.bson
Normal file
Binary file not shown.
BIN
test/data/json.org/3.json.bson
Normal file
BIN
test/data/json.org/3.json.bson
Normal file
Binary file not shown.
BIN
test/data/json.org/4.json.bson
Normal file
BIN
test/data/json.org/4.json.bson
Normal file
Binary file not shown.
BIN
test/data/json.org/5.json.bson
Normal file
BIN
test/data/json.org/5.json.bson
Normal file
Binary file not shown.
BIN
test/data/json_tests/pass3.json.bson
Normal file
BIN
test/data/json_tests/pass3.json.bson
Normal file
Binary file not shown.
68
test/src/fuzzer-parse_bson.cpp
Normal file
68
test/src/fuzzer-parse_bson.cpp
Normal file
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
__ _____ _____ _____
|
||||
__| | __| | | | JSON for Modern C++ (fuzz test support)
|
||||
| | |__ | | | | | | version 3.3.0
|
||||
|_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||
|
||||
This file implements a parser test suitable for fuzz testing. Given a byte
|
||||
array data, it performs the following steps:
|
||||
|
||||
- j1 = from_bson(data)
|
||||
- vec = to_bson(j1)
|
||||
- j2 = from_bson(vec)
|
||||
- assert(j1 == j2)
|
||||
|
||||
The provided function `LLVMFuzzerTestOneInput` can be used in different fuzzer
|
||||
drivers.
|
||||
|
||||
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
// see http://llvm.org/docs/LibFuzzer.html
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
|
||||
{
|
||||
try
|
||||
{
|
||||
// step 1: parse input
|
||||
std::vector<uint8_t> vec1(data, data + size);
|
||||
json j1 = json::from_bson(vec1);
|
||||
|
||||
try
|
||||
{
|
||||
// step 2: round trip
|
||||
std::vector<uint8_t> vec2 = json::to_bson(j1);
|
||||
|
||||
// parse serialization
|
||||
json j2 = json::from_bson(vec2);
|
||||
|
||||
// serializations must match
|
||||
assert(json::to_bson(j2) == vec2);
|
||||
}
|
||||
catch (const json::parse_error&)
|
||||
{
|
||||
// parsing a CBOR serialization must not fail
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
catch (const json::parse_error&)
|
||||
{
|
||||
// parse errors are ok, because input may be random bytes
|
||||
}
|
||||
catch (const json::type_error&)
|
||||
{
|
||||
// type errors can occur during parsing, too
|
||||
}
|
||||
catch (const json::out_of_range&)
|
||||
{
|
||||
// out of range errors can occur during parsing, too
|
||||
}
|
||||
|
||||
// return 0 - non-zero return values are reserved for future use
|
||||
return 0;
|
||||
}
|
|
@ -49,7 +49,7 @@ TEST_CASE("BSON")
|
|||
{
|
||||
json j = nullptr;
|
||||
REQUIRE_THROWS_AS(json::to_bson(j), json::type_error&);
|
||||
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] JSON value of type 0 cannot be serialized to requested format");
|
||||
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is null");
|
||||
}
|
||||
|
||||
SECTION("boolean")
|
||||
|
@ -58,14 +58,14 @@ TEST_CASE("BSON")
|
|||
{
|
||||
json j = true;
|
||||
REQUIRE_THROWS_AS(json::to_bson(j), json::type_error&);
|
||||
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] JSON value of type 4 cannot be serialized to requested format");
|
||||
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is boolean");
|
||||
}
|
||||
|
||||
SECTION("false")
|
||||
{
|
||||
json j = false;
|
||||
REQUIRE_THROWS_AS(json::to_bson(j), json::type_error&);
|
||||
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] JSON value of type 4 cannot be serialized to requested format");
|
||||
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is boolean");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,28 +73,28 @@ TEST_CASE("BSON")
|
|||
{
|
||||
json j = 42;
|
||||
REQUIRE_THROWS_AS(json::to_bson(j), json::type_error&);
|
||||
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] JSON value of type 5 cannot be serialized to requested format");
|
||||
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is number");
|
||||
}
|
||||
|
||||
SECTION("float")
|
||||
{
|
||||
json j = 4.2;
|
||||
REQUIRE_THROWS_AS(json::to_bson(j), json::type_error&);
|
||||
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] JSON value of type 7 cannot be serialized to requested format");
|
||||
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is number");
|
||||
}
|
||||
|
||||
SECTION("string")
|
||||
{
|
||||
json j = "not supported";
|
||||
REQUIRE_THROWS_AS(json::to_bson(j), json::type_error&);
|
||||
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] JSON value of type 3 cannot be serialized to requested format");
|
||||
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is string");
|
||||
}
|
||||
|
||||
SECTION("array")
|
||||
{
|
||||
json j = std::vector<int> {1, 2, 3, 4, 5, 6, 7};
|
||||
REQUIRE_THROWS_AS(json::to_bson(j), json::type_error&);
|
||||
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] JSON value of type 2 cannot be serialized to requested format");
|
||||
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is array");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1126,3 +1126,92 @@ TEST_CASE("BSON numerical data")
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("BSON roundtrips", "[hide]")
|
||||
{
|
||||
SECTION("reference files")
|
||||
{
|
||||
for (std::string filename :
|
||||
{
|
||||
"test/data/json.org/1.json",
|
||||
"test/data/json.org/2.json",
|
||||
"test/data/json.org/3.json",
|
||||
"test/data/json.org/4.json",
|
||||
"test/data/json.org/5.json"
|
||||
})
|
||||
{
|
||||
CAPTURE(filename);
|
||||
|
||||
SECTION(filename + ": std::vector<uint8_t>")
|
||||
{
|
||||
// parse JSON file
|
||||
std::ifstream f_json(filename);
|
||||
json j1 = json::parse(f_json);
|
||||
|
||||
// parse BSON file
|
||||
std::ifstream f_bson(filename + ".bson", std::ios::binary);
|
||||
std::vector<uint8_t> packed(
|
||||
(std::istreambuf_iterator<char>(f_bson)),
|
||||
std::istreambuf_iterator<char>());
|
||||
json j2;
|
||||
CHECK_NOTHROW(j2 = json::from_bson(packed));
|
||||
|
||||
// compare parsed JSON values
|
||||
CHECK(j1 == j2);
|
||||
}
|
||||
|
||||
SECTION(filename + ": std::ifstream")
|
||||
{
|
||||
// parse JSON file
|
||||
std::ifstream f_json(filename);
|
||||
json j1 = json::parse(f_json);
|
||||
|
||||
// parse BSON file
|
||||
std::ifstream f_bson(filename + ".bson", std::ios::binary);
|
||||
json j2;
|
||||
CHECK_NOTHROW(j2 = json::from_bson(f_bson));
|
||||
|
||||
// compare parsed JSON values
|
||||
CHECK(j1 == j2);
|
||||
}
|
||||
|
||||
SECTION(filename + ": uint8_t* and size")
|
||||
{
|
||||
// parse JSON file
|
||||
std::ifstream f_json(filename);
|
||||
json j1 = json::parse(f_json);
|
||||
|
||||
// parse BSON file
|
||||
std::ifstream f_bson(filename + ".bson", std::ios::binary);
|
||||
std::vector<uint8_t> packed(
|
||||
(std::istreambuf_iterator<char>(f_bson)),
|
||||
std::istreambuf_iterator<char>());
|
||||
json j2;
|
||||
CHECK_NOTHROW(j2 = json::from_bson({packed.data(), packed.size()}));
|
||||
|
||||
// compare parsed JSON values
|
||||
CHECK(j1 == j2);
|
||||
}
|
||||
|
||||
SECTION(filename + ": output to output adapters")
|
||||
{
|
||||
// parse JSON file
|
||||
std::ifstream f_json(filename);
|
||||
json j1 = json::parse(f_json);
|
||||
|
||||
// parse BSON file
|
||||
std::ifstream f_bson(filename + ".bson", std::ios::binary);
|
||||
std::vector<uint8_t> packed(
|
||||
(std::istreambuf_iterator<char>(f_bson)),
|
||||
std::istreambuf_iterator<char>());
|
||||
|
||||
SECTION(filename + ": output adapters: std::vector<uint8_t>")
|
||||
{
|
||||
std::vector<uint8_t> vec;
|
||||
json::to_bson(j1, vec);
|
||||
CHECK(vec == packed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue