/src/kea-fuzzer/fuzz_cc.cc
Line | Count | Source |
1 | | // Copyright (C) 2025 Ada Logcis Ltd. |
2 | | // |
3 | | // This Source Code Form is subject to the terms of the Mozilla Public |
4 | | // License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | // file, You can obtain one at http://mozilla.org/MPL/2.0/. |
6 | | //////////////////////////////////////////////////////////////////////////////// |
7 | | #include <config.h> |
8 | | #include <fuzzer/FuzzedDataProvider.h> |
9 | | |
10 | | #include <exceptions/exceptions.h> |
11 | | #include <cc/data.h> |
12 | | #include <cc/json_feed.h> |
13 | | #include <cc/simple_parser.h> |
14 | | #include <asiolink/io_address.h> |
15 | | |
16 | | #include <string> |
17 | | #include <vector> |
18 | | |
19 | | using namespace isc; |
20 | | using namespace isc::data; |
21 | | |
22 | 2.48k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
23 | 2.48k | FuzzedDataProvider fdp(data, size); |
24 | | |
25 | 2.48k | ElementPtr elem; |
26 | | |
27 | 2.48k | std::string val1 = fdp.ConsumeRandomLengthString(8); |
28 | 2.48k | std::string val2 = fdp.ConsumeRandomLengthString(8); |
29 | 2.48k | Element::types type1 = static_cast<Element::types>(fdp.ConsumeIntegralInRange<int>(0, 8)); |
30 | 2.48k | Element::types type2 = static_cast<Element::types>(fdp.ConsumeIntegralInRange<int>(0, 8)); |
31 | | |
32 | 2.48k | SimpleRequiredKeywords required; |
33 | 2.48k | required.push_back(val1); |
34 | 2.48k | required.push_back(val2); |
35 | | |
36 | 2.48k | SimpleKeywords keywords; |
37 | 2.48k | keywords[val1] = type1; |
38 | 2.48k | keywords[val2] = type2; |
39 | | |
40 | 2.48k | ParamsList params; |
41 | 2.48k | params.push_back(val1); |
42 | 2.48k | params.push_back(val2); |
43 | | |
44 | 2.48k | const std::string payload = fdp.ConsumeRemainingBytesAsString(); |
45 | | |
46 | | // Target JSONFeed with random data |
47 | 2.48k | try { |
48 | 2.48k | config::JSONFeed feed; |
49 | 2.48k | feed.initModel(); |
50 | 2.48k | feed.postBuffer(payload.c_str(), payload.length()); |
51 | 2.48k | feed.poll(); |
52 | 2.48k | feed.needData(); |
53 | 2.48k | feed.feedOk(); |
54 | 2.48k | feed.getProcessedText(); |
55 | 2.48k | feed.toElement(); |
56 | 2.48k | feed.getErrorMessage(); |
57 | 2.48k | } catch (const isc::Exception&) { |
58 | | // Slient exceptions |
59 | 2.02k | } |
60 | | |
61 | | // Try parse payload to Element pointer |
62 | 2.48k | try { |
63 | 2.48k | elem = Element::fromJSON(payload); |
64 | 2.48k | } catch (...) { |
65 | | // Early exit for invalid json |
66 | 1.83k | return 0; |
67 | 1.83k | } |
68 | | |
69 | | // Target parseIntTriplet |
70 | 653 | try { |
71 | 653 | SimpleParser parser; |
72 | 653 | parser.parseIntTriplet(elem, val1); |
73 | 653 | parser.parseIntTriplet(elem, val2); |
74 | 653 | } catch (const isc::Exception&) { |
75 | | // Slient exceptions |
76 | 292 | } |
77 | | |
78 | | // Target checkRequired |
79 | 653 | try { |
80 | 653 | SimpleParser::checkRequired(required, elem); |
81 | 653 | } catch (const isc::Exception&) { |
82 | | // Slient exceptions |
83 | 642 | } |
84 | | |
85 | | // Target checkKeywords |
86 | 653 | try { |
87 | 653 | SimpleParser::checkKeywords(keywords, elem); |
88 | 653 | } catch (const isc::Exception&) { |
89 | | // Slient exceptions |
90 | 643 | } |
91 | | |
92 | | // Target deriveParams |
93 | 653 | try { |
94 | 653 | SimpleParser::deriveParams(elem, Element::createMap(), params); |
95 | 653 | } catch (const isc::Exception&) { |
96 | | // Slient exceptions |
97 | 0 | } |
98 | | |
99 | 653 | return 0; |
100 | 653 | } |