PageRenderTime 87ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/fuzz/bech32.cpp

https://github.com/denis2342/bitcoin
C++ | 43 lines | 34 code | 5 blank | 4 comment | 9 complexity | 5a2986d1a15818bc87c045f8afdf837d MD5 | raw file
  1. // Copyright (c) 2019-2020 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. #include <bech32.h>
  5. #include <test/fuzz/fuzz.h>
  6. #include <test/util/str.h>
  7. #include <util/strencodings.h>
  8. #include <cassert>
  9. #include <cstdint>
  10. #include <string>
  11. #include <utility>
  12. #include <vector>
  13. FUZZ_TARGET(bech32)
  14. {
  15. const std::string random_string(buffer.begin(), buffer.end());
  16. const auto r1 = bech32::Decode(random_string);
  17. if (r1.hrp.empty()) {
  18. assert(r1.encoding == bech32::Encoding::INVALID);
  19. assert(r1.data.empty());
  20. } else {
  21. assert(r1.encoding != bech32::Encoding::INVALID);
  22. const std::string reencoded = bech32::Encode(r1.encoding, r1.hrp, r1.data);
  23. assert(CaseInsensitiveEqual(random_string, reencoded));
  24. }
  25. std::vector<unsigned char> input;
  26. ConvertBits<8, 5, true>([&](unsigned char c) { input.push_back(c); }, buffer.begin(), buffer.end());
  27. if (input.size() + 3 + 6 <= 90) {
  28. // If it's possible to encode input in Bech32(m) without exceeding the 90-character limit:
  29. for (auto encoding : {bech32::Encoding::BECH32, bech32::Encoding::BECH32M}) {
  30. const std::string encoded = bech32::Encode(encoding, "bc", input);
  31. assert(!encoded.empty());
  32. const auto r2 = bech32::Decode(encoded);
  33. assert(r2.encoding == encoding);
  34. assert(r2.hrp == "bc");
  35. assert(r2.data == input);
  36. }
  37. }
  38. }