diff options
author | Wambou <[email protected]> | 2018-04-24 16:17:02 +0200 |
---|---|---|
committer | Wambou <[email protected]> | 2018-05-31 15:33:56 +0200 |
commit | 4ba8c8db638b9f95b6c688d695ce8ca40f3218c7 (patch) | |
tree | 168699a373198116edcce60242e0480e1eea5ba9 /src/tests/test_utils.cpp | |
parent | b3e4f1421b432e239ce76d72869a0e28089748f5 (diff) |
Create unit tests for Base32 encoding
Diffstat (limited to 'src/tests/test_utils.cpp')
-rw-r--r-- | src/tests/test_utils.cpp | 89 |
1 files changed, 87 insertions, 2 deletions
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())); }); |