forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d72c8b2
commit 64c3770
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/libraries/System.Runtime.Serialization.Formatters/tests/DisableBitTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.IO; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using Microsoft.DotNet.RemoteExecutor; | ||
using Xunit; | ||
|
||
namespace System.Runtime.Serialization.Formatters.Tests | ||
{ | ||
public static class DisableBitTests | ||
{ | ||
// these tests only make sense on platforms with both SecureAppContext and RemoteExecutor support | ||
public static bool ShouldRunTests => !PlatformDetection.IsNetFramework && RemoteExecutor.IsSupported; | ||
|
||
private const string EnableBinaryFormatterSwitchName = "System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization"; | ||
private const string MoreInfoUrl = "https://aka.ms/binaryformatter"; | ||
|
||
[ConditionalFact(nameof(ShouldRunTests))] | ||
public static void DisabledThroughAppContext() | ||
{ | ||
RemoteExecutor.Invoke(() => | ||
{ | ||
AppContext.SetSwitch(EnableBinaryFormatterSwitchName, isEnabled: false); | ||
// First, test serialization | ||
MemoryStream ms = new MemoryStream(); | ||
BinaryFormatter bf = new BinaryFormatter(); | ||
var ex = Assert.Throws<NotSupportedException>(() => bf.Serialize(ms, "A string to serialize.")); | ||
Assert.Contains(MoreInfoUrl, ex.Message, StringComparison.Ordinal); // error message should link to the more info URL | ||
// Then test deserialization | ||
ex = Assert.Throws<NotSupportedException>(() => bf.Deserialize(ms)); | ||
Assert.Contains(MoreInfoUrl, ex.Message, StringComparison.Ordinal); // error message should link to the more info URL | ||
}).Dispose(); | ||
} | ||
|
||
[ConditionalFact(nameof(ShouldRunTests))] | ||
public static void DisabledThroughSecureAppContext_CannotOverride() | ||
{ | ||
RemoteInvokeOptions options = new RemoteInvokeOptions(); | ||
options.RuntimeConfigurationOptions[EnableBinaryFormatterSwitchName] = bool.FalseString; | ||
|
||
RemoteExecutor.Invoke(() => | ||
{ | ||
AppContext.SetSwitch(EnableBinaryFormatterSwitchName, isEnabled: true); // shouldn't override SecureAppContext | ||
// First, test serialization | ||
MemoryStream ms = new MemoryStream(); | ||
BinaryFormatter bf = new BinaryFormatter(); | ||
var ex = Assert.Throws<NotSupportedException>(() => bf.Serialize(ms, "A string to serialize.")); | ||
Assert.Contains(MoreInfoUrl, ex.Message, StringComparison.Ordinal); // error message should link to the more info URL | ||
// Then test deserialization | ||
ex = Assert.Throws<NotSupportedException>(() => bf.Deserialize(ms)); | ||
Assert.Contains(MoreInfoUrl, ex.Message, StringComparison.Ordinal); // error message should link to the more info URL | ||
}, options).Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters