Skip to content

Commit 34b0d9d

Browse files
adamsitnikericstjtmdsivanpovazan
authored
[release/9.0] BinaryFormatter tests improvements (#107540)
* 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>
1 parent 7832c5d commit 34b0d9d

File tree

39 files changed

+223
-147
lines changed

39 files changed

+223
-147
lines changed

Directory.Build.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,8 @@
473473
<PropertyGroup Condition="'$(IsTestProject)' == 'true' or '$(IsTestSupportProject)' == 'true' or '$(IsTrimmingTestProject)' == 'true'">
474474
<!-- we need to re-enable BinaryFormatter within test projects since some tests exercise these code paths to ensure compat -->
475475
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
476+
<!-- For DotNetBuildSourceOnly, only the bundled BinaryFormatter is built which does not support serialization. -->
477+
<EnableUnsafeBinaryFormatterSerialization Condition="'$(DotNetBuildSourceOnly)' == 'true'">false</EnableUnsafeBinaryFormatterSerialization>
476478
<!-- don't warn on usage of BinaryFormatter or legacy serialization infrastructure from test projects -->
477479
<NoWarn>$(NoWarn);SYSLIB0011;SYSLIB0050;SYSLIB0051</NoWarn>
478480
<!-- don't warn about unnecessary trim warning suppressions. can be removed with preview 6. -->

eng/Versions.props

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,6 @@
163163
<MicrosoftDiaSymReaderVersion>2.0.0</MicrosoftDiaSymReaderVersion>
164164
<MicrosoftDiaSymReaderNativeVersion>17.10.0-beta1.24272.1</MicrosoftDiaSymReaderNativeVersion>
165165
<SystemCommandLineVersion>2.0.0-beta4.24324.3</SystemCommandLineVersion>
166-
<!-- This package provides working implementation of BinaryFormatter and is used only by test projects -->
167-
<SystemRuntimeSerializationFormattersVersion>9.0.0-preview.7.24327.2</SystemRuntimeSerializationFormattersVersion>
168166
<TraceEventVersion>3.1.7</TraceEventVersion>
169167
<NETStandardLibraryRefVersion>2.1.0</NETStandardLibraryRefVersion>
170168
<NetStandardLibraryVersion>2.0.3</NetStandardLibraryVersion>

eng/references.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<ItemGroup>
4040
<ProjectReferenceWithConfiguration PrivateAssets="all"
4141
Private="false"
42-
Condition="$(NetCoreAppLibrary.Contains('%(Filename);'))" />
42+
Condition="$(NetCoreAppLibrary.Contains('%(Filename);')) and '%(ProjectReferenceWithConfiguration.Private)' == ''" />
4343
</ItemGroup>
4444
</Target>
4545

eng/testing/tests.ioslike.targets

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<BundleFiles Condition="'%(AppleAssembliesToBundle._IsNative)' != 'true'"
7272
Include="@(AppleAssembliesToBundle)" TargetDir="publish\%(AppleAssembliesToBundle.RecursiveDir)" />
7373
<BundleFiles Include="@(AppleNativeFilesToBundle)" TargetDir="publish\%(AppleNativeFilesToBundle.RecursiveDir)" />
74+
<BundleFiles Include="@(_SatelliteAssemblies)" TargetDir="publish\%(_SatelliteAssemblies.RecursiveDir)" />
7475
<BundleFiles Include="$(RuntimeConfigFilePath)" TargetDir="publish" />
7576

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

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

100100
<Copy SourceFiles="@(BundleFiles)" DestinationFolder="$(BundleDir)%(TargetDir)" />
@@ -213,7 +213,10 @@
213213
<_IsNative>false</_IsNative>
214214
</AppleAssembliesToBundle>
215215

216-
<AppleNativeFilesToBundle Include="$(PublishDir)\**\*.*" Exclude="$(PublishDir)\*.dll" />
216+
<ApplePdbsToBundle Include="$([System.IO.Path]::ChangeExtension('%(AppleAssembliesToBundle.Identity)', '.pdb'))" />
217+
<AppleXmlsToBundle Include="$([System.IO.Path]::ChangeExtension('%(AppleAssembliesToBundle.Identity)', '.xml'))" />
218+
219+
<AppleNativeFilesToBundle Include="$(PublishDir)\**\*.*" Exclude="@(AppleAssembliesToBundle);@(ApplePdbsToBundle);@(AppleXmlsToBundle);@(_SatelliteAssemblies)" />
217220
</ItemGroup>
218221
</Target>
219222

src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -736,22 +736,8 @@ private static bool DetermineBinaryFormatterSupport()
736736
return false;
737737
}
738738

