Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm] Throw exception if culture data does not exist in icu #47301

Merged
merged 27 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
164db20
[wasm] Throw exception if culture data does not exist in icu
tqiu8 Jan 9, 2021
fd96d0c
add lines
tqiu8 Jan 9, 2021
68a4fa3
add lines
tqiu8 Jan 9, 2021
82759a8
remove blank spaces
tqiu8 Jan 9, 2021
d92afab
Add check for culture data in ICU
tqiu8 Jan 22, 2021
df4d910
include documentation link in exception message
tqiu8 Jan 22, 2021
7167047
Add PredefinedOnly to Windows
tqiu8 Jan 22, 2021
7c677dc
move checks outside of CultureData.cs
tqiu8 Jan 22, 2021
53151f8
Add test for predefined culture env var
tqiu8 Jan 25, 2021
42c85fb
switch test to remoteexecutor
tqiu8 Jan 25, 2021
e0f14e8
try catch for all culture not supported exceptions
tqiu8 Jan 27, 2021
bb3f356
Fix test failures: Check for RegionInfo first before creating culture…
tqiu8 Feb 8, 2021
e894382
add another condition to env var test
tqiu8 Feb 8, 2021
462f7ad
fix conditional theory
tqiu8 Feb 9, 2021
18faed1
fix TypeConverter test failures
tqiu8 Feb 10, 2021
ef75159
change assembly test data to pl-PL to avoid culturenotfound exception
tqiu8 Feb 16, 2021
8cdeb96
Merge branch 'culture-dt-fix' of github.com:tqiu8/runtime into cultur…
tqiu8 Mar 2, 2021
84b8429
remove exception filters from tests and add additional parameter to t…
tqiu8 Mar 2, 2021
f8163f7
move exception out of CreateCulturedata
tqiu8 Mar 4, 2021
451a3ad
remove else block
tqiu8 Mar 4, 2021
0bc3a5c
remove redundant methods
tqiu8 Mar 4, 2021
c40d610
revert logic in nls
tqiu8 Mar 4, 2021
eeeda5f
Update src/libraries/System.Reflection.MetadataLoadContext/tests/src/…
tqiu8 Mar 5, 2021
b956ca6
Merge branch 'master' into culture-dt-fix
tqiu8 Mar 5, 2021
3b065d2
Merge branch 'culture-dt-fix' of github.com:tqiu8/runtime into cultur…
tqiu8 Mar 5, 2021
0aab18e
fix invariant tests
tqiu8 Mar 9, 2021
23842ee
remove aggressive inlining
tqiu8 Mar 10, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/libraries/Common/tests/Tests/System/StringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2567,8 +2567,16 @@ public void Equals_Encyclopaedia_ReturnsExpected(StringComparison comparison, bo
{
string source = "encyclop\u00e6dia";
string target = "encyclopaedia";
ThreadCultureChange culture;
if (PlatformDetection.IsBrowser)
{
culture = new ThreadCultureChange("pl-PL");
} else
{
culture = new ThreadCultureChange("se-SE");
}

using (new ThreadCultureChange("se-SE"))
tqiu8 marked this conversation as resolved.
Show resolved Hide resolved
using (culture)
{
Assert.Equal(expected, string.Equals(source, target, comparison));
Assert.Equal(expected, source.AsSpan().Equals(target.AsSpan(), comparison));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Runtime.InteropServices;
using Microsoft.DotNet.RemoteExecutor;
using System.Text;
using System.Diagnostics;
using Xunit;

namespace System.Globalization.Tests
Expand Down Expand Up @@ -809,5 +810,31 @@ public void CultureNotFoundExceptionTest()
e = AssertExtensions.Throws<CultureNotFoundException>("culture", () => new CultureInfo(0x1000));
Assert.Equal(0x1000, e.InvalidCultureId);
}

[ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[InlineData("1", "xx-XY")]
[InlineData("1", "zx-ZY")]
[InlineData("0", "xx-XY")]
[InlineData("0", "zx-ZY")]
public void PredefinedCulturesOnlyEnvVarTest(string predefinedCulturesOnlyEnvVar, string cultureName)
{
var psi = new ProcessStartInfo();
psi.Environment.Clear();

psi.Environment.Add("DOTNET_SYSTEM_GLOBALIZATION_PREDEFINED_CULTURES_ONLY", predefinedCulturesOnlyEnvVar);

RemoteExecutor.Invoke((culture, predefined) =>
{
if (predefined == "1")
{
AssertExtensions.Throws<CultureNotFoundException>(() => new CultureInfo(culture));
}
else
{
CultureInfo ci = new CultureInfo(culture);
Assert.Equal(culture, ci.Name);
}
}, cultureName, predefinedCulturesOnlyEnvVar, new RemoteInvokeOptions { StartInfo = psi }).Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,32 @@ public class CurrentCultureTests
[Fact]
public void CurrentCulture()
{
var newCulture = new CultureInfo(CultureInfo.CurrentCulture.Name.Equals("ja-JP", StringComparison.OrdinalIgnoreCase) ? "ar-SA" : "ja-JP");
using (new ThreadCultureChange(newCulture))
CultureInfo newCulture;
tqiu8 marked this conversation as resolved.
Show resolved Hide resolved
try
{
Assert.Equal(CultureInfo.CurrentCulture, newCulture);
newCulture = new CultureInfo(CultureInfo.CurrentCulture.Name.Equals("ja-JP", StringComparison.OrdinalIgnoreCase) ? "ar-SA" : "ja-JP");
using (new ThreadCultureChange(newCulture))
{
Assert.Equal(CultureInfo.CurrentCulture, newCulture);
}
}
catch (CultureNotFoundException)
{
return;
}

newCulture = new CultureInfo("de-DE_phoneb");
using (new ThreadCultureChange(newCulture))
try
{
newCulture = new CultureInfo("de-DE_phoneb");
using (new ThreadCultureChange(newCulture))
{
Assert.Equal(CultureInfo.CurrentCulture, newCulture);
Assert.Equal("de-DE_phoneb", newCulture.CompareInfo.Name);
}
}
catch (CultureNotFoundException)
{
Assert.Equal(CultureInfo.CurrentCulture, newCulture);
Assert.Equal("de-DE_phoneb", newCulture.CompareInfo.Name);
return;
}
}

Expand All @@ -39,17 +54,32 @@ public void CurrentCulture_Set_Null_ThrowsArgumentNullException()
[Fact]
public void CurrentUICulture()
{
var newUICulture = new CultureInfo(CultureInfo.CurrentUICulture.Name.Equals("ja-JP", StringComparison.OrdinalIgnoreCase) ? "ar-SA" : "ja-JP");
tqiu8 marked this conversation as resolved.
Show resolved Hide resolved
using (new ThreadCultureChange(null, newUICulture))
CultureInfo newUICulture;
try
{
Assert.Equal(CultureInfo.CurrentUICulture, newUICulture);
newUICulture = new CultureInfo(CultureInfo.CurrentUICulture.Name.Equals("ja-JP", StringComparison.OrdinalIgnoreCase) ? "ar-SA" : "ja-JP");
using (new ThreadCultureChange(null, newUICulture))
{
Assert.Equal(CultureInfo.CurrentUICulture, newUICulture);
}
}
catch (CultureNotFoundException)
{
return;
}

newUICulture = new CultureInfo("de-DE_phoneb");
using (new ThreadCultureChange(null, newUICulture))
try
{
newUICulture = new CultureInfo("de-DE_phoneb");
using (new ThreadCultureChange(null, newUICulture))
{
Assert.Equal(CultureInfo.CurrentUICulture, newUICulture);
Assert.Equal("de-DE_phoneb", newUICulture.CompareInfo.Name);
}
}
catch (CultureNotFoundException)
{
Assert.Equal(CultureInfo.CurrentUICulture, newUICulture);
Assert.Equal("de-DE_phoneb", newUICulture.CompareInfo.Name);
return;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,13 @@ private static void TestToLower(string name, string str, string expected)
[MemberData(nameof(ToLower_TestData))]
public void ToLower(string name, string str, string expected)
{
TestToLower(name, str, expected);
try{
tqiu8 marked this conversation as resolved.
Show resolved Hide resolved
TestToLower(name, str, expected);
}
catch (CultureNotFoundException)
{
return;
}
}

[Theory]
Expand Down Expand Up @@ -430,7 +436,14 @@ private static void TestToUpper(string name, string str, string expected)
[MemberData(nameof(ToUpper_TestData))]
public void ToUpper(string name, string str, string expected)
{
TestToUpper(name, str, expected);
try
tqiu8 marked this conversation as resolved.
Show resolved Hide resolved
{
TestToUpper(name, str, expected);
}
catch (CultureNotFoundException)
{
return;
}
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace System.Globalization
{
Expand Down Expand Up @@ -63,11 +64,8 @@ private bool InitIcuCultureDataCore()
{
_iLanguage = CultureInfo.LOCALE_CUSTOM_UNSPECIFIED;
}

_bNeutral = TwoLetterISOCountryName.Length == 0;

_sSpecificCulture = _bNeutral ? IcuLocaleData.GetSpecificCultureName(_sRealName) : _sRealName;

// Remove the sort from sName unless custom culture
if (index > 0 && !_bNeutral && !IsCustomCultureId(_iLanguage))
{
Expand Down Expand Up @@ -110,7 +108,6 @@ private string IcuGetLocaleInfo(LocaleStringData type)
{
Debug.Assert(!GlobalizationMode.Invariant);
Debug.Assert(!GlobalizationMode.UseNls);

Debug.Assert(_sWindowsName != null, "[CultureData.IcuGetLocaleInfo] Expected _sWindowsName to be populated already");
return IcuGetLocaleInfo(_sWindowsName, type);
}
Expand Down Expand Up @@ -138,7 +135,6 @@ private unsafe string IcuGetLocaleInfo(string localeName, LocaleStringData type)
Debug.Fail("[CultureData.IcuGetLocaleInfo(LocaleStringData)] Failed");
return string.Empty;
}

return new string(buffer);
}

Expand Down Expand Up @@ -226,6 +222,18 @@ private static string IcuGetLanguageDisplayName(string cultureName)
return null;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
tqiu8 marked this conversation as resolved.
Show resolved Hide resolved
internal static void IcuIsEnsurePredefinedLocaleName(string name)
{
Debug.Assert(!GlobalizationMode.UseNls);

if (!Interop.Globalization.IsPredefinedLocale(name))
{
throw new CultureNotFoundException(nameof(name), SR.Format(SR.Argument_InvalidPredefinedCultureName, name),
message: "View https://docs.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-5.0#blazor-webassembly to load a new culture");
tqiu8 marked this conversation as resolved.
Show resolved Hide resolved
}
}

private static string ConvertIcuTimeFormatString(ReadOnlySpan<char> icuFormatString)
{
Debug.Assert(icuFormatString.Length < ICU_ULOC_FULLNAME_CAPACITY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Runtime.CompilerServices;
using Internal.Runtime.CompilerServices;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -85,6 +86,17 @@ private int[] NlsGetLocaleInfo(LocaleGroupingData type)
return ConvertWin32GroupString(GetLocaleInfoFromLCType(_sWindowsName, (uint)type, _bUseOverrides));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
tqiu8 marked this conversation as resolved.
Show resolved Hide resolved
internal static void NlsIsEnsurePredefinedLocaleName(string name)
{
Debug.Assert(GlobalizationMode.UseNls);

if (CultureData.GetLocaleInfoExInt(name, Interop.Kernel32.LOCALE_ICONSTRUCTEDLOCALE) == 1)
{
throw new CultureNotFoundException(nameof(name), SR.Format(SR.Argument_InvalidPredefinedCultureName, name));
}
}

private string? NlsGetTimeFormatString()
{
Debug.Assert(ShouldUseUserOverrideNlsData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,18 @@ private static string NormalizeCultureName(string name, out bool isNeutralName)
return CultureData.Invariant;
}

if (GlobalizationMode.PredefinedCulturesOnly)
{
if (GlobalizationMode.UseNls)
{
NlsIsEnsurePredefinedLocaleName(cultureName);
}
else
{
IcuIsEnsurePredefinedLocaleName(cultureName);
}
}

CultureData culture = new CultureData();
culture._sRealName = cultureName;
culture._bUseOverridesUserSetting = useUserOverride;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ private static CultureInfo IcuGetPredefinedCultureInfo(string name)
{
Debug.Assert(!GlobalizationMode.UseNls);

if (!Interop.Globalization.IsPredefinedLocale(name))
{
throw new CultureNotFoundException(nameof(name), SR.Format(SR.Argument_InvalidPredefinedCultureName, name));
}
CultureData.IcuIsEnsurePredefinedLocaleName(name);

return GetCultureInfo(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ private static CultureInfo NlsGetPredefinedCultureInfo(string name)
{
Debug.Assert(GlobalizationMode.UseNls);

if (CultureData.GetLocaleInfoExInt(name, Interop.Kernel32.LOCALE_ICONSTRUCTEDLOCALE) == 1)
{
throw new CultureNotFoundException(nameof(name), SR.Format(SR.Argument_InvalidPredefinedCultureName, name));
}
CultureData.NlsIsEnsurePredefinedLocaleName(name);

return GetCultureInfo(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ private static bool GetInvariantSwitchValue() =>
private static bool TryGetAppLocalIcuSwitchValue([NotNullWhen(true)] out string? value) =>
TryGetStringValue("System.Globalization.AppLocalIcu", "DOTNET_SYSTEM_GLOBALIZATION_APPLOCALICU", out value);

internal static bool PredefinedCulturesOnly { get; } =
GetSwitchValue("System.Globalization.PredefinedCulturesOnly", "DOTNET_SYSTEM_GLOBALIZATION_PREDEFINED_CULTURES_ONLY");

private static bool GetSwitchValue(string switchName, string envVariable)
{
if (!AppContext.TryGetSwitch(switchName, out bool ret))
Expand Down
22 changes: 19 additions & 3 deletions src/libraries/System.Runtime/tests/System/DateTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1551,7 +1551,15 @@ public static void TryParseExact_EmptyAMPMDesignator()
[Fact]
public static void ParseExact_EscapedSingleQuotes()
{
var formatInfo = DateTimeFormatInfo.GetInstance(new CultureInfo("mt-MT"));
DateTimeFormatInfo formatInfo;
if (PlatformDetection.IsBrowser)
{
formatInfo = DateTimeFormatInfo.GetInstance(new CultureInfo("id-ID"));
}
else
{
formatInfo = DateTimeFormatInfo.GetInstance(new CultureInfo("mt-MT"));
}
const string format = @"dddd, d' ta\' 'MMMM yyyy";

DateTime expected = new DateTime(1999, 2, 28, 17, 00, 01);
Expand Down Expand Up @@ -1735,8 +1743,16 @@ public static IEnumerable<object[]> Parse_ValidInput_Succeeds_MemberData()
hebrewCulture.DateTimeFormat.Calendar = new HebrewCalendar();
yield return new object[] { today.ToString(hebrewCulture), hebrewCulture, today };

var mongolianCulture = new CultureInfo("mn-MN");
yield return new object[] { today.ToString(mongolianCulture), mongolianCulture, today };
CultureInfo culture;
if (PlatformDetection.IsBrowser)
{
culture = new CultureInfo("pl-PL");
}
else
{
culture = new CultureInfo("mn-MN");
}
yield return new object[] { today.ToString(culture), culture, today };
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/mono/wasm/runtime/library_mono.js
Original file line number Diff line number Diff line change
Expand Up @@ -1904,6 +1904,9 @@ var MonoSupportLib = {

if (invariantMode)
this.mono_wasm_setenv ("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT", "1");

// Set globalization mode to PredefinedCulturesOnly
this.mono_wasm_setenv ("DOTNET_SYSTEM_GLOBALIZATION_PREDEFINED_CULTURES_ONLY", "1");
},

// Used by the debugger to enumerate loaded dlls and pdbs
Expand Down