Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rel/3.8] Fix serialization of exceptions by BinaryFormatter in .NET Framework #5055

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Adapter/MSTest.Engine/Assertions/AssertFailedException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public AssertFailedException(string message, Exception ex)
{
}

#if NET8_0_OR_GREATER
[Obsolete(DiagnosticId = "SYSLIB0051")]
#endif
private AssertFailedException(SerializationInfo serializationInfo, StreamingContext streamingContext)
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// 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.ComponentModel;
using System.Runtime.Serialization;

namespace Microsoft.VisualStudio.TestTools.UnitTesting;

/// <summary>
Expand Down Expand Up @@ -35,4 +38,18 @@ public AssertFailedException()
: base()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="AssertFailedException"/> class.
/// </summary>
/// <param name="info">Serialization info.</param>
/// <param name="context">Streaming context.</param>
#if NET8_0_OR_GREATER
[Obsolete(DiagnosticId = "SYSLIB0051")]
#endif
[EditorBrowsable(EditorBrowsableState.Never)]
protected AssertFailedException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// 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.ComponentModel;
using System.Runtime.Serialization;

namespace Microsoft.VisualStudio.TestTools.UnitTesting;

/// <summary>
Expand Down Expand Up @@ -35,4 +38,18 @@ public AssertInconclusiveException()
: base()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="AssertInconclusiveException"/> class.
/// </summary>
/// <param name="info">Serialization info.</param>
/// <param name="context">Streaming context.</param>
#if NET8_0_OR_GREATER
[Obsolete(DiagnosticId = "SYSLIB0051")]
#endif
[EditorBrowsable(EditorBrowsableState.Never)]
protected AssertInconclusiveException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// 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.ComponentModel;
using System.Runtime.Serialization;

namespace Microsoft.VisualStudio.TestTools.UnitTesting;

/// <summary>
Expand Down Expand Up @@ -46,4 +49,18 @@ public InternalTestFailureException()
: base()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="InternalTestFailureException"/> class.
/// </summary>
/// <param name="info">Serialization info.</param>
/// <param name="context">Streaming context.</param>
#if NET8_0_OR_GREATER
[Obsolete(DiagnosticId = "SYSLIB0051")]
#endif
[EditorBrowsable(EditorBrowsableState.Never)]
protected InternalTestFailureException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// 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.ComponentModel;
using System.Runtime.Serialization;

namespace Microsoft.VisualStudio.TestTools.UnitTesting;

/// <summary>
Expand Down Expand Up @@ -34,4 +37,18 @@ protected UnitTestAssertException(string msg)
: base(msg)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="UnitTestAssertException"/> class.
/// </summary>
/// <param name="info">Serialization info.</param>
/// <param name="context">Streaming context.</param>
#if NET8_0_OR_GREATER
[Obsolete(DiagnosticId = "SYSLIB0051")]
#endif
[EditorBrowsable(EditorBrowsableState.Never)]
protected UnitTestAssertException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
<<<<<<< HEAD

Check failure on line 2 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L2

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt(2,1): error RS0017: (NETCORE_ENGINEERING_TELEMETRY=Build) Symbol '<<<<<<< HEAD' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 2 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L2

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt(2,1): error RS0017: (NETCORE_ENGINEERING_TELEMETRY=Build) Symbol '<<<<<<< HEAD' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 2 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L2

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt(2,1): error RS0017: (NETCORE_ENGINEERING_TELEMETRY=Build) Symbol '<<<<<<< HEAD' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 2 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L2

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt(2,1): error RS0017: (NETCORE_ENGINEERING_TELEMETRY=Build) Symbol '<<<<<<< HEAD' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 2 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L2

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt(2,1): error RS0017: (NETCORE_ENGINEERING_TELEMETRY=Build) Symbol '<<<<<<< HEAD' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 2 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L2

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt(2,1): error RS0017: (NETCORE_ENGINEERING_TELEMETRY=Build) Symbol '<<<<<<< HEAD' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 2 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L2

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt(2,1): error RS0017: (NETCORE_ENGINEERING_TELEMETRY=Build) Symbol '<<<<<<< HEAD' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
abstract Microsoft.VisualStudio.TestTools.UnitTesting.ConditionBaseAttribute.IgnoreMessage.get -> string?
abstract Microsoft.VisualStudio.TestTools.UnitTesting.ConditionBaseAttribute.GroupName.get -> string!
abstract Microsoft.VisualStudio.TestTools.UnitTesting.ConditionBaseAttribute.ShouldRun.get -> bool
Expand Down Expand Up @@ -375,3 +376,9 @@
[MSTESTEXP]Microsoft.VisualStudio.TestTools.UnitTesting.RetryResult
[MSTESTEXP]Microsoft.VisualStudio.TestTools.UnitTesting.RetryResult.AddResult(Microsoft.VisualStudio.TestTools.UnitTesting.TestResult![]! testResults) -> void
[MSTESTEXP]Microsoft.VisualStudio.TestTools.UnitTesting.RetryResult.RetryResult() -> void
=======

