aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_cryptobox.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/test_cryptobox.cpp')
-rw-r--r--src/tests/test_cryptobox.cpp75
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;
- }
+}
+}