Skip to content

Commit 8685859

Browse files
authored
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.
1 parent 1bad124 commit 8685859

5 files changed

+72
-108
lines changed

src/libraries/System.Runtime.Serialization.Formatters/System.Runtime.Serialization.Formatters.sln

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "tools\ref", "{CDD725
147147
EndProject
148148
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{21D78340-7F96-4519-983F-529077A11AE1}"
149149
EndProject
150+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Runtime.Serialization.Formatters.Disabled.Tests", "tests\Disabled\System.Runtime.Serialization.Formatters.Disabled.Tests.csproj", "{0C8ADEFB-33FB-4785-A828-93377745B2BA}"
151+
EndProject
150152
Global
151153
GlobalSection(SolutionConfigurationPlatforms) = preSolution
152154
Debug|Any CPU = Debug|Any CPU
@@ -1561,6 +1563,36 @@ Global
15611563
{5149AC24-C1A7-4A4C-BE83-FF3037B99E4C}.Checked|arm64.ActiveCfg = Debug|Any CPU
15621564
{5149AC24-C1A7-4A4C-BE83-FF3037B99E4C}.Checked|x64.ActiveCfg = Debug|Any CPU
15631565
{5149AC24-C1A7-4A4C-BE83-FF3037B99E4C}.Checked|x86.ActiveCfg = Debug|Any CPU
1566+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|Any CPU.ActiveCfg = Debug|Any CPU
1567+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|Any CPU.Build.0 = Debug|Any CPU
1568+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|arm.ActiveCfg = Debug|Any CPU
1569+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|arm.Build.0 = Debug|Any CPU
1570+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|arm64.ActiveCfg = Debug|Any CPU
1571+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|arm64.Build.0 = Debug|Any CPU
1572+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|x64.ActiveCfg = Debug|Any CPU
1573+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|x64.Build.0 = Debug|Any CPU
1574+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|x86.ActiveCfg = Debug|Any CPU
1575+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|x86.Build.0 = Debug|Any CPU
1576+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1577+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
1578+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|arm.ActiveCfg = Debug|Any CPU
1579+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|arm.Build.0 = Debug|Any CPU
1580+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|arm64.ActiveCfg = Debug|Any CPU
1581+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|arm64.Build.0 = Debug|Any CPU
1582+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|x64.ActiveCfg = Debug|Any CPU
1583+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|x64.Build.0 = Debug|Any CPU
1584+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|x86.ActiveCfg = Debug|Any CPU
1585+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|x86.Build.0 = Debug|Any CPU
1586+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
1587+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|Any CPU.Build.0 = Release|Any CPU
1588+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|arm.ActiveCfg = Release|Any CPU
1589+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|arm.Build.0 = Release|Any CPU
1590+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|arm64.ActiveCfg = Release|Any CPU
1591+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|arm64.Build.0 = Release|Any CPU
1592+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|x64.ActiveCfg = Release|Any CPU
1593+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|x64.Build.0 = Release|Any CPU
1594+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|x86.ActiveCfg = Release|Any CPU
1595+
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|x86.Build.0 = Release|Any CPU
15641596
EndGlobalSection
15651597
GlobalSection(SolutionProperties) = preSolution
15661598
HideSolutionNode = FALSE
@@ -1635,6 +1667,7 @@ Global
16351667
{E8705420-F755-4CBA-970D-B745F59F9780} = {21D78340-7F96-4519-983F-529077A11AE1}
16361668
{5149AC24-C1A7-4A4C-BE83-FF3037B99E4C} = {CDD72541-F0ED-41FA-9C9A-5D774CEB22E4}
16371669
{CDD72541-F0ED-41FA-9C9A-5D774CEB22E4} = {21D78340-7F96-4519-983F-529077A11AE1}
1670+
{0C8ADEFB-33FB-4785-A828-93377745B2BA} = {F8CB1B1F-F935-4ECE-B3C2-CF62B5654BF2}
16381671
EndGlobalSection
16391672
GlobalSection(ExtensibilityGlobals) = postSolution
16401673
SolutionGuid = {158060A0-BFF9-48E6-90A9-93F52FEB5D3B}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TestRuntime>true</TestRuntime>
4+
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
5+
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-freebsd;$(NetCoreAppCurrent)-illumos;$(NetCoreAppCurrent)-solaris;$(NetCoreAppCurrent)-haiku;$(NetCoreAppCurrent)-linux;$(NetCoreAppCurrent)-osx;$(NetCoreAppCurrent)-ios;$(NetCoreAppCurrent)-tvos</TargetFrameworks>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<Compile Include="DisableBitTests.cs" />
9+
<Compile Include="..\TestConfiguration.cs" />
10+
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj" />
11+
</ItemGroup>
12+
<!--
13+
We're testing the BinaryFormatter enablement / disablement switch, so we need to suppress any inherited behavior.
14+
See https://github.com/dotnet/runtime/issues/87811 for more details.
15+
This is a temporary solution and should be reverted back to the original once the necessary changes are made in
16+
shared frameworks and SDK.
17+
-->
18+
<Target Name="RemoveSerializationRuntimeOption" BeforeTargets="GenerateBuildRuntimeConfigurationFiles">
19+
<ItemGroup>
20+
<RuntimeHostConfigurationOption Remove="System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization" />
21+
</ItemGroup>
22+
</Target>
23+
</Project>

