diff options
author | Jack Lloyd <[email protected]> | 2018-03-06 16:12:58 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-03-06 16:12:58 -0500 |
commit | 5c8552b596c8ead5495625c4c276a23b6a15ff3a (patch) | |
tree | b1e1f0b5167f0c871e22fa11be1ff30232d4ae42 | |
parent | 499afa8646d8e52411e35c0fbc4467a8a21b0f12 (diff) |
Add test that invalid hash function param strings cause errors
-rw-r--r-- | src/tests/test_hash.cpp | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/src/tests/test_hash.cpp b/src/tests/test_hash.cpp index 0424e94f3..f97efe249 100644 --- a/src/tests/test_hash.cpp +++ b/src/tests/test_hash.cpp @@ -1,5 +1,5 @@ /* -* (C) 2014,2015 Jack Lloyd +* (C) 2014,2015,2018 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -16,6 +16,59 @@ namespace Botan_Tests { namespace { +class Invalid_Hash_Name_Tests final : public Test + { + public: + std::vector<Test::Result> run() override + { + Test::Result result("Invalid HashFunction names"); + test_invalid_name(result, "NonExistentHash"); + test_invalid_name(result, "Blake2b(9)", "Bad output bits size for Blake2b"); + test_invalid_name(result, "Comb4P(MD5,MD5)", "Comb4P: Must use two distinct hashes"); + test_invalid_name(result, "Comb4P(MD5,SHA-1)", "Comb4P: Incompatible hashes MD5 and SHA-160"); + test_invalid_name(result, "Tiger(168)", "Tiger: Illegal hash output size: 168"); + test_invalid_name(result, "Tiger(20,2)", "Tiger: Invalid number of passes: 2"); + test_invalid_name(result, "Keccak-1600(160)", "Keccak_1600: Invalid output length 160"); + test_invalid_name(result, "SHA-3(160)", "SHA_3: Invalid output length 160"); + + return {result}; + } + + private: + void test_invalid_name(Result& result, + const std::string& name, + const std::string& expected_msg = "") const + { + try + { + auto hash = Botan::HashFunction::create_or_throw(name); + result.test_failure("Was successfully able to create " + name); + } + catch(Botan::Invalid_Argument& e) + { + const std::string msg = e.what(); + const std::string full_msg = "Invalid argument " + expected_msg; + result.test_eq("expected error message", msg, full_msg); + } + catch(Botan::Lookup_Error& e) + { + const std::string algo_not_found_msg = "Unavailable Hash " + name; + const std::string msg = e.what(); + result.test_eq("expected error message", msg, algo_not_found_msg); + } + catch(std::exception& e) + { + result.test_failure("some unknown exception", e.what()); + } + catch(...) + { + result.test_failure("some unknown exception"); + } + } + }; + +BOTAN_REGISTER_TEST("invalid_name_hash", Invalid_Hash_Name_Tests); + class Hash_Function_Tests final : public Text_Based_Test { public: |