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 [ + - ]: 805 : FUZZ_TARGET(autofile)
19 : : {
20 : 329 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
21 : 329 : FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};
22 : 329 : const auto key_bytes{ConsumeFixedLengthByteVector<std::byte>(fuzzed_data_provider, Obfuscation::KEY_SIZE)};
23 : 329 : AutoFile auto_file{
24 : : fuzzed_file_provider.open(),
25 : 329 : Obfuscation{std::span{key_bytes}.first<Obfuscation::KEY_SIZE>()},
26 [ + - - + : 658 : };
+ - ]
27 [ + + + + ]: 7913 : LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 100) {
28 [ + - ]: 7584 : CallOneOf(
29 : : fuzzed_data_provider,
30 : 554 : [&] {
31 : 554 : std::array<std::byte, 4096> arr{};
32 : 554 : try {
33 [ + + ]: 554 : auto_file.read({arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)});
34 [ - + ]: 368 : } catch (const std::ios_base::failure&) {
35 : 368 : }
36 : 554 : },
37 : 1062 : [&] {
38 : 1062 : const std::array<std::byte, 4096> arr{};
39 : 1062 : try {
40 [ + + ]: 1062 : auto_file.write({arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)});
41 [ - + ]: 560 : } catch (const std::ios_base::failure&) {
42 : 560 : }
43 : 1062 : },
44 : 881 : [&] {
45 : 881 : try {
46 [ + + ]: 881 : auto_file.ignore(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
47 [ - + ]: 499 : } catch (const std::ios_base::failure&) {
48 : 499 : }
49 : 881 : },
50 : 226 : [&] {
51 : 226 : (void)auto_file.fclose();
52 : 226 : },
53 : 2612 : [&] {
54 : 2612 : ReadFromStream(fuzzed_data_provider, auto_file);
55 : 2612 : },
56 : 2249 : [&] {
57 : 2249 : WriteToStream(fuzzed_data_provider, auto_file);
58 : 2249 : });
59 : : }
60 : 329 : (void)auto_file.IsNull();
61 [ + + ]: 329 : if (fuzzed_data_provider.ConsumeBool()) {
62 [ + + ]: 17 : FILE* f = auto_file.release();
63 [ + + ]: 17 : if (f != nullptr) {
64 [ + - ]: 11 : fclose(f);
65 : : }
66 : : } else {
67 [ + - ]: 312 : (void)auto_file.fclose();
68 : : }
69 : 329 : }
|