LCOV - code coverage report
Current view: top level - src/test/fuzz - system.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 2 84 2.4 %
Date: 2023-09-26 12:08:55 Functions: 3 14 21.4 %

          Line data    Source code
       1             : // Copyright (c) 2020-2021 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 <common/args.h>
       6             : #include <test/fuzz/FuzzedDataProvider.h>
       7             : #include <test/fuzz/fuzz.h>
       8             : #include <test/fuzz/util.h>
       9             : #include <test/util/setup_common.h>
      10             : 
      11             : #include <cstdint>
      12             : #include <string>
      13             : #include <vector>
      14             : 
      15             : namespace {
      16           0 : void initialize_system()
      17             : {
      18           0 :     static const auto testing_setup = MakeNoLogFileContext<>();
      19           0 : }
      20             : 
      21           0 : std::string GetArgumentName(const std::string& name)
      22             : {
      23           0 :     size_t idx = name.find('=');
      24           0 :     if (idx == std::string::npos) {
      25           0 :         idx = name.size();
      26           0 :     }
      27           0 :     return name.substr(0, idx);
      28             : }
      29             : 
      30           4 : FUZZ_TARGET(system, .init = initialize_system)
      31             : {
      32           0 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      33           0 :     ArgsManager args_manager{};
      34             : 
      35           0 :     if (fuzzed_data_provider.ConsumeBool()) {
      36           0 :         SetupHelpOptions(args_manager);
      37           0 :     }
      38             : 
      39           0 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 3000)
      40             :     {
      41           0 :         CallOneOf(
      42             :             fuzzed_data_provider,
      43           0 :             [&] {
      44           0 :                 args_manager.SelectConfigNetwork(fuzzed_data_provider.ConsumeRandomLengthString(16));
      45           0 :             },
      46           0 :             [&] {
      47           0 :                 args_manager.SoftSetArg(fuzzed_data_provider.ConsumeRandomLengthString(16), fuzzed_data_provider.ConsumeRandomLengthString(16));
      48           0 :             },
      49           0 :             [&] {
      50           0 :                 args_manager.ForceSetArg(fuzzed_data_provider.ConsumeRandomLengthString(16), fuzzed_data_provider.ConsumeRandomLengthString(16));
      51           0 :             },
      52           0 :             [&] {
      53           0 :                 args_manager.SoftSetBoolArg(fuzzed_data_provider.ConsumeRandomLengthString(16), fuzzed_data_provider.ConsumeBool());
      54           0 :             },
      55           0 :             [&] {
      56           0 :                 const OptionsCategory options_category = fuzzed_data_provider.PickValueInArray<OptionsCategory>({OptionsCategory::OPTIONS, OptionsCategory::CONNECTION, OptionsCategory::WALLET, OptionsCategory::WALLET_DEBUG_TEST, OptionsCategory::ZMQ, OptionsCategory::DEBUG_TEST, OptionsCategory::CHAINPARAMS, OptionsCategory::NODE_RELAY, OptionsCategory::BLOCK_CREATION, OptionsCategory::RPC, OptionsCategory::GUI, OptionsCategory::COMMANDS, OptionsCategory::REGISTER_COMMANDS, OptionsCategory::HIDDEN});
      57             :                 // Avoid hitting:
      58             :                 // common/args.cpp:563: void ArgsManager::AddArg(const std::string &, const std::string &, unsigned int, const OptionsCategory &): Assertion `ret.second' failed.
      59           0 :                 const std::string argument_name = GetArgumentName(fuzzed_data_provider.ConsumeRandomLengthString(16));
      60           0 :                 if (args_manager.GetArgFlags(argument_name) != std::nullopt) {
      61           0 :                     return;
      62             :                 }
      63           0 :                 args_manager.AddArg(argument_name, fuzzed_data_provider.ConsumeRandomLengthString(16), fuzzed_data_provider.ConsumeIntegral<unsigned int>() & ~ArgsManager::COMMAND, options_category);
      64           0 :             },
      65           0 :             [&] {
      66             :                 // Avoid hitting:
      67             :                 // common/args.cpp:563: void ArgsManager::AddArg(const std::string &, const std::string &, unsigned int, const OptionsCategory &): Assertion `ret.second' failed.
      68           0 :                 const std::vector<std::string> names = ConsumeRandomLengthStringVector(fuzzed_data_provider);
      69           0 :                 std::vector<std::string> hidden_arguments;
      70           0 :                 for (const std::string& name : names) {
      71           0 :                     const std::string hidden_argument = GetArgumentName(name);
      72           0 :                     if (args_manager.GetArgFlags(hidden_argument) != std::nullopt) {
      73           0 :                         continue;
      74           2 :                     }
      75           0 :                     if (std::find(hidden_arguments.begin(), hidden_arguments.end(), hidden_argument) != hidden_arguments.end()) {
      76           0 :                         continue;
      77             :                     }
      78           0 :                     hidden_arguments.push_back(hidden_argument);
      79           0 :                 }
      80           0 :                 args_manager.AddHiddenArgs(hidden_arguments);
      81           0 :             },
      82           0 :             [&] {
      83           0 :                 args_manager.ClearArgs();
      84           0 :             },
      85           0 :             [&] {
      86           0 :                 const std::vector<std::string> random_arguments = ConsumeRandomLengthStringVector(fuzzed_data_provider);
      87           0 :                 std::vector<const char*> argv;
      88           0 :                 argv.reserve(random_arguments.size());
      89           0 :                 for (const std::string& random_argument : random_arguments) {
      90           0 :                     argv.push_back(random_argument.c_str());
      91             :                 }
      92             :                 try {
      93           0 :                     std::string error;
      94           0 :                     (void)args_manager.ParseParameters(argv.size(), argv.data(), error);
      95           0 :                 } catch (const std::logic_error&) {
      96           0 :                 }
      97           0 :             });
      98           0 :     }
      99             : 
     100           0 :     const std::string s1 = fuzzed_data_provider.ConsumeRandomLengthString(16);
     101           0 :     const std::string s2 = fuzzed_data_provider.ConsumeRandomLengthString(16);
     102           0 :     const int64_t i64 = fuzzed_data_provider.ConsumeIntegral<int64_t>();
     103           0 :     const bool b = fuzzed_data_provider.ConsumeBool();
     104             : 
     105           0 :     (void)args_manager.GetIntArg(s1, i64);
     106           0 :     (void)args_manager.GetArg(s1, s2);
     107           0 :     (void)args_manager.GetArgFlags(s1);
     108           0 :     (void)args_manager.GetArgs(s1);
     109           0 :     (void)args_manager.GetBoolArg(s1, b);
     110             :     try {
     111           0 :         (void)args_manager.GetChainTypeString();
     112           0 :     } catch (const std::runtime_error&) {
     113           0 :     }
     114           0 :     (void)args_manager.GetHelpMessage();
     115           0 :     (void)args_manager.GetUnrecognizedSections();
     116           0 :     (void)args_manager.GetUnsuitableSectionOnlyArgs();
     117           0 :     (void)args_manager.IsArgNegated(s1);
     118           0 :     (void)args_manager.IsArgSet(s1);
     119             : 
     120           0 :     (void)HelpRequested(args_manager);
     121           0 : }
     122             : } // namespace

Generated by: LCOV version 1.14