Skip to content

Commit

Permalink
add switch and Compat tests that simply reference existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsitnik committed May 20, 2024
1 parent 37200dc commit 6a36687
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "tools\ref", "{DB5983
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{7FF9B3DF-E383-4487-9ADB-E3BF59CFCE83}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Resources.Extensions.Compat.Tests", "tests\CompatTests\System.Resources.Extensions.Compat.Tests.csproj", "{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -503,6 +505,36 @@ Global
{7E29F73C-D2DE-4DA4-B399-F6A2475A11EC}.Checked|arm64.ActiveCfg = Debug|Any CPU
{7E29F73C-D2DE-4DA4-B399-F6A2475A11EC}.Checked|x64.ActiveCfg = Debug|Any CPU
{7E29F73C-D2DE-4DA4-B399-F6A2475A11EC}.Checked|x86.ActiveCfg = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Checked|Any CPU.ActiveCfg = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Checked|Any CPU.Build.0 = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Checked|arm.ActiveCfg = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Checked|arm.Build.0 = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Checked|arm64.ActiveCfg = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Checked|arm64.Build.0 = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Checked|x64.ActiveCfg = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Checked|x64.Build.0 = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Checked|x86.ActiveCfg = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Checked|x86.Build.0 = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Debug|arm.ActiveCfg = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Debug|arm.Build.0 = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Debug|arm64.ActiveCfg = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Debug|arm64.Build.0 = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Debug|x64.ActiveCfg = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Debug|x64.Build.0 = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Debug|x86.ActiveCfg = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Debug|x86.Build.0 = Debug|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Release|Any CPU.Build.0 = Release|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Release|arm.ActiveCfg = Release|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Release|arm.Build.0 = Release|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Release|arm64.ActiveCfg = Release|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Release|arm64.Build.0 = Release|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Release|x64.ActiveCfg = Release|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Release|x64.Build.0 = Release|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Release|x86.ActiveCfg = Release|Any CPU
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -531,6 +563,7 @@ Global
{BE197124-901B-4517-879B-93D0E6684A2C} = {7FF9B3DF-E383-4487-9ADB-E3BF59CFCE83}
{7E29F73C-D2DE-4DA4-B399-F6A2475A11EC} = {DB598308-6179-48EA-A670-0DA6CE5D8340}
{DB598308-6179-48EA-A670-0DA6CE5D8340} = {7FF9B3DF-E383-4487-9ADB-E3BF59CFCE83}
{BD76A85A-8E6F-4D4C-A628-661EFE6999F1} = {2195CD2B-EF9E-46A7-B4AA-A2CD31625957}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E201426B-999F-48A5-BCFD-3E757AA0E182}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace System.Resources.Extensions
{
public partial class DeserializingResourceReader
{
private static readonly bool UseBinaryFormatter = GetUseBinaryFormatterSetting();

private bool _assumeBinaryFormatter;

private bool ValidateReaderType(string readerType)
Expand All @@ -33,31 +35,40 @@ private bool ValidateReaderType(string readerType)
return false;
}

// Issue https://github.com/dotnet/runtime/issues/39292 tracks finding an alternative to BinaryFormatter
#pragma warning disable SYSLIB0011
private object ReadBinaryFormattedObject()
{
if (UseBinaryFormatter)
{
return DeserializeUsingBinaryFormatter(_store.BaseStream);
}

long position = _store.BaseStream.CanSeek ? _store.BaseStream.Position : -1;

try
{
BinaryFormattedObject binaryFormattedObject = new BinaryFormattedObject(_store.BaseStream);
BinaryFormattedObject binaryFormattedObject = new(_store.BaseStream);

return binaryFormattedObject.Deserialize();
}
catch (NotSupportedException) when (position >= 0)
{
_store.BaseStream.Position = position;

return DeserializeUsingBinaryFormatter(_store.BaseStream);
}

static object DeserializeUsingBinaryFormatter(Stream stream)
{
#pragma warning disable SYSLIB0011
BinaryFormatter? formatter = new()
{
Binder = new UndoTruncatedTypeNameSerializationBinder()
};

return formatter.Deserialize(_store.BaseStream);
return formatter.Deserialize(stream);
#pragma warning restore SYSLIB0011
}
}
#pragma warning restore SYSLIB0011

internal sealed class UndoTruncatedTypeNameSerializationBinder : SerializationBinder
{
Expand Down Expand Up @@ -237,5 +248,17 @@ private object DeserializeObject(int typeIndex)
return value;
}

private static bool GetUseBinaryFormatterSetting()
{
if (AppContext.TryGetSwitch("System.Resources.Extensions.UseBinaryFormatter", out bool fileConfig))
{
return fileConfig;
}

string? envVar = Environment.GetEnvironmentVariable("DOTNET_SYSTEM_RESOURCES_EXTENSIONS_USE_BINARY_FORMATTER");
return envVar is null
? false // disabled by default for security reasons!
: envVar.Equals("true", StringComparison.OrdinalIgnoreCase) || envVar.Equals("1");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Reflection;
using Xunit;

namespace System.Resources.Extensions.Compat.Tests
{
public class CompatSwitchTests
{
[Fact]
public void TheFlagIsSet()
{
FieldInfo fieldInfo = typeof(DeserializingResourceReader).GetField("UseBinaryFormatter", BindingFlags.NonPublic | BindingFlags.Static);

Assert.True((bool)fieldInfo.GetValue(null));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-unix;</TargetFrameworks>
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
<CustomResourceTypesSupport Condition="'$(EnableAggressiveTrimming)' == 'true' and '$(UseNativeAotRuntime)' != 'true'">true</CustomResourceTypesSupport>
<!-- some of the embeded resources have this assembly name hardcoded -->
<AssemblyName>System.Resources.Extensions.Tests</AssemblyName>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\**\*.cs" />
<Compile Include="$(CoreLibSharedDir)System\Numerics\Hashing\HashHelpers.cs"
Link="System\Numerics\Hashing\HashHelpers.cs" />
<Compile Include="..\..\src\System\Resources\Extensions\TypeNameComparer.cs"
Link="TypeNameComparer.cs" />
<EmbeddedResource Include="..\TestData.resources" WithCulture="false" Type="Non-Resx" LogicalName="System.Resources.Extensions.Tests.%(FileName)%(Extension)" />
<None Include="runtimeconfig.template.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Drawing.Common.TestData" Version="$(SystemDrawingCommonTestDataVersion)" />
<ProjectReference Include="..\..\src\System.Resources.Extensions.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
<PackageReference Include="System.Drawing.Common" Version="$(SystemDrawingCommonVersion)" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Windows.Extensions\src\System.Windows.Extensions.csproj" Condition="'$(TargetPlatformIdentifier)' == 'windows'" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<PackageReference Include="System.ValueTuple" Version="$(SystemValueTupleVersion)" />
</ItemGroup>
<ItemGroup>
<!-- Apple mobile trimming descriptor for Mono runtime -->
<TrimmerRootDescriptor Condition="'$(TargetsAppleMobile)' == 'true' and '$(EnableAggressiveTrimming)' == 'true' and '$(UseNativeAotRuntime)' != 'true'" Include="$(MSBuildThisFileDirectory)ILLink.Descriptors.xml" />
</ItemGroup>
<!-- Use the following target to regenerate the test resources file.
This is done from a test application and checked in so that we don't run
product code during the build.
Important:
When invoking this target, pass the target framework in as well, substituting
the X with the current major version:
dotnet build -f netX.0-windows && cd outputdir && dotnet build -f netX.0-windows /t:GenerateTestResourcesFile. -->
<Target Name="GenerateTestResourcesFile">
<PropertyGroup>
<_executor>Microsoft.DotNet.RemoteExecutor.dll</_executor>
</PropertyGroup>
<ItemGroup>
<ExecuteMethod Class="System.Resources.Extensions.Tests.TestData" Include="WriteResources" Parameters="$(MSBuildProjectDirectory)\..\TestData.resources" />
<ExecuteMethod>
<ExceptionFile>$(TargetDir)%(Class)%(Identity).exception.txt</ExceptionFile>
</ExecuteMethod>
<ExecuteMethod>
<Command>$(NetCoreAppCurrentTestHostPath)dotnet exec --runtimeconfig $(TargetDir)$(TargetName).runtimeconfig.json $(_executor) $(AssemblyName) %(Class) %(Identity) %(ExceptionFile) %(Parameters)</Command>
</ExecuteMethod>
</ItemGroup>
<Exec Command="%(ExecuteMethod.Command)" WorkingDirectory="$(TargetDir)" />
</Target>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"configProperties": {
"System.Resources.Extensions.UseBinaryFormatter": true
}
}

0 comments on commit 6a36687

Please sign in to comment.