|
| 1 | +// Copyright (c) 2020 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 <optional.h> |
| 6 | +#include <streams.h> |
| 7 | +#include <test/fuzz/FuzzedDataProvider.h> |
| 8 | +#include <test/fuzz/fuzz.h> |
| 9 | +#include <test/fuzz/util.h> |
| 10 | + |
| 11 | +#include <array> |
| 12 | +#include <cstdint> |
| 13 | +#include <iostream> |
| 14 | +#include <optional> |
| 15 | +#include <string> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +void test_one_input(const std::vector<uint8_t>& buffer) |
| 19 | +{ |
| 20 | + FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; |
| 21 | + FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider); |
| 22 | + CAutoFile auto_file = fuzzed_auto_file_provider.open(); |
| 23 | + while (fuzzed_data_provider.ConsumeBool()) { |
| 24 | + switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 5)) { |
| 25 | + case 0: { |
| 26 | + std::array<uint8_t, 4096> arr{}; |
| 27 | + try { |
| 28 | + auto_file.read((char*)arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)); |
| 29 | + } catch (const std::ios_base::failure&) { |
| 30 | + } |
| 31 | + break; |
| 32 | + } |
| 33 | + case 1: { |
| 34 | + const std::array<uint8_t, 4096> arr{}; |
| 35 | + try { |
| 36 | + auto_file.write((const char*)arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)); |
| 37 | + } catch (const std::ios_base::failure&) { |
| 38 | + } |
| 39 | + break; |
| 40 | + } |
| 41 | + case 2: { |
| 42 | + try { |
| 43 | + auto_file.ignore(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)); |
| 44 | + } catch (const std::ios_base::failure&) { |
| 45 | + } |
| 46 | + break; |
| 47 | + } |
| 48 | + case 3: { |
| 49 | + auto_file.fclose(); |
| 50 | + break; |
| 51 | + } |
| 52 | + case 4: { |
| 53 | + ReadFromStream(fuzzed_data_provider, auto_file); |
| 54 | + break; |
| 55 | + } |
| 56 | + case 5: { |
| 57 | + WriteToStream(fuzzed_data_provider, auto_file); |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + (void)auto_file.Get(); |
| 63 | + (void)auto_file.GetType(); |
| 64 | + (void)auto_file.GetVersion(); |
| 65 | + (void)auto_file.IsNull(); |
| 66 | + if (fuzzed_data_provider.ConsumeBool()) { |
| 67 | + FILE* f = auto_file.release(); |
| 68 | + if (f != nullptr) { |
| 69 | + fclose(f); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments