Skip to content

Unit tests should re-enable BinaryFormatter for compat testing #84437

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

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@

<!-- Warnings that should be disabled in our test projects. -->
<PropertyGroup Condition="'$(IsTestProject)' == 'true' or '$(IsTestSupportProject)' == 'true' or '$(IsPublishedAppTestProject)' == 'true'">
<!-- we need to re-enable BinaryFormatter within test projects since some tests exercise these code paths to ensure compat -->
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
<!-- don't warn on usage of BinaryFormatter from test projects -->
<NoWarn>$(NoWarn);SYSLIB0011</NoWarn>
<!-- don't warn about unnecessary trim warning suppressions. can be removed with preview 6. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ public static void DisabledAlwaysInBrowser()
Assert.Contains(MoreInfoUrl, ex.Message, StringComparison.Ordinal); // error message should link to the more info URL
}

[ConditionalFact(nameof(ShouldRunFullFeatureSwitchEnablementChecks))]
public static void EnabledThroughFeatureSwitch()
{
RemoteInvokeOptions options = new RemoteInvokeOptions();
options.RuntimeConfigurationOptions[EnableBinaryFormatterSwitchName] = bool.TrueString;

RemoteExecutor.Invoke(() =>
{
// Test serialization

MemoryStream ms = new MemoryStream();
new BinaryFormatter().Serialize(ms, "A string to serialize.");

// Test round-trippability

ms.Position = 0;
object roundTripped = new BinaryFormatter().Deserialize(ms);
Assert.Equal("A string to serialize.", roundTripped);
}, options).Dispose();
}

[ConditionalFact(nameof(ShouldRunFullFeatureSwitchEnablementChecks))]
public static void DisabledThroughFeatureSwitch()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-freebsd;$(NetCoreAppCurrent)-illumos;$(NetCoreAppCurrent)-solaris;$(NetCoreAppCurrent)-linux;$(NetCoreAppCurrent)-osx;$(NetCoreAppCurrent)-ios;$(NetCoreAppCurrent)-tvos;net48</TargetFrameworks>
</PropertyGroup>
<PropertyGroup>
<!--
We're testing the BinaryFormatter enablement / disablement switch, so we need to suppress any inherited behavior.
The specific combination below accomplishes this. Normal apps should *NOT* set this combination of switches and
should instead set the switches documented at:
https://learn.microsoft.com/dotnet/core/compatibility/core-libraries/7.0/binaryformatter-apis-produce-errors#recommended-action
-->
<_ProjectTypeRequiresBinaryFormatter>true</_ProjectTypeRequiresBinaryFormatter>
<EnableUnsafeBinaryFormatterSerialization><!-- intentionally left blank --></EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>
<ItemGroup>
<Compile Include="BinaryFormatterTestData.cs" />
<Compile Include="BinaryFormatterTests.cs" />
Expand Down