LCOV - code coverage report
Current view: top level - src/test/fuzz - buffered_file.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 3 45 6.7 %
Date: 2023-09-26 12:08:55 Functions: 3 9 33.3 %

          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           4 : 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 :     FILE* fuzzed_file = fuzzed_file_provider.open();
      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), fuzzed_data_provider.ConsumeIntegral<int>());
      25           2 :     } catch (const std::ios_base::failure&) {
      26           0 :         if (fuzzed_file != nullptr) {
      27           0 :             fclose(fuzzed_file);
      28           0 :         }
      29           0 :     }
      30           0 :     if (opt_buffered_file && fuzzed_file != nullptr) {
      31           0 :         bool setpos_fail = false;
      32           0 :         LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
      33           0 :             CallOneOf(
      34             :                 fuzzed_data_provider,
      35           0 :                 [&] {
      36           0 :                     std::array<std::byte, 4096> arr{};
      37             :                     try {
      38           0 :                         opt_buffered_file->read({arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096)});
      39           0 :                     } catch (const std::ios_base::failure&) {
      40           0 :                     }
      41           0 :                 },
      42           0 :                 [&] {
      43           0 :                     opt_buffered_file->SetLimit(fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096));
      44           0 :                 },
      45           0 :                 [&] {
      46           0 :                     if (!opt_buffered_file->SetPos(fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096))) {
      47           0 :                         setpos_fail = true;
      48           0 :                     }
      49           0 :                 },
      50           0 :                 [&] {
      51           0 :                     if (setpos_fail) {
      52             :                         // Calling FindByte(...) after a failed SetPos(...) call may result in an infinite loop.
      53           0 :                         return;
      54             :                     }
      55             :                     try {
      56           0 :                         opt_buffered_file->FindByte(std::byte(fuzzed_data_provider.ConsumeIntegral<uint8_t>()));
      57           0 :                     } catch (const std::ios_base::failure&) {
      58           0 :                     }
      59           0 :                 },
      60           0 :                 [&] {
      61           0 :                     ReadFromStream(fuzzed_data_provider, *opt_buffered_file);
      62           0 :                 });
      63           0 :         }
      64           0 :         opt_buffered_file->GetPos();
      65           0 :         opt_buffered_file->GetVersion();
      66           0 :     }
      67           0 : }

Generated by: LCOV version 1.14