Skip to content

Commit

Permalink
Add testing for #92539.
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriktsarpalis committed Oct 3, 2023
1 parent 2fc3196 commit c708fb5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Threading;
using Microsoft.DotNet.RemoteExecutor;
using Test.Cryptography;
using Xunit;

Expand Down Expand Up @@ -192,6 +195,31 @@ public static void ReadDateTimeOffset_SingleValue_HappyPath(string expectedValue
Assert.Equal(expectedValue.Offset, result.Offset);
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public static void ReadDateTimeOffset_IsCultureInvariant()
{
// Regression test for https://github.com/dotnet/runtime/pull/92539
RemoteExecutor.Invoke(static () =>
{
DateTimeOffset expectedValue = DateTimeOffset.Parse("2020-04-09T14:31:21.3535941+01:00", CultureInfo.InvariantCulture);
byte[] data = "c07821323032302d30342d30395431343a33313a32312e333533353934312b30313a3030".HexToByteArray();
// Install a non-Gregorian calendar
var culture = new CultureInfo("he-IL");
culture.DateTimeFormat.Calendar = new HebrewCalendar();
Thread.CurrentThread.CurrentCulture = culture;
var reader = new CborReader(data);
DateTimeOffset result = reader.ReadDateTimeOffset();
Assert.Equal(CborReaderState.Finished, reader.PeekState());
Assert.Equal(expectedValue, result);
Assert.Equal(expectedValue.Offset, result.Offset);
}).Dispose();
}

[Theory]
[InlineData("c01a514b67b0")] // string datetime tag with unix time payload
public static void ReadDateTimeOffset_InvalidTagPayload_ShouldThrowCborContentException(string hexEncoding)
Expand All @@ -206,6 +234,7 @@ public static void ReadDateTimeOffset_InvalidTagPayload_ShouldThrowCborContentEx
[Theory]
[InlineData("c07330392f30342f323032302031393a35313a3530")] // 0("09/04/2020 19:51:50")
[InlineData("c06e4c617374204368726973746d6173")] // 0("Last Christmas")
[InlineData("c07828d7aad7a922d7a42dd796272dd79822d7955431343a33313a32312e333533353934312b30313a3030")] // Non-Gregorian calendar date.
public static void ReadDateTimeOffset_InvalidDateString_ShouldThrowCborContentException(string hexEncoding)
{
byte[] encoding = hexEncoding.HexToByteArray();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks>
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
<nullable>enable</nullable>
<!-- Referenced assembly 'FsCheck' does not have a strong name.-->
<NoWarn>$(NoWarn);CS8002</NoWarn>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Threading;
using Microsoft.DotNet.RemoteExecutor;
using Test.Cryptography;
using Xunit;

Expand Down Expand Up @@ -88,6 +91,30 @@ public static void WriteDateTimeOffset_SingleValue_HappyPath(string valueString,
AssertHelper.HexEqual(expectedHexEncoding.HexToByteArray(), encoding);
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public static void WriteDateTimeOffset_IsCultureInvariant()
{
// Regression test for https://github.com/dotnet/runtime/pull/92539
RemoteExecutor.Invoke(static () =>
{
DateTimeOffset value = DateTimeOffset.Parse("2020-04-09T14:31:21.3535941+01:00", CultureInfo.InvariantCulture);
string expectedHexEncoding = "c07821323032302d30342d30395431343a33313a32312e333533353934312b30313a3030";
// Install a non-Gregorian calendar
var culture = new CultureInfo("he-IL");
culture.DateTimeFormat.Calendar = new HebrewCalendar();
Thread.CurrentThread.CurrentCulture = culture;
var writer = new CborWriter();
writer.WriteDateTimeOffset(value);
byte[] encoding = writer.Encode();
AssertHelper.HexEqual(expectedHexEncoding.HexToByteArray(), encoding);
}).Dispose();
}

[Theory]
[InlineData(1363896240, "c11a514b67b0")]
[InlineData(1586439081, "c11a5e8f23a9")]
Expand Down

0 comments on commit c708fb5

Please sign in to comment.