1// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2// See https://llvm.org/LICENSE.txt for license information.
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#include "gtest/gtest.h"
6#include <cstdint>
7#include <cstdlib>
8
9#include <fuzzer/FuzzedDataProvider.h>
10
11// The test is intentionally extensive, as behavior of |FuzzedDataProvider| must
12// not be broken, given than many fuzz targets depend on it. Changing the
13// behavior might invalidate existing corpora and make the fuzz targets using
14// |FuzzedDataProvider| to lose code coverage accumulated over time.
15
16/* A random 1KB buffer generated by:
17$ python -c "import os; print ',\n'.join([', '.join(['0x%02X' % ord(i) for i \
18 in list(os.urandom(8))]) for _ in xrange(128)])"
19*/
20const uint8_t Data[] = {
21 0x8A, 0x19, 0x0D, 0x44, 0x37, 0x0D, 0x38, 0x5E, 0x9B, 0xAA, 0xF3, 0xDA,
22 0xAA, 0x88, 0xF2, 0x9B, 0x6C, 0xBA, 0xBE, 0xB1, 0xF2, 0xCF, 0x13, 0xB8,
23 0xAC, 0x1A, 0x7F, 0x1C, 0xC9, 0x90, 0xD0, 0xD9, 0x5C, 0x42, 0xB3, 0xFD,
24 0xE3, 0x05, 0xA4, 0x03, 0x37, 0x49, 0x50, 0x4B, 0xBC, 0x39, 0xA2, 0x09,
25 0x6C, 0x2F, 0xAF, 0xD1, 0xB5, 0x47, 0xBF, 0x92, 0xBD, 0x79, 0xE5, 0xC5,
26 0x6E, 0x51, 0xA4, 0xED, 0xE9, 0xBD, 0x40, 0x4A, 0xFC, 0x25, 0x7A, 0x27,
27 0xC8, 0x92, 0xF7, 0x30, 0xDE, 0x40, 0x66, 0x66, 0xE8, 0x5F, 0x65, 0x39,
28 0x7E, 0x9E, 0x80, 0x2B, 0x01, 0x71, 0x2A, 0xFF, 0xD3, 0x0A, 0xAC, 0x6E,
29 0x49, 0x32, 0x79, 0x10, 0x6A, 0x6F, 0x97, 0x96, 0x70, 0x7E, 0x50, 0x65,
30 0xC9, 0x1D, 0xBD, 0x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 0xEB, 0x30, 0xF9, 0x51, 0xF6,
105 0x1F, 0x3A, 0x11, 0x4D, 0xFD, 0x54, 0xD6, 0x3D, 0x43, 0x73, 0x39, 0x16,
106 0xCF, 0x3D, 0x29, 0x4A};
107
108TEST(FuzzedDataProvider, ConsumeBytes) {
109 FuzzedDataProvider DataProv(Data, sizeof(Data));
110 EXPECT_EQ(std::vector<unsigned char>(1, 0x8A),
111 DataProv.ConsumeBytes<unsigned char>(num_bytes: 1));
112 EXPECT_EQ(std::vector<uint8_t>(
113 {0x19, 0x0D, 0x44, 0x37, 0x0D, 0x38, 0x5E, 0x9B, 0xAA, 0xF3}),
114 DataProv.ConsumeBytes<uint8_t>(num_bytes: 10));
115
116 std::vector<unsigned char> UChars = DataProv.ConsumeBytes<unsigned char>(num_bytes: 24);
117 EXPECT_EQ(std::vector<unsigned char>({0xDA, 0xAA, 0x88, 0xF2, 0x9B, 0x6C,
118 0xBA, 0xBE, 0xB1, 0xF2, 0xCF, 0x13,
119 0xB8, 0xAC, 0x1A, 0x7F, 0x1C, 0xC9,
120 0x90, 0xD0, 0xD9, 0x5C, 0x42, 0xB3}),
121 UChars);
122
123 EXPECT_EQ(std::vector<signed char>(Data + 1 + 10 + 24, Data + sizeof(Data)),
124 DataProv.ConsumeBytes<signed char>(num_bytes: 31337));
125}
126
127TEST(FuzzedDataProvider, ConsumeBytesWithTerminator) {
128 FuzzedDataProvider DataProv(Data, sizeof(Data));
129 EXPECT_EQ(std::vector<unsigned char>({0x8A, 0x00}),
130 DataProv.ConsumeBytesWithTerminator<unsigned char>(num_bytes: 1));
131 EXPECT_EQ(std::vector<uint8_t>({0x19, 0x0D, 0x44, 0x37, 0x0D, 0x38, 0x5E,
132 0x9B, 0xAA, 0xF3, 111}),
133 DataProv.ConsumeBytesWithTerminator<uint8_t>(num_bytes: 10, terminator: 111));
134
135 std::vector<unsigned char> UChars =
136 DataProv.ConsumeBytesWithTerminator<unsigned char>(num_bytes: 24);
137 EXPECT_EQ(std::vector<unsigned char>(
138 {0xDA, 0xAA, 0x88, 0xF2, 0x9B, 0x6C, 0xBA, 0xBE, 0xB1,
139 0xF2, 0xCF, 0x13, 0xB8, 0xAC, 0x1A, 0x7F, 0x1C, 0xC9,
140 0x90, 0xD0, 0xD9, 0x5C, 0x42, 0xB3, 0x00}),
141 UChars);
142
143 std::vector<signed char> Expected(Data + 1 + 10 + 24, Data + sizeof(Data));
144 Expected.push_back(x: 65);
145 EXPECT_EQ(Expected,
146 DataProv.ConsumeBytesWithTerminator<signed char>(num_bytes: 31337, terminator: 65));
147}
148
149TEST(FuzzedDataProvider, ConsumeBytesAsString) {
150 FuzzedDataProvider DataProv(Data, sizeof(Data));
151 EXPECT_EQ(std::string("\x8A\x19\x0D\x44\x37\x0D\x38\x5E\x9B\xAA\xF3\xDA"),
152 DataProv.ConsumeBytesAsString(num_bytes: 12));
153 EXPECT_EQ(std::string(Data + 12, Data + sizeof(Data)),
154 DataProv.ConsumeBytesAsString(num_bytes: 31337));
155}
156
157TEST(FuzzedDataProvider, ConsumeIntegralInRange) {
158 FuzzedDataProvider DataProv(Data, sizeof(Data));
159 EXPECT_EQ(int32_t(21), DataProv.ConsumeIntegralInRange<int32_t>(min: 10, max: 30));
160 EXPECT_EQ(int32_t(1337),
161 DataProv.ConsumeIntegralInRange<int32_t>(min: 1337, max: 1337));
162 EXPECT_EQ(int8_t(-59), DataProv.ConsumeIntegralInRange<int8_t>(min: -100, max: 100));
163 EXPECT_EQ(uint16_t(15823),
164 DataProv.ConsumeIntegralInRange<uint16_t>(min: 0, max: 65535));
165 EXPECT_EQ((signed char)(-101),
166 DataProv.ConsumeIntegralInRange<signed char>(min: -123, max: 123));
167 EXPECT_EQ(int64_t(-53253077544), DataProv.ConsumeIntegralInRange<int64_t>(
168 min: -99999999999, max: 99999999999));
169
170 // Exhaust the buffer.
171 auto String = DataProv.ConsumeBytesAsString(num_bytes: 31337);
172 EXPECT_EQ(size_t(1014), String.length());
173 EXPECT_EQ(uint64_t(123456789),
174 DataProv.ConsumeIntegralInRange<uint64_t>(min: 123456789, max: 987654321));
175}
176
177TEST(FuzzedDataProvider, ConsumeRandomLengthString) {
178 FuzzedDataProvider DataProv(Data, sizeof(Data));
179 EXPECT_EQ(
180 std::string(
181 "\x8A\x19\x0D\x44\x37\x0D\x38\x5E\x9B\xAA\xF3\xDA\xAA\x88\xF2\x9B\x6C"
182 "\xBA\xBE\xB1\xF2\xCF\x13\xB8\xAC\x1A\x7F\x1C\xC9\x90\xD0\xD9"),
183 DataProv.ConsumeRandomLengthString(max_length: 1337));
184 EXPECT_EQ(std::string(
185 "\xB3\xFD\xE3\x05\xA4\x03\x37\x49\x50\x4B\xBC\x39\xA2\x09\x6C"
186 "\x2F\xAF\xD1\xB5\x47\xBF\x92\xBD\x79\xE5\xC5\x6E\x51\xA4\xED"
187 "\xE9\xBD\x40\x4A\xFC\x25\x7A\x27\xC8\x92\xF7\x30\xDE\x40\x66"
188 "\x66\xE8\x5F\x65\x39\x7E\x9E\x80\x2B\x01\x71\x2A\xFF\xD3\x0A"
189 "\xAC\x6E\x49\x32\x79\x10\x6A\x6F\x97\x96\x70\x7E\x50\x65\xC9"
190 "\x1D\xBD\x4E\x17\x04\x1E\xBA\x26\xAC\x1F\xE3\x37\x1C\x15\x43"
191 "\x60\x41\x2A\x7C\xCA\x70\xCE\xAB\x20\x24\xF8\xD9\x1F\x14\x7C"),
192 DataProv.ConsumeRandomLengthString(max_length: 31337));
193 size_t Offset = 141;
194 EXPECT_EQ(std::string(Data + Offset, Data + Offset + 5),
195 DataProv.ConsumeRandomLengthString(max_length: 5));
196 Offset += 5;
197 EXPECT_EQ(std::string(Data + Offset, Data + Offset + 2),
198 DataProv.ConsumeRandomLengthString(max_length: 2));
199 Offset += 2;
200
201 // Call the overloaded method without arguments (uses max length available).
202 EXPECT_EQ(std::string(Data + Offset, Data + Offset + 664),
203 DataProv.ConsumeRandomLengthString());
204 Offset += 664 + 2; // +2 because of '\' character followed by any other byte.
205
206 EXPECT_EQ(std::string(Data + Offset, Data + Offset + 92),
207 DataProv.ConsumeRandomLengthString());
208 Offset += 92 + 2;
209
210 // Exhaust the buffer.
211 auto String = DataProv.ConsumeBytesAsString(num_bytes: 31337);
212 EXPECT_EQ(size_t(116), String.length());
213 EXPECT_EQ(std::string(), DataProv.ConsumeRandomLengthString(max_length: 1));
214}
215
216TEST(FuzzedDataProvider, ConsumeRemainingBytes) {
217 {
218 FuzzedDataProvider DataProv(Data, sizeof(Data));
219 EXPECT_EQ(std::vector<uint8_t>(Data, Data + sizeof(Data)),
220 DataProv.ConsumeRemainingBytes<uint8_t>());
221 EXPECT_EQ(std::vector<uint8_t>(),
222 DataProv.ConsumeRemainingBytes<uint8_t>());
223 }
224
225 {
226 FuzzedDataProvider DataProv(Data, sizeof(Data));
227 EXPECT_EQ(std::vector<uint8_t>(Data, Data + 123),
228 DataProv.ConsumeBytes<uint8_t>(num_bytes: 123));
229 EXPECT_EQ(std::vector<char>(Data + 123, Data + sizeof(Data)),
230 DataProv.ConsumeRemainingBytes<char>());
231 }
232}
233
234TEST(FuzzedDataProvider, ConsumeRemainingBytesAsString) {
235 {
236 FuzzedDataProvider DataProv(Data, sizeof(Data));
237 EXPECT_EQ(std::string(Data, Data + sizeof(Data)),
238 DataProv.ConsumeRemainingBytesAsString());
239 EXPECT_EQ(std::string(""), DataProv.ConsumeRemainingBytesAsString());
240 }
241
242 {
243 FuzzedDataProvider DataProv(Data, sizeof(Data));
244 EXPECT_EQ(std::vector<uint8_t>(Data, Data + 123),
245 DataProv.ConsumeBytes<uint8_t>(num_bytes: 123));
246 EXPECT_EQ(std::string(Data + 123, Data + sizeof(Data)),
247 DataProv.ConsumeRemainingBytesAsString());
248 }
249}
250
251TEST(FuzzedDataProvider, ConsumeIntegral) {
252 FuzzedDataProvider DataProv(Data, sizeof(Data));
253 EXPECT_EQ(int32_t(-903266865), DataProv.ConsumeIntegral<int32_t>());
254 EXPECT_EQ(uint32_t(372863811), DataProv.ConsumeIntegral<uint32_t>());
255 EXPECT_EQ(uint8_t(61), DataProv.ConsumeIntegral<uint8_t>());
256 EXPECT_EQ(int16_t(22100), DataProv.ConsumeIntegral<int16_t>());
257 EXPECT_EQ(uint64_t(18252263806144500217u),
258 DataProv.ConsumeIntegral<uint64_t>());
259
260 // Exhaust the buffer.
261 auto String = DataProv.ConsumeBytesAsString(num_bytes: 31337);
262 EXPECT_EQ(size_t(1005), String.length());
263 EXPECT_EQ(std::numeric_limits<int64_t>::min(),
264 DataProv.ConsumeIntegral<int64_t>());
265}
266
267TEST(FuzzedDataProvider, ConsumeBool) {
268 FuzzedDataProvider DataProv(Data, sizeof(Data));
269 EXPECT_EQ(false, DataProv.ConsumeBool());
270 EXPECT_EQ(true, DataProv.ConsumeBool());
271 EXPECT_EQ(true, DataProv.ConsumeBool());
272 EXPECT_EQ(true, DataProv.ConsumeBool());
273 EXPECT_EQ(false, DataProv.ConsumeBool());
274 EXPECT_EQ(true, DataProv.ConsumeBool());
275 EXPECT_EQ(true, DataProv.ConsumeBool());
276 EXPECT_EQ(true, DataProv.ConsumeBool());
277 EXPECT_EQ(true, DataProv.ConsumeBool());
278 EXPECT_EQ(false, DataProv.ConsumeBool());
279
280 // Exhaust the buffer.
281 auto String = DataProv.ConsumeBytesAsString(num_bytes: 31337);
282 EXPECT_EQ(size_t(1014), String.length());
283 EXPECT_EQ(false, DataProv.ConsumeBool());
284}
285
286TEST(FuzzedDataProvider, PickValueInStdArray) {
287 FuzzedDataProvider DataProv(Data, sizeof(Data));
288 const std::array<int, 5> Array = {1, 2, 3, 4, 5};
289 EXPECT_EQ(5, DataProv.PickValueInArray(array: Array));
290 EXPECT_EQ(2, DataProv.PickValueInArray(array: Array));
291 EXPECT_EQ(2, DataProv.PickValueInArray(array: Array));
292 EXPECT_EQ(3, DataProv.PickValueInArray(array: Array));
293 EXPECT_EQ(3, DataProv.PickValueInArray(array: Array));
294 EXPECT_EQ(3, DataProv.PickValueInArray(array: Array));
295 EXPECT_EQ(1, DataProv.PickValueInArray(array: Array));
296 EXPECT_EQ(3, DataProv.PickValueInArray(array: Array));
297 EXPECT_EQ(2, DataProv.PickValueInArray(array: Array));
298}
299
300TEST(FuzzedDataProvider, PickValueInArray) {
301 FuzzedDataProvider DataProv(Data, sizeof(Data));
302 const int Array[] = {1, 2, 3, 4, 5};
303 EXPECT_EQ(5, DataProv.PickValueInArray(array: Array));
304 EXPECT_EQ(2, DataProv.PickValueInArray(array: Array));
305 EXPECT_EQ(2, DataProv.PickValueInArray(array: Array));
306 EXPECT_EQ(3, DataProv.PickValueInArray(array: Array));
307 EXPECT_EQ(3, DataProv.PickValueInArray(array: Array));
308 EXPECT_EQ(3, DataProv.PickValueInArray(array: Array));
309 EXPECT_EQ(1, DataProv.PickValueInArray(array: Array));
310 EXPECT_EQ(3, DataProv.PickValueInArray(array: Array));
311 EXPECT_EQ(2, DataProv.PickValueInArray(array: Array));
312
313 EXPECT_EQ(uint8_t(0x9D), DataProv.PickValueInArray(array: Data));
314 EXPECT_EQ(uint8_t(0xBA), DataProv.PickValueInArray(array: Data));
315 EXPECT_EQ(uint8_t(0x69), DataProv.PickValueInArray(array: Data));
316 EXPECT_EQ(uint8_t(0xD6), DataProv.PickValueInArray(array: Data));
317
318 EXPECT_EQ(uint32_t(777), DataProv.PickValueInArray<uint32_t>(list: {1337, 777}));
319 EXPECT_EQ(uint32_t(777), DataProv.PickValueInArray<uint32_t>(list: {1337, 777}));
320 EXPECT_EQ(uint64_t(1337), DataProv.PickValueInArray<uint64_t>(list: {1337, 777}));
321 EXPECT_EQ(size_t(777), DataProv.PickValueInArray<size_t>(list: {1337, 777}));
322 EXPECT_EQ(int16_t(1337), DataProv.PickValueInArray<int16_t>(list: {1337, 777}));
323 EXPECT_EQ(int32_t(777), DataProv.PickValueInArray<int32_t>(list: {1337, 777}));
324 EXPECT_EQ(int64_t(777), DataProv.PickValueInArray<int64_t>(list: {1337, 777}));
325
326 // Exhaust the buffer.
327 auto String = DataProv.ConsumeBytesAsString(num_bytes: 31337);
328 EXPECT_EQ(size_t(1000), String.length());
329 EXPECT_EQ(uint8_t(0x8A), DataProv.PickValueInArray(array: Data));
330}
331
332TEST(FuzzedDataProvider, ConsumeEnum) {
333 FuzzedDataProvider DataProv(Data, sizeof(Data));
334 enum class Enum {
335 Zero,
336 One,
337 Two,
338 Three,
339 Four,
340 Five,
341 Six,
342 Seven,
343 kMaxValue = Seven
344 };
345 EXPECT_EQ(Enum::Two, DataProv.ConsumeEnum<Enum>());
346 EXPECT_EQ(Enum::One, DataProv.ConsumeEnum<Enum>());
347 EXPECT_EQ(Enum::Five, DataProv.ConsumeEnum<Enum>());
348 EXPECT_EQ(Enum::Seven, DataProv.ConsumeEnum<Enum>());
349 EXPECT_EQ(Enum::Six, DataProv.ConsumeEnum<Enum>());
350 EXPECT_EQ(Enum::One, DataProv.ConsumeEnum<Enum>());
351 EXPECT_EQ(Enum::Three, DataProv.ConsumeEnum<Enum>());
352 EXPECT_EQ(Enum::Three, DataProv.ConsumeEnum<Enum>());
353 EXPECT_EQ(Enum::Five, DataProv.ConsumeEnum<Enum>());
354 EXPECT_EQ(Enum::Six, DataProv.ConsumeEnum<Enum>());
355
356 // Exhaust the buffer.
357 auto String = DataProv.ConsumeBytesAsString(num_bytes: 31337);
358 EXPECT_EQ(size_t(1014), String.length());
359 EXPECT_EQ(Enum::Zero, DataProv.ConsumeEnum<Enum>());
360}
361
362TEST(FuzzedDataProvider, remaining_bytes) {
363 FuzzedDataProvider DataProv(Data, sizeof(Data));
364 EXPECT_EQ(size_t(1024), DataProv.remaining_bytes());
365 EXPECT_EQ(false, DataProv.ConsumeBool());
366 EXPECT_EQ(size_t(1024 - 1), DataProv.remaining_bytes());
367 EXPECT_EQ(std::vector<uint8_t>(Data, Data + 8),
368 DataProv.ConsumeBytes<uint8_t>(num_bytes: 8));
369 EXPECT_EQ(size_t(1024 - 1 - 8), DataProv.remaining_bytes());
370
371 // Exhaust the buffer.
372 EXPECT_EQ(std::vector<uint8_t>(Data + 8, Data + sizeof(Data) - 1),
373 DataProv.ConsumeRemainingBytes<uint8_t>());
374 EXPECT_EQ(size_t(0), DataProv.remaining_bytes());
375}
376
377TEST(FuzzedDataProvider, ConsumeProbability) {
378 FuzzedDataProvider DataProv(Data, sizeof(Data));
379 ASSERT_FLOAT_EQ(float(0.28969181), DataProv.ConsumeProbability<float>());
380 ASSERT_DOUBLE_EQ(double(0.086814121166605432),
381 DataProv.ConsumeProbability<double>());
382 ASSERT_FLOAT_EQ(float(0.30104411), DataProv.ConsumeProbability<float>());
383 ASSERT_DOUBLE_EQ(double(0.96218831486039413),
384 DataProv.ConsumeProbability<double>());
385 ASSERT_FLOAT_EQ(float(0.67005056), DataProv.ConsumeProbability<float>());
386 ASSERT_DOUBLE_EQ(double(0.69210584173832279),
387 DataProv.ConsumeProbability<double>());
388
389 // Exhaust the buffer.
390 EXPECT_EQ(std::vector<uint8_t>(Data, Data + sizeof(Data) - 36),
391 DataProv.ConsumeRemainingBytes<uint8_t>());
392 ASSERT_FLOAT_EQ(float(0.0), DataProv.ConsumeProbability<float>());
393}
394
395TEST(FuzzedDataProvider, ConsumeFloatingPoint) {
396 FuzzedDataProvider DataProv(Data, sizeof(Data));
397 ASSERT_FLOAT_EQ(float(-2.8546307e+38),
398 DataProv.ConsumeFloatingPoint<float>());
399 ASSERT_DOUBLE_EQ(double(8.0940194040236032e+307),
400 DataProv.ConsumeFloatingPoint<double>());
401 ASSERT_FLOAT_EQ(float(271.49084),
402 DataProv.ConsumeFloatingPointInRange<float>(min: 123.0, max: 777.0));
403 ASSERT_DOUBLE_EQ(double(30.859126145478349),
404 DataProv.ConsumeFloatingPointInRange<double>(min: 13.37, max: 31.337));
405 ASSERT_FLOAT_EQ(
406 float(-903.47729),
407 DataProv.ConsumeFloatingPointInRange<float>(min: -999.9999, max: -777.77));
408 ASSERT_DOUBLE_EQ(
409 double(24.561393182922771),
410 DataProv.ConsumeFloatingPointInRange<double>(min: -13.37, max: 31.337));
411 ASSERT_FLOAT_EQ(float(1.0),
412 DataProv.ConsumeFloatingPointInRange<float>(min: 1.0, max: 1.0));
413 ASSERT_DOUBLE_EQ(double(-1.0),
414 DataProv.ConsumeFloatingPointInRange<double>(min: -1.0, max: -1.0));
415
416 // Exhaust the buffer.
417 EXPECT_EQ((std::vector<uint8_t>(Data, Data + sizeof(Data) - 50)).size(),
418 DataProv.ConsumeRemainingBytes<uint8_t>().size());
419 ASSERT_FLOAT_EQ(float(0.0), DataProv.ConsumeProbability<float>());
420 ASSERT_NEAR(std::numeric_limits<double>::lowest(),
421 DataProv.ConsumeFloatingPoint<double>(), 1e-10);
422 ASSERT_FLOAT_EQ(float(123.0),
423 DataProv.ConsumeFloatingPointInRange<float>(min: 123.0, max: 777.0));
424 ASSERT_DOUBLE_EQ(double(-13.37), DataProv.ConsumeFloatingPointInRange<double>(
425 min: -13.37, max: 31.337));
426}
427
428TEST(FuzzedDataProvider, ConsumeData) {
429 FuzzedDataProvider DataProv(Data, sizeof(Data));
430 uint8_t Buffer[10] = {};
431 EXPECT_EQ(sizeof(Buffer), DataProv.ConsumeData(destination: Buffer, num_bytes: sizeof(Buffer)));
432 std::vector<uint8_t> Expected(Data, Data + sizeof(Buffer));
433 EXPECT_EQ(Expected, std::vector<uint8_t>(Buffer, Buffer + sizeof(Buffer)));
434
435 EXPECT_EQ(size_t(2), DataProv.ConsumeData(destination: Buffer, num_bytes: 2));
436 Expected[0] = Data[sizeof(Buffer)];
437 Expected[1] = Data[sizeof(Buffer) + 1];
438 EXPECT_EQ(Expected, std::vector<uint8_t>(Buffer, Buffer + sizeof(Buffer)));
439
440 // Exhaust the buffer.
441 EXPECT_EQ(std::vector<uint8_t>(Data + 12, Data + sizeof(Data)),
442 DataProv.ConsumeRemainingBytes<uint8_t>());
443 EXPECT_EQ(size_t(0), DataProv.ConsumeData(destination: Buffer, num_bytes: sizeof(Buffer)));
444 EXPECT_EQ(Expected, std::vector<uint8_t>(Buffer, Buffer + sizeof(Buffer)));
445}
446
447int main(int argc, char **argv) {
448 testing::InitGoogleTest(&argc, argv);
449 return RUN_ALL_TESTS();
450}
451

source code of compiler-rt/lib/fuzzer/tests/FuzzedDataProviderUnittest.cpp