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 all 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
3 changes: 1 addition & 2 deletions src/libraries/Common/tests/Tests/System/StringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2567,8 +2567,7 @@ public void Equals_Encyclopaedia_ReturnsExpected(StringComparison comparison, bo
{
string source = "encyclop\u00e6dia";
string target = "encyclopaedia";

using (new ThreadCultureChange("se-SE"))
tqiu8 marked this conversation as resolved.
Show resolved Hide resolved
using (new ThreadCultureChange(PlatformDetection.IsBrowser ?"pl-PL" : "se-SE"))
{
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 @@ -33,8 +33,10 @@ public override IEnumerable<ConvertTest> ConvertFromTestData()
yield return ConvertTest.Valid("nl-B", new CultureInfo("nl--B"), CultureInfo.InvariantCulture);
yield return ConvertTest.Valid("nl-B", new CultureInfo("nl--B"), new CultureInfo("en-US"));
}

yield return ConvertTest.Valid("Afrikaans", new CultureInfo("af"));
if (PlatformDetection.IsNotBrowser)
{
yield return ConvertTest.Valid("Afrikaans", new CultureInfo("af"));
}

yield return ConvertTest.CantConvertFrom(CultureInfo.CurrentCulture);
yield return ConvertTest.CantConvertFrom(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,11 @@ public static IEnumerable<object[]> Compare_TestData()
yield return new object[] { s_hungarianCompare, "dzsdzs", "ddzs", CompareOptions.None, useNls ? 0 : -1 };
yield return new object[] { s_invariantCompare, "Test's", "Tests", CompareOptions.None, useNls ? 1 : -1 };
yield return new object[] { new CultureInfo("de-DE").CompareInfo, "\u00DC", "UE", CompareOptions.None, -1 };
yield return new object[] { new CultureInfo("de-DE_phoneb").CompareInfo, "\u00DC", "UE", CompareOptions.None, useNls ? 0 : -1 };
yield return new object[] { new CultureInfo("es-ES_tradnl").CompareInfo, "llegar", "lugar", CompareOptions.None, useNls ? 1 : -1 };
if (PlatformDetection.IsNotBrowser)
{
yield return new object[] { new CultureInfo("de-DE_phoneb").CompareInfo, "\u00DC", "UE", CompareOptions.None, useNls ? 0 : -1 };
yield return new object[] { new CultureInfo("es-ES_tradnl").CompareInfo, "llegar", "lugar", CompareOptions.None, useNls ? 1 : -1 };
}
}

// There is a regression in Windows 190xx version with the Kana comparison. Avoid running this test there.
Expand Down
197 changes: 106 additions & 91 deletions src/libraries/System.Globalization/tests/CultureInfo/CultureInfoAll.cs

Large diffs are not rendered by default.

483 changes: 245 additions & 238 deletions src/libraries/System.Globalization/tests/CultureInfo/CultureInfoCtor.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ public void CurrentCulture()
Assert.Equal(CultureInfo.CurrentCulture, newCulture);
}

newCulture = new CultureInfo("de-DE_phoneb");
using (new ThreadCultureChange(newCulture))
if (PlatformDetection.IsNotBrowser)
{
Assert.Equal(CultureInfo.CurrentCulture, newCulture);
Assert.Equal("de-DE_phoneb", newCulture.CompareInfo.Name);
newCulture = new CultureInfo("de-DE_phoneb");
using (new ThreadCultureChange(newCulture))
{
Assert.Equal(CultureInfo.CurrentCulture, newCulture);
Assert.Equal("de-DE_phoneb", newCulture.CompareInfo.Name);
}
}
}

Expand All @@ -45,11 +48,14 @@ public void CurrentUICulture()
Assert.Equal(CultureInfo.CurrentUICulture, newUICulture);
}

