Skip to content

Commit ac22a6d

Browse files
youssef-backport-botnohwndEvangelinkYoussef1313
authored
[rel/3.8] Fix serialization of exceptions by BinaryFormatter in .NET Framework (#5055)
Co-authored-by: Jakub Jareš <[email protected]> Co-authored-by: Amaury Levé <[email protected]> Co-authored-by: Youssef Victor <[email protected]>
1 parent 51d51a8 commit ac22a6d

File tree

8 files changed

+120
-1
lines changed

8 files changed

+120
-1
lines changed

src/Adapter/MSTest.Engine/Assertions/AssertFailedException.cs

+3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public AssertFailedException(string message, Exception ex)
8282
{
8383
}
8484

85+
#if NET8_0_OR_GREATER
86+
[Obsolete(DiagnosticId = "SYSLIB0051")]
87+
#endif
8588
private AssertFailedException(SerializationInfo serializationInfo, StreamingContext streamingContext)
8689
{
8790
}

src/TestFramework/TestFramework/Exceptions/AssertFailedException.cs

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System.ComponentModel;
5+
using System.Runtime.Serialization;
6+
47
namespace Microsoft.VisualStudio.TestTools.UnitTesting;
58

69
/// <summary>
@@ -35,4 +38,18 @@ public AssertFailedException()
3538
: base()
3639
{
3740
}
41+
42+
/// <summary>
43+
/// Initializes a new instance of the <see cref="AssertFailedException"/> class.
44+
/// </summary>
45+
/// <param name="info">Serialization info.</param>
46+
/// <param name="context">Streaming context.</param>
47+
#if NET8_0_OR_GREATER
48+
[Obsolete(DiagnosticId = "SYSLIB0051")]
49+
#endif
50+
[EditorBrowsable(EditorBrowsableState.Never)]
51+
protected AssertFailedException(SerializationInfo info, StreamingContext context)
52+
: base(info, context)
53+
{
54+
}
3855
}

src/TestFramework/TestFramework/Exceptions/AssertInconclusiveException.cs

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System.ComponentModel;
5+
using System.Runtime.Serialization;
6+
47
namespace Microsoft.VisualStudio.TestTools.UnitTesting;
58

69
/// <summary>
@@ -35,4 +38,18 @@ public AssertInconclusiveException()
3538
: base()
3639
{
3740
}
41+
42+
/// <summary>
43+
/// Initializes a new instance of the <see cref="AssertInconclusiveException"/> class.
44+
/// </summary>
45+
/// <param name="info">Serialization info.</param>
46+
/// <param name="context">Streaming context.</param>
47+
#if NET8_0_OR_GREATER
48+
[Obsolete(DiagnosticId = "SYSLIB0051")]
49+
#endif
50+
[EditorBrowsable(EditorBrowsableState.Never)]
51+
protected AssertInconclusiveException(SerializationInfo info, StreamingContext context)
52+
: base(info, context)
53+
{
54+
}
3855
}

src/TestFramework/TestFramework/Exceptions/InternalTestFailureException.cs

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System.ComponentModel;
5+
using System.Runtime.Serialization;
6+
47
namespace Microsoft.VisualStudio.TestTools.UnitTesting;
58

69
/// <summary>
@@ -46,4 +49,18 @@ public InternalTestFailureException()
4649
: base()
4750
{
4851
}
52+
53+
/// <summary>
54+
/// Initializes a new instance of the <see cref="InternalTestFailureException"/> class.
55+
/// </summary>
56+
/// <param name="info">Serialization info.</param>
57+
/// <param name="context">Streaming context.</param>
58+
#if NET8_0_OR_GREATER
59+
[Obsolete(DiagnosticId = "SYSLIB0051")]
60+
#endif
61+
[EditorBrowsable(EditorBrowsableState.Never)]
62+
protected InternalTestFailureException(SerializationInfo info, StreamingContext context)
63+
: base(info, context)
64+
{
65+
}
4966
}

src/TestFramework/TestFramework/Exceptions/UnitTestAssertException.cs

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System.ComponentModel;
5+
using System.Runtime.Serialization;
6+
47
namespace Microsoft.VisualStudio.TestTools.UnitTesting;
58

