/src/kea-fuzzer/fuzz_util.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 <util/str.h> |
12 | | #include <util/csv_file.h> |
13 | | #include <util/encode/utf8.h> |
14 | | #include <util/boost_time_utils.h> |
15 | | |
16 | | #include <boost/date_time/posix_time/posix_time.hpp> |
17 | | |
18 | | #include <string> |
19 | | #include <vector> |
20 | | #include <cstddef> |
21 | | |
22 | | using namespace boost::posix_time; |
23 | | using namespace isc::util; |
24 | | |
25 | 2.20k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
26 | 2.20k | FuzzedDataProvider fdp(data, size); |
27 | | |
28 | 2.20k | const bool isEscape = fdp.ConsumeBool(); |
29 | 2.20k | const std::string delim = fdp.ConsumeBytesAsString(1); |
30 | 2.20k | const std::string payload = fdp.ConsumeRemainingBytesAsString(); |
31 | | |
32 | 2.20k | std::vector<uint8_t> out; |
33 | | |
34 | | // Target str tokens |
35 | 2.20k | try { |
36 | 2.20k | str::tokens(payload, delim, isEscape); |
37 | 2.20k | } catch (const isc::Exception&) { |
38 | | // Slient exceptions |
39 | 0 | } catch (const boost::exception&) { |
40 | | // Slient exceptions |
41 | 0 | } |
42 | | |
43 | | // Target str quotedStringToBinary |
44 | 2.20k | try { |
45 | 2.20k | str::quotedStringToBinary(payload); |
46 | 2.20k | } catch (const isc::Exception&) { |
47 | | // Slient exceptions |
48 | 0 | } catch (const boost::exception&) { |
49 | | // Slient exceptions |
50 | 0 | } |
51 | | |
52 | | // Target str decodeFormattedHexString |
53 | 2.20k | try { |
54 | 2.20k | str::decodeFormattedHexString(payload, out); |
55 | 2.20k | } catch (const isc::Exception&) { |
56 | | // Slient exceptions |
57 | 2.03k | } catch (const boost::exception&) { |
58 | | // Slient exceptions |
59 | 0 | } |
60 | | |
61 | | // Target str decodeColonSeparatedHexString |
62 | 2.20k | try { |
63 | 2.20k | str::decodeColonSeparatedHexString(payload, out); |
64 | 2.20k | } catch (const isc::Exception&) { |
65 | | // Slient exceptions |
66 | 2.15k | } catch (const boost::exception&) { |
67 | | // Slient exceptions |
68 | 0 | } |
69 | | |
70 | | // Target str decodeSeparatedHexString |
71 | 2.20k | try { |
72 | 2.20k | str::decodeSeparatedHexString(payload, delim, out); |
73 | 2.20k | } catch (const isc::Exception&) { |
74 | | // Slient exceptions |
75 | 2.14k | } catch (const boost::exception&) { |
76 | | // Slient exceptions |
77 | 0 | } |
78 | | |
79 | | // Target str trim |
80 | 2.20k | try { |
81 | 2.20k | str::trim(payload); |
82 | 2.20k | } catch (const isc::Exception&) { |
83 | | // Slient exceptions |
84 | 0 | } catch (const boost::exception&) { |
85 | | // Slient exceptions |
86 | 0 | } |
87 | | |
88 | | // Target str lowercase/uppercase |
89 | 2.20k | try { |
90 | 2.20k | std::string temp = payload; |
91 | 2.20k | str::lowercase(temp); |
92 | 2.20k | str::uppercase(temp); |
93 | 2.20k | } catch (const isc::Exception&) { |
94 | | // Slient exceptions |
95 | 0 | } catch (const boost::exception&) { |
96 | | // Slient exceptions |
97 | 0 | } |
98 | | |
99 | | // Target CSVRow |
100 | 2.20k | try { |
101 | 2.20k | CSVRow row(payload, delim[0]); |
102 | 16.4M | for (int i = 0; i < row.getValuesCount(); i++) { |
103 | 16.4M | row.readAt(i); |
104 | 16.4M | row.readAtEscaped(i); |
105 | 16.4M | } |
106 | 2.20k | } catch (const isc::Exception&) { |
107 | | // Slient exceptions |
108 | 0 | } catch (const boost::exception&) { |
109 | | // Slient exceptions |
110 | 0 | } |
111 | | |
112 | | // Target encodeUtf8 |
113 | 2.20k | try { |
114 | 2.20k | encode::encodeUtf8(payload); |
115 | 2.20k | } catch (const isc::Exception&) { |
116 | | // Slient exceptions |
117 | 0 | } catch (const boost::exception&) { |
118 | | // Slient exceptions |
119 | 0 | } |
120 | | |
121 | | // Prepare posix_time object |
122 | 2.20k | ptime pt; |
123 | 2.20k | try { |
124 | 2.20k | pt = time_from_string(payload); |
125 | 2.20k | } catch (...) { |
126 | | // Failed for time_from_string, try from_iso_extended_string |
127 | 1.96k | try { |
128 | 1.96k | pt = from_iso_extended_string(payload); |
129 | 1.96k | } catch (...) { |
130 | | // Failed to create posix_time object, early exit |
131 | 1.85k | return 0; |
132 | 1.85k | } |
133 | 1.96k | } |
134 | | |
135 | | // Target ptimeToText |
136 | 346 | try { |
137 | 346 | ptimeToText(pt); |
138 | 346 | } catch (const isc::Exception&) { |
139 | | // Slient exceptions |
140 | 59 | } catch (const boost::exception&) { |
141 | | // Slient exceptions |
142 | 59 | } |
143 | | |
144 | | // Target durationToText |
145 | 346 | try { |
146 | 346 | time_duration td = pt.time_of_day(); |
147 | 346 | isc::util::durationToText(td); |
148 | 346 | } catch (const isc::Exception&) { |
149 | | // Slient exceptions |
150 | 0 | } catch (const boost::exception&) { |
151 | | // Slient exceptions |
152 | 0 | } |
153 | | |
154 | 346 | return 0; |
155 | 346 | } |
156 | | |