LCOV - code coverage report
Current view: top level - src/test/fuzz - bech32.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 0.0 % 22 0
Test Date: 2024-08-28 04:44:32 Functions: 0.0 % 2 0
Branches: 0.0 % 40 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2019-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 <bech32.h>
       6                 :             : #include <test/fuzz/fuzz.h>
       7                 :             : #include <test/util/str.h>
       8                 :             : #include <util/strencodings.h>
       9                 :             : 
      10                 :             : #include <cassert>
      11                 :             : #include <cstdint>
      12                 :             : #include <string>
      13                 :             : #include <utility>
      14                 :             : #include <vector>
      15                 :             : 
      16         [ #  # ]:           0 : FUZZ_TARGET(bech32)
      17                 :             : {
      18         [ #  # ]:           0 :     const std::string random_string(buffer.begin(), buffer.end());
      19         [ #  # ]:           0 :     const auto r1 = bech32::Decode(random_string);
      20         [ #  # ]:           0 :     if (r1.hrp.empty()) {
      21         [ #  # ]:           0 :         assert(r1.encoding == bech32::Encoding::INVALID);
      22         [ #  # ]:           0 :         assert(r1.data.empty());
      23                 :             :     } else {
      24         [ #  # ]:           0 :         assert(r1.encoding != bech32::Encoding::INVALID);
      25         [ #  # ]:           0 :         const std::string reencoded = bech32::Encode(r1.encoding, r1.hrp, r1.data);
      26   [ #  #  #  # ]:           0 :         assert(CaseInsensitiveEqual(random_string, reencoded));
      27                 :           0 :     }
      28                 :             : 
      29                 :           0 :     std::vector<unsigned char> input;
      30         [ #  # ]:           0 :     ConvertBits<8, 5, true>([&](unsigned char c) { input.push_back(c); }, buffer.begin(), buffer.end());
      31                 :             : 
      32                 :             :     // Input data part + 3 characters for the HRP and separator (bc1) + the checksum characters
      33         [ #  # ]:           0 :     if (input.size() + 3 + bech32::CHECKSUM_SIZE <= bech32::CharLimit::BECH32) {
      34                 :             :         // If it's possible to encode input in Bech32(m) without exceeding the bech32-character limit:
      35         [ #  # ]:           0 :         for (auto encoding : {bech32::Encoding::BECH32, bech32::Encoding::BECH32M}) {
      36   [ #  #  #  # ]:           0 :             const std::string encoded = bech32::Encode(encoding, "bc", input);
      37         [ #  # ]:           0 :             assert(!encoded.empty());
      38         [ #  # ]:           0 :             const auto r2 = bech32::Decode(encoded);
      39         [ #  # ]:           0 :             assert(r2.encoding == encoding);
      40         [ #  # ]:           0 :             assert(r2.hrp == "bc");
      41         [ #  # ]:           0 :             assert(r2.data == input);
      42                 :           0 :         }
      43                 :             :     }
      44                 :           0 : }
        

Generated by: LCOV version 2.0-1