From 08e59d97e2c621a1148903f0607b11ac6507f11e Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Fri, 6 Sep 2024 15:41:48 +0200 Subject: [PATCH] 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. --- ...ystem.Runtime.Serialization.Formatters.sln | 33 ++++++ .../tests/{ => Disabled}/DisableBitTests.cs | 0 ...alization.Formatters.Disabled.Tests.csproj | 23 ++++ .../tests/SerializationGuardTests.cs | 105 ++---------------- ...time.Serialization.Formatters.Tests.csproj | 19 +--- 5 files changed, 72 insertions(+), 108 deletions(-) rename src/libraries/System.Runtime.Serialization.Formatters/tests/{ => Disabled}/DisableBitTests.cs (100%) create mode 100644 src/libraries/System.Runtime.Serialization.Formatters/tests/Disabled/System.Runtime.Serialization.Formatters.Disabled.Tests.csproj diff --git a/src/libraries/System.Runtime.Serialization.Formatters/System.Runtime.Serialization.Formatters.sln b/src/libraries/System.Runtime.Serialization.Formatters/System.Runtime.Serialization.Formatters.sln index 868141ca7b35a..1ba7307c7f880 100644 --- a/src/libraries/System.Runtime.Serialization.Formatters/System.Runtime.Serialization.Formatters.sln +++ b/src/libraries/System.Runtime.Serialization.Formatters/System.Runtime.Serialization.Formatters.sln @@ -149,6 +149,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 @@ -1584,6 +1586,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 @@ -1659,6 +1691,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} diff --git a/src/libraries/System.Runtime.Serialization.Formatters/tests/DisableBitTests.cs b/src/libraries/System.Runtime.Serialization.Formatters/tests/Disabled/DisableBitTests.cs similarity index 100% rename from src/libraries/System.Runtime.Serialization.Formatters/tests/DisableBitTests.cs rename to src/libraries/System.Runtime.Serialization.Formatters/tests/Disabled/DisableBitTests.cs diff --git a/src/libraries/System.Runtime.Serialization.Formatters/tests/Disabled/System.Runtime.Serialization.Formatters.Disabled.Tests.csproj b/src/libraries/System.Runtime.Serialization.Formatters/tests/Disabled/System.Runtime.Serialization.Formatters.Disabled.Tests.csproj new file mode 100644 index 0000000000000..8ea7a29a3930b --- /dev/null +++ b/src/libraries/System.Runtime.Serialization.Formatters/tests/Disabled/System.Runtime.Serialization.Formatters.Disabled.Tests.csproj @@ -0,0 +1,23 @@ + + + true + true + $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-freebsd;$(NetCoreAppCurrent)-illumos;$(NetCoreAppCurrent)-solaris;$(NetCoreAppCurrent)-haiku;$(NetCoreAppCurrent)-linux;$(NetCoreAppCurrent)-osx;$(NetCoreAppCurrent)-ios;$(NetCoreAppCurrent)-tvos + + + + + + + + + + + + + diff --git a/src/libraries/System.Runtime.Serialization.Formatters/tests/SerializationGuardTests.cs b/src/libraries/System.Runtime.Serialization.Formatters/tests/SerializationGuardTests.cs index 3805070b47d8e..444e0cc2f3856 100644 --- a/src/libraries/System.Runtime.Serialization.Formatters/tests/SerializationGuardTests.cs +++ b/src/libraries/System.Runtime.Serialization.Formatters/tests/SerializationGuardTests.cs @@ -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(() => reader.Deserialize(ms)); - Assert.IsAssignableFrom(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) diff --git a/src/libraries/System.Runtime.Serialization.Formatters/tests/System.Runtime.Serialization.Formatters.Tests.csproj b/src/libraries/System.Runtime.Serialization.Formatters/tests/System.Runtime.Serialization.Formatters.Tests.csproj index 03df94a257abd..96a0f784a2372 100644 --- a/src/libraries/System.Runtime.Serialization.Formatters/tests/System.Runtime.Serialization.Formatters.Tests.csproj +++ b/src/libraries/System.Runtime.Serialization.Formatters/tests/System.Runtime.Serialization.Formatters.Tests.csproj @@ -7,7 +7,6 @@ - @@ -36,6 +35,8 @@ + @@ -48,8 +49,11 @@ - + + @@ -65,15 +69,4 @@ - - - - - -