69
/// <summary>
@@ -34,4 +37,18 @@ protected UnitTestAssertException(string msg)
3437
: base(msg)
3538
{
3639
}
40+
41+
/// <summary>
42+
/// Initializes a new instance of the <see cref="UnitTestAssertException"/> class.
43+
/// </summary>
44+
/// <param name="info">Serialization info.</param>
45+
/// <param name="context">Streaming context.</param>
46+
#if NET8_0_OR_GREATER
47+
[Obsolete(DiagnosticId = "SYSLIB0051")]
48+
#endif
49+
[EditorBrowsable(EditorBrowsableState.Never)]
50+
protected UnitTestAssertException(SerializationInfo info, StreamingContext context)
51+
: base(info, context)
52+
{
53+
}
3754
}

src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt

+4
Original file line numberDiff line numberDiff line change
@@ -375,3 +375,7 @@ static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsExactlyAsync<TE
375375
[MSTESTEXP]Microsoft.VisualStudio.TestTools.UnitTesting.RetryResult
376376
[MSTESTEXP]Microsoft.VisualStudio.TestTools.UnitTesting.RetryResult.AddResult(Microsoft.VisualStudio.TestTools.UnitTesting.TestResult![]! testResults) -> void
377377
[MSTESTEXP]Microsoft.VisualStudio.TestTools.UnitTesting.RetryResult.RetryResult() -> void
378+
Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException.AssertFailedException(System.Runtime.Serialization.SerializationInfo! info, System.Runtime.Serialization.StreamingContext context) -> void
379+
Microsoft.VisualStudio.TestTools.UnitTesting.AssertInconclusiveException.AssertInconclusiveException(System.Runtime.Serialization.SerializationInfo! info, System.Runtime.Serialization.StreamingContext context) -> void
380+
Microsoft.VisualStudio.TestTools.UnitTesting.InternalTestFailureException.InternalTestFailureException(System.Runtime.Serialization.SerializationInfo! info, System.Runtime.Serialization.StreamingContext context) -> void
381+
Microsoft.VisualStudio.TestTools.UnitTesting.UnitTestAssertException.UnitTestAssertException(System.Runtime.Serialization.SerializationInfo! info, System.Runtime.Serialization.StreamingContext context) -> void
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Runtime.Serialization.Formatters.Binary;
5+
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
8+
using TestFramework.ForTestingMSTest;
9+
10+
namespace Microsoft.VisualStudio.TestPlatform.TestFramework.UnitTests.Attributes;
11+
12+
[Obsolete]
13+
public sealed class BinaryFormatterExceptionSerializationTests : TestContainer
14+
{
15+
public void AssertFailedExceptionCanBeSerializedAndDeserialized()
16+
=> VerifySerialization(Assert.Fail);
17+
18+
public void AssertInconclusiveExceptionCanBeSerializedAndDeserialized()
19+
=> VerifySerialization(Assert.Inconclusive);
20+
21+
public void InternalTestFailureExceptionCanBeSerializedAndDeserialized()
22+
=> VerifySerialization(() => throw new InternalTestFailureException("Some internal error."));
23+
24+
private void VerifySerialization(Action actionThatThrows)
25+
{
26+
try
27+
{
28+
actionThatThrows();
29+
}
30+
catch (Exception ex)
31+
{
32+
// Ensure the thrown exception can be serialized and deserialized by binary formatter to keep compatibility with it,
33+
// even though it is obsoleted and removed in .NET.
34+
var mem = new MemoryStream();
35+
new BinaryFormatter().Serialize(mem, ex);
36+
mem.Position = 0;
37+
new BinaryFormatter().Deserialize(mem);
38+
39+
return;
40+
}
41+
42+
throw new InvalidOperationException($"The provided '{nameof(actionThatThrows)}' did not throw any exception.");
43+
}
44+
}

test/UnitTests/TestFramework.UnitTests/TestFramework.UnitTests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</PropertyGroup>
1515

1616
<ItemGroup>
17-
<ProjectReference Include="$(RepoRoot)src\TestFramework\TestFramework\TestFramework.csproj"/>
17+
<ProjectReference Include="$(RepoRoot)src\TestFramework\TestFramework\TestFramework.csproj" />
1818
<ProjectReference Include="$(RepoRoot)test\Utilities\TestFramework.ForTestingMSTest\TestFramework.ForTestingMSTest.csproj" />
1919
</ItemGroup>
2020

0 commit comments

Comments
 (0)