newUICulture = new CultureInfo("de-DE_phoneb");
using (new ThreadCultureChange(null, newUICulture))
if (PlatformDetection.IsNotBrowser)
{
Assert.Equal(CultureInfo.CurrentUICulture, newUICulture);
Assert.Equal("de-DE_phoneb", newUICulture.CompareInfo.Name);
newUICulture = new CultureInfo("de-DE_phoneb");
using (new ThreadCultureChange(null, newUICulture))
{
Assert.Equal(CultureInfo.CurrentUICulture, newUICulture);
Assert.Equal("de-DE_phoneb", newUICulture.CompareInfo.Name);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,20 @@ public void Parent(string name, string expectedParentName)
[Fact]
public void Parent_ParentChain()
{
CultureInfo myExpectParentCulture = new CultureInfo("uz-Cyrl-UZ");
Assert.Equal("uz-Cyrl", myExpectParentCulture.Parent.Name);
Assert.Equal("uz", myExpectParentCulture.Parent.Parent.Name);
Assert.Equal("", myExpectParentCulture.Parent.Parent.Parent.Name);
if (PlatformDetection.IsNotBrowser)
{
CultureInfo myExpectParentCulture = new CultureInfo("uz-Cyrl-UZ");
Assert.Equal("uz-Cyrl", myExpectParentCulture.Parent.Name);
Assert.Equal("uz", myExpectParentCulture.Parent.Parent.Name);
Assert.Equal("", myExpectParentCulture.Parent.Parent.Parent.Name);
}
else
{
CultureInfo myExpectParentCulture = new CultureInfo("zh-Hans-CN");
Assert.Equal("zh-Hans", myExpectParentCulture.Parent.Name);
Assert.Equal("zh", myExpectParentCulture.Parent.Parent.Name);
Assert.Equal("", myExpectParentCulture.Parent.Parent.Parent.Name);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using Microsoft.DotNet.RemoteExecutor;
using System.Diagnostics;
using Xunit;

namespace System.Globalization.Tests
{
public class GetCultureInfoTests
{
public static bool PlatformSupportsFakeCulture => !PlatformDetection.IsWindows || (PlatformDetection.WindowsVersion >= 10 && !PlatformDetection.IsNetFramework);
public static bool PlatformSupportsFakeCulture => (!PlatformDetection.IsWindows || (PlatformDetection.WindowsVersion >= 10 && !PlatformDetection.IsNetFramework)) && PlatformDetection.IsNotBrowser;
public static bool PlatformSupportsFakeCultureAndRemoteExecutor => PlatformSupportsFakeCulture && RemoteExecutor.IsSupported;

public static IEnumerable<object[]> GetCultureInfoTestData()
{
Expand Down Expand Up @@ -110,5 +113,31 @@ public void TestFakeCultureNames(string name)
Assert.Equal(name, CultureInfo.GetCultureInfo(name, predefinedOnly: false).Name);
Assert.Throws<CultureNotFoundException>(() => CultureInfo.GetCultureInfo(name, predefinedOnly: true));
}

[ConditionalTheory(nameof(PlatformSupportsFakeCultureAndRemoteExecutor))]
[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 @@ -288,7 +288,7 @@ public static IEnumerable<object[]> ToLower_TestData()
// ICU has special tailoring for the en-US-POSIX locale which treats "i" and "I" as different letters
// instead of two letters with a case difference during collation. Make sure this doesn't confuse our
// casing implementation, which uses collation to understand if we need to do Turkish casing or not.
if (!PlatformDetection.IsWindows)
if (!PlatformDetection.IsWindows && PlatformDetection.IsNotBrowser)
{
yield return new object[] { "en-US-POSIX", "I", "i" };
}
Expand Down Expand Up @@ -411,7 +411,7 @@ public static IEnumerable<object[]> ToUpper_TestData()
// ICU has special tailoring for the en-US-POSIX locale which treats "i" and "I" as different letters
// instead of two letters with a case difference during collation. Make sure this doesn't confuse our
// casing implementation, which uses collation to understand if we need to do Turkish casing or not.
if (!PlatformDetection.IsWindows)
if (!PlatformDetection.IsWindows && PlatformDetection.IsNotBrowser)
{
yield return new object[] { "en-US-POSIX", "i", "I" };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,6 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CultureData.Icu.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CultureData.Nls.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CultureInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CultureInfo.Icu.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CultureInfo.Nls.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CultureNotFoundException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\CultureTypes.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Globalization\DateTimeFormat.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,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 +107,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 +134,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 +221,12 @@ private static string IcuGetLanguageDisplayName(string cultureName)
return null;
}

internal static bool IcuIsEnsurePredefinedLocaleName(string name)
{
Debug.Assert(!GlobalizationMode.UseNls);
return Interop.Globalization.IsPredefinedLocale(name);
}

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 @@ -85,6 +85,12 @@ private int[] NlsGetLocaleInfo(LocaleGroupingData type)
return ConvertWin32GroupString(GetLocaleInfoFromLCType(_sWindowsName, (uint)type, _bUseOverrides));
}

internal static bool NlsIsEnsurePredefinedLocaleName(string name)
{
Debug.Assert(GlobalizationMode.UseNls);
return CultureData.GetLocaleInfoExInt(name, Interop.Kernel32.LOCALE_ICONSTRUCTEDLOCALE) != 1;
}

private string? NlsGetTimeFormatString()
{
Debug.Assert(ShouldUseUserOverrideNlsData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,17 +422,14 @@ internal partial class CultureData
{
return CultureData.Invariant;
}

CultureData? retVal = null;
// First check if GetCultureData() can find it (ie: its a real culture)
CultureData? retVal = GetCultureData(cultureName, useUserOverride);
retVal = GetCultureData(cultureName, useUserOverride);
if (retVal != null && !retVal.IsNeutralCulture)
{
return retVal;
}

// Not a specific culture, perhaps it's region-only name
// (Remember this isn't a core clr path where that's not supported)

// If it was neutral remember that so that RegionInfo() can throw the right exception
CultureData? neutral = retVal;

Expand Down Expand Up @@ -676,6 +673,12 @@ private static CultureData CreateCultureWithInvariantData()
return CultureData.Invariant;
}

if (GlobalizationMode.PredefinedCulturesOnly && !GlobalizationMode.Invariant)
{
if (GlobalizationMode.UseNls ? !NlsIsEnsurePredefinedLocaleName(cultureName): !IcuIsEnsurePredefinedLocaleName(cultureName))
return null;
}

// Try the hash table first
string hashName = AnsiToLower(useUserOverride ? cultureName : cultureName + '*');
Dictionary<string, CultureData>? tempHashTable = s_cachedCultures;
Expand All @@ -698,7 +701,6 @@ private static CultureData CreateCultureWithInvariantData()
return retVal;
}
}

// Not found in the hash table, need to see if we can build one that works for us
CultureData? culture = CreateCultureData(cultureName, useUserOverride);
if (culture == null)
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -1169,9 +1169,10 @@ public static CultureInfo GetCultureInfo(string name, bool predefinedOnly)

if (predefinedOnly && !GlobalizationMode.Invariant)
{
return GlobalizationMode.UseNls ?
NlsGetPredefinedCultureInfo(name) :
IcuGetPredefinedCultureInfo(name);
if (GlobalizationMode.UseNls ? !CultureData.NlsIsEnsurePredefinedLocaleName(name): !CultureData.IcuIsEnsurePredefinedLocaleName(name))
tqiu8 marked this conversation as resolved.
Show resolved Hide resolved
{
throw new CultureNotFoundException(nameof(name), name, SR.Format(SR.Argument_InvalidPredefinedCultureName, 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
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ internal static class TestData
"ABYAAAEAAAACAAAAAQAAAAIAAAACAAAAAQAAAAMAAAAAAG4AAQAAAAAABgAqAAoABgBQAAoAAAAAAAEAAAAAAAEAAQAJAEoAAQARAEoABgAuAAsAEwAuABMA" +
"HAAEgAAAAAAAAAAAAAAAAAAAAAChAAAABAAAAAAAAAAAAAAACgCIAAAAAAAAAAAAAAAAAAAAAAAAAJEAAAAAAAEAAgADAAQAAAAAAAAAlgCbAAAAAAAAPE1v" +
"ZHVsZT4AU3lzdGVtLlJ1bnRpbWUuQ29tcGlsZXJTZXJ2aWNlcwBDb21waWxhdGlvblJlbGF4YXRpb25zQXR0cmlidXRlAC5jdG9yAFJ1bnRpbWVDb21wYXRp" +
"YmlsaXR5QXR0cmlidXRlAEFzc2VtYmx5UmVmZXJlbmNlVGVzdC5kbGwAbXNjb3JsaWIAZGVwMQBkZXAyAGFyLUxZAEFzc2VtYmx5UmVmZXJlbmNlVGVzdAAA" +
"YmlsaXR5QXR0cmlidXRlAEFzc2VtYmx5UmVmZXJlbmNlVGVzdC5kbGwAbXNjb3JsaWIAZGVwMQBkZXAyAHBsLVBMAEFzc2VtYmx5UmVmZXJlbmNlVGVzdAAA" +
"AAMgAAAAAAD+latRaKErR6lFRbe04zMTAAQgAQEIAyAAAQi3elxWGTTgiQgBAAgAAAAAAB4BAAEAVAIWV3JhcE5vbkV4Y2VwdGlvblRocm93cwEAtCIAAAAA" +
"AAAAAAAAziIAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAiAAAAAAAAAAAAAAAAX0NvckRsbE1haW4AbXNjb3JlZS5kbGwAAAAAAP8lACAAEAAAAAAAAAAA" +
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ public static void AssemblyGetReferencedAssemblies()
Assembly a = lc.LoadFromByteArray(TestData.s_AssemblyReferencesTestImage);
AssemblyName[] ans = a.GetReferencedAssemblies();
Assert.Equal(3, ans.Length);

{
AssemblyName an = ans.Single(an2 => an2.Name == "mscorlib");
Assert.Equal(default(AssemblyNameFlags), an.Flags);
Expand Down Expand Up @@ -211,7 +210,7 @@ public static void AssemblyGetReferencedAssemblies()
Assert.Equal(2, v.Minor);
Assert.Equal(3, v.Build);
Assert.Equal(4, v.Revision);
Assert.Equal("ar-LY", an.CultureName);
Assert.Equal("pl-PL", an.CultureName);
Assert.Null(an.GetPublicKey());
Assert.Equal(0, an.GetPublicKeyToken().Length);
}
Expand Down
Loading