diff options
author | Jack Lloyd <[email protected]> | 2018-11-17 13:52:02 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-12-09 13:50:26 -0500 |
commit | 0b92da57db9397e799265a04eefc14852d2e4b28 (patch) | |
tree | 1a9b36b5b7a24607e3db66e8d5abc006c773958f /src/tests/test_utils.cpp | |
parent | f18d0ed18fa6162a1a268183b42d79a0d93cc05a (diff) |
Add base58 encoding/decoding
Diffstat (limited to 'src/tests/test_utils.cpp')
-rw-r--r-- | src/tests/test_utils.cpp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/tests/test_utils.cpp b/src/tests/test_utils.cpp index f1c6bef43..6708524b4 100644 --- a/src/tests/test_utils.cpp +++ b/src/tests/test_utils.cpp @@ -27,6 +27,10 @@ #include <botan/base32.h> #endif +#if defined(BOTAN_HAS_BASE58_CODEC) + #include <botan/base58.h> +#endif + #if defined(BOTAN_HAS_POLY_DBL) #include <botan/internal/poly_dbl.h> #endif @@ -459,6 +463,98 @@ BOTAN_REGISTER_TEST("base32", Base32_Tests); #endif +#if defined(BOTAN_HAS_BASE58_CODEC) + +class Base58_Tests final : public Text_Based_Test + { + public: + Base58_Tests() : Text_Based_Test("base58.vec", "Base58", "Binary") {} + + Test::Result run_one_test(const std::string& type, const VarMap& vars) override + { + Test::Result result("Base58"); + + const bool is_valid = (type == "valid"); + const std::string base58 = vars.get_req_str("Base58"); + + try + { + if(is_valid) + { + const std::vector<uint8_t> binary = vars.get_req_bin("Binary"); + result.test_eq("base58 decoding", Botan::base58_decode(base58), binary); + result.test_eq("base58 encoding", Botan::base58_encode(binary), base58); + } + else + { + auto res = Botan::base58_decode(base58); + result.test_failure("decoded invalid base58 to " + Botan::hex_encode(res)); + } + } + catch(std::exception& e) + { + if(is_valid) + { + result.test_failure("rejected valid base58", e.what()); + } + else + { + result.test_note("rejected invalid base58"); + } + } + + return result; + } + }; + +BOTAN_REGISTER_TEST("base58", Base58_Tests); + +class Base58_Check_Tests final : public Text_Based_Test + { + public: + Base58_Check_Tests() : Text_Based_Test("base58c.vec", "Base58", "Binary") {} + + Test::Result run_one_test(const std::string& type, const VarMap& vars) override + { + Test::Result result("Base58 Check"); + + const bool is_valid = (type == "valid"); + const std::string base58 = vars.get_req_str("Base58"); + + try + { + if(is_valid) + { + const std::vector<uint8_t> binary = vars.get_req_bin("Binary"); + result.test_eq("base58 decoding", Botan::base58_check_decode(base58), binary); + result.test_eq("base58 encoding", Botan::base58_check_encode(binary), base58); + } + else + { + auto res = Botan::base58_check_decode(base58); + result.test_failure("decoded invalid base58c to " + Botan::hex_encode(res)); + } + } + catch(std::exception& e) + { + if(is_valid) + { + result.test_failure("rejected valid base58c", e.what()); + } + else + { + result.test_note("rejected invalid base58c"); + } + } + + return result; + } + }; + +BOTAN_REGISTER_TEST("base58c", Base58_Check_Tests); + +#endif + #if defined(BOTAN_HAS_BASE64_CODEC) class Base64_Tests final : public Text_Based_Test |