Repository navigation
BinaryFormatter obsolete as error in net7 #26559
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
196 changes: 196 additions & 0 deletions
196
src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToUseBinaryFormatter.cs
This file contains hidden or 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,196 @@ | ||
| // Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System; | ||
| using FluentAssertions; | ||
| using Microsoft.NET.TestFramework; | ||
| using Microsoft.NET.TestFramework.Assertions; | ||
| using Microsoft.NET.TestFramework.Commands; | ||
| using Microsoft.NET.TestFramework.ProjectConstruction; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace Microsoft.NET.Build.Tests | ||
| { | ||
| public class GivenThatWeWantToUseBinaryFormatter : SdkTest | ||
| { | ||
| public GivenThatWeWantToUseBinaryFormatter(ITestOutputHelper log) : base(log) | ||
| { | ||
| } | ||
|
|
||
| private const string SourceWithPragmaSuppressions = @" | ||
| using System; | ||
| using System.IO; | ||
| using System.Runtime.Serialization.Formatters.Binary; | ||
|
|
||
| #pragma warning disable SYSLIB0011 | ||
|
|
||
| namespace BinaryFormatterTests | ||
| { | ||
| public class TestClass | ||
| { | ||
| public static void Main(string[] args) | ||
| { | ||
| var formatter = new BinaryFormatter(); | ||
| var stream = new MemoryStream(); | ||
| var deserializedObj = formatter.Deserialize(stream); | ||
| } | ||
| } | ||
| }"; | ||
|
|
||
| private const string SourceWithoutPragmaSuppressions = @" | ||
| using System; | ||
| using System.IO; | ||
| using System.Runtime.Serialization.Formatters.Binary; | ||
|
|
||
| namespace BinaryFormatterTests | ||
| { | ||
| public class TestClass | ||
| { | ||
| public static void Main(string[] args) | ||
| { | ||
| var formatter = new BinaryFormatter(); | ||
| var stream = new MemoryStream(); | ||
| var deserializedObj = formatter.Deserialize(stream); | ||
| } | ||
| } | ||
| }"; | ||
|
|
||
| [Theory] | ||
| [InlineData("netcoreapp3.1")] | ||
| [InlineData("netstandard2.0")] | ||
| [InlineData("net472")] | ||
| public void It_does_not_warn_when_targeting_downlevel_frameworks(string targetFramework) | ||
| { | ||
| var testProject = new TestProject() | ||
| { | ||
| Name = "BinaryFormatterTests", | ||
| TargetFrameworks = targetFramework, | ||
| IsExe = true | ||
| }; | ||
|
|
||
| testProject.SourceFiles.Add("TestClass.cs", SourceWithoutPragmaSuppressions); | ||
|
|
||
| var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: targetFramework); | ||
| var buildCommand = new BuildCommand(testAsset, "BinaryFormatterTests"); | ||
|
|
||
| buildCommand | ||
| .Execute() | ||
| .Should() | ||
| .Pass() | ||
| .And | ||
| .NotHaveStdOutContaining("SYSLIB0011"); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("netcoreapp3.1")] | ||
| [InlineData("netstandard2.0")] | ||
| [InlineData("net472")] | ||
| [InlineData("net5.0")] | ||
| [InlineData("net6.0")] | ||
| [InlineData("net7.0")] | ||
| public void It_does_not_warn_on_any_framework_when_using_pragma_suppressions(string targetFramework) | ||
| { | ||
| var testProject = new TestProject() | ||
| { | ||
| Name = "BinaryFormatterTests", | ||
| TargetFrameworks = targetFramework, | ||
| IsExe = true | ||
| }; | ||
|
|
||
| testProject.SourceFiles.Add("TestClass.cs", SourceWithPragmaSuppressions); | ||
|
|
||
| var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: targetFramework); | ||
| var buildCommand = new BuildCommand(testAsset, "BinaryFormatterTests"); | ||
|
|
||
| buildCommand | ||
| .Execute() | ||
| .Should() | ||
| .Pass() | ||
| .And | ||
| .NotHaveStdOutContaining("warning SYSLIB0011") | ||
| .And | ||
| .NotHaveStdOutContaining("error SYSLIB0011"); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("net5.0")] | ||
| [InlineData("net6.0")] | ||
| public void It_warns_when_targeting_certain_frameworks_and_not_using_pragma_suppressions(string targetFramework) | ||
| { | ||
| var testProject = new TestProject() | ||
| { | ||
| Name = "BinaryFormatterTests", | ||
| TargetFrameworks = targetFramework, | ||
| IsExe = true | ||
| }; | ||
|
|
||
| testProject.SourceFiles.Add("TestClass.cs", SourceWithoutPragmaSuppressions); | ||
|
|
||
| var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: targetFramework); | ||
| var buildCommand = new BuildCommand(testAsset, "BinaryFormatterTests"); | ||
|
|
||
| buildCommand | ||
| .Execute() | ||
| .Should() | ||
| .Pass() | ||
| .And | ||
| .HaveStdOutContaining("warning SYSLIB0011") | ||
| .And | ||
| .NotHaveStdOutContaining("error SYSLIB0011"); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("net7.0")] | ||
| public void It_errors_when_targeting_certain_frameworks_and_not_using_pragma_suppressions(string targetFramework) | ||
| { | ||
| var testProject = new TestProject() | ||
| { | ||
| Name = "BinaryFormatterTests", | ||
| TargetFrameworks = targetFramework, | ||
| IsExe = true | ||
| }; | ||
|
|
||
| testProject.SourceFiles.Add("TestClass.cs", SourceWithoutPragmaSuppressions); | ||
|
|
||
| var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: targetFramework); | ||
| var buildCommand = new BuildCommand(testAsset, "BinaryFormatterTests"); | ||
|
|
||
| buildCommand | ||
| .Execute() | ||
| .Should() | ||
| .Fail() | ||
| .And | ||
| .NotHaveStdOutContaining("warning SYSLIB0011") | ||
| .And | ||
| .HaveStdOutContaining("error SYSLIB0011"); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("net7.0")] | ||
| public void It_allows_downgrading_errors_to_warnings_via_project_config(string targetFramework) | ||
| { | ||
| var testProject = new TestProject() | ||
| { | ||
| Name = "BinaryFormatterTests", | ||
| TargetFrameworks = targetFramework, | ||
| IsExe = true | ||
| }; | ||
|
|
||
| testProject.SourceFiles.Add("TestClass.cs", SourceWithoutPragmaSuppressions); | ||
| testProject.AdditionalProperties["EnableUnsafeBinaryFormatterSerialization"] = "true"; | ||
|
|
||
| var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: targetFramework); | ||
| var buildCommand = new BuildCommand(testAsset, "BinaryFormatterTests"); | ||
|
|
||
| buildCommand | ||
| .Execute() | ||
| .Should() | ||
| .Pass() | ||
| .And | ||
| .HaveStdOutContaining("warning SYSLIB0011") | ||
| .And | ||
| .NotHaveStdOutContaining("error SYSLIB0011"); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.