diff options
Diffstat (limited to 'src/tests/test_hash.cpp')
-rw-r--r-- | src/tests/test_hash.cpp | 116 |
1 files changed, 44 insertions, 72 deletions
diff --git a/src/tests/test_hash.cpp b/src/tests/test_hash.cpp index 42ffcc11a..6c0dfb1c6 100644 --- a/src/tests/test_hash.cpp +++ b/src/tests/test_hash.cpp @@ -7,98 +7,70 @@ #include "tests.h" #include <botan/hash.h> -#include <botan/hex.h> -#include <iostream> -#include <fstream> -using namespace Botan; +namespace Botan_Tests { namespace { -size_t hash_test(const std::string& algo, - const std::string& in_hex, - const std::string& out_hex) +class Hash_Function_Tests : public Text_Based_Test { - size_t fails = 0; + public: + Hash_Function_Tests() : Text_Based_Test(Test::data_dir("hash"), {"In", "Out"}) {} - const std::vector<std::string> providers = HashFunction::providers(algo); - - if(providers.empty()) - { - std::cout << "Unknown hash '" << algo << "'" << std::endl; - return 0; - } - - for(auto provider: providers) - { - std::unique_ptr<HashFunction> hash(HashFunction::create(algo, provider)); - - if(!hash) + Test::Result run_one_test(const std::string& algo, const VarMap& vars) override { - std::cout << "Unable to get " << algo << " from " << provider << std::endl; - ++fails; - continue; - } + const std::vector<uint8_t> input = get_req_bin(vars, "In"); + const std::vector<uint8_t> expected = get_req_bin(vars, "Out"); - const std::vector<byte> in = hex_decode(in_hex); + Test::Result result(algo); - hash->update(in); + const std::vector<std::string> providers = Botan::HashFunction::providers(algo); - auto h = hash->final(); + if(providers.empty()) + { + result.note_missing("block cipher " + algo); + return result; + } - if(h != hex_decode_locked(out_hex)) - { - std::cout << algo << " " << provider << " got " << hex_encode(h) << " != " << out_hex << std::endl; - ++fails; - } + for(auto&& provider: providers) + { + std::unique_ptr<Botan::HashFunction> hash(Botan::HashFunction::create(algo, provider)); - // Test to make sure clear() resets what we need it to - hash->update("some discarded input"); - hash->clear(); + if(!hash) + { + result.note_missing(algo + " from " + provider); + continue; + } - hash->update(in); + result.test_eq(provider.c_str(), hash->name(), algo); - h = hash->final(); + hash->update(input); - if(h != hex_decode_locked(out_hex)) - { - std::cout << algo << " " << provider << " got " << hex_encode(h) << " != " << out_hex - << " (with discarded input)" << std::endl; - ++fails; - } + result.test_eq(provider, "hashing", hash->final(), expected); - if(in.size() > 1) - { - hash->update(in[0]); - hash->update(&in[1], in.size() - 1); - h = hash->final(); + // Test to make sure clear() resets what we need it to + hash->update("some discarded input"); + hash->clear(); + hash->update(nullptr, 0); // this should be effectively ignored + hash->update(input); - if(h != hex_decode_locked(out_hex)) - { - std::cout << algo << " " << provider << " got " << hex_encode(h) << " != " << out_hex - << " (with offset input)" << std::endl; - ++fails; + result.test_eq(provider, "hashing after clear", hash->final(), expected); + + if(input.size() > 1) + { + hash->update(input[0]); + hash->update(&input[1], input.size() - 1); + result.test_eq(provider, "hashing split", hash->final(), expected); + } } + + return result; } - } - return fails; - } + }; + +BOTAN_REGISTER_TEST("hash", Hash_Function_Tests); } -size_t test_hash() - { - auto test = [](const std::string& input) - { - std::ifstream vec(input); - - return run_tests_bb(vec, "Hash", "Out", true, - [](std::map<std::string, std::string> m) -> size_t - { - return hash_test(m["Hash"], m["In"], m["Out"]); - }); - }; - - return run_tests_in_dir(TEST_DATA_DIR "/hash", test); - } +} |