LCOV - code coverage report
Current view: top level - src/test/fuzz - buffered_file.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 2 42 4.8 %
Date: 2023-11-10 23:46:46 Functions: 1 8 12.5 %
Branches: 2 50 4.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2020-2022 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 <streams.h>
       6                 :            : #include <test/fuzz/FuzzedDataProvider.h>
       7                 :            : #include <test/fuzz/fuzz.h>
       8                 :            : #include <test/fuzz/util.h>
       9                 :            : 
      10                 :            : #include <array>
      11                 :            : #include <cstdint>
      12                 :            : #include <iostream>
      13                 :            : #include <optional>
      14                 :            : #include <string>
      15                 :            : #include <vector>
      16                 :            : 
      17 [ +  - ][ +  - ]:          6 : FUZZ_TARGET(buffered_file)
      18                 :            : {
      19                 :          0 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
      20                 :          0 :     FuzzedFileProvider fuzzed_file_provider = ConsumeFile(fuzzed_data_provider);
      21                 :          0 :     std::optional<BufferedFile> opt_buffered_file;
      22 [ #  # ][ #  # ]:          0 :     CAutoFile fuzzed_file{fuzzed_file_provider.open(), 0};
      23                 :            :     try {
      24 [ #  # ][ #  # ]:          0 :         opt_buffered_file.emplace(fuzzed_file, fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096), fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096));
                 [ #  # ]
      25         [ #  # ]:          2 :     } catch (const std::ios_base::failure&) {
      26         [ #  # ]:          0 :     }
      27 [ #  # ][ #  # ]:          0 :     if (opt_buffered_file && !fuzzed_file.IsNull()) {
                 [ #  # ]
      28                 :          0 :         bool setpos_fail = false;
      29 [ #  # ][ #  # ]:          0 :         LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
                 [ #  # ]
      30         [ #  # ]:          0 :             CallOneOf(
      31                 :            :                 fuzzed_data_provider,
      32                 :          0 :                 [&] {
      33                 :          0 :                     std::array<std::byte, 4096> arr{};
      34                 :            :                     try {
      35         [ #  # ]:          0 :                         opt_buffered_file->read({arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)});
      36         [ #  # ]:          0 :                     } catch (const std::ios_base::failure&) {
      37                 :          0 :                     }
      38                 :          0 :                 },
      39                 :          0 :                 [&] {
      40                 :          0 :                     opt_buffered_file->SetLimit(fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096));
      41                 :          0 :                 },
      42                 :          0 :                 [&] {
      43         [ #  # ]:          0 :                     if (!opt_buffered_file->SetPos(fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096))) {
      44                 :          0 :                         setpos_fail = true;
      45                 :          0 :                     }
      46                 :          0 :                 },
      47                 :          0 :                 [&] {
      48         [ #  # ]:          0 :                     if (setpos_fail) {
      49                 :            :                         // Calling FindByte(...) after a failed SetPos(...) call may result in an infinite loop.
      50                 :          0 :                         return;
      51                 :            :                     }
      52                 :            :                     try {
      53 [ #  # ][ #  # ]:          0 :                         opt_buffered_file->FindByte(std::byte(fuzzed_data_provider.ConsumeIntegral<uint8_t>()));
      54         [ #  # ]:          0 :                     } catch (const std::ios_base::failure&) {
      55                 :          0 :                     }
      56                 :          0 :                 },
      57                 :          0 :                 [&] {
      58                 :          0 :                     ReadFromStream(fuzzed_data_provider, *opt_buffered_file);
      59                 :          0 :                 });
      60                 :          0 :         }
      61         [ #  # ]:          0 :         opt_buffered_file->GetPos();
      62         [ #  # ]:          0 :         opt_buffered_file->GetVersion();
      63                 :          0 :     }
      64                 :          0 : }

Generated by: LCOV version 1.14