diff options
author | Jack Lloyd <[email protected]> | 2015-11-11 05:43:01 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2015-11-11 05:43:01 -0500 |
commit | cf05aea092fad448c2f4a8e8b66159237096ba8e (patch) | |
tree | 00631bcc84809a1eeac5dd32dd92c62143ef831b /src/tests/test_passhash.cpp | |
parent | 6bb38ae2fa0e1be46b3a3256ac03f435b16a57ea (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_passhash.cpp')
-rw-r--r-- | src/tests/test_passhash.cpp | 139 |
1 files changed, 72 insertions, 67 deletions
diff --git a/src/tests/test_passhash.cpp b/src/tests/test_passhash.cpp index 4bc69125b..e9606062c 100644 --- a/src/tests/test_passhash.cpp +++ b/src/tests/test_passhash.cpp @@ -6,94 +6,99 @@ #include "tests.h" -#include <iostream> +#if defined(BOTAN_HAS_BCRYPT) + #include <botan/bcrypt.h> +#endif #if defined(BOTAN_HAS_PASSHASH9) #include <botan/passhash9.h> #endif -#if defined(BOTAN_HAS_BCRYPT) - #include <botan/bcrypt.h> -#endif - -using namespace Botan; +namespace Botan_Tests { -size_t test_bcrypt() - { - size_t fails = 0; +namespace { #if defined(BOTAN_HAS_BCRYPT) +class Bcrypt_Tests : public Text_Based_Test + { + public: + Bcrypt_Tests() : Text_Based_Test(Test::data_file("bcrypt.vec"), {"Password","Passhash"}) {} - // Generated by jBCrypt 0.3 - if(!check_bcrypt("abc", "$2a$05$DfPyLs.G6.To9fXEFgUL1O6HpYw3jIXgPcl/L3Qt3jESuWmhxtmpS")) - { - std::cout << "Bcrypt test 1 failed" << std::endl; - fails++; - } - - // http://www.openwall.com/lists/john-dev/2011/06/19/2 - if(!check_bcrypt("\xA3", - "$2a$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq")) - { - std::cout << "Bcrypt test 2 failed" << std::endl; - fails++; - } - - auto& rng = test_rng(); - - for(u16bit level = 1; level != 5; ++level) - { - const std::string input = "some test passphrase 123"; - const std::string gen_hash = generate_bcrypt(input, rng, level); - - if(!check_bcrypt(input, gen_hash)) + Test::Result run_one_test(const std::string&, const VarMap& vars) override { - std::cout << "Gen and check for bcrypt failed: " << gen_hash << " not valid" << std::endl; - ++fails; - } - } + // Encoded as binary so we can test binary inputs + const std::vector<byte> password_vec = get_req_bin(vars, "Password"); + const std::string password(reinterpret_cast<const char*>(password_vec.data()), + password_vec.size()); - test_report("Bcrypt", 6, fails); + const std::string passhash = get_req_str(vars, "Passhash"); -#endif + Test::Result result("bcrypt"); + result.test_eq("correct hash accepted", Botan::check_bcrypt(password, passhash), true); - return fails; - } + const size_t max_level = 1 + std::min<size_t>(Test::soak_level() / 2, 10); -size_t test_passhash9() - { - size_t fails = 0; + for(size_t level = 1; level <= max_level; ++level) + { + const std::string gen_hash = generate_bcrypt(password, Test::rng(), level); + result.test_eq("generated hash accepted", Botan::check_bcrypt(password, gen_hash), true); + } -#if defined(BOTAN_HAS_PASSHASH9) - const std::string input = "secret"; - const std::string fixed_hash = - "$9$AAAKhiHXTIUhNhbegwBXJvk03XXJdzFMy+i3GFMIBYKtthTTmXZA"; - - size_t ran = 0; + return result; + } + }; - ++ran; - if(!check_passhash9(input, fixed_hash)) - { - std::cout << "Passhash9 fixed input test failed" << std::endl; - fails++; - } +BOTAN_REGISTER_TEST("bcrypt", Bcrypt_Tests); - auto& rng = test_rng(); +#endif - for(byte alg_id = 0; alg_id <= 4; ++alg_id) - { - std::string gen_hash = generate_passhash9(input, rng, 2, alg_id); +#if defined(BOTAN_HAS_PASSHASH9) +class Passhash9_Tests : public Text_Based_Test + { + public: + Passhash9_Tests() : Text_Based_Test(Test::data_file("passhash9.vec"), {"Password","Passhash"}) {} - ++ran; - if(!check_passhash9(input, gen_hash)) + Test::Result run_one_test(const std::string&, const VarMap& vars) override { - std::cout << "Passhash9 gen and check " << static_cast<int>(alg_id) << " failed" << std::endl; - ++fails; + // Encoded as binary so we can test binary inputs + const std::vector<byte> password_vec = get_req_bin(vars, "Password"); + const std::string password(reinterpret_cast<const char*>(password_vec.data()), + password_vec.size()); + + const std::string passhash = get_req_str(vars, "Passhash"); + + Test::Result result("passhash9"); + result.test_eq("correct hash accepted", Botan::check_passhash9(password, passhash), true); + + for(byte alg_id = 0; alg_id <= 4; ++alg_id) + { + const std::string gen_hash = Botan::generate_passhash9(password, Test::rng(), 2, alg_id); + + if(!result.test_eq("generated hash accepted", Botan::check_passhash9(password, gen_hash), true)) + { + result.test_note("hash was " + gen_hash); + } + } + + const size_t max_level = 1 + std::min<size_t>(Test::soak_level() / 2, 10); + + for(size_t level = 1; level <= max_level; ++level) + { + const std::string gen_hash = Botan::generate_passhash9(password, Test::rng(), level); + if(!result.test_eq("generated hash accepted", Botan::check_passhash9(password, gen_hash), true)) + { + result.test_note("hash was " + gen_hash); + } + } + + return result; } - } + }; + +BOTAN_REGISTER_TEST("passhash9", Passhash9_Tests); - test_report("Passhash9", ran, fails); #endif - return fails; - } +} + +} |