Branch data Line data Source code
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>
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 [ + - ]: 675 : FUZZ_TARGET(autofile)
19 : : {
20 : 219 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
21 : 219 : FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};
22 : 219 : const auto key_bytes{ConsumeFixedLengthByteVector<std::byte>(fuzzed_data_provider, Obfuscation::KEY_SIZE)};
23 : 219 : AutoFile auto_file{
24 : : fuzzed_file_provider.open(),
25 : 219 : Obfuscation{std::span{key_bytes}.first<Obfuscation::KEY_SIZE>()},
26 [ + - - + : 438 : };
+ - ]
27 [ + + + + ]: 4937 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100)
28 : : {
29 [ + - ]: 4718 : CallOneOf(
30 : : fuzzed_data_provider,
31 : 295 : [&] {
32 : 295 : std::array<std::byte, 4096> arr{};
33 : 295 : try {
34 [ + + ]: 295 : auto_file.read({arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)});
35 [ - + ]: 180 : } catch (const std::ios_base::failure&) {
36 : 180 : }
37 : 295 : },
38 : 576 : [&] {
39 : 576 : const std::array<std::byte, 4096> arr{};
40 : 576 : try {
41 [ + + ]: 576 : auto_file.write({arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)});
42 [ - + ]: 241 : } catch (const std::ios_base::failure&) {
43 : 241 : }
44 : 576 : },
45 : 536 : [&] {
46 : 536 : try {
47 [ + + ]: 536 : auto_file.ignore(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
48 [ - + ]: 315 : } catch (const std::ios_base::failure&) {
49 : 315 : }
50 : 536 : },
51 : 146 : [&] {
52 : 146 : (void)auto_file.fclose();
53 : 146 : },
54 : 1800 : [&] {
55 : 1800 : ReadFromStream(fuzzed_data_provider, auto_file);
56 : 1800 : },
57 : 1365 : [&] {
58 : 1365 : WriteToStream(fuzzed_data_provider, auto_file);
59 : 1365 : });
60 : : }
61 : 219 : (void)auto_file.IsNull();
62 [ + + ]: 219 : if (fuzzed_data_provider.ConsumeBool()) {
63 [ + + ]: 19 : FILE* f = auto_file.release();
64 [ + + ]: 19 : if (f != nullptr) {
65 [ + - ]: 13 : fclose(f);
66 : : }
67 : : } else {
68 [ + - ]: 200 : (void)auto_file.fclose();
69 : : }
70 : 219 : }
|