From cf2633e7cd4f2d29f4e3e56a3d3b81b11e254a41 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 7 Nov 2022 20:04:44 +0700 Subject: Overhaul UTCTime classes --- crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs | 9 ++- crypto/test/src/asn1/test/TimeTest.cs | 7 +- crypto/test/src/asn1/test/UTCTimeTest.cs | 76 ++++++++-------------- crypto/test/src/test/nist/NistCertPathTest.cs | 10 +-- 4 files changed, 41 insertions(+), 61 deletions(-) (limited to 'crypto/test') diff --git a/crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs b/crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs index 83f30fd73..fb035de18 100644 --- a/crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs +++ b/crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs @@ -28,8 +28,8 @@ namespace Org.BouncyCastle.Asn1.Tests DerBoolean.True, DerBoolean.False, new DerEnumerated(100), - new DerGeneralizedTime("20070315173729Z"), - new DerGeneralString("hello world"), + new DerGeneralizedTime("20070315173729Z"), + new DerGeneralString("hello world"), new DerIA5String("hello"), new DerInteger(1000), DerNull.Instance, @@ -43,7 +43,10 @@ namespace Org.BouncyCastle.Asn1.Tests new DerT61String("hello world"), new DerTaggedObject(0, new DerPrintableString("hello world")), new DerUniversalString(data), - new DerUtcTime(new DateTime()), +#pragma warning disable CS0618 // Type or member is obsolete + new DerUtcTime(DateTime.Now), +#pragma warning restore CS0618 // Type or member is obsolete + new DerUtcTime(DateTime.Now, 2049), new DerUtf8String("hello world"), new DerVisibleString("hello world"), new DerGraphicString(Hex.Decode("deadbeef")), diff --git a/crypto/test/src/asn1/test/TimeTest.cs b/crypto/test/src/asn1/test/TimeTest.cs index 04dac3c9e..4d080b535 100644 --- a/crypto/test/src/asn1/test/TimeTest.cs +++ b/crypto/test/src/asn1/test/TimeTest.cs @@ -17,11 +17,10 @@ namespace Org.BouncyCastle.Asn1.Tests // Time classes only have a resolution of seconds now = SimpleTest.MakeUtcDateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second); - Org.BouncyCastle.Asn1.Cms.Time cmsTime = new Org.BouncyCastle.Asn1.Cms.Time(now); - Org.BouncyCastle.Asn1.X509.Time x509Time = new Org.BouncyCastle.Asn1.X509.Time(now); + Cms.Time cmsTime = new Cms.Time(now); + X509.Time x509Time = new X509.Time(now); -// Assert.AreEqual(cmsTime.Date, x509Time.ToDateTime()); - Assert.AreEqual(now, cmsTime.Date); + Assert.AreEqual(now, cmsTime.ToDateTime()); Assert.AreEqual(now, x509Time.ToDateTime()); } } diff --git a/crypto/test/src/asn1/test/UTCTimeTest.cs b/crypto/test/src/asn1/test/UTCTimeTest.cs index bac0e1188..fcf72a4fc 100644 --- a/crypto/test/src/asn1/test/UTCTimeTest.cs +++ b/crypto/test/src/asn1/test/UTCTimeTest.cs @@ -1,4 +1,4 @@ -using System; +using System.Globalization; using NUnit.Framework; @@ -18,56 +18,39 @@ namespace Org.BouncyCastle.Asn1.Tests "020122122220Z", "020122122220-1000", "020122122220+1000", - "020122122220+00", "0201221222Z", "0201221222-1000", "0201221222+1000", - "0201221222+00", "550122122220Z", - "5501221222Z" - }; + "5501221222Z", + "4007270730Z", + }; - private static readonly string[] output = - { - "20020122122220GMT+00:00", - "20020122122220GMT-10:00", - "20020122122220GMT+10:00", - "20020122122220GMT+00:00", - "20020122122200GMT+00:00", - "20020122122200GMT-10:00", - "20020122122200GMT+10:00", - "20020122122200GMT+00:00", - "19550122122220GMT+00:00", - "19550122122200GMT+00:00" - }; - - private static readonly string[] zOutput1 = + private static readonly string[] outputPre2040 = { "20020122122220Z", "20020122222220Z", "20020122022220Z", - "20020122122220Z", "20020122122200Z", "20020122222200Z", "20020122022200Z", - "20020122122200Z", "19550122122220Z", - "19550122122200Z" - }; + "19550122122200Z", + "19400727073000Z", + }; - private static readonly string[] zOutput2 = + private static readonly string[] outputPost2040 = { "20020122122220Z", "20020122222220Z", "20020122022220Z", - "20020122122220Z", "20020122122200Z", "20020122222200Z", "20020122022200Z", - "20020122122200Z", "19550122122220Z", - "19550122122200Z" - }; + "19550122122200Z", + "20400727073000Z", + }; public override string Name { @@ -76,34 +59,29 @@ namespace Org.BouncyCastle.Asn1.Tests public override void PerformTest() { -// SimpleDateFormat yyyyF = new SimpleDateFormat("yyyyMMddHHmmss'Z'"); -// SimpleDateFormat yyF = new SimpleDateFormat("yyyyMMddHHmmss'Z'"); - -// yyyyF.setTimeZone(new SimpleTimeZone(0,"Z")); -// yyF.setTimeZone(new SimpleTimeZone(0,"Z")); + bool pre2040 = DateTimeFormatInfo.InvariantInfo.Calendar.TwoDigitYearMax < 2040; + string[] outputDefault = pre2040 ? outputPre2040 : outputPost2040; - for (int i = 0; i != input.Length; i++) + for (int i = 0; i != input.Length; i++) { DerUtcTime t = new DerUtcTime(input[i]); - if (!t.AdjustedTimeString.Equals(output[i])) + if (!t.ToDateTime().ToString(@"yyyyMMddHHmmssK").Equals(outputDefault[i])) { - Fail("failed conversion test " + i); + Fail("failed date shortened conversion test " + i); } -// if (!yyyyF.format(t.getAdjustedDate()).Equals(zOutput1[i])) - if (!t.ToAdjustedDateTime().ToString(@"yyyyMMddHHmmss\Z").Equals(zOutput1[i])) - { - Fail("failed date conversion test " + i); - } + if (!t.ToDateTime(2029).ToString(@"yyyyMMddHHmmssK").Equals(outputPre2040[i])) + { + Fail("failed date conversion test " + i); + } -// if (!yyF.format(t.getDate()).Equals(zOutput2[i])) - if (!t.ToDateTime().ToString(@"yyyyMMddHHmmss\Z").Equals(zOutput2[i])) - { - Fail("failed date shortened conversion test " + i); - } - } - } + if (!t.ToDateTime(2049).ToString(@"yyyyMMddHHmmssK").Equals(outputPost2040[i])) + { + Fail("failed date conversion test " + i); + } + } + } [Test] public void TestFunction() diff --git a/crypto/test/src/test/nist/NistCertPathTest.cs b/crypto/test/src/test/nist/NistCertPathTest.cs index bdcd1b51f..c8bf44566 100644 --- a/crypto/test/src/test/nist/NistCertPathTest.cs +++ b/crypto/test/src/test/nist/NistCertPathTest.cs @@ -116,7 +116,7 @@ namespace Org.BouncyCastle.Tests.Nist new string[] { "BadnotBeforeDateCACert", "InvalidCAnotBeforeDateTest1EE" }, new string[] { TRUST_ANCHOR_ROOT_CRL, "BadnotBeforeDateCACRL" }, 1, - "Could not validate certificate: certificate not valid until 20470101120100GMT+00:00"); + "Could not validate certificate: certificate not valid until 20470101120100Z"); } [Test] @@ -126,7 +126,7 @@ namespace Org.BouncyCastle.Tests.Nist new string[] { GOOD_CA_CERT, "InvalidEEnotBeforeDateTest2EE" }, new string[] { TRUST_ANCHOR_ROOT_CRL, GOOD_CA_CRL }, 0, - "Could not validate certificate: certificate not valid until 20470101120100GMT+00:00"); + "Could not validate certificate: certificate not valid until 20470101120100Z"); } [Test] @@ -152,7 +152,7 @@ namespace Org.BouncyCastle.Tests.Nist new string[] { "BadnotAfterDateCACert", "InvalidCAnotAfterDateTest5EE" }, new string[] { TRUST_ANCHOR_ROOT_CRL, "BadnotAfterDateCACRL" }, 1, - "Could not validate certificate: certificate expired on 20020101120100GMT+00:00"); + "Could not validate certificate: certificate expired on 20020101120100Z"); } [Test] @@ -162,7 +162,7 @@ namespace Org.BouncyCastle.Tests.Nist new string[] { GOOD_CA_CERT, "InvalidEEnotAfterDateTest6EE" }, new string[] { TRUST_ANCHOR_ROOT_CRL, GOOD_CA_CRL }, 0, - "Could not validate certificate: certificate expired on 20020101120100GMT+00:00"); + "Could not validate certificate: certificate expired on 20020101120100Z"); } [Test] @@ -172,7 +172,7 @@ namespace Org.BouncyCastle.Tests.Nist new string[] { GOOD_CA_CERT, "Invalidpre2000UTCEEnotAfterDateTest7EE" }, new string[] { TRUST_ANCHOR_ROOT_CRL, GOOD_CA_CRL }, 0, - "Could not validate certificate: certificate expired on 19990101120100GMT+00:00"); + "Could not validate certificate: certificate expired on 19990101120100Z"); } [Test] -- cgit 1.4.1