diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-10-25 00:13:45 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-10-25 00:13:45 +0700 |
commit | 11b4d6a7afeb6f3be906a1a0ddf132df3f255b4a (patch) | |
tree | f76b51eae3e754f33c61dde13c7dd429e0f70753 /crypto/test | |
parent | fixed refactored class names in pqc/utils (diff) | |
download | BouncyCastle.NET-ed25519-11b4d6a7afeb6f3be906a1a0ddf132df3f255b4a.tar.xz |
Add Asn1GeneralizedTime and use
Diffstat (limited to 'crypto/test')
-rw-r--r-- | crypto/test/src/asn1/test/DeclarationOfMajorityUnitTest.cs | 6 | ||||
-rw-r--r-- | crypto/test/src/asn1/test/GeneralizedTimeTest.cs | 101 | ||||
-rw-r--r-- | crypto/test/src/asn1/test/PersonalDataUnitTest.cs | 6 | ||||
-rw-r--r-- | crypto/test/src/asn1/test/X509NameTest.cs | 4 |
4 files changed, 64 insertions, 53 deletions
diff --git a/crypto/test/src/asn1/test/DeclarationOfMajorityUnitTest.cs b/crypto/test/src/asn1/test/DeclarationOfMajorityUnitTest.cs index 0975ea1b8..3af922a5c 100644 --- a/crypto/test/src/asn1/test/DeclarationOfMajorityUnitTest.cs +++ b/crypto/test/src/asn1/test/DeclarationOfMajorityUnitTest.cs @@ -17,7 +17,7 @@ namespace Org.BouncyCastle.Asn1.Tests public override void PerformTest() { - DerGeneralizedTime dateOfBirth = new DerGeneralizedTime("20070315173729Z"); + Asn1GeneralizedTime dateOfBirth = new Asn1GeneralizedTime("20070315173729Z"); DeclarationOfMajority decl = new DeclarationOfMajority(dateOfBirth); CheckConstruction(decl, DeclarationOfMajority.Choice.DateOfBirth, dateOfBirth, -1); @@ -48,7 +48,7 @@ namespace Org.BouncyCastle.Asn1.Tests private void CheckConstruction( DeclarationOfMajority decl, DeclarationOfMajority.Choice type, - DerGeneralizedTime dateOfBirth, + Asn1GeneralizedTime dateOfBirth, int notYoungerThan) { CheckValues(decl, type, dateOfBirth, notYoungerThan); @@ -65,7 +65,7 @@ namespace Org.BouncyCastle.Asn1.Tests private void CheckValues( DeclarationOfMajority decl, DeclarationOfMajority.Choice type, - DerGeneralizedTime dateOfBirth, + Asn1GeneralizedTime dateOfBirth, int notYoungerThan) { checkMandatoryField("type", (int) type, (int) decl.Type); diff --git a/crypto/test/src/asn1/test/GeneralizedTimeTest.cs b/crypto/test/src/asn1/test/GeneralizedTimeTest.cs index d9d84462a..1cc1d8433 100644 --- a/crypto/test/src/asn1/test/GeneralizedTimeTest.cs +++ b/crypto/test/src/asn1/test/GeneralizedTimeTest.cs @@ -163,7 +163,7 @@ namespace Org.BouncyCastle.Asn1.Tests { string ii = input[i], oi = output[i]; - DerGeneralizedTime t = new DerGeneralizedTime(ii); + Asn1GeneralizedTime t = new Asn1GeneralizedTime(ii); DateTime dt = t.ToDateTime(); string st = t.GetTime(); @@ -171,14 +171,14 @@ namespace Org.BouncyCastle.Asn1.Tests { if (!st.Equals(oi)) { - Fail("failed conversion test"); + 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"); + Fail("failed date conversion test " + i); } } else @@ -186,70 +186,74 @@ namespace Org.BouncyCastle.Asn1.Tests string offset = CalculateGmtOffset(dt); if (!st.Equals(oi + offset)) { - Fail("failed conversion test"); + Fail("failed conversion test " + i); } } } for (int i = 0; i != input.Length; i++) { - DerGeneralizedTime t = new DerGeneralizedTime(input[i]); + Asn1GeneralizedTime t = new Asn1GeneralizedTime(input[i]); if (!t.ToDateTime().ToString(@"yyyyMMddHHmmss.fff\Z").Equals(mzOutput[i])) { - Console.WriteLine("{0} != {1}", t.ToDateTime().ToString(@"yyyyMMddHHmmss.SSS\Z"), mzOutput[i]); + Console.WriteLine("{0} != {1}", t.ToDateTime().ToString(@"yyyyMMddHHmmss.fff\Z"), mzOutput[i]); - Fail("failed long date conversion test"); + Fail("failed long date conversion test " + i); } } - // TODO - //for (int i = 0; i != mzOutput.Length; i++) - //{ - // DerGeneralizedTime t = new DerGeneralizedTime(mzOutput[i]); + for (int i = 0; i != mzOutput.Length; i++) + { + DerGeneralizedTime t = new DerGeneralizedTime(mzOutput[i]); - // if (!AreEqual(t.GetEncoded(), new DerGeneralizedTime(derMzOutput[i]).GetEncoded())) - // { - // Fail("der encoding wrong"); - // } - //} + if (!AreEqual(t.GetEncoded(), new Asn1GeneralizedTime(derMzOutput[i]).GetEncoded())) + { + Fail("der encoding wrong"); + } + } - // TODO - //for (int i = 0; i != truncOutput.Length; i++) - //{ - // DerGeneralizedTime t = new DerGeneralizedTime(truncOutput[i]); + for (int i = 0; i != truncOutput.Length; i++) + { + DerGeneralizedTime t = new DerGeneralizedTime(truncOutput[i]); - // if (!AreEqual(t.GetEncoded(), new DerGeneralizedTime(derTruncOutput[i]).GetEncoded())) - // { - // Fail("trunc der encoding wrong"); - // } - //} + if (!AreEqual(t.GetEncoded(), new Asn1GeneralizedTime(derTruncOutput[i]).GetEncoded())) + { + Fail("trunc der encoding wrong"); + } + } { // check BER encoding is still "as given" - DerGeneralizedTime t = new DerGeneralizedTime("202208091215Z"); + Asn1GeneralizedTime ber = new Asn1GeneralizedTime("202208091215Z"); + + //IsTrue(Arrays.AreEqual(Hex.Decode("180d3230323230383039313231355a"), ber.GetEncoded(Asn1Encodable.DL))); + IsTrue(Arrays.AreEqual(Hex.Decode("180d3230323230383039313231355a"), ber.GetEncoded(Asn1Encodable.Ber))); + IsTrue(Arrays.AreEqual(Hex.Decode("180f32303232303830393132313530305a"), ber.GetEncoded(Asn1Encodable.Der))); - //IsTrue(Arrays.AreEqual(Hex.Decode("180d3230323230383039313231355a"), t.GetEncoded(Asn1Encodable.DL))); - IsTrue(Arrays.AreEqual(Hex.Decode("180d3230323230383039313231355a"), t.GetEncoded(Asn1Encodable.Ber))); - IsTrue(Arrays.AreEqual(Hex.Decode("180f32303232303830393132313530305a"), t.GetEncoded(Asn1Encodable.Der))); + // check always uses DER encoding + DerGeneralizedTime der = new DerGeneralizedTime("202208091215Z"); + + //IsTrue(Arrays.AreEqual(Hex.Decode("180f32303232303830393132313530305a"), der.GetEncoded(Asn1Encodable.DL))); + IsTrue(Arrays.AreEqual(Hex.Decode("180f32303232303830393132313530305a"), der.GetEncoded(Asn1Encodable.Ber))); + IsTrue(Arrays.AreEqual(Hex.Decode("180f32303232303830393132313530305a"), der.GetEncoded(Asn1Encodable.Der))); } - // TODO - //{ - // // check an actual GMT string comes back untampered - // DerGeneralizedTime time = new DerGeneralizedTime("20190704031318GMT+00:00"); + { + // check an actual GMT string comes back untampered + Asn1GeneralizedTime time = new Asn1GeneralizedTime("20190704031318GMT+00:00"); - // IsTrue("20190704031318GMT+00:00".Equals(time.GetTime())); + IsTrue("20190704031318GMT+00:00".Equals(time.GetTime())); + } - // try - // { - // DerGeneralizedTime.GetInstance(new byte[0]); - // } - // catch (ArgumentException e) - // { - // IsTrue(e.Message.Equals("GeneralizedTime string too short")); - // } - //} + try + { + new DerGeneralizedTime(new byte[0]); + } + catch (ArgumentException e) + { + IsTrue(e.Message.StartsWith("GeneralizedTime string too short")); + } /* * [BMA-87] @@ -275,17 +279,24 @@ namespace Org.BouncyCastle.Asn1.Tests private string CalculateGmtOffset(DateTime date) { - char sign = '+'; + TimeZoneInfo timeZone = TimeZoneInfo.Local; + TimeSpan offset = timeZone.BaseUtcOffset; - TimeSpan offset = TimeZoneInfo.Local.GetUtcOffset(date); + 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); } diff --git a/crypto/test/src/asn1/test/PersonalDataUnitTest.cs b/crypto/test/src/asn1/test/PersonalDataUnitTest.cs index b965578c1..7f79fa743 100644 --- a/crypto/test/src/asn1/test/PersonalDataUnitTest.cs +++ b/crypto/test/src/asn1/test/PersonalDataUnitTest.cs @@ -21,7 +21,7 @@ namespace Org.BouncyCastle.Asn1.Tests { NameOrPseudonym nameOrPseudonym = new NameOrPseudonym("pseudonym"); BigInteger nameDistinguisher = BigInteger.ValueOf(10); - DerGeneralizedTime dateOfBirth= new DerGeneralizedTime("20070315173729Z"); + Asn1GeneralizedTime dateOfBirth = new Asn1GeneralizedTime("20070315173729Z"); DirectoryString placeOfBirth = new DirectoryString("placeOfBirth"); string gender = "M"; DirectoryString postalAddress = new DirectoryString("address"); @@ -73,7 +73,7 @@ namespace Org.BouncyCastle.Asn1.Tests PersonalData data, NameOrPseudonym nameOrPseudonym, BigInteger nameDistinguisher, - DerGeneralizedTime dateOfBirth, + Asn1GeneralizedTime dateOfBirth, DirectoryString placeOfBirth, string gender, DirectoryString postalAddress) @@ -97,7 +97,7 @@ namespace Org.BouncyCastle.Asn1.Tests PersonalData data, NameOrPseudonym nameOrPseudonym, BigInteger nameDistinguisher, - DerGeneralizedTime dateOfBirth, + Asn1GeneralizedTime dateOfBirth, DirectoryString placeOfBirth, string gender, DirectoryString postalAddress) diff --git a/crypto/test/src/asn1/test/X509NameTest.cs b/crypto/test/src/asn1/test/X509NameTest.cs index bafec3226..098500a0b 100644 --- a/crypto/test/src/asn1/test/X509NameTest.cs +++ b/crypto/test/src/asn1/test/X509NameTest.cs @@ -100,12 +100,12 @@ namespace Org.BouncyCastle.Asn1.Tests string val) { IAsn1Convertible converted = createEntryValue(oid, val); - if (!(converted is DerGeneralizedTime)) + if (!(converted is Asn1GeneralizedTime)) { Fail("encoding for " + oid + " not GeneralizedTime"); } converted = createEntryValueFromString(oid, val); - if (!(converted is DerGeneralizedTime)) + if (!(converted is Asn1GeneralizedTime)) { Fail("encoding for " + oid + " not GeneralizedTime"); } |