Bitcoin Core
32.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
test
fuzz
autofile.cpp
Go to the documentation of this file.
1
// Copyright (c) 2020-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <
span.h
>
6
#include <
streams.h
>
7
#include <
test/fuzz/fuzz.h
>
8
#include <
test/fuzz/FuzzedDataProvider.h
>
9
#include <
test/fuzz/util.h
>
10
#include <
util/obfuscation.h
>
11
12
#include <array>
13
#include <cstddef>
14
#include <cstdio>
15
#include <iostream>
16
#include <vector>
17
18
FUZZ_TARGET
(autofile)
19
{
20
FuzzedDataProvider
fuzzed_data_provider{buffer.data(), buffer.size()};
21
FuzzedFileProvider
fuzzed_file_provider{fuzzed_data_provider};
22
const
auto
key_bytes{
ConsumeFixedLengthByteVector<std::byte>
(fuzzed_data_provider,
Obfuscation::KEY_SIZE
)};
23
AutoFile
auto_file{
24
fuzzed_file_provider.
open
(),
25
Obfuscation
{std::span{key_bytes}.first<
Obfuscation::KEY_SIZE
>()},
26
};
27
LIMITED_WHILE
(fuzzed_data_provider.
ConsumeBool
(), 100) {
28
CallOneOf
(
29
fuzzed_data_provider,
30
[&] {
31
std::array<std::byte, 4096> arr{};
32
try
{
33
auto_file.
read
({arr.data(), fuzzed_data_provider.
ConsumeIntegralInRange
<
size_t
>(0, 4096)});
34
}
catch
(
const
std::ios_base::failure&) {
35
}
36
},
37
[&] {
38
const
std::array<std::byte, 4096> arr{};
39
try
{
40
auto_file.
write
({arr.data(), fuzzed_data_provider.
ConsumeIntegralInRange
<
size_t
>(0, 4096)});
41
}
catch
(
const
std::ios_base::failure&) {
42
}
43
},
44
[&] {
45
try
{
46
auto_file.
ignore
(fuzzed_data_provider.
ConsumeIntegralInRange
<
size_t
>(0, 4096));
47
}
catch
(
const
std::ios_base::failure&) {
48
}
49
},
50
[&] {
51
(void)auto_file.
fclose
();
52
},
53
[&] {
54
ReadFromStream
(fuzzed_data_provider, auto_file);
55
},
56
[&] {
57
WriteToStream
(fuzzed_data_provider, auto_file);
58
});
59
}
60
(void)auto_file.
IsNull
();
61
if
(fuzzed_data_provider.
ConsumeBool
()) {
62
FILE* f = auto_file.
release
();
63
if
(f !=
nullptr
) {
64
fclose(f);
65
}
66
}
else
{
67
(void)auto_file.
fclose
();
68
}
69
}
FuzzedDataProvider.h
ReadFromStream
void ReadFromStream(AddrMan &addr, DataStream &ssPeers)
Only used by tests.
Definition
addrdb.cpp:191
AutoFile
Non-refcounted RAII wrapper for FILE*.
Definition
streams.h:395
AutoFile::release
std::FILE * release()
Get wrapped FILE* with transfer of ownership.
Definition
streams.h:439
AutoFile::ignore
void ignore(size_t nSize)
Definition
streams.cpp:81
AutoFile::IsNull
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition
streams.h:448
AutoFile::write
void write(std::span< const std::byte > src)
Definition
streams.cpp:95
AutoFile::read
void read(std::span< std::byte > dst)
Definition
streams.cpp:74
AutoFile::fclose
int fclose()
Definition
streams.h:429
FuzzedDataProvider
Definition
FuzzedDataProvider.h:32
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition
FuzzedDataProvider.h:289
FuzzedDataProvider::ConsumeIntegralInRange
T ConsumeIntegralInRange(T min, T max)
Definition
FuzzedDataProvider.h:205
FuzzedFileProvider
Definition
util.h:289
FuzzedFileProvider::open
FILE * open()
Definition
util.cpp:251
Obfuscation
Definition
obfuscation.h:21
Obfuscation::KEY_SIZE
static constexpr size_t KEY_SIZE
Definition
obfuscation.h:24
fuzz.h
FUZZ_TARGET
#define FUZZ_TARGET(...)
Definition
fuzz.h:35
LIMITED_WHILE
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition
fuzz.h:22
obfuscation.h
span.h
streams.h
util.h
ConsumeFixedLengthByteVector
std::vector< B > ConsumeFixedLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const size_t length) noexcept
Returns a byte vector of specified size regardless of the number of remaining bytes available from th...
Definition
util.h:280
CallOneOf
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition
util.h:37
WriteToStream
void WriteToStream(FuzzedDataProvider &fuzzed_data_provider, Stream &stream) noexcept
Definition
util.h:315
Generated on
for Bitcoin Core by
1.18.0