diff options
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/data/codec/base32.vec (renamed from src/tests/data/base32.vec) | 0 | ||||
-rw-r--r-- | src/tests/data/codec/base58.vec (renamed from src/tests/data/base58.vec) | 0 | ||||
-rw-r--r-- | src/tests/data/codec/base58c.vec (renamed from src/tests/data/base58c.vec) | 0 | ||||
-rw-r--r-- | src/tests/data/codec/base64.vec (renamed from src/tests/data/base64.vec) | 0 | ||||
-rw-r--r-- | src/tests/test_codec.cpp | 275 | ||||
-rw-r--r-- | src/tests/test_utils.cpp | 264 |
6 files changed, 275 insertions, 264 deletions
diff --git a/src/tests/data/base32.vec b/src/tests/data/codec/base32.vec index c681d85d1..c681d85d1 100644 --- a/src/tests/data/base32.vec +++ b/src/tests/data/codec/base32.vec diff --git a/src/tests/data/base58.vec b/src/tests/data/codec/base58.vec index 6c8d30fcc..6c8d30fcc 100644 --- a/src/tests/data/base58.vec +++ b/src/tests/data/codec/base58.vec diff --git a/src/tests/data/base58c.vec b/src/tests/data/codec/base58c.vec index 30ea5a840..30ea5a840 100644 --- a/src/tests/data/base58c.vec +++ b/src/tests/data/codec/base58c.vec diff --git a/src/tests/data/base64.vec b/src/tests/data/codec/base64.vec index e1aa028a0..e1aa028a0 100644 --- a/src/tests/data/base64.vec +++ b/src/tests/data/codec/base64.vec diff --git a/src/tests/test_codec.cpp b/src/tests/test_codec.cpp new file mode 100644 index 000000000..4130ce200 --- /dev/null +++ b/src/tests/test_codec.cpp @@ -0,0 +1,275 @@ +/* +* (C) 2015,2018,2021 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include "tests.h" + +#if defined(BOTAN_HAS_BASE64_CODEC) + #include <botan/base64.h> +#endif + +#if defined(BOTAN_HAS_BASE32_CODEC) + #include <botan/base32.h> +#endif + +#if defined(BOTAN_HAS_BASE58_CODEC) + #include <botan/base58.h> +#endif + +namespace Botan_Tests { + +#if defined(BOTAN_HAS_BASE32_CODEC) + +class Base32_Tests final : public Text_Based_Test + { + public: + Base32_Tests() : Text_Based_Test("codec/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("codec", "base32", Base32_Tests); + +#endif + +#if defined(BOTAN_HAS_BASE58_CODEC) + +class Base58_Tests final : public Text_Based_Test + { + public: + Base58_Tests() : Text_Based_Test("codec/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("codec", "base58", Base58_Tests); + +class Base58_Check_Tests final : public Text_Based_Test + { + public: + Base58_Check_Tests() : Text_Based_Test("codec/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("codec", "base58c", Base58_Check_Tests); + +#endif + +#if defined(BOTAN_HAS_BASE64_CODEC) + +class Base64_Tests final : public Text_Based_Test + { + public: + Base64_Tests() : Text_Based_Test("codec/base64.vec", "Base64", "Binary") {} + + Test::Result run_one_test(const std::string& type, const VarMap& vars) override + { + Test::Result result("Base64"); + + const bool is_valid = (type == "valid"); + const std::string base64 = vars.get_req_str("Base64"); + + try + { + if(is_valid) + { + const std::vector<uint8_t> binary = vars.get_req_bin("Binary"); + result.test_eq("base64 decoding", Botan::base64_decode(base64), binary); + result.test_eq("base64 encoding", Botan::base64_encode(binary), base64); + } + else + { + auto res = Botan::base64_decode(base64); + result.test_failure("decoded invalid base64 to " + Botan::hex_encode(res)); + } + } + catch(std::exception& e) + { + if(is_valid) + { + result.test_failure("rejected valid base64", e.what()); + } + else + { + result.test_note("rejected invalid base64"); + } + } + + return result; + } + + std::vector<Test::Result> run_final_tests() override + { + Test::Result result("Base64"); + const std::string valid_b64 = "Zg=="; + + for(char ws_char : { ' ', '\t', '\r', '\n' }) + { + for(size_t i = 0; i <= valid_b64.size(); ++i) + { + std::string b64_ws = valid_b64; + b64_ws.insert(i, 1, ws_char); + + try + { + result.test_failure("decoded whitespace base64", Botan::base64_decode(b64_ws, false)); + } + catch(std::exception&) {} + + try + { + result.test_eq("base64 decoding with whitespace", Botan::base64_decode(b64_ws, true), "66"); + } + catch(std::exception& e) + { + result.test_failure(b64_ws, e.what()); + } + } + } + + return {result}; + } + }; + +BOTAN_REGISTER_TEST("codec", "base64", Base64_Tests); + +#endif + +} diff --git a/src/tests/test_utils.cpp b/src/tests/test_utils.cpp index e9eda1a28..d080566e7 100644 --- a/src/tests/test_utils.cpp +++ b/src/tests/test_utils.cpp @@ -19,18 +19,6 @@ #include <botan/internal/parsing.h> #include <botan/version.h> -#if defined(BOTAN_HAS_BASE64_CODEC) - #include <botan/base64.h> -#endif - -#if defined(BOTAN_HAS_BASE32_CODEC) - #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 @@ -522,258 +510,6 @@ class Date_Format_Tests final : public Text_Based_Test BOTAN_REGISTER_TEST("utils", "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("utils", "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("utils", "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("utils", "base58c", Base58_Check_Tests); - -#endif - -#if defined(BOTAN_HAS_BASE64_CODEC) - -class Base64_Tests final : public Text_Based_Test - { - public: - Base64_Tests() : Text_Based_Test("base64.vec", "Base64", "Binary") {} - - Test::Result run_one_test(const std::string& type, const VarMap& vars) override - { - Test::Result result("Base64"); - - const bool is_valid = (type == "valid"); - const std::string base64 = vars.get_req_str("Base64"); - - try - { - if(is_valid) - { - const std::vector<uint8_t> binary = vars.get_req_bin("Binary"); - result.test_eq("base64 decoding", Botan::base64_decode(base64), binary); - result.test_eq("base64 encoding", Botan::base64_encode(binary), base64); - } - else - { - auto res = Botan::base64_decode(base64); - result.test_failure("decoded invalid base64 to " + Botan::hex_encode(res)); - } - } - catch(std::exception& e) - { - if(is_valid) - { - result.test_failure("rejected valid base64", e.what()); - } - else - { - result.test_note("rejected invalid base64"); - } - } - - return result; - } - - std::vector<Test::Result> run_final_tests() override - { - Test::Result result("Base64"); - const std::string valid_b64 = "Zg=="; - - for(char ws_char : { ' ', '\t', '\r', '\n' }) - { - for(size_t i = 0; i <= valid_b64.size(); ++i) - { - std::string b64_ws = valid_b64; - b64_ws.insert(i, 1, ws_char); - - try - { - result.test_failure("decoded whitespace base64", Botan::base64_decode(b64_ws, false)); - } - catch(std::exception&) {} - - try - { - result.test_eq("base64 decoding with whitespace", Botan::base64_decode(b64_ws, true), "66"); - } - catch(std::exception& e) - { - result.test_failure(b64_ws, e.what()); - } - } - } - - return {result}; - } - }; - -BOTAN_REGISTER_TEST("utils", "base64", Base64_Tests); - -#endif - class Charset_Tests final : public Text_Based_Test { public: |