diff options
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/test_octetstring.cpp | 168 | ||||
-rw-r--r-- | src/tests/test_oid.cpp | 115 | ||||
-rw-r--r-- | src/tests/tests.cpp | 17 | ||||
-rw-r--r-- | src/tests/tests.h | 5 |
4 files changed, 305 insertions, 0 deletions
diff --git a/src/tests/test_octetstring.cpp b/src/tests/test_octetstring.cpp new file mode 100644 index 000000000..ff689518e --- /dev/null +++ b/src/tests/test_octetstring.cpp @@ -0,0 +1,168 @@ +/* +* (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include "tests.h" + +#include <botan/symkey.h> + +namespace Botan_Tests { + +namespace { + +using Botan::OctetString; + +Test::Result test_from_rng() + { + Test::Result result("OctetString"); + + OctetString os(Test::rng(), 32); + result.test_eq("length is 32 bytes", os.size(), 32); + + return result; + } + +Test::Result test_from_hex() + { + Test::Result result("OctetString"); + + OctetString os("0123456789ABCDEF"); + result.test_eq("length is 8 bytes", os.size(), 8); + + return result; + } + +Test::Result test_from_byte() + { + Test::Result result("OctetString"); + + auto rand_bytes = Test::rng().random_vec(8); + OctetString os(rand_bytes.data(), rand_bytes.size()); + result.test_eq("length is 8 bytes", os.size(), 8); + + return result; + } + +Test::Result test_odd_parity() + { + Test::Result result("OctetString"); + + OctetString os("FFFFFFFFFFFFFFFF"); + os.set_odd_parity(); + OctetString expected("FEFEFEFEFEFEFEFE"); + result.test_eq("odd parity set correctly", os, expected); + + OctetString os2("EFCBDA4FAA997F63"); + os2.set_odd_parity(); + OctetString expected2("EFCBDA4FAB987F62"); + result.test_eq("odd parity set correctly", os2, expected2); + + return result; + } + +Test::Result test_as_string() + { + Test::Result result("OctetString"); + + OctetString os("0123456789ABCDEF"); + result.test_eq("OctetString::as_string() returns correct string", os.as_string(), "0123456789ABCDEF"); + + return result; + } + +Test::Result test_xor() + { + Test::Result result("OctetString"); + + OctetString os1("0000000000000000"); + OctetString os2("FFFFFFFFFFFFFFFF"); + + OctetString xor_result = os1 ^ os2; + result.test_eq("OctetString XOR operations works as expected", xor_result, os2); + + xor_result = os1; + xor_result ^= os2; + result.test_eq("OctetString XOR operations works as expected", xor_result, os2); + + xor_result = os2 ^ os2; + result.test_eq("OctetString XOR operations works as expected", xor_result, os1); + + OctetString os3("0123456789ABCDEF"); + xor_result = os3 ^ os2; + OctetString expected("FEDCBA9876543210"); + result.test_eq("OctetString XOR operations works as expected", xor_result, expected); + + return result; + } + +Test::Result test_equality() + { + Test::Result result("OctetString"); + + OctetString os1("0000000000000000"); + OctetString os2("FFFFFFFFFFFFFFFF"); + + result.confirm("OctetString equality operations works as expected", os1 == os1); + result.confirm("OctetString equality operations works as expected", os2 == os2); + result.confirm("OctetString equality operations works as expected", os1 != os2); + + return result; + } + +Test::Result test_append() + { + Test::Result result("OctetString"); + + OctetString os1("0000"); + OctetString os2("FFFF"); + OctetString expected("0000FFFF"); + + OctetString append_result = os1 + os2; + + result.test_eq("OctetString append operations works as expected", append_result, expected); + + return result; + } + +class OctetString_Tests : public Test + { + public: + std::vector<Test::Result> run() override + { + std::vector<Test::Result> results; + + std::vector<std::function<Test::Result()>> fns = + { + test_from_rng, + test_from_hex, + test_from_byte, + test_odd_parity, + test_as_string, + test_xor, + test_equality, + test_append + }; + + for(size_t i = 0; i != fns.size(); ++i) + { + try + { + results.push_back(fns[ i ]()); + } + catch(std::exception& e) + { + results.push_back(Test::Result::Failure("OctetString tests " + std::to_string(i), e.what())); + } + } + + return results; + } + }; + +BOTAN_REGISTER_TEST("octetstring", OctetString_Tests); + +} + +} diff --git a/src/tests/test_oid.cpp b/src/tests/test_oid.cpp new file mode 100644 index 000000000..087fdc64f --- /dev/null +++ b/src/tests/test_oid.cpp @@ -0,0 +1,115 @@ +/* +* (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include "tests.h" + +#if defined(BOTAN_HAS_OID_LOOKUP) + #include <botan/oids.h> +#endif + +namespace Botan_Tests { + +namespace { + +#if defined(BOTAN_HAS_OID_LOOKUP) + +Test::Result test_add_have_OID() + { + Test::Result result("OID add"); + + result.test_eq("there is no OID 'botan-test-oid1'", Botan::OIDS::have_oid("botan-test-oid1"), false); + + Botan::OIDS::add_oid(Botan::OID("1.2.345.6.666"), "botan-test-oid1"); + + result.test_eq("OID 'botan-test-oid1' added successfully", Botan::OIDS::have_oid("botan-test-oid1"), true); + + result.test_eq("name of OID '1.2.345.6.666' is 'botan-test-oid1'", Botan::OIDS::name_of(Botan::OID("1.2.345.6.666"), + "botan-test-oid1"), true); + + return result; + } + +Test::Result test_add_have_OID_str() + { + Test::Result result("OID add string"); + + result.test_eq("there is no OID 'botan-test-oid2'", Botan::OIDS::have_oid("botan-test-oid2"), false); + + Botan::OIDS::add_oidstr("1.2.345.6.777", "botan-test-oid2"); + + result.test_eq("OID 'botan-test-oid2' added successfully", Botan::OIDS::have_oid("botan-test-oid2"), true); + + result.test_eq("name of OID '1.2.345.6.777' is 'botan-test-oid2'", Botan::OIDS::name_of(Botan::OID("1.2.345.6.777"), + "botan-test-oid2"), true); + return result; + } + +Test::Result test_add_and_lookup() + { + Test::Result result("OID add and lookup"); + + result.test_eq("OIDS::lookup returns empty string for non-existent OID object", + Botan::OIDS::lookup(Botan::OID("1.2.345.6.888")), std::string()); + + result.test_throws("OIDS::lookup thows for non-existent OID name", []() + { + Botan::OIDS::lookup("botan-test-oid3"); + }); + + // add oid -> string mapping + Botan::OIDS::add_oid2str(Botan::OID("1.2.345.6.888"), "botan-test-oid3"); + result.test_eq("", Botan::OIDS::lookup(Botan::OID("1.2.345.6.888")), "botan-test-oid3"); + + // still throws + result.test_throws("OIDS::lookup still throws without adding name mapping", []() + { + Botan::OIDS::lookup("botan-test-oid3"); + }); + + // add string -> oid mapping + Botan::OIDS::add_str2oid(Botan::OID("1.2.345.6.888"), "botan-test-oid3"); + Botan::OIDS::lookup("botan-test-oid3"); + + return result; + } + +class OID_Tests : public Test + { + public: + std::vector<Test::Result> run() override + { + std::vector<Test::Result> results; + + std::vector<std::function<Test::Result()>> fns = + { + test_add_have_OID, + test_add_have_OID_str, + test_add_and_lookup, + }; + + for(size_t i = 0; i != fns.size(); ++i) + { + try + { + results.push_back(fns[ i ]()); + } + catch(std::exception& e) + { + results.push_back(Test::Result::Failure("OID tests " + std::to_string(i), e.what())); + } + } + + return results; + } + }; + +BOTAN_REGISTER_TEST("oid", OID_Tests); + +#endif + +} + +} diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp index aae4174b9..578418ffb 100644 --- a/src/tests/tests.cpp +++ b/src/tests/tests.cpp @@ -190,6 +190,23 @@ bool Test::Result::test_eq(const std::string& what, size_t produced, size_t expe return test_is_eq(what, produced, expected); } +bool Test::Result::test_eq(const std::string& what, OctetString produced, OctetString expected) + { + std::ostringstream out; + out << m_who << " " << what; + + if(produced == expected) + { + out << " produced expected result " << produced.as_string(); + return test_success(out.str()); + } + else + { + out << " produced unexpected result " << produced.as_string() << " expected " << expected.as_string(); + return test_failure(out.str()); + } + } + bool Test::Result::test_lt(const std::string& what, size_t produced, size_t expected) { if(produced >= expected) diff --git a/src/tests/tests.h b/src/tests/tests.h index 0016a4d90..b33b2837f 100644 --- a/src/tests/tests.h +++ b/src/tests/tests.h @@ -11,6 +11,7 @@ #include <botan/build.h> #include <botan/rng.h> #include <botan/hex.h> +#include <botan/symkey.h> #if defined(BOTAN_HAS_BIGINT) #include <botan/bigint.h> @@ -38,6 +39,8 @@ using Botan::byte; using Botan::BigInt; #endif +using Botan::OctetString; + class Test_Error : public Botan::Exception { public: @@ -175,6 +178,8 @@ class Test bool test_eq(const std::string& what, size_t produced, size_t expected); + bool test_eq(const std::string& what, OctetString produced, OctetString expected); + template<typename I1, typename I2> bool test_int_eq(I1 x, I2 y, const char* what) { |