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