Check failure on line 379 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L379

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt(379,1): error RS0017: (NETCORE_ENGINEERING_TELEMETRY=Build) Symbol '=======' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 379 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L379

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt(379,1): error RS0017: (NETCORE_ENGINEERING_TELEMETRY=Build) Symbol '=======' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 379 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L379

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt(379,1): error RS0017: (NETCORE_ENGINEERING_TELEMETRY=Build) Symbol '=======' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 379 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L379

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt(379,1): error RS0017: (NETCORE_ENGINEERING_TELEMETRY=Build) Symbol '=======' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 379 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L379

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt(379,1): error RS0017: (NETCORE_ENGINEERING_TELEMETRY=Build) Symbol '=======' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 379 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L379

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt(379,1): error RS0017: (NETCORE_ENGINEERING_TELEMETRY=Build) Symbol '=======' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException.AssertFailedException(System.Runtime.Serialization.SerializationInfo! info, System.Runtime.Serialization.StreamingContext context) -> void
Microsoft.VisualStudio.TestTools.UnitTesting.AssertInconclusiveException.AssertInconclusiveException(System.Runtime.Serialization.SerializationInfo! info, System.Runtime.Serialization.StreamingContext context) -> void
Microsoft.VisualStudio.TestTools.UnitTesting.InternalTestFailureException.InternalTestFailureException(System.Runtime.Serialization.SerializationInfo! info, System.Runtime.Serialization.StreamingContext context) -> void
Microsoft.VisualStudio.TestTools.UnitTesting.UnitTestAssertException.UnitTestAssertException(System.Runtime.Serialization.SerializationInfo! info, System.Runtime.Serialization.StreamingContext context) -> void
>>>>>>> Address review feedback

Check failure on line 384 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L384

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt(384,1): error RS0017: (NETCORE_ENGINEERING_TELEMETRY=Build) Symbol '>>>>>>> Address review feedback' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 384 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L384

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt(384,1): error RS0017: (NETCORE_ENGINEERING_TELEMETRY=Build) Symbol '>>>>>>> Address review feedback' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 384 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L384

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt(384,1): error RS0017: (NETCORE_ENGINEERING_TELEMETRY=Build) Symbol '>>>>>>> Address review feedback' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 384 in src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt#L384

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt(384,1): error RS0017: (NETCORE_ENGINEERING_TELEMETRY=Build) Symbol '>>>>>>> Address review feedback' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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.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();
new BinaryFormatter().Serialize(mem, ex);
mem.Position = 0;
new BinaryFormatter().Deserialize(mem);

return;
}

throw new InvalidOperationException($"The provided '{nameof(actionThatThrows)}' did not throw any exception.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="$(RepoRoot)src\TestFramework\TestFramework\TestFramework.csproj"/>
<ProjectReference Include="$(RepoRoot)src\TestFramework\TestFramework\TestFramework.csproj" />
<ProjectReference Include="$(RepoRoot)test\Utilities\TestFramework.ForTestingMSTest\TestFramework.ForTestingMSTest.csproj" />
</ItemGroup>

Expand Down
Loading