From fa739928e15d95be8b3704ff1ff9025267c28ec9 Mon Sep 17 00:00:00 2001 From: Daniel Neus Date: Tue, 19 Jul 2016 10:42:29 +0200 Subject: add oid tests --- src/tests/test_oid.cpp | 115 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/tests/test_oid.cpp (limited to 'src/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 +#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 run() override + { + std::vector results; + + std::vector> 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 + +} + +} -- cgit v1.2.3