Skip to content

Commit

Permalink
BinaryFormatter tests should be skipped only on AOT, WASM and Mobile (#…
Browse files Browse the repository at this point in the history
…106858)

* respect AppContext switch (which is currently enabled for all projects in the root Directory.Build.props file)

* add project reference to all test projects that need working BF (and were being skipped for a while)

* adjust to changes from #104202: EqualityComparer<string>.Default is mapped to StringEqualityComparer, but serialized as GenericEqualityComparer<string>
  • Loading branch information
adamsitnik authored and github-actions committed Sep 17, 2024
1 parent 173a7ff commit 5f4f896
Show file tree
Hide file tree
Showing 22 changed files with 109 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -736,22 +736,8 @@ private static bool DetermineBinaryFormatterSupport()
return false;
}

Assembly assembly = typeof(System.Runtime.Serialization.Formatters.Binary.BinaryFormatter).Assembly;
AssemblyName name = assembly.GetName();
Version assemblyVersion = name.Version;

bool isSupported = true;

// Version 8.1 is the version in the shared runtime (.NET 9+) that has the type disabled with no config.
// Assembly versions beyond 8.1 are the fully functional version from NuGet.
// Assembly versions before 8.1 probably won't be encountered, since that's the past.

if (assemblyVersion.Major == 8 && assemblyVersion.Minor == 1)
{
isSupported = false;
}

return isSupported;
return AppContext.TryGetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", out bool isBinaryFormatterEnabled)
&& isBinaryFormatterEnabled;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Hosting\src\Microsoft.Extensions.Hosting.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETFramework'">
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@
<Compile Include="CaseInsensitiveHashCodeProviderTests.cs" />
<Compile Include="$(CommonTestPath)System\Collections\IEnumerable.NonGeneric.Serialization.Tests.cs"
Link="Common\System\Collections\IEnumerable.NonGeneric.Serialization.Tests.cs" />

<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,10 @@
<Compile Include="$(CommonTestPath)System\Collections\IEnumerable.NonGeneric.Serialization.Tests.cs"
Link="Common\System\Collections\IEnumerable.NonGeneric.Serialization.Tests.cs" />
<Compile Include="NameObjectCollectionBase\NameObjectCollectionBase.ConstructorTests.cs" />

