aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/tests.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/tests.h')
-rw-r--r--src/tests/tests.h67
1 files changed, 66 insertions, 1 deletions
diff --git a/src/tests/tests.h b/src/tests/tests.h
index 88102f289..14ec5a17b 100644
--- a/src/tests/tests.h
+++ b/src/tests/tests.h
@@ -16,6 +16,7 @@
#include <string>
#include <vector>
#include <iostream>
+#include <sstream>
Botan::RandomNumberGenerator& test_rng();
@@ -45,7 +46,69 @@ typedef std::function<size_t ()> test_fn;
size_t run_tests(const std::vector<std::pair<std::string, test_fn>>& tests);
void test_report(const std::string& name, size_t ran, size_t failed);
-#define TEST(expr, msg) do { if(!(expr)) { ++fails; std::cout << msg; } while(0)
+class Test_State
+ {
+ public:
+ void started(const std::string& /*msg*/) { m_tests_run++; }
+
+ void test_ran(const char* msg);
+
+ void failure(const char* test, const std::string& what_failed)
+ {
+ std::cout << "FAIL " << test << " " << what_failed << "\n";
+ m_tests_failed++;
+ }
+
+ size_t ran() const { return m_tests_run; }
+ size_t failed() const { return m_tests_failed; }
+ private:
+ size_t m_tests_run = 0, m_tests_failed = 0;
+ };
+
+#define BOTAN_CONFIRM_NOTHROW(block) do { \
+ try { block } \
+ catch(std::exception& e) { \
+ _test.failure(BOTAN_CURRENT_FUNCTION, e.what()); \
+ } } while(0) \
+
+#define BOTAN_TEST(lhs, rhs, msg) do { \
+ _test.started(msg); \
+ BOTAN_CONFIRM_NOTHROW({ \
+ const auto lhs_val = lhs; \
+ const auto rhs_val = rhs; \
+ const bool cmp = lhs_val == rhs_val; \
+ if(!cmp) \
+ { \
+ std::ostringstream fmt; \
+ fmt << "expr '" << #lhs << " == " << #rhs << "' false, " \
+ << "actually " << lhs_val << " " << rhs_val \
+ << " (" << msg << ")"; \
+ _test.failure(BOTAN_CURRENT_FUNCTION, fmt.str()); \
+ } \
+ }); \
+ } while(0)
+
+#define BOTAN_CONFIRM(expr, msg) do { \
+ _test.started(msg); \
+ BOTAN_CONFIRM_NOTHROW({ \
+ const bool expr_val = expr; \
+ if(!expr_val) \
+ { \
+ std::ostringstream fmt; \
+ fmt << "expr '" << #expr << " false (" << msg << ")"; \
+ _test.failure(BOTAN_CURRENT_FUNCTION, fmt.str()); \
+ } \
+ }); \
+ } while(0)
+
+#define BOTAN_TEST_CASE(name, descr, block) size_t test_ ## name() { \
+ Test_State _test; \
+ BOTAN_CONFIRM_NOTHROW(block); \
+ test_report(descr, _test.ran(), _test.failed()); \
+ return _test.failed(); \
+ }
+
+//#define TEST(expr, msg) do { if(!(expr)) { ++fails; std::cout << msg; } while(0)
#define TEST_DATA_DIR "src/tests/data"
#define TEST_DATA_DIR_PK "src/tests/data/pubkey"
@@ -75,6 +138,7 @@ size_t test_dh();
size_t test_dlies();
size_t test_elgamal();
size_t test_ecc_pointmul();
+size_t test_ecc_random();
size_t test_ecdsa();
size_t test_gost_3410();
size_t test_curve25519();
@@ -94,6 +158,7 @@ size_t test_pk_keygen();
size_t test_bigint();
size_t test_ecc_unit();
+size_t test_ecc_randomized();
size_t test_ecdsa_unit();
size_t test_ecdh_unit();