From 74d32ea1045704c1f2c5db61bad9c58cdb92f5d8 Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Tue, 31 Aug 2021 18:45:05 -0400 Subject: [PATCH] Simplified DateUtils and added more unit tests --- MimeKit/Utils/DateUtils.cs | 36 ++----------------- UnitTests/Utils/DateParserTests.cs | 56 +++++++++++++++++++++++------- 2 files changed, 47 insertions(+), 45 deletions(-) diff --git a/MimeKit/Utils/DateUtils.cs b/MimeKit/Utils/DateUtils.cs index 230d64b0d2..4b9494d10d 100644 --- a/MimeKit/Utils/DateUtils.cs +++ b/MimeKit/Utils/DateUtils.cs @@ -587,18 +587,7 @@ public static bool TryParse (byte[] buffer, int startIndex, out DateTimeOffset d if (startIndex < 0 || startIndex > buffer.Length) throw new ArgumentOutOfRangeException (nameof (startIndex)); - int length = buffer.Length - startIndex; - var tokens = new List (TokenizeDate (buffer, startIndex, length)); - - if (TryParseStandardDateFormat (tokens, buffer, out date)) - return true; - - if (TryParseUnknownDateFormat (tokens, buffer, out date)) - return true; - - date = new DateTimeOffset (); - - return false; + return TryParse (buffer, startIndex, buffer.Length - startIndex, out date); } /// @@ -618,17 +607,7 @@ public static bool TryParse (byte[] buffer, out DateTimeOffset date) if (buffer == null) throw new ArgumentNullException (nameof (buffer)); - var tokens = new List (TokenizeDate (buffer, 0, buffer.Length)); - - if (TryParseStandardDateFormat (tokens, buffer, out date)) - return true; - - if (TryParseUnknownDateFormat (tokens, buffer, out date)) - return true; - - date = new DateTimeOffset (); - - return false; + return TryParse (buffer, 0, buffer.Length, out date); } /// @@ -649,17 +628,8 @@ public static bool TryParse (string text, out DateTimeOffset date) throw new ArgumentNullException (nameof (text)); var buffer = Encoding.UTF8.GetBytes (text); - var tokens = new List (TokenizeDate (buffer, 0, buffer.Length)); - if (TryParseStandardDateFormat (tokens, buffer, out date)) - return true; - - if (TryParseUnknownDateFormat (tokens, buffer, out date)) - return true; - - date = new DateTimeOffset (); - - return false; + return TryParse (buffer, 0, buffer.Length, out date); } // Note: this method exists because BouncyCastle's DerUtcTime.ParseDateString() fails diff --git a/UnitTests/Utils/DateParserTests.cs b/UnitTests/Utils/DateParserTests.cs index ce92876dba..4deef0fd12 100644 --- a/UnitTests/Utils/DateParserTests.cs +++ b/UnitTests/Utils/DateParserTests.cs @@ -52,7 +52,17 @@ public class DateParserTests "Tue, 21 Apr 15 14:44:51 GMT", "Tue, 21 April 15 14:44:51 GMT", "Thu, 1 Oct 2015 14:40:57 +0200 (Mitteleuropäische Sommerzeit)", - "Tue, 12 Jun 2012 19:22:28 0200" + "Tue, 12 Jun 2012 19:22:28 0200", + "Fri, 8 May 2015", + "Fri, 8 May 2015 12", + "Fri, 8 May 2015 12:05", + "Fri, 8 May 2015 12:05:01", + "Fri, 8 May 2015 12:05:01 400", + "Sat, 9 May 2015 24:00:00 -0400", + "Sat, 9 May 2015 25:00:00 -0400", + "May 9 2015 25:00:00 -0400", + "2015 May 9 25:00:00 -0400", + "2015 May 9 25:99:78 -0400", }; static readonly string[] expected = { @@ -73,18 +83,26 @@ public class DateParserTests "Tue, 21 Apr 2015 14:44:51 +0000", "Tue, 21 Apr 2015 14:44:51 +0000", "Thu, 01 Oct 2015 14:40:57 +0200", - "Tue, 12 Jun 2012 19:22:28 +0200" + "Tue, 12 Jun 2012 19:22:28 +0200", + "Fri, 08 May 2015 00:00:00 +0000", + "Fri, 08 May 2015 00:00:00 +0000", + "Fri, 08 May 2015 12:05:00 +0000", + "Fri, 08 May 2015 12:05:01 +0000", + "Fri, 08 May 2015 12:05:01 +0400", + "Sat, 09 May 2015 00:00:00 -0400", + "Sat, 09 May 2015 00:00:00 -0400", + "Sat, 09 May 2015 00:00:00 -0400", + "Sat, 09 May 2015 00:00:00 -0400", + "Sat, 09 May 2015 00:00:00 -0400", }; [Test] public void TestDateParser () { - DateTimeOffset date; - string parsed; - byte[] text; - for (int i = 0; i < dates.Length; i++) { - text = Encoding.UTF8.GetBytes (dates[i]); + var text = Encoding.UTF8.GetBytes (dates[i]); + DateTimeOffset date; + string parsed; Assert.IsTrue (DateUtils.TryParse (text, 0, text.Length, out date), "Failed to parse date: {0}", dates[i]); parsed = DateUtils.FormatDate (date); @@ -102,13 +120,27 @@ public void TestDateParser () parsed = DateUtils.FormatDate (date); Assert.AreEqual (expected[i], parsed, "Parsed date does not match: '{0}' vs '{1}'", parsed, expected[i]); } + } - text = Encoding.ASCII.GetBytes ("this is pure junk"); + static readonly string[] invalidDates = { + "this is pure junk", + "Sunday is the day of our Lord", + "Sun is so bright, I gotta wear shades", + "Sat, 8 dogs did while 8 cats hid", + "Sat, 9 May flies bit my arms" + }; - Assert.IsFalse (DateUtils.TryParse (text, 0, text.Length, out date), "Should not have parsed junk."); - Assert.IsFalse (DateUtils.TryParse (text, 0, out date), "Should not have parsed junk."); - Assert.IsFalse (DateUtils.TryParse (text, out date), "Should not have parsed junk."); - Assert.IsFalse (DateUtils.TryParse ("this is pure junk", out date), "Should not have parsed junk."); + [Test] + public void TestParseInvalidDates () + { + for (int i = 0; i < invalidDates.Length; i++) { + var text = Encoding.UTF8.GetBytes (invalidDates[i]); + + Assert.IsFalse (DateUtils.TryParse (text, 0, text.Length, out _), "Should not have parsed '{0}'", invalidDates[i]); + Assert.IsFalse (DateUtils.TryParse (text, 0, out _), "Should not have parsed '{0}'", invalidDates[i]); + Assert.IsFalse (DateUtils.TryParse (text, out _), "Should not have parsed '{0}'", invalidDates[i]); + Assert.IsFalse (DateUtils.TryParse (invalidDates[i], out _), "Should not have parsed '{0}'", invalidDates[i]); + } } } }