forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: Add fuzzing harness for CBufferedFile (streams.h)
- Loading branch information
1 parent
f3aa659
commit 9823376
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) 2020 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <optional.h> | ||
#include <streams.h> | ||
#include <test/fuzz/FuzzedDataProvider.h> | ||
#include <test/fuzz/fuzz.h> | ||
#include <test/fuzz/util.h> | ||
|
||
#include <array> | ||
#include <cstdint> | ||
#include <iostream> | ||
#include <optional> | ||
#include <string> | ||
#include <vector> | ||
|
||
void test_one_input(const std::vector<uint8_t>& buffer) | ||
{ | ||
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; | ||
FuzzedFileProvider fuzzed_file_provider = ConsumeFile(fuzzed_data_provider); | ||
std::optional<CBufferedFile> opt_buffered_file; | ||
FILE* fuzzed_file = fuzzed_file_provider.open(); | ||
try { | ||
opt_buffered_file.emplace(fuzzed_file, fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096), fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096), fuzzed_data_provider.ConsumeIntegral<int>(), fuzzed_data_provider.ConsumeIntegral<int>()); | ||
} catch (const std::ios_base::failure&) { | ||
if (fuzzed_file != nullptr) { | ||
fclose(fuzzed_file); | ||
} | ||
} | ||
if (opt_buffered_file && fuzzed_file != nullptr) { | ||
while (fuzzed_data_provider.ConsumeBool()) { | ||
switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 4)) { | ||
case 0: { | ||
std::array<uint8_t, 4096> arr{}; | ||
try { | ||
opt_buffered_file->read((char*)arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)); | ||
} catch (const std::ios_base::failure&) { | ||
} | ||
break; | ||
} | ||
case 1: { | ||
opt_buffered_file->Seek(fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096)); | ||
break; | ||
} | ||
case 2: { | ||
opt_buffered_file->SetLimit(fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096)); | ||
break; | ||
} | ||
case 3: { | ||
try { | ||
opt_buffered_file->FindByte(fuzzed_data_provider.ConsumeIntegral<char>()); | ||
} catch (const std::ios_base::failure&) { | ||
} | ||
break; | ||
} | ||
case 4: { | ||
ReadFromStream(fuzzed_data_provider, *opt_buffered_file); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |