diff options
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/catchy/catchy_tests.h | 41 | ||||
-rw-r--r-- | src/tests/catchy/test_x509.cpp | 143 |
2 files changed, 153 insertions, 31 deletions
diff --git a/src/tests/catchy/catchy_tests.h b/src/tests/catchy/catchy_tests.h index f1170fa71..99ad03f31 100644 --- a/src/tests/catchy/catchy_tests.h +++ b/src/tests/catchy/catchy_tests.h @@ -19,13 +19,33 @@ namespace Catch { namespace Matchers { namespace Impl { + namespace Generic { + template<typename ExpressionT> + struct Not : public MatcherImpl<Not<ExpressionT>, ExpressionT> + { + Not( Matcher<ExpressionT> const& matcher ) : m_matcher(matcher.clone()) {} + Not( Not const& other ) : m_matcher( other.m_matcher ) {} + + virtual bool match( ExpressionT const& expr ) const + { + return !m_matcher->match( expr ); + } + virtual std::string toString() const { + return "not " + m_matcher->toString(); + } + + Ptr<Matcher<ExpressionT>> m_matcher; + }; + } // namespace Generic + namespace StdVector { template<typename T, typename Alloc> - struct Equals : MatcherImpl<Equals<T, Alloc>, std::vector<T, Alloc> > { + struct Equals : MatcherImpl<Equals<T, Alloc>, std::vector<T, Alloc> > + { Equals( std::vector<T, Alloc> const& vec ) : m_vector( vec ){} Equals( Equals const& other ) : m_vector( other.m_vector ){} - virtual ~Equals() {}; + virtual ~Equals() {} virtual bool match( std::vector<T, Alloc> const& expr ) const { return m_vector == expr; @@ -39,17 +59,18 @@ namespace Matchers { } // namespace StdVector namespace Boolean { - struct Equals : MatcherImpl<Equals, bool> { + struct Equals : MatcherImpl<Equals, bool> + { Equals( const bool expected ) : m_expected( expected ){} Equals( Equals const& other ) : m_expected( other.m_expected ){} - virtual ~Equals() override {}; + virtual ~Equals() override {} virtual bool match( bool const& expr ) const { return m_expected == expr; } virtual std::string toString() const { - return " == " + Catch::toString(m_expected); + return "== " + Catch::toString(m_expected); } bool m_expected; @@ -58,11 +79,12 @@ namespace Matchers { namespace Integer { template<typename T> - struct Equals : MatcherImpl<Equals<T>, T> { + struct Equals : MatcherImpl<Equals<T>, T> + { Equals( const T expected ) : m_expected( expected ){} Equals( Equals const& other ) : m_expected( other.m_expected ){} - virtual ~Equals() override {}; + virtual ~Equals() override {} virtual bool match( T const& expr ) const { return m_expected == expr; @@ -79,6 +101,11 @@ namespace Matchers { // The following functions create the actual matcher objects. // This allows the types to be inferred + template<typename ExpressionT> + inline Impl::Generic::Not<ExpressionT> Not( Impl::Matcher<ExpressionT> const& m ) { + return Impl::Generic::Not<ExpressionT>( m ); + } + template <typename T, typename Alloc> inline Impl::StdVector::Equals<T, Alloc> Equals( std::vector<T, Alloc> const& vec ) { return Impl::StdVector::Equals<T, Alloc>( vec ); diff --git a/src/tests/catchy/test_x509.cpp b/src/tests/catchy/test_x509.cpp index 01c01dbdc..cb2f8f0cf 100644 --- a/src/tests/catchy/test_x509.cpp +++ b/src/tests/catchy/test_x509.cpp @@ -3,45 +3,140 @@ #include "catchy_tests.h" -// deacticate due to -// https://github.com/randombit/botan/issues/185 - -#if 0 - #if defined(BOTAN_HAS_ASN1) +#include <botan/exceptn.h> #include <botan/asn1_time.h> +using namespace Botan; +using namespace Catch; + TEST_CASE("human readable time", "[X509]") { - auto time1 = Botan::X509_Time("2008-02-01"); - auto time2 = Botan::X509_Time("2008-02-01 17:24:33"); - auto time3 = Botan::X509_Time("2004-06-14T23:34:30"); + auto time1 = X509_Time("0802010000Z", ASN1_Tag::UTC_TIME); + auto time2 = X509_Time("0802011724Z", ASN1_Tag::UTC_TIME); + auto time3 = X509_Time("040614233430Z", ASN1_Tag::UTC_TIME); + + CHECK_THAT(time1.time_is_set(), Equals(true)); + CHECK_THAT(time2.time_is_set(), Equals(true)); + CHECK_THAT(time3.time_is_set(), Equals(true)); + + CHECK_THAT(time1.readable_string(), Equals("2008/02/01 00:00:00 UTC")); + CHECK_THAT(time2.readable_string(), Equals("2008/02/01 17:24:00 UTC")); + CHECK_THAT(time3.readable_string(), Equals("2004/06/14 23:34:30 UTC")); + } + +TEST_CASE("Implicit copy constructor", "[X509]") + { + auto time_orig = X509_Time("0802010000Z", ASN1_Tag::UTC_TIME); + auto time_copy = time_orig; - CHECK(( time1.time_is_set() == true )); - CHECK(( time2.time_is_set() == true )); - CHECK(( time3.time_is_set() == true )); + // Check that implicit copy and assignment work: + // time_copy and time_orig must have the same data but + // must sit at different places in memory + CHECK((time_orig == time_copy)); - CHECK(( time1.readable_string() == "2008/02/01 00:00:00 UTC" )); - CHECK(( time2.readable_string() == "2008/02/01 17:24:33 UTC" )); - CHECK(( time3.readable_string() == "2004/06/14 23:34:30 UTC" )); + auto address1 = reinterpret_cast<uintptr_t>(&time_orig); + auto address2 = reinterpret_cast<uintptr_t>(&time_copy); + + CHECK_THAT(address1, Not(Equals(address2))); } TEST_CASE("no time", "[X509]") { - auto time = Botan::X509_Time(""); - CHECK(( time.time_is_set() == false )); + auto time = X509_Time(); + CHECK_THAT(time.time_is_set(), Equals(false)); } -TEST_CASE("invalid time", "[X509]") +TEST_CASE("valid UTCTime", "[X509]") { - CHECK_THROWS( Botan::X509_Time(" ") ); - CHECK_THROWS( Botan::X509_Time("2008`02-01") ); - CHECK_THROWS( Botan::X509_Time("9999-02-01") ); - CHECK_THROWS( Botan::X509_Time("2000-02-01 17") ); - CHECK_THROWS( Botan::X509_Time("999921") ); + SECTION("precision: minute; including timezone: no", "Length 11") + { + CHECK_NOTHROW(X509_Time("0802010000Z", ASN1_Tag::UTC_TIME)); + CHECK_NOTHROW(X509_Time("0802011724Z", ASN1_Tag::UTC_TIME)); + CHECK_NOTHROW(X509_Time("0406142334Z", ASN1_Tag::UTC_TIME)); + CHECK_NOTHROW(X509_Time("9906142334Z", ASN1_Tag::UTC_TIME)); + CHECK_NOTHROW(X509_Time("0006142334Z", ASN1_Tag::UTC_TIME)); + } + + SECTION("precision: seconds; including timezone: no", "Length 13") + { + CHECK_NOTHROW(X509_Time("080201000000Z", ASN1_Tag::UTC_TIME)); + CHECK_NOTHROW(X509_Time("080201172412Z", ASN1_Tag::UTC_TIME)); + CHECK_NOTHROW(X509_Time("040614233433Z", ASN1_Tag::UTC_TIME)); + CHECK_NOTHROW(X509_Time("990614233444Z", ASN1_Tag::UTC_TIME)); + CHECK_NOTHROW(X509_Time("000614233455Z", ASN1_Tag::UTC_TIME)); + } + + SECTION("precision: minute; including timezone: yes", "Length 15") + { + // Valid times that are not supported by Botan + CHECK_THROWS_AS(X509_Time("0802010000-0000", ASN1_Tag::UTC_TIME), Unsupported_Argument); + CHECK_THROWS_AS(X509_Time("0802011724+0000", ASN1_Tag::UTC_TIME), Unsupported_Argument); + CHECK_THROWS_AS(X509_Time("0406142334-0500", ASN1_Tag::UTC_TIME), Unsupported_Argument); + CHECK_THROWS_AS(X509_Time("9906142334+0500", ASN1_Tag::UTC_TIME), Unsupported_Argument); + CHECK_THROWS_AS(X509_Time("0006142334-0530", ASN1_Tag::UTC_TIME), Unsupported_Argument); + CHECK_THROWS_AS(X509_Time("0006142334+0530", ASN1_Tag::UTC_TIME), Unsupported_Argument); + } + + SECTION("precision: seconds; including timezone: yes", "Length 17") + { + // Valid times that are not supported by Botan + CHECK_THROWS_AS(X509_Time("080201000000-0000", ASN1_Tag::UTC_TIME), Unsupported_Argument); + CHECK_THROWS_AS(X509_Time("080201172412+0000", ASN1_Tag::UTC_TIME), Unsupported_Argument); + CHECK_THROWS_AS(X509_Time("040614233433-0500", ASN1_Tag::UTC_TIME), Unsupported_Argument); + CHECK_THROWS_AS(X509_Time("990614233444+0500", ASN1_Tag::UTC_TIME), Unsupported_Argument); + CHECK_THROWS_AS(X509_Time("000614233455-0530", ASN1_Tag::UTC_TIME), Unsupported_Argument); + CHECK_THROWS_AS(X509_Time("000614233455+0530", ASN1_Tag::UTC_TIME), Unsupported_Argument); + } } -#endif // BOTAN_HAS_ASN1 +TEST_CASE("invalid UTCTime", "[X509]") + { + // invalid length + CHECK_THROWS(X509_Time("", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time(" ", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("2008`02-01", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("9999-02-01", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("2000-02-01 17", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("999921", ASN1_Tag::UTC_TIME)); + + // valid length 13 -> range check + CHECK_THROWS(X509_Time("080201000061Z", ASN1_Tag::UTC_TIME)); // seconds too big (61) + CHECK_THROWS(X509_Time("080201000060Z", ASN1_Tag::UTC_TIME)); // seconds too big (60, leap seconds not covered by the standard) + CHECK_THROWS(X509_Time("0802010000-1Z", ASN1_Tag::UTC_TIME)); // seconds too small (-1) + CHECK_THROWS(X509_Time("080201006000Z", ASN1_Tag::UTC_TIME)); // minutes too big (60) + CHECK_THROWS(X509_Time("080201240000Z", ASN1_Tag::UTC_TIME)); // hours too big (24:00) -#endif + // valid length 13 -> invalid numbers + CHECK_THROWS(X509_Time("08020123112 Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("08020123112!Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("08020123112,Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("08020123112\nZ", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("080201232 33Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("080201232!33Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("080201232,33Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("080201232\n33Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("0802012 3344Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("0802012!3344Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("0802012,3344Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("08022\n334455Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("08022 334455Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("08022!334455Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("08022,334455Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("08022\n334455Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("082 33445511Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("082!33445511Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("082,33445511Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("082\n33445511Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("2 2211221122Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("2!2211221122Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("2,2211221122Z", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("2\n2211221122Z", ASN1_Tag::UTC_TIME)); + + // wrong time zone + CHECK_THROWS(X509_Time("0802010000", ASN1_Tag::UTC_TIME)); + CHECK_THROWS(X509_Time("0802010000z", ASN1_Tag::UTC_TIME)); + } + +#endif // BOTAN_HAS_ASN1 |