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