aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_modes.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_modes.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_modes.cpp')
-rw-r--r--src/tests/test_modes.cpp127
1 files changed, 48 insertions, 79 deletions
diff --git a/src/tests/test_modes.cpp b/src/tests/test_modes.cpp
index f443ddabf..cc1645e92 100644
--- a/src/tests/test_modes.cpp
+++ b/src/tests/test_modes.cpp
@@ -7,93 +7,62 @@
#include "tests.h"
#if defined(BOTAN_HAS_MODES)
+ #include <botan/cipher_mode.h>
+#endif
-#include <botan/hex.h>
-#include <botan/cipher_mode.h>
-#include <iostream>
-#include <fstream>
-#include <memory>
+namespace Botan_Tests {
-using namespace Botan;
-
-namespace {
+#if defined(BOTAN_HAS_MODES)
-secure_vector<byte> run_mode(const std::string& algo,
- Cipher_Dir dir,
- const secure_vector<byte>& pt,
- const secure_vector<byte>& nonce,
- const secure_vector<byte>& key)
+class Cipher_Mode_Tests : public Text_Based_Test
{
- std::unique_ptr<Cipher_Mode> cipher(get_cipher_mode(algo, dir));
- if(!cipher)
- throw std::runtime_error("No cipher " + algo + " enabled in build");
-
- cipher->set_key(key);
- cipher->start(nonce);
-
- secure_vector<byte> ct = pt;
- cipher->finish(ct);
- return ct;
- }
-
-size_t mode_test(const std::string& algo,
- const std::string& pt,
- const std::string& ct,
- const std::string& key_hex,
- const std::string& nonce_hex)
- {
- auto nonce = hex_decode_locked(nonce_hex);
- auto key = hex_decode_locked(key_hex);
-
- size_t fails = 0;
-
- const std::string ct2 = hex_encode(run_mode(algo,
- ENCRYPTION,
- hex_decode_locked(pt),
- nonce,
- key));
-
- if(ct != ct2)
- {
- std::cout << algo << " got ct " << ct2 << " expected " << ct << std::endl;
- ++fails;
- }
-
- const std::string pt2 = hex_encode(run_mode(algo,
- DECRYPTION,
- hex_decode_locked(ct),
- nonce,
- key));
-
- if(pt != pt2)
- {
- std::cout << algo << " got pt " << pt2 << " expected " << pt << std::endl;
- ++fails;
- }
-
- return fails;
- }
+ public:
+ Cipher_Mode_Tests() :
+ Text_Based_Test(Test::data_dir("modes"), {"Key", "Nonce", "In", "Out"})
+ {}
-}
+ Test::Result run_one_test(const std::string& algo, const VarMap& vars) override
+ {
+ const std::vector<uint8_t> key = get_req_bin(vars, "Key");
+ const std::vector<uint8_t> nonce = get_opt_bin(vars, "Nonce");
+ const std::vector<uint8_t> input = get_req_bin(vars, "In");
+ const std::vector<uint8_t> expected = get_req_bin(vars, "Out");
-size_t test_modes()
- {
- auto test = [](const std::string& input)
- {
- std::ifstream vec(input);
+ Test::Result result(algo);
+
+ std::unique_ptr<Botan::Cipher_Mode> enc(Botan::get_cipher_mode(algo, Botan::ENCRYPTION));
+ std::unique_ptr<Botan::Cipher_Mode> dec(Botan::get_cipher_mode(algo, Botan::DECRYPTION));
+
+ if(!enc || !dec)
+ {
+ result.note_missing(algo);
+ return result;
+ }
- return run_tests_bb(vec, "Mode", "Out", true,
- [](std::map<std::string, std::string> m)
- {
- return mode_test(m["Mode"], m["In"], m["Out"], m["Key"], m["Nonce"]);
- });
- };
+ result.test_eq("mode not authenticated", enc->authenticated(), false);
- return run_tests_in_dir(TEST_DATA_DIR "/modes", test);
- }
+ enc->set_key(key);
+ enc->start(nonce);
-#else
+ Botan::secure_vector<uint8_t> buf(input.begin(), input.end());
+ // TODO: should first update if possible
+ enc->finish(buf);
-SKIP_TEST(modes);
+ result.test_eq("encrypt", buf, expected);
-#endif // BOTAN_HAS_MODES
+ buf.assign(expected.begin(), expected.end());
+
+ dec->set_key(key);
+ dec->start(nonce);
+ dec->finish(buf);
+ result.test_eq("decrypt", buf, input);
+
+ return result;
+ }
+ };
+
+BOTAN_REGISTER_TEST("modes", Cipher_Mode_Tests);
+
+#endif
+
+}