diff options
-rw-r--r-- | src/tests/data/base32.vec | 79 | ||||
-rw-r--r-- | src/tests/test_utils.cpp | 89 |
2 files changed, 166 insertions, 2 deletions
diff --git a/src/tests/data/base32.vec b/src/tests/data/base32.vec new file mode 100644 index 000000000..c681d85d1 --- /dev/null +++ b/src/tests/data/base32.vec @@ -0,0 +1,79 @@ + +[valid] +# empty string +Binary = +Base32 = + +Binary = 66 +Base32 = MY====== + +Binary = 666F +Base32 = MZXQ==== + +Binary = 666F6F +Base32 = MZXW6=== + +Binary = 666F6F66 +Base32 = MZXW6ZQ= + +Binary = 666F6F666F +Base32 = MZXW6ZTP + +Binary = 68656C6C6F20776F726C64 +Base32 = NBSWY3DPEB3W64TMMQ====== + +Binary = 68656C6C6F20776F726C6421 +Base32 = NBSWY3DPEB3W64TMMQQQ==== + +Binary = 48656C6C6F2C20776F726C642E +Base32 = JBSWY3DPFQQHO33SNRSC4=== + +Binary = 546865203132206368617273 +Base32 = KRUGKIBRGIQGG2DBOJZQ==== + +Binary = 5468652031332063686172732E +Base32 = KRUGKIBRGMQGG2DBOJZS4=== + +Binary = 5468652031342063686172732E2E +Base32 = KRUGKIBRGQQGG2DBOJZS4LQ= + +Binary = 5468652031352063686172732E2E2E +Base32 = KRUGKIBRGUQGG2DBOJZS4LRO + +Binary = 416E205554462D382075756D6C3A20C3BC +Base32 = IFXCAVKUIYWTQIDVOVWWYORAYO6A==== + +Binary = 5765697264204765726D616E20322062797465207468696E673A20C39F2E +Base32 = K5SWS4TEEBDWK4TNMFXCAMRAMJ4XIZJAORUGS3THHIQMHHZO + +Binary = 9B +Base32 = TM====== + +Binary = 1C60 +Base32 = DRQA==== + +Binary = 8134BD +Base32 = QE2L2=== + +Binary = 5E6CFFDE +Base32 = LZWP7XQ= + +Binary = b2cdf0dc7f +Base32 = WLG7BXD7 + +Binary = fc562ddad40e +Base32 = 7RLC3WWUBY====== + +Binary = 29b2322e8841e8 +Base32 = FGZDELUIIHUA==== + +Binary = 0f0fced9497aaf92 +Base32 = B4H45WKJPKXZE=== + +Binary = 270fb18982800da640 +Base32 = E4H3DCMCQAG2MQA= + +[invalid] +Base32 = ZOOL!isnotvalidbase32 + +Base32 = Neitheris:this? diff --git a/src/tests/test_utils.cpp b/src/tests/test_utils.cpp index d3b00e897..6b082a474 100644 --- a/src/tests/test_utils.cpp +++ b/src/tests/test_utils.cpp @@ -22,6 +22,10 @@ #include <botan/base64.h> #endif +#if defined(BOTAN_HAS_BASE32_CODEC) + #include <botan/base32.h> +#endif + #if defined(BOTAN_HAS_POLY_DBL) #include <botan/internal/poly_dbl.h> #endif @@ -320,6 +324,86 @@ class Date_Format_Tests final : public Text_Based_Test BOTAN_REGISTER_TEST("util_dates", Date_Format_Tests); +#if defined(BOTAN_HAS_BASE32_CODEC) + +class Base32_Tests final : public Text_Based_Test + { + public: + Base32_Tests() : Text_Based_Test("base32.vec", "Base32", "Binary") {} + + Test::Result run_one_test(const std::string& type, const VarMap& vars) override + { + Test::Result result("Base32"); + + const bool is_valid = (type == "valid"); + const std::string base32 = vars.get_req_str("Base32"); + + try + { + if(is_valid) + { + const std::vector<uint8_t> binary = vars.get_req_bin("Binary"); + result.test_eq("base32 decoding", Botan::base32_decode(base32), binary); + result.test_eq("base32 encoding", Botan::base32_encode(binary), base32); + } + else + { + auto res = Botan::base32_decode(base32); + result.test_failure("decoded invalid base32 to " + Botan::hex_encode(res)); + } + } + catch(std::exception& e) + { + if(is_valid) + { + result.test_failure("rejected valid base32", e.what()); + } + else + { + result.test_note("rejected invalid base32"); + } + } + + return result; + } + + std::vector<Test::Result> run_final_tests() override + { + Test::Result result("Base32"); + const std::string valid_b32 = "MY======"; + + for(char ws_char : { ' ', '\t', '\r', '\n' }) + { + for(size_t i = 0; i <= valid_b32.size(); ++i) + { + std::string b32_ws = valid_b32; + b32_ws.insert(i, 1, ws_char); + + try + { + result.test_failure("decoded whitespace base32", Botan::base32_decode(b32_ws, false)); + } + catch(std::exception&) {} + + try + { + result.test_eq("base32 decoding with whitespace", Botan::base32_decode(b32_ws, true), "66"); + } + catch(std::exception& e) + { + result.test_failure(b32_ws, e.what()); + } + } + } + + return {result}; + } + }; + +BOTAN_REGISTER_TEST("base32", Base32_Tests); + +#endif + #if defined(BOTAN_HAS_BASE64_CODEC) class Base64_Tests final : public Text_Based_Test @@ -485,10 +569,11 @@ class Charset_Tests final : public Text_Based_Test result.test_throws("conversion fails for non-Latin1 characters", []() { // "abcdefÅžabcdef" - const std::vector<uint8_t> input = { + const std::vector<uint8_t> input = + { 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0xC5, 0xB8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 - }; + }; Botan::utf8_to_latin1(std::string(input.begin(), input.end())); }); |