Bitcoin Core 29.99.0
P2P Digital Currency
autofile.cpp
Go to the documentation of this file.
1// Copyright (c) 2020-2022 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>
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
18FUZZ_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 {
30 fuzzed_data_provider,
31 [&] {
32 std::array<std::byte, 4096> arr{};
33 try {
34 auto_file.read({arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)});
35 } catch (const std::ios_base::failure&) {
36 }
37 },
38 [&] {
39 const std::array<std::byte, 4096> arr{};
40 try {
41 auto_file.write({arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)});
42 } catch (const std::ios_base::failure&) {
43 }
44 },
45 [&] {
46 try {
47 auto_file.ignore(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
48 } catch (const std::ios_base::failure&) {
49 }
50 },
51 [&] {
52 (void)auto_file.fclose();
53 },
54 [&] {
55 ReadFromStream(fuzzed_data_provider, auto_file);
56 },
57 [&] {
58 WriteToStream(fuzzed_data_provider, auto_file);
59 });
60 }
61 (void)auto_file.IsNull();
62 if (fuzzed_data_provider.ConsumeBool()) {
63 FILE* f = auto_file.release();
64 if (f != nullptr) {
65 fclose(f);
66 }
67 } else {
68 (void)auto_file.fclose();
69 }
70}
void ReadFromStream(AddrMan &addr, DataStream &ssPeers)
Only used by tests.
Definition: addrdb.cpp:191
FUZZ_TARGET(autofile)
Definition: autofile.cpp:18
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:371
static constexpr size_t KEY_SIZE
Definition: obfuscation.h:23
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:22
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:35
void WriteToStream(FuzzedDataProvider &fuzzed_data_provider, Stream &stream) noexcept
Definition: util.h:291