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_cryptobox.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_cryptobox.cpp')
-rw-r--r-- | src/tests/test_cryptobox.cpp | 75 |
1 files changed, 43 insertions, 32 deletions
diff --git a/src/tests/test_cryptobox.cpp b/src/tests/test_cryptobox.cpp index 773360952..4eb9cdcdb 100644 --- a/src/tests/test_cryptobox.cpp +++ b/src/tests/test_cryptobox.cpp @@ -6,45 +6,56 @@ #include "tests.h" -#include <iostream> - #if defined(BOTAN_HAS_CRYPTO_BOX) #include <botan/cryptobox.h> + #include <botan/hex.h> #endif -using namespace Botan; +namespace Botan_Tests { -size_t test_cryptobox() - { - size_t fails = 0; +namespace { #if defined(BOTAN_HAS_CRYPTO_BOX) - auto& rng = test_rng(); - - const byte msg[] = { 0xAA, 0xBB, 0xCC }; - std::string ciphertext = CryptoBox::encrypt(msg, sizeof(msg), - "secret password", - rng); - - try - { - std::string plaintext = CryptoBox::decrypt(ciphertext, - "secret password"); - - if(plaintext.size() != sizeof(msg) || - !same_mem(reinterpret_cast<const byte*>(plaintext.data()), msg, sizeof(msg))) - ++fails; - - } - catch(std::exception& e) - { - std::cout << "Error during Cryptobox test " << e.what() << std::endl; - ++fails; - } - - test_report("Cryptobox", 1, fails); + +class Cryptobox_Tests : public Test + { + public: + std::vector<Test::Result> run() override + { + std::vector<Test::Result> results; + Test::Result result("cryptobox"); + + const std::vector<byte> msg = Botan::hex_decode("AABBCC"); + const std::string password = "secret"; + + std::string ciphertext = Botan::CryptoBox::encrypt(msg.data(), msg.size(), + password, + Test::rng()); + + try + { + std::string plaintext = Botan::CryptoBox::decrypt(ciphertext, password); + + const byte* pt_b = reinterpret_cast<const byte*>(plaintext.data()); + + std::vector<byte> pt_vec(pt_b, pt_b + plaintext.size()); + + result.test_eq("decrypt", pt_vec, msg); + } + catch(std::exception& e) + { + result.test_failure("cryptobox decrypt", e.what()); + } + + results.push_back(result); + return results; + } + }; + +BOTAN_REGISTER_TEST("cryptobox", Cryptobox_Tests); + #endif - return fails; - } +} +} |