]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | /* |
2 | __ _____ _____ _____ | |
3 | __| | __| | | | JSON for Modern C++ (fuzz test support) | |
4 | | | |__ | | | | | | version 3.10.5 | |
5 | |_____|_____|_____|_|___| https://github.com/nlohmann/json | |
6 | ||
7 | This file implements a parser test suitable for fuzz testing. Given a byte | |
8 | array data, it performs the following steps: | |
9 | ||
10 | - j1 = from_msgpack(data) | |
11 | - vec = to_msgpack(j1) | |
12 | - j2 = from_msgpack(vec) | |
13 | - assert(j1 == j2) | |
14 | ||
15 | The provided function `LLVMFuzzerTestOneInput` can be used in different fuzzer | |
16 | drivers. | |
17 | ||
18 | Licensed under the MIT License <http://opensource.org/licenses/MIT>. | |
19 | */ | |
20 | ||
21 | #include <iostream> | |
22 | #include <sstream> | |
23 | #include <nlohmann/json.hpp> | |
24 | ||
25 | using json = nlohmann::json; | |
26 | ||
27 | // see http://llvm.org/docs/LibFuzzer.html | |
28 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) | |
29 | { | |
30 | try | |
31 | { | |
32 | // step 1: parse input | |
33 | std::vector<uint8_t> vec1(data, data + size); | |
34 | json j1 = json::from_msgpack(vec1); | |
35 | ||
36 | try | |
37 | { | |
38 | // step 2: round trip | |
39 | std::vector<uint8_t> vec2 = json::to_msgpack(j1); | |
40 | ||
41 | // parse serialization | |
42 | json j2 = json::from_msgpack(vec2); | |
43 | ||
44 | // serializations must match | |
45 | assert(json::to_msgpack(j2) == vec2); | |
46 | } | |
47 | catch (const json::parse_error&) | |
48 | { | |
49 | // parsing a MessagePack serialization must not fail | |
50 | assert(false); | |
51 | } | |
52 | } | |
53 | catch (const json::parse_error&) | |
54 | { | |
55 | // parse errors are ok, because input may be random bytes | |
56 | } | |
57 | catch (const json::type_error&) | |
58 | { | |
59 | // type errors can occur during parsing, too | |
60 | } | |
61 | catch (const json::out_of_range&) | |
62 | { | |
63 | // out of range errors may happen if provided sizes are excessive | |
64 | } | |
65 | ||
66 | // return 0 - non-zero return values are reserved for future use | |
67 | return 0; | |
68 | } |