diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-11-08 13:18:17 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-11-08 13:18:17 +0700 |
commit | 879bb29bb0058a0723326de6edebb201e4cbc0b8 (patch) | |
tree | 089e7769c2342a99dfa7549c57290522f72c451b /crypto/test | |
parent | removed unused SecureRandom (diff) | |
download | BouncyCastle.NET-ed25519-879bb29bb0058a0723326de6edebb201e4cbc0b8.tar.xz |
Overhaul GeneralizedTime classes
Diffstat (limited to 'crypto/test')
-rw-r--r-- | crypto/test/src/asn1/test/GeneralizedTimeTest.cs | 131 | ||||
-rw-r--r-- | crypto/test/src/tsp/test/NewTspTest.cs | 10 | ||||
-rw-r--r-- | crypto/test/src/tsp/test/TimeStampTokenInfoTest.cs | 21 |
3 files changed, 19 insertions, 143 deletions
diff --git a/crypto/test/src/asn1/test/GeneralizedTimeTest.cs b/crypto/test/src/asn1/test/GeneralizedTimeTest.cs index 1cc1d8433..266ca2753 100644 --- a/crypto/test/src/asn1/test/GeneralizedTimeTest.cs +++ b/crypto/test/src/asn1/test/GeneralizedTimeTest.cs @@ -40,56 +40,6 @@ namespace Org.BouncyCastle.Asn1.Tests "20020122122220.0001+1000" }; - private static readonly string[] output = - { - "20020122122220", - "20020122122220GMT+00:00", - "20020122122220GMT-10:00", - "20020122122220GMT+00:00", - "20020122122220.1", - "20020122122220.1GMT+00:00", - "20020122122220.1GMT-10:00", - "20020122122220.1GMT+00:00", - "20020122122220.01", - "20020122122220.01GMT+00:00", - "20020122122220.01GMT-10:00", - "20020122122220.01GMT+00:00", - "20020122122220.001", - "20020122122220.001GMT+00:00", - "20020122122220.001GMT-10:00", - "20020122122220.001GMT+00:00", - "20020122122220.0001", - "20020122122220.0001GMT+00:00", - "20020122122220.0001GMT-10:00", - "20020122122220.0001GMT+00:00", - "20020122122220.0001GMT+10:00" - }; - - private static readonly string[] zOutput = - { - "20020122122220Z", - "20020122122220Z", - "20020122222220Z", - "20020122122220Z", - "20020122122220Z", - "20020122122220Z", - "20020122222220Z", - "20020122122220Z", - "20020122122220Z", - "20020122122220Z", - "20020122222220Z", - "20020122122220Z", - "20020122122220Z", - "20020122122220Z", - "20020122222220Z", - "20020122122220Z", - "20020122122220Z", - "20020122122220Z", - "20020122222220Z", - "20020122122220Z", - "20020122022220Z" - }; - private static readonly string[] mzOutput = { "20020122122220.000Z", @@ -161,38 +111,6 @@ namespace Org.BouncyCastle.Asn1.Tests { for (int i = 0; i != input.Length; i++) { - string ii = input[i], oi = output[i]; - - Asn1GeneralizedTime t = new Asn1GeneralizedTime(ii); - DateTime dt = t.ToDateTime(); - string st = t.GetTime(); - - if (oi.IndexOf('G') > 0) // don't check local time the same way - { - if (!st.Equals(oi)) - { - Fail("failed GMT conversion test " + i); - } - - string dts = dt.ToString(@"yyyyMMddHHmmss\Z"); - string zi = zOutput[i]; - if (!dts.Equals(zi)) - { - Fail("failed date conversion test " + i); - } - } - else - { - string offset = CalculateGmtOffset(dt); - if (!st.Equals(oi + offset)) - { - Fail("failed conversion test " + i); - } - } - } - - for (int i = 0; i != input.Length; i++) - { Asn1GeneralizedTime t = new Asn1GeneralizedTime(input[i]); if (!t.ToDateTime().ToString(@"yyyyMMddHHmmss.fff\Z").Equals(mzOutput[i])) @@ -209,7 +127,7 @@ namespace Org.BouncyCastle.Asn1.Tests if (!AreEqual(t.GetEncoded(), new Asn1GeneralizedTime(derMzOutput[i]).GetEncoded())) { - Fail("der encoding wrong"); + Fail("DER encoding wrong"); } } @@ -219,7 +137,7 @@ namespace Org.BouncyCastle.Asn1.Tests if (!AreEqual(t.GetEncoded(), new Asn1GeneralizedTime(derTruncOutput[i]).GetEncoded())) { - Fail("trunc der encoding wrong"); + Fail("trunc DER encoding wrong"); } } @@ -239,20 +157,14 @@ namespace Org.BouncyCastle.Asn1.Tests IsTrue(Arrays.AreEqual(Hex.Decode("180f32303232303830393132313530305a"), der.GetEncoded(Asn1Encodable.Der))); } - { - // check an actual GMT string comes back untampered - Asn1GeneralizedTime time = new Asn1GeneralizedTime("20190704031318GMT+00:00"); - - IsTrue("20190704031318GMT+00:00".Equals(time.GetTime())); - } - try { - new DerGeneralizedTime(new byte[0]); + new DerGeneralizedTime(string.Empty); + Fail("Expected exception"); } catch (ArgumentException e) { - IsTrue(e.Message.StartsWith("GeneralizedTime string too short")); + IsTrue(e.Message.StartsWith("invalid date string")); } /* @@ -277,39 +189,6 @@ namespace Org.BouncyCastle.Asn1.Tests } } - private string CalculateGmtOffset(DateTime date) - { - TimeZoneInfo timeZone = TimeZoneInfo.Local; - TimeSpan offset = timeZone.BaseUtcOffset; - - char sign = '+'; - if (offset.CompareTo(TimeSpan.Zero) < 0) - { - sign = '-'; - offset = offset.Duration(); - } - - int hours = offset.Hours; - int minutes = offset.Minutes; - - if (timeZone.SupportsDaylightSavingTime && timeZone.IsDaylightSavingTime(date)) - { - hours += sign.Equals("+") ? 1 : -1; - } - - return "GMT" + sign + Convert(hours) + ":" + Convert(minutes); - } - - private string Convert(int time) - { - if (time < 10) - { - return "0" + time; - } - - return time.ToString(); - } - [Test] public void TestFunction() { diff --git a/crypto/test/src/tsp/test/NewTspTest.cs b/crypto/test/src/tsp/test/NewTspTest.cs index c98e5d191..69bec7958 100644 --- a/crypto/test/src/tsp/test/NewTspTest.cs +++ b/crypto/test/src/tsp/test/NewTspTest.cs @@ -787,12 +787,12 @@ namespace Org.BouncyCastle.Tsp.Tests } private void resolutionTest(AsymmetricKeyParameter privateKey, X509Certificate cert, - IStore<X509Certificate> certs, Resolution resoution, string timeString) + IStore<X509Certificate> certs, Resolution resolution, string timeString) { TimeStampTokenGenerator tsTokenGen = new TimeStampTokenGenerator( - privateKey, cert, TspAlgorithms.Sha1, "1.2"); + privateKey, cert, TspAlgorithms.Sha1, "1.2"); - tsTokenGen.Resolution = resoution; + tsTokenGen.Resolution = resolution; tsTokenGen.SetCertificates(certs); TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator(); @@ -813,14 +813,14 @@ namespace Org.BouncyCastle.Tsp.Tests tsToken = tsResp.TimeStampToken; Assert.AreEqual("19700101000009Z", tsToken.TimeStampInfo.TstInfo.GenTime.TimeString); - if ((int)resoution > (int)Resolution.R_HUNDREDTHS_OF_SECONDS) + if ((int)resolution > (int)Resolution.R_HUNDREDTHS_OF_SECONDS) { tsResp = tsRespGen.Generate(request, new BigInteger("23"), UnixEpoch.AddMilliseconds(9990)); tsToken = tsResp.TimeStampToken; Assert.AreEqual("19700101000009.99Z", tsToken.TimeStampInfo.TstInfo.GenTime.TimeString); } - if ((int)resoution > (int)Resolution.R_TENTHS_OF_SECONDS) + if ((int)resolution > (int)Resolution.R_TENTHS_OF_SECONDS) { tsResp = tsRespGen.Generate(request, new BigInteger("23"), UnixEpoch.AddMilliseconds(9900)); tsToken = tsResp.TimeStampToken; diff --git a/crypto/test/src/tsp/test/TimeStampTokenInfoTest.cs b/crypto/test/src/tsp/test/TimeStampTokenInfoTest.cs index a1a7355e5..03f611beb 100644 --- a/crypto/test/src/tsp/test/TimeStampTokenInfoTest.cs +++ b/crypto/test/src/tsp/test/TimeStampTokenInfoTest.cs @@ -1,7 +1,5 @@ using NUnit.Framework; -using Org.BouncyCastle.Asn1; -using Org.BouncyCastle.Asn1.Tsp; using Org.BouncyCastle.Math; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Date; @@ -31,7 +29,7 @@ namespace Org.BouncyCastle.Tsp.Tests [Test] public void TestTstInfo1() { - TimeStampTokenInfo tstInfo = getTimeStampTokenInfo(tstInfo1); + TimeStampTokenInfo tstInfo = CreateTimeStampTokenInfo(tstInfo1); // // verify @@ -60,7 +58,7 @@ namespace Org.BouncyCastle.Tsp.Tests [Test] public void TestTstInfo2() { - TimeStampTokenInfo tstInfo = getTimeStampTokenInfo(tstInfo2); + TimeStampTokenInfo tstInfo = CreateTimeStampTokenInfo(tstInfo2); // // verify @@ -81,7 +79,9 @@ namespace Org.BouncyCastle.Tsp.Tests Assert.AreEqual(tstInfo.Nonce, BigInteger.ValueOf(100)); - Assert.IsTrue(Arrays.AreEqual(Hex.Decode("ffffffffffffffffffffffffffffffffffffffff"), tstInfo.GetMessageImprintDigest())); + Assert.IsTrue(Arrays.AreEqual( + Hex.Decode("ffffffffffffffffffffffffffffffffffffffff"), + tstInfo.GetMessageImprintDigest())); Assert.IsTrue(Arrays.AreEqual(tstInfo2, tstInfo.GetEncoded())); } @@ -89,7 +89,7 @@ namespace Org.BouncyCastle.Tsp.Tests [Test] public void TestTstInfo3() { - TimeStampTokenInfo tstInfo = getTimeStampTokenInfo(tstInfo3); + TimeStampTokenInfo tstInfo = CreateTimeStampTokenInfo(tstInfo3); // // verify @@ -122,7 +122,7 @@ namespace Org.BouncyCastle.Tsp.Tests { try { - getTimeStampTokenInfo(tstInfoDudDate); + CreateTimeStampTokenInfo(tstInfoDudDate); Assert.Fail("dud date not detected."); } @@ -132,12 +132,9 @@ namespace Org.BouncyCastle.Tsp.Tests } } - private TimeStampTokenInfo getTimeStampTokenInfo( - byte[] tstInfo) + private TimeStampTokenInfo CreateTimeStampTokenInfo(byte[] tstInfo) { - return new TimeStampTokenInfo( - TstInfo.GetInstance( - Asn1Object.FromByteArray(tstInfo))); + return new TimeStampTokenInfo(tstInfo); } } } |