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