src/libraries/System.Runtime.Serialization.Formatters/tests/SerializationGuardTests.cs

Lines changed: 10 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,40 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
5-
using System.Diagnostics;
64
using System.IO;
7-
using System.Reflection;
85
using System.Runtime.Serialization.Formatters.Binary;
9-
using System.Threading;
10-
using System.Threading.Tasks;
116
using Xunit;
127

138
namespace System.Runtime.Serialization.Formatters.Tests
149
{
10+
// When BinaryFormatter was built-in to the platform we used to activate SerializationGuard in ObjectReader.Deserialize,
11+
// but now that it has moved to an OOB offering it no longer does.
1512
[ConditionalClass(typeof(TestConfiguration), nameof(TestConfiguration.IsBinaryFormatterEnabled))]
1613
public static class SerializationGuardTests
1714
{
1815
[Fact]
19-
public static void BlockAssemblyLoads()
20-
{
21-
TryPayload(new AssemblyLoader());
22-
}
23-
24-
[Fact]
25-
public static void BlockProcessStarts()
26-
{
27-
TryPayload(new ProcessStarter());
28-
}
29-
30-
[Fact]
31-
public static void BlockFileWrites()
32-
{
33-
TryPayload(new FileWriter());
34-
}
35-
36-
[Fact]
37-
public static void BlockAsyncDodging()
38-
{
39-
TryPayload(new AsyncDodger());
40-
}
41-
42-
private static void TryPayload(object payload)
16+
public static void IsNoLongerActivated()
4317
{
4418
MemoryStream ms = new MemoryStream();
4519
BinaryFormatter writer = new BinaryFormatter();
46-
writer.Serialize(ms, payload);
20+
writer.Serialize(ms, new ThrowIfDeserializationInProgress());
4721
ms.Position = 0;
4822

4923
BinaryFormatter reader = new BinaryFormatter();
50-
SerializationException se = Assert.Throws<SerializationException>(() => reader.Deserialize(ms));
51-
Assert.IsAssignableFrom<TargetInvocationException>(se.InnerException);
52-
}
53-
}
54-
55-
[Serializable]
56-
internal class AssemblyLoader : ISerializable
57-
{
58-
public AssemblyLoader() { }
59-
60-
public AssemblyLoader(SerializationInfo info, StreamingContext context)
61-
{
62-
Assembly.Load(new byte[1000]);
63-
}
64-
65-
public void GetObjectData(SerializationInfo info, StreamingContext context)
66-
{
24+
Assert.NotNull(reader.Deserialize(ms));
6725
}
6826
}
6927

7028
[Serializable]
71-
internal class ProcessStarter : ISerializable
29+
internal class ThrowIfDeserializationInProgress : ISerializable
7230
{
73-
public ProcessStarter() { }
74-
75-
private ProcessStarter(SerializationInfo info, StreamingContext context)
76-
{
77-
Process.Start("calc.exe");
78-
}
31+
private static int s_cachedSerializationSwitch;
7932

80-
public void GetObjectData(SerializationInfo info, StreamingContext context)
81-
{
82-
}
83-
}
84-
85-
[Serializable]
86-
internal class FileWriter : ISerializable
87-
{
88-
public FileWriter() { }
89-
90-
private FileWriter(SerializationInfo info, StreamingContext context)
91-
{
92-
string tempPath = Path.GetTempFileName();
93-
File.WriteAllText(tempPath, "This better not be written...");
94-
throw new UnreachableException("Unreachable code (SerializationGuard should have kicked in)");
95-
}
96-
97-
public void GetObjectData(SerializationInfo info, StreamingContext context)
98-
{
99-
}
100-
}
101-
102-
[Serializable]
103-
internal class AsyncDodger : ISerializable
104-
{
105-
public AsyncDodger() { }
106-
107-
private AsyncDodger(SerializationInfo info, StreamingContext context)
108-
{
109-
try
110-
{
111-
Task t = Task.Factory.StartNew(LoadAssemblyOnBackgroundThread, TaskCreationOptions.LongRunning);
112-
t.Wait();
113-
}
114-
catch (AggregateException ex)
115-
{
116-
throw ex.InnerException;
117-
}
118-
}
33+
public ThrowIfDeserializationInProgress() { }
11934

120-
private void LoadAssemblyOnBackgroundThread()
35+
private ThrowIfDeserializationInProgress(SerializationInfo info, StreamingContext context)
12136
{
122-
Assembly.Load(new byte[1000]);
37+
SerializationGuard.ThrowIfDeserializationInProgress("AllowProcessCreation", ref s_cachedSerializationSwitch);
12338
}
12439

12540
public void GetObjectData(SerializationInfo info, StreamingContext context)

src/libraries/System.Runtime.Serialization.Formatters/tests/System.Runtime.Serialization.Formatters.Tests.csproj

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
<ItemGroup>
88
<Compile Include="BinaryFormatterTestData.cs" />
99
<Compile Include="BinaryFormatterTests.cs" />
10-
<Compile Include="DisableBitTests.cs" />
1110
<Compile Include="EqualityExtensions.cs" />
1211
<Compile Include="OptionalFieldAttributeTests.cs" />
1312
<Compile Include="FormatterConverterTests.cs" />
@@ -36,6 +35,8 @@
3635
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
3736
<Compile Include="BinaryFormatterEventSourceTests.cs" />
3837
<Compile Include="SerializationGuardTests.cs" />
38+
<Compile Include="$(CommonPath)System\Runtime\Serialization\SerializationGuard.cs"
39+
Link="Common\System\Runtime\Serialization\SerializationGuard.cs" />
3940
</ItemGroup>
4041
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
4142
<PackageReference Include="System.Drawing.Common" Version="$(SystemDrawingCommonVersion)" />
@@ -48,8 +49,11 @@
4849
<ProjectReference Include="$(LibrariesProjectRoot)System.DirectoryServices.Protocols\src\System.DirectoryServices.Protocols.csproj" />
4950
<ProjectReference Include="$(LibrariesProjectRoot)System.DirectoryServices\src\System.DirectoryServices.csproj" />
5051
<ProjectReference Include="$(LibrariesProjectRoot)System.IO.Packaging\src\System.IO.Packaging.csproj" />
51-
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj" />
5252
<ProjectReference Include="$(LibrariesProjectRoot)System.Security.Permissions\src\System.Security.Permissions.csproj" />
53+
<!-- Reference the `NetCoreAppMinimum` build which has a functional BinaryFormatter and force a private copy to ensure it's not excluded -->
54+
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj"
55+
Private="true"
56+
SetTargetFramework="TargetFramework=$(NetCoreAppMinimum)" />
5357
</ItemGroup>
5458
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
5559
<ProjectReference Include="$(LibrariesProjectRoot)System.Reflection.Metadata\src\System.Reflection.Metadata.csproj" />
@@ -65,15 +69,4 @@
6569
<Reference Include="WindowsBase" />
6670
</ItemGroup>
6771

68-
<!--
69-
We're testing the BinaryFormatter enablement / disablement switch, so we need to suppress any inherited behavior.
70-
See https://github.com/dotnet/runtime/issues/87811 for more details.
71-
This is a temporary solution and should be reverted back to the original once the necessary changes are made in
72-
shared frameworks and SDK.
73-
-->
74-
<Target Name="RemoveSerializationRuntimeOption" BeforeTargets="GenerateBuildRuntimeConfigurationFiles">
75-
<ItemGroup>
76-
<RuntimeHostConfigurationOption Remove="System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization" />
77-
</ItemGroup>
78-
</Target>
7972
</Project>

0 commit comments

Comments
 (0)