Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit badc315

Browse files
committed
Improve robustness of MailBnfHelper + add tests
1 parent 4457eb8 commit badc315

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

src/System.Net.Http/src/Internal/Mail/MailBnfHelper.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ internal static class MailBnfHelper
4747
internal static readonly char EndSquareBracket = ']';
4848
internal static readonly char Comma = ',';
4949
internal static readonly char Dot = '.';
50-
internal static readonly IList<char> Whitespace = CreateAllowedWhitespace();
50+
internal static readonly List<char> Whitespace = CreateAllowedWhitespace();
5151

5252
private static List<char> CreateAllowedWhitespace()
5353
{
@@ -293,6 +293,9 @@ internal static string ReadToken(string data, ref int offset, StringBuilder buil
293293

294294
private static string[] s_months = new string[] { null, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
295295

296+
// Given a DateTime like Jul 8 2009, 6:05:04
297+
// this method will return something like
298+
// "8 Jul 2009 06:05:04 +0000" (where 00:00 is the UTC offset)
296299
internal static string GetDateTimeString(DateTime value, StringBuilder builder)
297300
{
298301
StringBuilder localBuilder = (builder != null ? builder : new StringBuilder());
@@ -331,7 +334,18 @@ internal static string GetDateTimeString(DateTime value, StringBuilder builder)
331334
}
332335

333336
int colonIndex = offset.IndexOf(':');
337+
if (colonIndex == -1)
338+
{
339+
throw new InvalidOperationException("TimeSpan.ToString() should contain at least one colon.");
340+
}
341+
334342
int nextColonIndex = offset.IndexOf(':', colonIndex + 1);
343+
if (nextColonIndex == -1)
344+
{
345+
// Set it past the end of the string
346+
nextColonIndex = offset.Length;
347+
}
348+
335349
localBuilder.Append(offset, 0, colonIndex);
336350
localBuilder.Append(offset, colonIndex + 1, nextColonIndex - (colonIndex + 1));
337351
return (builder != null ? null : localBuilder.ToString());
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Net.Mime;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
using Xunit;
12+
13+
namespace System.Net.Http.Tests
14+
{
15+
public class MailBnfHelperTest
16+
{
17+
public static IEnumerable<DateTime> GetDateTimeString_TestDates()
18+
{
19+
yield return new DateTime(2009, 1, 1, 8, 7, 39);
20+
yield return new DateTime(2010, 2, 3, 5, 10, 32);
21+
yield return new DateTime(2019, 12, 12, 20, 18, 0);
22+
23+
for (int i = 1; i < 12; i++)
24+
yield return new DateTime(2006, i, i, i, i, i);
25+
}
26+
27+
// Given a DateTime like Jul 8 2009, 6:05:04
28+
// GetDateTimeString should return something like
29+
// "8 Jul 2009 06:05:04 +0000" (where 00:00 is the UTC offset)
30+
public static IEnumerable<object[]> GetDateTimeString_TestData()
31+
{
32+
foreach (var dateTime in GetDateTimeString_TestDates())
33+
{
34+
var offset = TimeZoneInfo.Local.GetUtcOffset(dateTime);
35+
char prefix = offset >= TimeSpan.Zero ? '+' : '-';
36+
yield return new object[] { dateTime, $"{dateTime:d MMM yyyy HH:mm:ss} {prefix}{offset:hhmm}" };
37+
}
38+
}
39+
40+
[Theory]
41+
[MemberData(nameof(GetDateTimeString_TestData))]
42+
public void GetDateTimeString(DateTime input, string expected)
43+
{
44+
var builder = new StringBuilder();
45+
MailBnfHelper.GetDateTimeString(input, builder);
46+
Assert.Equal(expected, builder.ToString());
47+
}
48+
}
49+
}

src/System.Net.Http/tests/UnitTests/System.Net.Http.Unit.Tests.csproj

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
33
<PropertyGroup>
44
<Configuration Condition="'$(Configuration)'==''">Windows_Debug</Configuration>
@@ -14,7 +14,6 @@
1414
<StringResourcesPath>../../src/Resources/Strings.resx</StringResourcesPath>
1515
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1616
</PropertyGroup>
17-
1817
<!-- Help VS understand available configurations -->
1918
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'FreeBSD_Debug|AnyCPU'" />
2019
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'FreeBSD_Release|AnyCPU'" />
@@ -23,8 +22,7 @@
2322
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'OSX_Debug|AnyCPU'" />
2423
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'OSX_Release|AnyCPU'" />
2524
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Windows_Debug|AnyCPU'" />
26-
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Windows_Release|AnyCPU'" />
27-
25+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Windows_Release|AnyCPU'" />
2826
<ItemGroup>
2927
<Compile Include="$(CommonPath)\System\NotImplemented.cs">
3028
<Link>ProductionCode\Common\System\NotImplemented.cs</Link>
@@ -343,10 +341,11 @@
343341
<Compile Include="Headers\WarningHeaderValueTest.cs" />
344342
<Compile Include="HttpContentTest.cs" />
345343
<Compile Include="HttpRuleParserTest.cs" />
344+
<Compile Include="Internal\MailBnfHelperTest.cs" />
346345
<Compile Include="MockContent.cs" />
347346
</ItemGroup>
348347
<ItemGroup>
349348
<None Include="project.json" />
350349
</ItemGroup>
351350
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
352-
</Project>
351+
</Project>

0 commit comments

Comments
 (0)