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 [ + - ]: 975 : FUZZ_TARGET(autofile)
19 : : {
20 : 521 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
21 : 521 : FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};
22 : 521 : const auto key_bytes{ConsumeFixedLengthByteVector<std::byte>(fuzzed_data_provider, Obfuscation::KEY_SIZE)};
23 : 521 : AutoFile auto_file{
24 : : fuzzed_file_provider.open(),
25 : 521 : Obfuscation{std::span{key_bytes}.first<Obfuscation::KEY_SIZE>()},
26 [ + - + - ]: 521 : };
27 [ + + + + ]: 13015 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100)
28 : : {
29 [ + - ]: 12494 : CallOneOf(
30 : : fuzzed_data_provider,
31 : 713 : [&] {
32 : 713 : std::array<std::byte, 4096> arr{};
33 : 713 : try {
34 [ + + ]: 713 : auto_file.read({arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)});
35 [ - + ]: 505 : } catch (const std::ios_base::failure&) {
36 : 505 : }
37 : 713 : },
38 : 1642 : [&] {
39 : 1642 : const std::array<std::byte, 4096> arr{};
40 : 1642 : try {
41 [ + + ]: 1642 : auto_file.write({arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)});
42 [ - + ]: 655 : } catch (const std::ios_base::failure&) {
43 : 655 : }
44 : 1642 : },
45 : 1244 : [&] {
46 : 1244 : try {
47 [ + + ]: 1244 : auto_file.ignore(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
48 [ - + ]: 692 : } catch (const std::ios_base::failure&) {
49 : 692 : }
50 : 1244 : },
51 : 800 : [&] {
52 : 800 : (void)auto_file.fclose();
53 : 800 : },
54 : 4619 : [&] {
55 : 4619 : ReadFromStream(fuzzed_data_provider, auto_file);
56 : 4619 : },
57 : 3476 : [&] {
58 : 3476 : WriteToStream(fuzzed_data_provider, auto_file);
59 : 3476 : });
60 : : }
61 : 521 : (void)auto_file.IsNull();
62 [ + + ]: 521 : if (fuzzed_data_provider.ConsumeBool()) {
63 [ + + ]: 118 : FILE* f = auto_file.release();
64 [ + + ]: 118 : if (f != nullptr) {
65 [ + - ]: 86 : fclose(f);
66 : : }
67 : : } else {
68 [ + - ]: 403 : (void)auto_file.fclose();
69 : : }
70 : 521 : }
|