LCOV - code coverage report
Current view: top level - src/test/fuzz/util - descriptor.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 0 48 0.0 %
Date: 2024-01-03 14:57:53 Functions: 0 3 0.0 %
Branches: 0 84 0.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2023-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 <test/fuzz/util/descriptor.h>
       6                 :            : 
       7                 :          0 : void MockedDescriptorConverter::Init() {
       8                 :            :     // The data to use as a private key or a seed for an xprv.
       9                 :          0 :     std::array<std::byte, 32> key_data{std::byte{1}};
      10                 :            :     // Generate keys of all kinds and store them in the keys array.
      11         [ #  # ]:          0 :     for (size_t i{0}; i < TOTAL_KEYS_GENERATED; i++) {
      12                 :          0 :         key_data[31] = std::byte(i);
      13                 :            : 
      14                 :            :         // If this is a "raw" key, generate a normal privkey. Otherwise generate
      15                 :            :         // an extended one.
      16 [ #  # ][ #  # ]:          0 :         if (IdIsCompPubKey(i) || IdIsUnCompPubKey(i) || IdIsXOnlyPubKey(i) || IdIsConstPrivKey(i)) {
         [ #  # ][ #  # ]
      17                 :          0 :             CKey privkey;
      18 [ #  # ][ #  # ]:          0 :             privkey.Set(UCharCast(key_data.begin()), UCharCast(key_data.end()), !IdIsUnCompPubKey(i));
         [ #  # ][ #  # ]
      19 [ #  # ][ #  # ]:          0 :             if (IdIsCompPubKey(i) || IdIsUnCompPubKey(i)) {
         [ #  # ][ #  # ]
      20         [ #  # ]:          0 :                 CPubKey pubkey{privkey.GetPubKey()};
      21 [ #  # ][ #  # ]:          0 :                 keys_str[i] = HexStr(pubkey);
      22 [ #  # ][ #  # ]:          0 :             } else if (IdIsXOnlyPubKey(i)) {
      23 [ #  # ][ #  # ]:          0 :                 const XOnlyPubKey pubkey{privkey.GetPubKey()};
      24 [ #  # ][ #  # ]:          0 :                 keys_str[i] = HexStr(pubkey);
      25                 :          0 :             } else {
      26         [ #  # ]:          0 :                 keys_str[i] = EncodeSecret(privkey);
      27                 :            :             }
      28                 :          0 :         } else {
      29                 :          0 :             CExtKey ext_privkey;
      30 [ #  # ][ #  # ]:          0 :             ext_privkey.SetSeed(key_data);
      31 [ #  # ][ #  # ]:          0 :             if (IdIsXprv(i)) {
      32         [ #  # ]:          0 :                 keys_str[i] = EncodeExtKey(ext_privkey);
      33                 :          0 :             } else {
      34         [ #  # ]:          0 :                 const CExtPubKey ext_pubkey{ext_privkey.Neuter()};
      35         [ #  # ]:          0 :                 keys_str[i] = EncodeExtPubKey(ext_pubkey);
      36                 :            :             }
      37                 :          0 :         }
      38                 :          0 :     }
      39                 :          0 : }
      40                 :            : 
      41                 :          0 : std::optional<uint8_t> MockedDescriptorConverter::IdxFromHex(std::string_view hex_characters) const {
      42         [ #  # ]:          0 :     if (hex_characters.size() != 2) return {};
      43                 :          0 :     auto idx = ParseHex(hex_characters);
      44         [ #  # ]:          0 :     if (idx.size() != 1) return {};
      45                 :          0 :     return idx[0];
      46                 :          0 : }
      47                 :            : 
      48                 :          0 : std::optional<std::string> MockedDescriptorConverter::GetDescriptor(std::string_view mocked_desc) const {
      49                 :            :     // The smallest fragment would be "pk(%00)"
      50         [ #  # ]:          0 :     if (mocked_desc.size() < 7) return {};
      51                 :            : 
      52                 :            :     // The actual descriptor string to be returned.
      53                 :          0 :     std::string desc;
      54         [ #  # ]:          0 :     desc.reserve(mocked_desc.size());
      55                 :            : 
      56                 :            :     // Replace all occurrences of '%' followed by two hex characters with the corresponding key.
      57         [ #  # ]:          0 :     for (size_t i = 0; i < mocked_desc.size();) {
      58         [ #  # ]:          0 :         if (mocked_desc[i] == '%') {
      59         [ #  # ]:          0 :             if (i + 3 >= mocked_desc.size()) return {};
      60 [ #  # ][ #  # ]:          0 :             if (const auto idx = IdxFromHex(mocked_desc.substr(i + 1, 2))) {
                 [ #  # ]
      61         [ #  # ]:          0 :                 desc += keys_str[*idx];
      62                 :          0 :                 i += 3;
      63                 :          0 :             } else {
      64                 :          0 :                 return {};
      65                 :            :             }
      66                 :          0 :         } else {
      67         [ #  # ]:          0 :             desc += mocked_desc[i++];
      68                 :            :         }
      69                 :            :     }
      70                 :            : 
      71                 :          0 :     return desc;
      72                 :          0 : }

Generated by: LCOV version 1.14