зеркало из https://github.com/dotnet/winforms.git
Added more clipboard roundtrip tests (#12167)
* Added round trip tests for binary formatted payload under restricted clipboard formats * Removed verification is BinaryFormatter is on or off from the switch as this behavior is now shipped. * Removed CollectionDefinition attribute from a test class
This commit is contained in:
Родитель
ed603422e7
Коммит
facab3e935
|
@ -31,29 +31,4 @@ public readonly ref struct BinaryFormatterScope
|
|||
Monitor.Exit(typeof(BinaryFormatterScope));
|
||||
}
|
||||
}
|
||||
|
||||
static BinaryFormatterScope()
|
||||
{
|
||||
// Need to explicitly set the switch to whatever the default is as its default value is in transition.
|
||||
|
||||
#pragma warning disable SYSLIB0011 // Type or member is obsolete
|
||||
BinaryFormatter formatter = new();
|
||||
#pragma warning restore SYSLIB0011
|
||||
try
|
||||
{
|
||||
formatter.Serialize(null!, null!);
|
||||
}
|
||||
catch (NotSupportedException)
|
||||
{
|
||||
AppContext.SetSwitch(AppContextSwitchNames.EnableUnsafeBinaryFormatterSerialization, false);
|
||||
return;
|
||||
}
|
||||
catch (ArgumentNullException)
|
||||
{
|
||||
AppContext.SetSwitch(AppContextSwitchNames.EnableUnsafeBinaryFormatterSerialization, true);
|
||||
return;
|
||||
}
|
||||
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -194,16 +194,16 @@ Namespace Microsoft.VisualBasic.MyServices
|
|||
''' <summary>
|
||||
''' Saves the passed in String to the clipboard.
|
||||
''' </summary>
|
||||
''' <param name="text">The String to save.</param>
|
||||
''' <param name="text">The <see cref="String"/> to save.</param>
|
||||
Public Sub SetText(text As String)
|
||||
Clipboard.SetText(text)
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' Saves the passed in String to the clipboard in the passed in format.
|
||||
''' Saves the passed in <see cref="String" /> to the clipboard in the passed in <paramref name="format"/>.
|
||||
''' </summary>
|
||||
''' <param name="text">The String to save.</param>
|
||||
''' <param name="format">The format in which to save the String.</param>
|
||||
''' <param name="text">The <see cref="String" /> to save.</param>
|
||||
''' <param name="format">The format in which to save the <see cref="String" /> .</param>
|
||||
Public Sub SetText(text As String, format As TextDataFormat)
|
||||
Clipboard.SetText(text, format)
|
||||
End Sub
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System.Drawing;
|
||||
using Microsoft.VisualBasic.Devices;
|
||||
using DataFormats = System.Windows.Forms.DataFormats;
|
||||
|
@ -11,7 +13,6 @@ namespace Microsoft.VisualBasic.MyServices.Tests;
|
|||
// Each registered Clipboard format is an OS singleton,
|
||||
// and we should not run this test at the same time as other tests using the same format.
|
||||
[Collection("Sequential")]
|
||||
[CollectionDefinition("Sequential", DisableParallelization = true)]
|
||||
public class ClipboardProxyTests
|
||||
{
|
||||
private static string GetUniqueText() => Guid.NewGuid().ToString("D");
|
||||
|
@ -55,7 +56,7 @@ public class ClipboardProxyTests
|
|||
var clipboard = new Computer().Clipboard;
|
||||
object data = GetUniqueText();
|
||||
clipboard.SetDataObject(new System.Windows.Forms.DataObject(data));
|
||||
clipboard.GetDataObject().GetData(DataFormats.UnicodeText).Should().Be(System.Windows.Forms.Clipboard.GetDataObject().GetData(DataFormats.UnicodeText));
|
||||
clipboard.GetDataObject().GetData(DataFormats.UnicodeText).Should().Be(System.Windows.Forms.Clipboard.GetDataObject()?.GetData(DataFormats.UnicodeText));
|
||||
}
|
||||
|
||||
[WinFormsFact]
|
||||
|
|
|
@ -164,7 +164,7 @@ internal static class SerializationRecordExtensions
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// Tries to get this object as a <see cref="SizeF"/>.
|
||||
/// </summary>
|
||||
public static bool TryGetSizeF(this SerializationRecord record, [NotNullWhen(true)] out object? value)
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
SYSLIB0051: Formatters obsolete
|
||||
SYSLIB5005: System.Formats.Nrbf is experimental
|
||||
-->
|
||||
<NoWarn>$(NoWarn);CS1574;CS1580;CA1036;CA1051;CA1066;SYSLIB0011;SYSLIB0050;SYSLIB0051;SYSLIB5005;xUnit1013</NoWarn>
|
||||
<NoWarn>$(NoWarn);CS1574;CS1580;CA1036;CA1051;CA1066;SYSLIB0011;SYSLIB0050;SYSLIB0051;SYSLIB5005;xUnit1013;xUnit1045</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -3,10 +3,10 @@
|
|||
|
||||
using System.Collections;
|
||||
using System.Drawing;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Private.Windows.Core.BinaryFormat;
|
||||
using FormatTests.Common;
|
||||
using System.Formats.Nrbf;
|
||||
using System.Private.Windows.Core.BinaryFormat;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using FormatTests.Common;
|
||||
using System.Windows.Forms.Nrbf;
|
||||
|
||||
namespace FormatTests.FormattedObject;
|
||||
|
@ -78,7 +78,7 @@ public class ListTests : SerializationTest
|
|||
|
||||
[Theory]
|
||||
[MemberData(nameof(PrimitiveLists_TestData))]
|
||||
public void BinaryFormattedObjectExtensions_TryGetPrimitiveList(IList list)
|
||||
public void SerializationRecordExtensions_TryGetPrimitiveList(IList list)
|
||||
{
|
||||
SerializationRecord rootRecord = NrbfDecoder.Decode(Serialize(list));
|
||||
rootRecord.TryGetPrimitiveList(out object? deserialized).Should().BeTrue();
|
||||
|
|
|
@ -106,7 +106,7 @@ public class PrimitiveTypeTests : SerializationTest
|
|||
[Theory]
|
||||
[MemberData(nameof(Primitive_Data))]
|
||||
[MemberData(nameof(Primitive_ExtendedData))]
|
||||
public void BinaryFormattedObject_ReadPrimitive(object value)
|
||||
public void SerializationRecord_ReadPrimitive(object value)
|
||||
{
|
||||
SerializationRecord rootRecord = NrbfDecoder.Decode(Serialize(value));
|
||||
rootRecord.TryGetPrimitiveType(out object? deserialized).Should().BeTrue();
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
SYSLIB0051: This API supports obsolete formatter-based serialization
|
||||
SYSLIB5005: System.Formats.Nrbf is experimental
|
||||
-->
|
||||
<NoWarn>$(NoWarn),1573,1591,1712,WFDEV001,SYSLIB0050,SYSLIB0051,SYSLIB5005</NoWarn>
|
||||
<NoWarn>$(NoWarn),1573,1591,1712,WFDEV001,SYSLIB0050,SYSLIB0051,SYSLIB5005,xUnit1045</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
using System.Collections;
|
||||
using System.Drawing;
|
||||
using System.Runtime.Serialization;
|
||||
using Utilities = System.Windows.Forms.DataObject.Composition.BinaryFormatUtilities;
|
||||
|
||||
namespace System.Windows.Forms.Tests;
|
||||
|
@ -23,13 +24,19 @@ public partial class BinaryFormatUtilitiesTests : IDisposable
|
|||
return ReadObjectFromStream();
|
||||
}
|
||||
|
||||
private void WriteObjectToStream(object value) =>
|
||||
Utilities.WriteObjectToStream(_stream, value, restrictSerialization: false);
|
||||
private object? RoundTripObject_RestrictedFormat(object value)
|
||||
{
|
||||
Utilities.WriteObjectToStream(_stream, value, restrictSerialization: true);
|
||||
return ReadObjectFromStream(restrictDeserialization: true);
|
||||
}
|
||||
|
||||
private object? ReadObjectFromStream()
|
||||
private void WriteObjectToStream(object value, bool restrictSerialization = false) =>
|
||||
Utilities.WriteObjectToStream(_stream, value, restrictSerialization);
|
||||
|
||||
private object? ReadObjectFromStream(bool restrictDeserialization = false)
|
||||
{
|
||||
_stream.Position = 0;
|
||||
return Utilities.ReadObjectFromStream(_stream, restrictDeserialization: false);
|
||||
return Utilities.ReadObjectFromStream(_stream, restrictDeserialization);
|
||||
}
|
||||
|
||||
// Primitive types as defined by the NRBF spec.
|
||||
|
@ -169,11 +176,22 @@ public partial class BinaryFormatUtilitiesTests : IDisposable
|
|||
public void BinaryFormatUtilities_RoundTrip_Simple(object value) =>
|
||||
RoundTripObject(value).Should().Be(value);
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(PrimitiveObjects_TheoryData))]
|
||||
[MemberData(nameof(KnownObjects_TheoryData))]
|
||||
public void BinaryFormatUtilities_RoundTripRestrictedFormat_Simple(object value) =>
|
||||
RoundTripObject_RestrictedFormat(value).Should().Be(value);
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(NotSupportedException_TestData))]
|
||||
public void BinaryFormatUtilities_RoundTrip_NotSupportedException(NotSupportedException value) =>
|
||||
RoundTripObject(value).Should().BeEquivalentTo(value);
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(NotSupportedException_TestData))]
|
||||
public void BinaryFormatUtilities_RoundTripRestrictedFormat_NotSupportedException(NotSupportedException value) =>
|
||||
RoundTripObject_RestrictedFormat(value).Should().BeEquivalentTo(value);
|
||||
|
||||
[Fact]
|
||||
public void BinaryFormatUtilities_RoundTrip_NotSupportedException_DataLoss()
|
||||
{
|
||||
|
@ -181,11 +199,23 @@ public partial class BinaryFormatUtilitiesTests : IDisposable
|
|||
RoundTripObject(value).Should().BeEquivalentTo(new NotSupportedException("Error message", innerException: null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BinaryFormatUtilities_RoundTripRestrictedFormat_NotSupportedException_DataLoss()
|
||||
{
|
||||
NotSupportedException value = new("Error message", new ArgumentException());
|
||||
RoundTripObject_RestrictedFormat(value).Should().BeEquivalentTo(new NotSupportedException("Error message", innerException: null));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(PrimitiveListObjects_TheoryData))]
|
||||
public void BinaryFormatUtilities_RoundTrip_PrimitiveList(IList value) =>
|
||||
RoundTripObject(value).Should().BeEquivalentTo(value);
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(PrimitiveListObjects_TheoryData))]
|
||||
public void BinaryFormatUtilities_RoundTripRestrictedFormat_PrimitiveList(IList value) =>
|
||||
RoundTripObject_RestrictedFormat(value).Should().BeEquivalentTo(value);
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(PrimitiveArrayObjects_TheoryData))]
|
||||
public void BinaryFormatUtilities_RoundTrip_PrimitiveArray(Array value) =>
|
||||
|
@ -196,11 +226,21 @@ public partial class BinaryFormatUtilitiesTests : IDisposable
|
|||
public void BinaryFormatUtilities_RoundTrip_PrimitiveArrayList(ArrayList value) =>
|
||||
RoundTripObject(value).Should().BeEquivalentTo(value);
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(PrimitiveArrayListObjects_TheoryData))]
|
||||
public void BinaryFormatUtilities_RoundTripRestrictedFormat_PrimitiveArrayList(ArrayList value) =>
|
||||
RoundTripObject_RestrictedFormat(value).Should().BeEquivalentTo(value);
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(PrimitiveTypeHashtables_TheoryData))]
|
||||
public void BinaryFormatUtilities_RoundTrip_PrimitiveHashtable(Hashtable value) =>
|
||||
RoundTripObject(value).Should().BeEquivalentTo(value);
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(PrimitiveTypeHashtables_TheoryData))]
|
||||
public void BinaryFormatUtilities_RoundTripRestrictedFormat_PrimitiveHashtable(Hashtable value) =>
|
||||
RoundTripObject_RestrictedFormat(value).Should().BeEquivalentTo(value);
|
||||
|
||||
[Fact]
|
||||
public void BinaryFormatUtilities_RoundTrip_ImageList()
|
||||
{
|
||||
|
@ -216,6 +256,21 @@ public partial class BinaryFormatUtilitiesTests : IDisposable
|
|||
newList.Images.Count.Should().Be(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BinaryFormatUtilities_RoundTripRestrictedFormat_ImageList()
|
||||
{
|
||||
using ImageList sourceList = new();
|
||||
using Bitmap image = new(10, 10);
|
||||
sourceList.Images.Add(image);
|
||||
using ImageListStreamer value = sourceList.ImageStream!;
|
||||
|
||||
var result = RoundTripObject_RestrictedFormat(value).Should().BeOfType<ImageListStreamer>().Which;
|
||||
|
||||
using ImageList newList = new();
|
||||
newList.ImageStream = result;
|
||||
newList.Images.Count.Should().Be(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BinaryFormatUtilities_RoundTrip_Bitmap()
|
||||
{
|
||||
|
@ -223,6 +278,13 @@ public partial class BinaryFormatUtilitiesTests : IDisposable
|
|||
RoundTripObject(value).Should().BeOfType<Bitmap>().Subject.Size.Should().Be(value.Size);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BinaryFormatUtilities_RoundTripRestrictedFormat_Bitmap()
|
||||
{
|
||||
using Bitmap value = new(10, 10);
|
||||
RoundTripObject_RestrictedFormat(value).Should().BeOfType<Bitmap>().Subject.Size.Should().Be(value.Size);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Lists_UnsupportedTestData))]
|
||||
public void BinaryFormatUtilities_RoundTrip_Unsupported(IList value)
|
||||
|
@ -237,4 +299,14 @@ public partial class BinaryFormatUtilitiesTests : IDisposable
|
|||
|
||||
((Action)(() => ReadObjectFromStream())).Should().Throw<NotSupportedException>();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Lists_UnsupportedTestData))]
|
||||
public void BinaryFormatUtilities_RoundTripRestrictedFormat_Unsupported(IList value)
|
||||
{
|
||||
((Action)(() => WriteObjectToStream(value, restrictSerialization: true))).Should().Throw<NotSupportedException>();
|
||||
|
||||
using BinaryFormatterScope scope = new(enable: true);
|
||||
((Action)(() => WriteObjectToStream(value, restrictSerialization: true))).Should().Throw<SerializationException>();
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче