Skip to content

Commit

Permalink
Enable more BinaryFormatter tests (#107408)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
adamsitnik authored Sep 6, 2024
1 parent 1bad124 commit 8685859
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "tools\ref", "{CDD725
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{21D78340-7F96-4519-983F-529077A11AE1}"
EndProject
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1561,6 +1563,36 @@ Global
{5149AC24-C1A7-4A4C-BE83-FF3037B99E4C}.Checked|arm64.ActiveCfg = Debug|Any CPU
{5149AC24-C1A7-4A4C-BE83-FF3037B99E4C}.Checked|x64.ActiveCfg = Debug|Any CPU
{5149AC24-C1A7-4A4C-BE83-FF3037B99E4C}.Checked|x86.ActiveCfg = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|Any CPU.ActiveCfg = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|Any CPU.Build.0 = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|arm.ActiveCfg = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|arm.Build.0 = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|arm64.ActiveCfg = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|arm64.Build.0 = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|x64.ActiveCfg = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|x64.Build.0 = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|x86.ActiveCfg = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Checked|x86.Build.0 = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|arm.ActiveCfg = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|arm.Build.0 = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|arm64.ActiveCfg = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|arm64.Build.0 = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|x64.ActiveCfg = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|x64.Build.0 = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|x86.ActiveCfg = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Debug|x86.Build.0 = Debug|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|Any CPU.Build.0 = Release|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|arm.ActiveCfg = Release|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|arm.Build.0 = Release|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|arm64.ActiveCfg = Release|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|arm64.Build.0 = Release|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|x64.ActiveCfg = Release|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|x64.Build.0 = Release|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|x86.ActiveCfg = Release|Any CPU
{0C8ADEFB-33FB-4785-A828-93377745B2BA}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1635,6 +1667,7 @@ Global
{E8705420-F755-4CBA-970D-B745F59F9780} = {21D78340-7F96-4519-983F-529077A11AE1}
{5149AC24-C1A7-4A4C-BE83-FF3037B99E4C} = {CDD72541-F0ED-41FA-9C9A-5D774CEB22E4}
{CDD72541-F0ED-41FA-9C9A-5D774CEB22E4} = {21D78340-7F96-4519-983F-529077A11AE1}
{0C8ADEFB-33FB-4785-A828-93377745B2BA} = {F8CB1B1F-F935-4ECE-B3C2-CF62B5654BF2}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {158060A0-BFF9-48E6-90A9-93F52FEB5D3B}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TestRuntime>true</TestRuntime>
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-freebsd;$(NetCoreAppCurrent)-illumos;$(NetCoreAppCurrent)-solaris;$(NetCoreAppCurrent)-haiku;$(NetCoreAppCurrent)-linux;$(NetCoreAppCurrent)-osx;$(NetCoreAppCurrent)-ios;$(NetCoreAppCurrent)-tvos</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Compile Include="DisableBitTests.cs" />
<Compile Include="..\TestConfiguration.cs" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj" />
</ItemGroup>
<!--
We're testing the BinaryFormatter enablement / disablement switch, so we need to suppress any inherited behavior.
See https://github.com/dotnet/runtime/issues/87811 for more details.
This is a temporary solution and should be reverted back to the original once the necessary changes are made in
shared frameworks and SDK.
-->
<Target Name="RemoveSerializationRuntimeOption" BeforeTargets="GenerateBuildRuntimeConfigurationFiles">
<ItemGroup>
<RuntimeHostConfigurationOption Remove="System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization" />
</ItemGroup>
</Target>
</Project>
Original file line number Diff line number Diff line change
@@ -1,125 +1,40 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace System.Runtime.Serialization.Formatters.Tests
{
// When BinaryFormatter was built-in to the platform we used to activate SerializationGuard in ObjectReader.Deserialize,
// but now that it has moved to an OOB offering it no longer does.
[ConditionalClass(typeof(TestConfiguration), nameof(TestConfiguration.IsBinaryFormatterEnabled))]
public static class SerializationGuardTests
{
[Fact]
public static void BlockAssemblyLoads()
{
TryPayload(new AssemblyLoader());
}

[Fact]
public static void BlockProcessStarts()
{
TryPayload(new ProcessStarter());
}

[Fact]
public static void BlockFileWrites()
{
TryPayload(new FileWriter());
}

[Fact]
public static void BlockAsyncDodging()
{
TryPayload(new AsyncDodger());
}

private static void TryPayload(object payload)
public static void IsNoLongerActivated()
{
MemoryStream ms = new MemoryStream();
BinaryFormatter writer = new BinaryFormatter();
writer.Serialize(ms, payload);
writer.Serialize(ms, new ThrowIfDeserializationInProgress());
ms.Position = 0;

BinaryFormatter reader = new BinaryFormatter();
SerializationException se = Assert.Throws<SerializationException>(() => reader.Deserialize(ms));
Assert.IsAssignableFrom<TargetInvocationException>(se.InnerException);
}
}

[Serializable]
internal class AssemblyLoader : ISerializable
{
public AssemblyLoader() { }

public AssemblyLoader(SerializationInfo info, StreamingContext context)
{
Assembly.Load(new byte[1000]);
}

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
Assert.NotNull(reader.Deserialize(ms));
}
}

[Serializable]
internal class ProcessStarter : ISerializable
internal class ThrowIfDeserializationInProgress : ISerializable
{
public ProcessStarter() { }

private ProcessStarter(SerializationInfo info, StreamingContext context)
{
Process.Start("calc.exe");
}
private static int s_cachedSerializationSwitch;

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
}
}

[Serializable]
internal class FileWriter : ISerializable
{
public FileWriter() { }

private FileWriter(SerializationInfo info, StreamingContext context)
{
string tempPath = Path.GetTempFileName();
File.WriteAllText(tempPath, "This better not be written...");
throw new UnreachableException("Unreachable code (SerializationGuard should have kicked in)");
}

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
}
}

[Serializable]
internal class AsyncDodger : ISerializable
{
public AsyncDodger() { }

private AsyncDodger(SerializationInfo info, StreamingContext context)
{
try
{
Task t = Task.Factory.StartNew(LoadAssemblyOnBackgroundThread, TaskCreationOptions.LongRunning);
t.Wait();
}
catch (AggregateException ex)
{
throw ex.InnerException;
}
}
public ThrowIfDeserializationInProgress() { }

private void LoadAssemblyOnBackgroundThread()
private ThrowIfDeserializationInProgress(SerializationInfo info, StreamingContext context)
{
Assembly.Load(new byte[1000]);
SerializationGuard.ThrowIfDeserializationInProgress("AllowProcessCreation", ref s_cachedSerializationSwitch);
}

public void GetObjectData(SerializationInfo info, StreamingContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<ItemGroup>
<Compile Include="BinaryFormatterTestData.cs" />
<Compile Include="BinaryFormatterTests.cs" />
<Compile Include="DisableBitTests.cs" />
<Compile Include="EqualityExtensions.cs" />
<Compile Include="OptionalFieldAttributeTests.cs" />
<Compile Include="FormatterConverterTests.cs" />
Expand Down Expand Up @@ -36,6 +35,8 @@
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
<Compile Include="BinaryFormatterEventSourceTests.cs" />
<Compile Include="SerializationGuardTests.cs" />
<Compile Include="$(CommonPath)System\Runtime\Serialization\SerializationGuard.cs"
Link="Common\System\Runtime\Serialization\SerializationGuard.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
<PackageReference Include="System.Drawing.Common" Version="$(SystemDrawingCommonVersion)" />
Expand All @@ -48,8 +49,11 @@
<ProjectReference Include="$(LibrariesProjectRoot)System.DirectoryServices.Protocols\src\System.DirectoryServices.Protocols.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.DirectoryServices\src\System.DirectoryServices.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.IO.Packaging\src\System.IO.Packaging.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.Serialization.Formatters\src\System.Runtime.Serialization.Formatters.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Security.Permissions\src\System.Security.Permissions.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>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Reflection.Metadata\src\System.Reflection.Metadata.csproj" />
Expand All @@ -65,15 +69,4 @@
<Reference Include="WindowsBase" />
</ItemGroup>

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

0 comments on commit 8685859

Please sign in to comment.