Skip to content

Commit

Permalink
[release/9.0] BinaryFormatter tests improvements (#107540)
Browse files Browse the repository at this point in the history
* Remove package references from library tests (#106737)

* Remove package references from library tests

These tests should be referencing the product assemblies so that they
test latest and not old bits.

* Reference the OOB version of SRSF and make sure it's copied

* BinaryFormatter tests should be skipped only on AOT, WASM and Mobile (#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>

* Don't use WeakReferences in the round trip test, as the target may get freed in the meantime, fixes #104905 (#106967)

* Enable more BinaryFormatter tests (#107408)

* enable the BinaryFormatter tests in System.Runtime.Serialization.Formatters.Tests

* add new test project, where the flag is disabled and it runs only 3 tests in total that ensure that

* The SerializationGuard is no longer activated since BF was moved to the OOB package, the tests need to reflect that.

* Disable binary formatter tests when DotNetBuildSourceOnly. (#107549)

* [mono][tvos] Do not treat assembly.pdb/xml files as native files to bundle when AOTing on Helix (#107079)

* Do not treat assembly.pdb/xml files as native files to bundle

* Bundle satellite assemblies as well

* [mono][ci] Include PDBs from runtime pack when building on Helix if required (#107348)

---------

Co-authored-by: Eric StJohn <ericstj@microsoft.com>
Co-authored-by: Tom Deseyn <tom.deseyn@gmail.com>
Co-authored-by: Ivan Povazan <55002338+ivanpovazan@users.noreply.github.com>
  • Loading branch information
4 people committed Sep 17, 2024
1 parent 7832c5d commit 34b0d9d
Show file tree
Hide file tree
Showing 39 changed files with 223 additions and 147 deletions.
2 changes: 2 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,8 @@
<PropertyGroup Condition="'$(IsTestProject)' == 'true' or '$(IsTestSupportProject)' == 'true' or '$(IsTrimmingTestProject)' == 'true'">
<!-- we need to re-enable BinaryFormatter within test projects since some tests exercise these code paths to ensure compat -->
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
<!-- For DotNetBuildSourceOnly, only the bundled BinaryFormatter is built which does not support serialization. -->
<EnableUnsafeBinaryFormatterSerialization Condition="'$(DotNetBuildSourceOnly)' == 'true'">false</EnableUnsafeBinaryFormatterSerialization>
<!-- don't warn on usage of BinaryFormatter or legacy serialization infrastructure from test projects -->
<NoWarn>$(NoWarn);SYSLIB0011;SYSLIB0050;SYSLIB0051</NoWarn>
<!-- don't warn about unnecessary trim warning suppressions. can be removed with preview 6. -->
Expand Down
2 changes: 0 additions & 2 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@
<MicrosoftDiaSymReaderVersion>2.0.0</MicrosoftDiaSymReaderVersion>
<MicrosoftDiaSymReaderNativeVersion>17.10.0-beta1.24272.1</MicrosoftDiaSymReaderNativeVersion>
<SystemCommandLineVersion>2.0.0-beta4.24324.3</SystemCommandLineVersion>
<!-- This package provides working implementation of BinaryFormatter and is used only by test projects -->
<SystemRuntimeSerializationFormattersVersion>9.0.0-preview.7.24327.2</SystemRuntimeSerializationFormattersVersion>
<TraceEventVersion>3.1.7</TraceEventVersion>
<NETStandardLibraryRefVersion>2.1.0</NETStandardLibraryRefVersion>
<NetStandardLibraryVersion>2.0.3</NetStandardLibraryVersion>
Expand Down
2 changes: 1 addition & 1 deletion eng/references.targets
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<ItemGroup>
<ProjectReferenceWithConfiguration PrivateAssets="all"
Private="false"
Condition="$(NetCoreAppLibrary.Contains('%(Filename);'))" />
Condition="$(NetCoreAppLibrary.Contains('%(Filename);')) and '%(ProjectReferenceWithConfiguration.Private)' == ''" />
</ItemGroup>
</Target>

Expand Down
9 changes: 6 additions & 3 deletions eng/testing/tests.ioslike.targets
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<BundleFiles Condition="'%(AppleAssembliesToBundle._IsNative)' != 'true'"
Include="@(AppleAssembliesToBundle)" TargetDir="publish\%(AppleAssembliesToBundle.RecursiveDir)" />
<BundleFiles Include="@(AppleNativeFilesToBundle)" TargetDir="publish\%(AppleNativeFilesToBundle.RecursiveDir)" />
<BundleFiles Include="@(_SatelliteAssemblies)" TargetDir="publish\%(_SatelliteAssemblies.RecursiveDir)" />
<BundleFiles Include="$(RuntimeConfigFilePath)" TargetDir="publish" />

<BundleFiles Include="$(MonoProjectRoot)\msbuild\apple\data\*" TargetDir="publish" />
Expand All @@ -93,8 +94,7 @@

<ItemGroup Condition="'$(DebuggerSupport)' == 'true'">
<!-- Add any pdb files, if available -->
<_BundlePdbFiles Include="$([System.IO.Path]::ChangeExtension('%(AppleAssembliesToBundle.Identity)', '.pdb'))" />
<BundleFiles Include="@(_BundlePdbFiles)" TargetDir="publish" Condition="Exists(%(_BundlePdbFiles.Identity))" />
<BundleFiles Include="@(ApplePdbsToBundle)" TargetDir="publish" Condition="Exists(%(ApplePdbsToBundle.Identity))" />
</ItemGroup>

<Copy SourceFiles="@(BundleFiles)" DestinationFolder="$(BundleDir)%(TargetDir)" />
Expand Down Expand Up @@ -213,7 +213,10 @@
<_IsNative>false</_IsNative>
</AppleAssembliesToBundle>

<AppleNativeFilesToBundle Include="$(PublishDir)\**\*.*" Exclude="$(PublishDir)\*.dll" />
<ApplePdbsToBundle Include="$([System.IO.Path]::ChangeExtension('%(AppleAssembliesToBundle.Identity)', '.pdb'))" />
<AppleXmlsToBundle Include="$([System.IO.Path]::ChangeExtension('%(AppleAssembliesToBundle.Identity)', '.xml'))" />

<AppleNativeFilesToBundle Include="$(PublishDir)\**\*.*" Exclude="@(AppleAssembliesToBundle);@(ApplePdbsToBundle);@(AppleXmlsToBundle);@(_SatelliteAssemblies)" />
</ItemGroup>
</Target>

Expand Down
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 @@ -15,11 +15,14 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<PackageReference Include="System.Text.Json" Version="$(SystemTextJsonVersion)" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Text.Json\src\System.Text.Json.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETFramework'">
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="$(SystemRuntimeSerializationFormattersVersion)" />
<!-- 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 @@ -331,11 +331,9 @@
<ItemGroup>
<PackageReference Include="System.Net.TestData" Version="$(SystemNetTestDataVersion)" />
<ProjectReference Include="$(LibrariesProjectRoot)System.DirectoryServices.Protocols\src\System.DirectoryServices.Protocols.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.IO.Pipelines\src\System.IO.Pipelines.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(CommonTestPath)StreamConformanceTests\StreamConformanceTests.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.IO.Pipelines" Version="7.0.0" />
</ItemGroup>
</Project>
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 @@ -10,10 +10,11 @@

<ItemGroup>
<ProjectReference Include="..\src\System.Net.ServerSentEvents.csproj"/>
<ProjectReference Include="$(LibrariesProjectRoot)System.Text.Json\gen\System.Text.Json.SourceGeneration.Roslyn4.4.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" SetTargetFramework="TargetFramework=netstandard2.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<PackageReference Include="System.Text.Json" Version="$(SystemTextJsonVersion)" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Text.Json\src\System.Text.Json.csproj" />
</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 @@ -14,6 +14,14 @@ public class BasicObjectTests : Common.BasicObjectTests<FormattedObjectSerialize
[MemberData(nameof(SerializableObjects))]
public void BasicObjectsRoundTripAndMatch(object value, TypeSerializableValue[] _)
{
if (value is WeakReference || (value.GetType().IsGenericType && value.GetType().GetGenericTypeDefinition() == typeof(WeakReference<>)))
{
// We can root the provided value, but we can't root the deserialized value:
// GC can free the target of WeakReference after it gets deserialized,
// but before it gets returned from BinaryFormatter.Deserialize.
return;
}

// We need to round trip through the BinaryFormatter as a few objects in tests remove
// serialized data on deserialization.
BinaryFormatter formatter = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@
</EmbeddedResource>
<EmbeddedResource Include="TestResources.resx" LogicalName="TestResources.resources" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Drawing.Common.TestData" Version="$(SystemDrawingCommonTestDataVersion)" />
<ProjectReference Include="..\..\src\System.Resources.Extensions.csproj" />
<PackageReference Include="System.Drawing.Common" Version="$(SystemDrawingCommonVersion)" />
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="$(SystemRuntimeSerializationFormattersVersion)" />
<!-- 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>
Expand Down
Loading

0 comments on commit 34b0d9d

Please sign in to comment.