739-
Assembly assembly = typeof(System.Runtime.Serialization.Formatters.Binary.BinaryFormatter).Assembly;
740-
AssemblyName name = assembly.GetName();
741-
Version assemblyVersion = name.Version;
742-
743-
bool isSupported = true;
744-
745-
// Version 8.1 is the version in the shared runtime (.NET 9+) that has the type disabled with no config.
746-
// Assembly versions beyond 8.1 are the fully functional version from NuGet.
747-
// Assembly versions before 8.1 probably won't be encountered, since that's the past.
748-
749-
if (assemblyVersion.Major == 8 && assemblyVersion.Minor == 1)
750-
{
751-
isSupported = false;
752-
}
753-
754-
return isSupported;
739+
return AppContext.TryGetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", out bool isBinaryFormatterEnabled)
740+
&& isBinaryFormatterEnabled;
755741
}
756742
}
757743
}

src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/Microsoft.Extensions.Hosting.Unit.Tests.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@
1919
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Hosting\src\Microsoft.Extensions.Hosting.csproj" />
2020
</ItemGroup>
2121

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

src/libraries/System.Collections.NonGeneric/tests/System.Collections.NonGeneric.Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,10 @@
4444
<Compile Include="CaseInsensitiveHashCodeProviderTests.cs" />
4545
<Compile Include="$(CommonTestPath)System\Collections\IEnumerable.NonGeneric.Serialization.Tests.cs"
4646
Link="Common\System\Collections\IEnumerable.NonGeneric.Serialization.Tests.cs" />
47+
48+
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
49+
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
50+
Private="true"
51+
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
4752
</ItemGroup>
4853
</Project>

src/libraries/System.Collections.Specialized/tests/System.Collections.Specialized.Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,10 @@
7575
<Compile Include="$(CommonTestPath)System\Collections\IEnumerable.NonGeneric.Serialization.Tests.cs"
7676
Link="Common\System\Collections\IEnumerable.NonGeneric.Serialization.Tests.cs" />
7777
<Compile Include="NameObjectCollectionBase\NameObjectCollectionBase.ConstructorTests.cs" />
78+
79+
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
80+
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
81+
Private="true"
82+
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
7883
</ItemGroup>
7984
</Project>

src/libraries/System.Collections/tests/Generic/Dictionary/Dictionary.Tests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,13 @@ private static void TestComparerSerialization<T>(IEqualityComparer<T> equalityCo
452452
s.Position = 0;
453453
dict = (Dictionary<T, T>)bf.Deserialize(s);
454454

455+
if (equalityComparer.Equals(EqualityComparer<string>.Default))
456+
{
457+
// EqualityComparer<string>.Default is mapped to StringEqualityComparer, but serialized as GenericEqualityComparer<string>
458+
Assert.Equal("System.Collections.Generic.GenericEqualityComparer`1[System.String]", dict.Comparer.GetType().ToString());
459+
return;
460+
}
461+
455462
if (internalTypeName == null)
456463
{
457464
Assert.IsType(equalityComparer.GetType(), dict.Comparer);

src/libraries/System.Collections/tests/Generic/HashSet/HashSet.Generic.Tests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,13 @@ static void TestComparerSerialization<TCompared>(IEqualityComparer<TCompared> eq
886886
s.Position = 0;
887887
set = (HashSet<TCompared>)bf.Deserialize(s);
888888

889+
if (equalityComparer.Equals(EqualityComparer<string>.Default))
890+
{
891+
// EqualityComparer<string>.Default is mapped to StringEqualityComparer, but serialized as GenericEqualityComparer<string>
892+
Assert.Equal("System.Collections.Generic.GenericEqualityComparer`1[System.String]", set.Comparer.GetType().ToString());
893+
return;
894+
}
895+
889896
if (internalTypeName == null)
890897
{
891898
Assert.IsType(equalityComparer.GetType(), set.Comparer);

0 commit comments

Comments
 (0)