From 6bf6651bbe4ed5d3d19b6a70b3c8b4f64d157917 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Fri, 21 Feb 2025 17:26:33 +0100 Subject: [PATCH] Remove BinaryFormatter tests (#5097) --- .../Exceptions/AssertFailedException.cs | 3 + .../Exceptions/AssertInconclusiveException.cs | 3 + .../InternalTestFailureException.cs | 3 + .../Exceptions/UnitTestAssertException.cs | 3 + ...aryFormatterExceptionSerializationTests.cs | 69 ------------------- 5 files changed, 12 insertions(+), 69 deletions(-) delete mode 100644 test/UnitTests/TestFramework.UnitTests/Exceptions/BinaryFormatterExceptionSerializationTests.cs diff --git a/src/TestFramework/TestFramework/Exceptions/AssertFailedException.cs b/src/TestFramework/TestFramework/Exceptions/AssertFailedException.cs index b2c5e7cdd6..bff933dbd6 100644 --- a/src/TestFramework/TestFramework/Exceptions/AssertFailedException.cs +++ b/src/TestFramework/TestFramework/Exceptions/AssertFailedException.cs @@ -51,5 +51,8 @@ public AssertFailedException() protected AssertFailedException(SerializationInfo info, StreamingContext context) : base(info, context) { + // Do not remove this as unused, it is used by BinaryFormatter when communicating between tested VisualStudio instance, + // and the UI testing framework that tests it. Don't attempt testing this in the repository using BinaryFormatter will trigger + // many compliance issues. } } diff --git a/src/TestFramework/TestFramework/Exceptions/AssertInconclusiveException.cs b/src/TestFramework/TestFramework/Exceptions/AssertInconclusiveException.cs index a0ab122863..b6734d5e38 100644 --- a/src/TestFramework/TestFramework/Exceptions/AssertInconclusiveException.cs +++ b/src/TestFramework/TestFramework/Exceptions/AssertInconclusiveException.cs @@ -51,5 +51,8 @@ public AssertInconclusiveException() protected AssertInconclusiveException(SerializationInfo info, StreamingContext context) : base(info, context) { + // Do not remove this as unused, it is used by BinaryFormatter when communicating between tested VisualStudio instance, + // and the UI testing framework that tests it. Don't attempt testing this in the repository using BinaryFormatter will trigger + // many compliance issues. } } diff --git a/src/TestFramework/TestFramework/Exceptions/InternalTestFailureException.cs b/src/TestFramework/TestFramework/Exceptions/InternalTestFailureException.cs index 41e1d43342..59786bce46 100644 --- a/src/TestFramework/TestFramework/Exceptions/InternalTestFailureException.cs +++ b/src/TestFramework/TestFramework/Exceptions/InternalTestFailureException.cs @@ -62,5 +62,8 @@ public InternalTestFailureException() protected InternalTestFailureException(SerializationInfo info, StreamingContext context) : base(info, context) { + // Do not remove this as unused, it is used by BinaryFormatter when communicating between tested VisualStudio instance, + // and the UI testing framework that tests it. Don't attempt testing this in the repository using BinaryFormatter will trigger + // many compliance issues. } } diff --git a/src/TestFramework/TestFramework/Exceptions/UnitTestAssertException.cs b/src/TestFramework/TestFramework/Exceptions/UnitTestAssertException.cs index d76cea2d92..37e2a86510 100644 --- a/src/TestFramework/TestFramework/Exceptions/UnitTestAssertException.cs +++ b/src/TestFramework/TestFramework/Exceptions/UnitTestAssertException.cs @@ -50,5 +50,8 @@ protected UnitTestAssertException(string msg) protected UnitTestAssertException(SerializationInfo info, StreamingContext context) : base(info, context) { + // Do not remove this as unused, it is used by BinaryFormatter when communicating between tested VisualStudio instance, + // and the UI testing framework that tests it. Don't attempt testing this in the repository using BinaryFormatter will trigger + // many compliance issues. } } diff --git a/test/UnitTests/TestFramework.UnitTests/Exceptions/BinaryFormatterExceptionSerializationTests.cs b/test/UnitTests/TestFramework.UnitTests/Exceptions/BinaryFormatterExceptionSerializationTests.cs deleted file mode 100644 index 5f1c9ff7e4..0000000000 --- a/test/UnitTests/TestFramework.UnitTests/Exceptions/BinaryFormatterExceptionSerializationTests.cs +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System.Runtime.Serialization; -using System.Runtime.Serialization.Formatters.Binary; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -using TestFramework.ForTestingMSTest; - -namespace Microsoft.VisualStudio.TestPlatform.TestFramework.UnitTests.Attributes; - -[Obsolete] -public sealed class BinaryFormatterExceptionSerializationTests : TestContainer -{ - public void AssertFailedExceptionCanBeSerializedAndDeserialized() - => VerifySerialization(Assert.Fail); - - public void AssertInconclusiveExceptionCanBeSerializedAndDeserialized() - => VerifySerialization(Assert.Inconclusive); - - public void InternalTestFailureExceptionCanBeSerializedAndDeserialized() - => VerifySerialization(() => throw new InternalTestFailureException("Some internal error.")); - - private void VerifySerialization(Action actionThatThrows) - { - try - { - actionThatThrows(); - } - catch (Exception ex) - { - // Ensure the thrown exception can be serialized and deserialized by binary formatter to keep compatibility with it, - // even though it is obsoleted and removed in .NET. - var mem = new MemoryStream(); - var formatter = new BinaryFormatter - { - Binder = new FormatterBinder(ex.GetType()), - }; - formatter.Serialize(mem, ex); - mem.Position = 0; - string str = Encoding.UTF8.GetString(mem.GetBuffer(), 0, (int)mem.Length); - Assert.IsNotNull(str); - var deserializedException = (Exception)formatter.Deserialize(mem); - - Assert.AreEqual(ex.Message, deserializedException.Message); - - return; - } - - throw new InvalidOperationException($"The provided '{nameof(actionThatThrows)}' did not throw any exception."); - } - - /// - /// This is for compliance, usage of BinaryFormatter without binder is not allowed. - /// - private class FormatterBinder : SerializationBinder - { - private readonly Type _type; - - public FormatterBinder(Type type) - => _type = type; - - public override Type BindToType(string assemblyName, string typeName) - => assemblyName == _type.Assembly.ToString() && typeName == _type.FullName - ? _type - : throw new InvalidOperationException(); - } -}