parse_univalue.cpp raw

   1  // Copyright (c) 2009-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 <chainparams.h>
   6  #include <rpc/client.h>
   7  #include <rpc/util.h>
   8  #include <test/fuzz/fuzz.h>
   9  #include <util/chaintype.h>
  10  
  11  #include <limits>
  12  #include <string>
  13  
  14  void initialize_parse_univalue()
  15  {
  16      SelectParams(ChainType::REGTEST);
  17  }
  18  
  19  FUZZ_TARGET(parse_univalue, .init = initialize_parse_univalue)
  20  {
  21      const std::string random_string(buffer.begin(), buffer.end());
  22      bool valid = true;
  23      const UniValue univalue = [&] {
  24          UniValue uv;
  25          if (!uv.read(random_string)) valid = false;
  26          return valid ? uv : UniValue{};
  27      }();
  28      if (!valid) {
  29          return;
  30      }
  31      try {
  32          (void)ParseHashO(univalue, "A");
  33      } catch (const UniValue&) {
  34      } catch (const std::runtime_error&) {
  35      }
  36      try {
  37          (void)ParseHashO(univalue, random_string);
  38      } catch (const UniValue&) {
  39      } catch (const std::runtime_error&) {
  40      }
  41      try {
  42          (void)ParseHashV(univalue, "A");
  43      } catch (const UniValue&) {
  44      } catch (const std::runtime_error&) {
  45      }
  46      try {
  47          (void)ParseHashV(univalue, random_string);
  48      } catch (const UniValue&) {
  49      } catch (const std::runtime_error&) {
  50      }
  51      try {
  52          (void)ParseHexO(univalue, "A");
  53      } catch (const UniValue&) {
  54      }
  55      try {
  56          (void)ParseHexO(univalue, random_string);
  57      } catch (const UniValue&) {
  58      }
  59      try {
  60          (void)ParseHexV(univalue, "A");
  61      } catch (const UniValue&) {
  62      } catch (const std::runtime_error&) {
  63      }
  64      try {
  65          (void)ParseHexV(univalue, random_string);
  66      } catch (const UniValue&) {
  67      } catch (const std::runtime_error&) {
  68      }
  69      try {
  70          if (univalue.isNull() || univalue.isStr()) (void)ParseSighashString(univalue);
  71      } catch (const UniValue&) {
  72      }
  73      try {
  74          (void)AmountFromValue(univalue);
  75      } catch (const UniValue&) {
  76      } catch (const std::runtime_error&) {
  77      }
  78      try {
  79          FlatSigningProvider provider;
  80          if (buffer.size() < 10'000) (void)EvalDescriptorStringOrObject(univalue, provider);
  81      } catch (const UniValue&) {
  82      } catch (const std::runtime_error&) {
  83      }
  84      try {
  85          (void)ParseConfirmTarget(univalue, std::numeric_limits<unsigned int>::max());
  86      } catch (const UniValue&) {
  87      } catch (const std::runtime_error&) {
  88      }
  89      try {
  90          (void)ParseDescriptorRange(univalue);
  91      } catch (const UniValue&) {
  92      } catch (const std::runtime_error&) {
  93      }
  94  }
  95