aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_hash.cpp
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2015-11-11 05:43:01 -0500
committerJack Lloyd <[email protected]>2015-11-11 05:43:01 -0500
commitcf05aea092fad448c2f4a8e8b66159237096ba8e (patch)
tree00631bcc84809a1eeac5dd32dd92c62143ef831b /src/tests/test_hash.cpp
parent6bb38ae2fa0e1be46b3a3256ac03f435b16a57ea (diff)
Update and consolidate the test framework.
The tests previously had used 4 to 6 different schemes internally (the vec file reader framework, Catch, the old InSiTo Boost.Test tests, the PK/BigInt tests which escaped the rewrite in 1.11.7, plus a number of one-offs). Converge on a design that works everywhere, and update all the things. Fix also a few bugs found by the test changes: SHA-512-256 name incorrect, OpenSSL RC4 name incorrect, signature of FFI function botan_pubkey_destroy was wrong.
Diffstat (limited to 'src/tests/test_hash.cpp')
-rw-r--r--src/tests/test_hash.cpp116
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);
- }
+}