diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-02-19 13:56:35 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-02-19 13:56:35 +0700 |
commit | 4d353ca4d930cb985b474aabb498c0c0e88b515c (patch) | |
tree | cde3085bed6c263bf1faaff136ae1a40231aa1a0 | |
parent | Tabs -> spaces (diff) | |
download | BouncyCastle.NET-ed25519-4d353ca4d930cb985b474aabb498c0c0e88b515c.tar.xz |
[BMA-87]
Fix for UTC-type GeneralizedTime instances
-rw-r--r-- | crypto/src/asn1/DerGeneralizedTime.cs | 27 | ||||
-rw-r--r-- | crypto/test/src/asn1/test/GeneralizedTimeTest.cs | 406 |
2 files changed, 241 insertions, 192 deletions
diff --git a/crypto/src/asn1/DerGeneralizedTime.cs b/crypto/src/asn1/DerGeneralizedTime.cs index 54fbabbbe..548a268e1 100644 --- a/crypto/src/asn1/DerGeneralizedTime.cs +++ b/crypto/src/asn1/DerGeneralizedTime.cs @@ -257,15 +257,26 @@ namespace Org.BouncyCastle.Asn1 return sb.ToString(); } - private DateTime ParseDateString( - string dateStr, - string formatStr, - bool makeUniversal) + private DateTime ParseDateString(string s, string format, bool makeUniversal) { - DateTime dt = DateTime.ParseExact( - dateStr, - formatStr, - DateTimeFormatInfo.InvariantInfo); + /* + * NOTE: DateTime.Kind and DateTimeStyles.AssumeUniversal not available in .NET 1.1 + */ + DateTimeStyles style = DateTimeStyles.None; + if (format.EndsWith("Z")) + { + try + { + style = (DateTimeStyles)Enum.Parse(typeof(DateTimeStyles), "AssumeUniversal"); + } + catch (Exception) + { + } + + style |= DateTimeStyles.AdjustToUniversal; + } + + DateTime dt = DateTime.ParseExact(s, format, DateTimeFormatInfo.InvariantInfo, style); return makeUniversal ? dt.ToUniversalTime() : dt; } diff --git a/crypto/test/src/asn1/test/GeneralizedTimeTest.cs b/crypto/test/src/asn1/test/GeneralizedTimeTest.cs index b169cfea8..51995e195 100644 --- a/crypto/test/src/asn1/test/GeneralizedTimeTest.cs +++ b/crypto/test/src/asn1/test/GeneralizedTimeTest.cs @@ -1,193 +1,231 @@ using System; +using NUnit.Framework; + using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Asn1.Tests { - /** - * X.690 test example - */ - public class GeneralizedTimeTest - : SimpleTest - { - private static readonly string[] input = - { - "20020122122220", - "20020122122220Z", - "20020122122220-1000", - "20020122122220+00", - "20020122122220.1", - "20020122122220.1Z", - "20020122122220.1-1000", - "20020122122220.1+00", - "20020122122220.01", - "20020122122220.01Z", - "20020122122220.01-1000", - "20020122122220.01+00", - "20020122122220.001", - "20020122122220.001Z", - "20020122122220.001-1000", - "20020122122220.001+00", - "20020122122220.0001", - "20020122122220.0001Z", - "20020122122220.0001-1000", - "20020122122220.0001+00", - "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", - "20020122122220.000Z", - "20020122222220.000Z", - "20020122122220.000Z", - "20020122122220.100Z", - "20020122122220.100Z", - "20020122222220.100Z", - "20020122122220.100Z", - "20020122122220.010Z", - "20020122122220.010Z", - "20020122222220.010Z", - "20020122122220.010Z", - "20020122122220.001Z", - "20020122122220.001Z", - "20020122222220.001Z", - "20020122122220.001Z", - "20020122122220.000Z", - "20020122122220.000Z", - "20020122222220.000Z", - "20020122122220.000Z", - "20020122022220.000Z" - }; - - public override string Name - { - get { return "GeneralizedTime"; } - } - - public override void PerformTest() - { - for (int i = 0; i != input.Length; i++) - { - DerGeneralizedTime t = new DerGeneralizedTime(input[i]); - - if (output[i].IndexOf('G') > 0) // don't check local time the same way - { - if (!t.GetTime().Equals(output[i])) - { - Fail("failed conversion test"); - } - - if (!t.ToDateTime().ToString(@"yyyyMMddHHmmss\Z").Equals(zOutput[i])) - { - Fail("failed date conversion test"); - } - } - else - { - string offset = CalculateGmtOffset(t.ToDateTime()); - if (!t.GetTime().Equals(output[i] + offset)) - { - Fail("failed conversion test"); - } - } - } - - for (int i = 0; i != input.Length; i++) - { - DerGeneralizedTime t = new DerGeneralizedTime(input[i]); - - if (!t.ToDateTime().ToString(@"yyyyMMddHHmmss.fff\Z").Equals(mzOutput[i])) - { - Console.WriteLine("{0} != {1}", t.ToDateTime().ToString(@"yyyyMMddHHmmss.SSS\Z"), mzOutput[i]); - - Fail("failed long date conversion test"); - } - } - } - - private string CalculateGmtOffset( - DateTime date) - { - char sign = '+'; + /** + * X.690 test example + */ + [TestFixture] + public class GeneralizedTimeTest + : SimpleTest + { + private static readonly string[] input = + { + "20020122122220", + "20020122122220Z", + "20020122122220-1000", + "20020122122220+00", + "20020122122220.1", + "20020122122220.1Z", + "20020122122220.1-1000", + "20020122122220.1+00", + "20020122122220.01", + "20020122122220.01Z", + "20020122122220.01-1000", + "20020122122220.01+00", + "20020122122220.001", + "20020122122220.001Z", + "20020122122220.001-1000", + "20020122122220.001+00", + "20020122122220.0001", + "20020122122220.0001Z", + "20020122122220.0001-1000", + "20020122122220.0001+00", + "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", + "20020122122220.000Z", + "20020122222220.000Z", + "20020122122220.000Z", + "20020122122220.100Z", + "20020122122220.100Z", + "20020122222220.100Z", + "20020122122220.100Z", + "20020122122220.010Z", + "20020122122220.010Z", + "20020122222220.010Z", + "20020122122220.010Z", + "20020122122220.001Z", + "20020122122220.001Z", + "20020122222220.001Z", + "20020122122220.001Z", + "20020122122220.000Z", + "20020122122220.000Z", + "20020122222220.000Z", + "20020122122220.000Z", + "20020122022220.000Z" + }; + + public override string Name + { + get { return "GeneralizedTime"; } + } + + public override void PerformTest() + { + for (int i = 0; i != input.Length; i++) + { + string ii = input[i], oi = output[i]; + + DerGeneralizedTime t = new DerGeneralizedTime(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 conversion test"); + } + + string dts = dt.ToString(@"yyyyMMddHHmmss\Z"); + string zi = zOutput[i]; + if (!dts.Equals(zi)) + { + Fail("failed date conversion test"); + } + } + else + { + string offset = CalculateGmtOffset(dt); + if (!st.Equals(oi + offset)) + { + Fail("failed conversion test"); + } + } + } + + for (int i = 0; i != input.Length; i++) + { + DerGeneralizedTime t = new DerGeneralizedTime(input[i]); + + if (!t.ToDateTime().ToString(@"yyyyMMddHHmmss.fff\Z").Equals(mzOutput[i])) + { + Console.WriteLine("{0} != {1}", t.ToDateTime().ToString(@"yyyyMMddHHmmss.SSS\Z"), mzOutput[i]); + + Fail("failed long date conversion test"); + } + } + + /* + * [BMA-87] + */ + { + DateTime t1 = new DerUtcTime("110616114855Z").ToDateTime(); + DateTime t2 = new DerGeneralizedTime("20110616114855Z").ToDateTime(); + + if (t1 != t2) + { + Fail("failed UTC equivalence test"); + } + + DateTime u1 = t1.ToUniversalTime(); + DateTime u2 = t2.ToUniversalTime(); + + if (u1 != u2) + { + Fail("failed UTC conversion test"); + } + } + } + + private string CalculateGmtOffset( + DateTime date) + { + char sign = '+'; // Note: GetUtcOffset incorporates Daylight Savings offset - TimeSpan offset = TimeZone.CurrentTimeZone.GetUtcOffset(date); - if (offset.CompareTo(TimeSpan.Zero) < 0) - { - sign = '-'; - offset = offset.Duration(); - } - int hours = offset.Hours; - int minutes = offset.Minutes; - - return "GMT" + sign + Convert(hours) + ":" + Convert(minutes); - } - - private string Convert(int time) - { - if (time < 10) - { - return "0" + time; - } - - return time.ToString(); - } - - public static void Main( - string[] args) - { - RunTest(new GeneralizedTimeTest()); - } - } + TimeSpan offset = TimeZone.CurrentTimeZone.GetUtcOffset(date); + if (offset.CompareTo(TimeSpan.Zero) < 0) + { + sign = '-'; + offset = offset.Duration(); + } + int hours = offset.Hours; + int minutes = offset.Minutes; + + return "GMT" + sign + Convert(hours) + ":" + Convert(minutes); + } + + private string Convert(int time) + { + if (time < 10) + { + return "0" + time; + } + + return time.ToString(); + } + + public static void Main( + string[] args) + { + RunTest(new GeneralizedTimeTest()); + } + + [Test] + public void TestFunction() + { + string resultText = Perform().ToString(); + + Assert.AreEqual(Name + ": Okay", resultText); + } + } } |