/root/bitcoin/src/test/fuzz/autofile.cpp
Line | Count | Source |
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 | | FUZZ_TARGET(autofile) |
19 | 233 | { |
20 | 233 | FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; |
21 | 233 | FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider}; |
22 | 233 | const auto key_bytes{ConsumeFixedLengthByteVector<std::byte>(fuzzed_data_provider, Obfuscation::KEY_SIZE)}; |
23 | 233 | AutoFile auto_file{ |
24 | 233 | fuzzed_file_provider.open(), |
25 | 233 | Obfuscation{std::span{key_bytes}.first<Obfuscation::KEY_SIZE>()}, |
26 | 233 | }; |
27 | 4.49k | LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 100) { |
28 | 4.49k | CallOneOf( |
29 | 4.49k | fuzzed_data_provider, |
30 | 4.49k | [&] { |
31 | 342 | std::array<std::byte, 4096> arr{}; |
32 | 342 | try { |
33 | 342 | auto_file.read({arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)}); |
34 | 342 | } catch (const std::ios_base::failure&) { |
35 | 222 | } |
36 | 342 | }, |
37 | 4.49k | [&] { |
38 | 644 | const std::array<std::byte, 4096> arr{}; |
39 | 644 | try { |
40 | 644 | auto_file.write({arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)}); |
41 | 644 | } catch (const std::ios_base::failure&) { |
42 | 342 | } |
43 | 644 | }, |
44 | 4.49k | [&] { |
45 | 627 | try { |
46 | 627 | auto_file.ignore(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)); |
47 | 627 | } catch (const std::ios_base::failure&) { |
48 | 363 | } |
49 | 627 | }, |
50 | 4.49k | [&] { |
51 | 132 | (void)auto_file.fclose(); |
52 | 132 | }, |
53 | 4.49k | [&] { |
54 | 1.43k | ReadFromStream(fuzzed_data_provider, auto_file); |
55 | 1.43k | }, |
56 | 4.49k | [&] { |
57 | 1.31k | WriteToStream(fuzzed_data_provider, auto_file); |
58 | 1.31k | }); |
59 | 4.49k | } |
60 | 233 | (void)auto_file.IsNull(); |
61 | 233 | if (fuzzed_data_provider.ConsumeBool()) { Branch (61:9): [True: 11, False: 222]
|
62 | 11 | FILE* f = auto_file.release(); |
63 | 11 | if (f != nullptr) { Branch (63:13): [True: 8, False: 3]
|
64 | 8 | fclose(f); |
65 | 8 | } |
66 | 222 | } else { |
67 | 222 | (void)auto_file.fclose(); |
68 | 222 | } |
69 | 233 | } |