<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,13 @@ private static void TestComparerSerialization<T>(IEqualityComparer<T> equalityCo
s.Position = 0;
dict = (Dictionary<T, T>)bf.Deserialize(s);

if (equalityComparer.Equals(EqualityComparer<string>.Default))
{
// EqualityComparer<string>.Default is mapped to StringEqualityComparer, but serialized as GenericEqualityComparer<string>
Assert.Equal("System.Collections.Generic.GenericEqualityComparer`1[System.String]", dict.Comparer.GetType().ToString());
return;
}

if (internalTypeName == null)
{
Assert.IsType(equalityComparer.GetType(), dict.Comparer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,13 @@ static void TestComparerSerialization<TCompared>(IEqualityComparer<TCompared> eq
s.Position = 0;
set = (HashSet<TCompared>)bf.Deserialize(s);

if (equalityComparer.Equals(EqualityComparer<string>.Default))
{
// EqualityComparer<string>.Default is mapped to StringEqualityComparer, but serialized as GenericEqualityComparer<string>
Assert.Equal("System.Collections.Generic.GenericEqualityComparer`1[System.String]", set.Comparer.GetType().ToString());
return;
}

if (internalTypeName == null)
{
Assert.IsType(equalityComparer.GetType(), set.Comparer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
<TestRuntime>true</TestRuntime>
<DebuggerSupport Condition="'$(DebuggerSupport)' == '' and ('$(TargetOS)' == 'browser' or '$(TargetOS)' == 'wasi')">true</DebuggerSupport>
</PropertyGroup>
<ItemGroup>
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>
<ItemGroup>
<RdXmlFile Include="default.rd.xml" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,10 @@
<Compile Include="System\ComponentModel\DataAnnotations\ValidationResultTests.cs" />
<Compile Include="System\ComponentModel\DataAnnotations\ValidatorTests.cs" />
<Compile Include="System\ComponentModel\DataAnnotations\DeniedValuesAttributeTests.cs" />

<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@
<Compile Include="System\ComponentModel\ParenthesizePropertyNameAttributeTests.cs" />
<Compile Include="System\ComponentModel\ReadOnlyAttributeTests.cs" />
<Compile Include="System\ComponentModel\RefreshPropertiesAttributeTests.cs" />

<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@
<ItemGroup>
<PackageReference Include="System.ComponentModel.TypeConverter.TestData" Version="$(SystemComponentModelTypeConverterTestDataVersion)" />
<PackageReference Include="Moq" Version="$(MoqVersion)" />
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\TestResx.resx">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,10 @@
<!-- Manually reference the transitive dependency to make NuGet pick the package over the transitive project: https://github.com/NuGet/Home/issues/10368 -->
<PackageReference Include="System.Security.Principal.Windows" Version="$(SystemSecurityPrincipalWindowsVersion)" PrivateAssets="all" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETFramework'">
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@
Link="Common\System\Diagnostics\Tracing\TestEventListener.cs" />
<Compile Include="$(CommonTestPath)System\Runtime\Serialization\Formatters\BinaryFormatterHelpers.cs"
Link="Common\System\Runtime\Serialization\Formatters\BinaryFormatterHelpers.cs" />

<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>
<!-- S.D.SqlClient isn't live built anymore. -->
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,9 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Net.TestData" Version="$(SystemNetTestDataVersion)" />
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,9 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(CommonTestPath)StreamConformanceTests\StreamConformanceTests.csproj" />
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@
<Compile Include="ReadOnlyObservableCollection\ReadOnlyObservableCollection_SerializationTests.cs" />
<Compile Include="$(CommonTestPath)System\Runtime\Serialization\Formatters\BinaryFormatterHelpers.cs"
Link="Common\System\Runtime\Serialization\Formatters\BinaryFormatterHelpers.cs" />

<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<PackageReference Include="System.ValueTuple" Version="$(SystemValueTupleVersion)" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETFramework'">
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetOS)' == 'browser'">
<WasmFilesToIncludeFromPublishDir Include="$(AssemblyName).dll" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@
<Compile Include="System\Runtime\Serialization\IgnoreDataMemberAttributeTests.cs" />
<Compile Include="System\Runtime\Serialization\InvalidDataContractExceptionTests.cs" />
<Compile Include="System\Runtime\Serialization\KnownTypeAttributeTests.cs" />

<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
<PackageReference Include="System.Drawing.Common" Version="$(SystemDrawingCommonVersion)" />
<!--Reference to System.Resources.Extensions is required in order to be able to embed a resource that requires a custom reader-->
<ProjectReference Include="$(LibrariesProjectRoot)System.Resources.Extensions\src\System.Resources.Extensions.csproj" />
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>
<!--
MSBuild on .NET Core doesn't support non-string resources. See https://github.com/Microsoft/msbuild/issues/2221
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,5 +370,9 @@
<!-- Used during reflection in tests. -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Security.Permissions\src\System.Security.Permissions.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Threading.AccessControl\src\System.Threading.AccessControl.csproj" />
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -519,5 +519,9 @@
<ProjectReference Include="$(CommonTestPath)StreamConformanceTests\StreamConformanceTests.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Security.Cryptography.Pkcs\src\System.Security.Cryptography.Pkcs.csproj" />
<PackageReference Include="System.Security.Cryptography.X509Certificates.TestData" Version="$(SystemSecurityCryptographyX509CertificatesTestDataVersion)" />
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
SetTargetFramework="TargetFramework=netstandard2.0"
OutputItemType="Analyzer"
ReferenceOutputAssembly="true" />

<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Text.RegularExpressions.TestData" Version="$(SystemTextRegularExpressionsTestDataVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
<Compile Include="ChannelClosedExceptionTests.netcoreapp.cs" />
<Compile Include="PriorityUnboundedChannelTests.cs" />
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
Private="true"
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == '$(NetFrameworkMinimum)'">
<ProjectReference Include="..\src\System.Threading.Channels.csproj" />
Expand Down

0 comments on commit 5f4f896

Please sign in to comment.