diff --git a/eng/Versions.props b/eng/Versions.props
index 5b79c203f59..6df39770b30 100644
--- a/eng/Versions.props
+++ b/eng/Versions.props
@@ -34,6 +34,7 @@
9.0.0-preview.7.24376.15
9.0.0-preview.7.24379.1
9.0.0-preview.7.24376.15
+ 9.0.0-preview.7.24376.15
9.0.0-preview.7.24376.15
9.0.0-preview.7.24376.15
4.6.0-preview4.19176.11
diff --git a/eng/WpfArcadeSdk/tools/TestProjects.targets b/eng/WpfArcadeSdk/tools/TestProjects.targets
index 2c8fd22bf80..8fdc440ae42 100644
--- a/eng/WpfArcadeSdk/tools/TestProjects.targets
+++ b/eng/WpfArcadeSdk/tools/TestProjects.targets
@@ -29,7 +29,6 @@
-
diff --git a/global.json b/global.json
index e1127c3c06f..94ae565be4d 100644
--- a/global.json
+++ b/global.json
@@ -1,6 +1,6 @@
{
"tools": {
- "dotnet": "9.0.100-preview.5.24307.3",
+ "dotnet": "9.0.100-preview.6.24328.19",
"runtimes": {
"dotnet": [
"2.1.7",
@@ -16,7 +16,7 @@
"Microsoft.DotNet.Helix.Sdk": "9.0.0-beta.24368.9"
},
"sdk": {
- "version": "9.0.100-preview.5.24307.3"
+ "version": "9.0.100-preview.6.24328.19"
},
"native-tools": {
"strawberry-perl": "5.38.0.1",
diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/PresentationCore.csproj b/src/Microsoft.DotNet.Wpf/src/PresentationCore/PresentationCore.csproj
index 4572d7307cd..036da859299 100644
--- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/PresentationCore.csproj
+++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/PresentationCore.csproj
@@ -338,19 +338,15 @@
-
-
-
-
-
+
@@ -366,19 +362,14 @@
-
-
-
-
-
@@ -1461,6 +1452,7 @@
+
@@ -1476,10 +1468,9 @@
-
-
+
diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/BinaryFormat/ArrayInfo.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/BinaryFormat/ArrayInfo.cs
index f041cebb1ee..fea38896b8a 100644
--- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/BinaryFormat/ArrayInfo.cs
+++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/BinaryFormat/ArrayInfo.cs
@@ -26,13 +26,6 @@ public ArrayInfo(Id objectId, Count length)
ObjectId = objectId;
}
- public static ArrayInfo Parse(BinaryReader reader, out Count length)
- {
- ArrayInfo arrayInfo = new(reader.ReadInt32(), reader.ReadInt32());
- length = arrayInfo.Length;
- return arrayInfo;
- }
-
public readonly void Write(BinaryWriter writer)
{
writer.Write(ObjectId);
diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/BinaryFormat/ArraySingleObject.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/BinaryFormat/ArraySingleObject.cs
index 67212d7b576..ccb65ec4d24 100644
--- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/BinaryFormat/ArraySingleObject.cs
+++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/BinaryFormat/ArraySingleObject.cs
@@ -16,7 +16,7 @@ namespace System.Windows
///
///
///
- internal sealed class ArraySingleObject : ArrayRecord, IRecord
+ internal sealed class ArraySingleObject : ArrayRecord
{
public static RecordType RecordType => RecordType.ArraySingleObject;
@@ -24,18 +24,6 @@ public ArraySingleObject(ArrayInfo arrayInfo, IReadOnlyList
-
-
diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/DataStreams.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/DataStreams.cs
index dc681cc17b6..fb38fea8b64 100644
--- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/DataStreams.cs
+++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/DataStreams.cs
@@ -12,6 +12,7 @@
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
+using System.Formats.Nrbf;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
@@ -254,9 +255,11 @@ private void LoadSubStreams(UIElement element, ArrayList subStreams)
object newValue = null;
if (subStream._data != null)
{
+ using MemoryStream dataStream = new(subStream._data);
+
try
{
- new BinaryFormattedObject(new MemoryStream(subStream._data),leaveOpen: true).TryGetFrameworkObject(out object val);
+ NrbfDecoder.Decode(dataStream, leaveOpen: true).TryGetFrameworkObject(out object val);
newValue = val;
}
catch (Exception ex) when (!ex.IsCriticalException())
@@ -268,8 +271,9 @@ private void LoadSubStreams(UIElement element, ArrayList subStreams)
//Using Binary formatter
if(newValue == null)
{
+ dataStream.Position = 0;
#pragma warning disable SYSLIB0011 // BinaryFormatter is obsolete
- newValue = this.Formatter.Deserialize(new MemoryStream(subStream._data));
+ newValue = this.Formatter.Deserialize(dataStream);
#pragma warning restore SYSLIB0011 // BinaryFormatter is obsolete
}
diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj
index 0b63773acd8..6e84493822b 100644
--- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj
+++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj
@@ -1406,6 +1406,7 @@
+
@@ -1431,6 +1432,7 @@
+
@@ -1449,8 +1451,6 @@
-
-
diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/ref/PresentationFramework-ref.csproj b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/ref/PresentationFramework-ref.csproj
index def41db3429..b7ea8c46323 100644
--- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/ref/PresentationFramework-ref.csproj
+++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/ref/PresentationFramework-ref.csproj
@@ -58,6 +58,7 @@
+
@@ -89,8 +90,6 @@
-
-
diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System.Xaml.csproj b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System.Xaml.csproj
index 73ce444dbb0..12c475845a1 100644
--- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System.Xaml.csproj
+++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System.Xaml.csproj
@@ -111,6 +111,7 @@
+
@@ -121,8 +122,6 @@
-
-
diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/ref/System.Xaml-ref.csproj b/src/Microsoft.DotNet.Wpf/src/System.Xaml/ref/System.Xaml-ref.csproj
index c125f185415..7498f329404 100644
--- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/ref/System.Xaml-ref.csproj
+++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/ref/System.Xaml-ref.csproj
@@ -38,6 +38,7 @@
+
@@ -48,8 +49,6 @@
-
-
diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/ArrayTests.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/ArrayTests.cs
index d188b4938df..695e7bb9ee8 100644
--- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/ArrayTests.cs
+++ b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/ArrayTests.cs
@@ -1,68 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
-using System.Collections;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Drawing;
-using System.IO;
-using FluentAssertions;
-using System.Threading;
-using System.Windows;
-using PresentationCore.Tests.TestUtilities;
-using PresentationCore.Tests.FluentAssertions;
-
namespace PresentationCore.Tests.BinaryFormat;
public class ArrayTests
{
- [Theory]
- [MemberData(nameof(ArrayInfo_ParseSuccessData))]
- public void ArrayInfo_Parse_Success(MemoryStream stream, int expectedId, int expectedLength)
- {
- using BinaryReader reader = new(stream);
- ArrayInfo info = ArrayInfo.Parse(reader, out Count length);
- info.ObjectId.Should().Be(expectedId);
- info.Length.Should().Be(expectedLength);
- length.Should().Be(expectedLength);
- }
-
- public static TheoryData ArrayInfo_ParseSuccessData => new()
- {
- { new MemoryStream(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }), 0, 0 },
- { new MemoryStream(new byte[] { 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 }), 1, 1 },
- { new MemoryStream(new byte[] { 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 }), 2, 1 },
- { new MemoryStream(new byte[] { 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0x7F }), int.MaxValue, int.MaxValue }
- };
-
- [Theory]
- [MemberData(nameof(ArrayInfo_ParseNegativeData))]
- public void ArrayInfo_Parse_Negative(MemoryStream stream, Type expectedException)
- {
- using BinaryReader reader = new(stream);
- Assert.Throws(expectedException, () => ArrayInfo.Parse(reader, out Count length));
- }
-
- public static TheoryData ArrayInfo_ParseNegativeData => new()
- {
- // Not enough data
- { new MemoryStream(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }), typeof(EndOfStreamException) },
- { new MemoryStream(new byte[] { 0x00, 0x00, 0x00 }), typeof(EndOfStreamException) },
- // Negative numbers
- { new MemoryStream(new byte[] { 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF }), typeof(ArgumentOutOfRangeException) }
- };
-
- [Theory]
- [MemberData(nameof(StringArray_Parse_Data))]
- public void StringArray_Parse(string?[] strings)
- {
- BinaryFormattedObject format = strings.SerializeAndParse();
- format.RecordCount.Should().BeGreaterThanOrEqualTo(3);
- ArraySingleString array = (ArraySingleString)format[1];
- format.GetStringValues(array, strings.Length).Should().BeEquivalentTo(strings);
- }
-
public static TheoryData StringArray_Parse_Data => new()
{
new string?[] { "one", "two" },
@@ -70,16 +12,6 @@ public void StringArray_Parse(string?[] strings)
new string?[] { "same", "same", "same" }
};
- [Theory]
- [MemberData(nameof(PrimitiveArray_Parse_Data))]
- public void PrimitiveArray_Parse(Array array)
- {
- BinaryFormattedObject format = array.SerializeAndParse();
- format.RecordCount.Should().BeGreaterThanOrEqualTo(3);
- ArraySinglePrimitive arrayRecord = (ArraySinglePrimitive)format[1];
- ((IEnumerable)arrayRecord.ArrayObjects).Should().BeEquivalentTo((IEnumerable)array);
- }
-
public static TheoryData PrimitiveArray_Parse_Data => new()
{
new int[] { 1, 2, 3 },
diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/BinaryFormatWriterTests.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/BinaryFormatWriterTests.cs
index 342a28b21ee..1965b78edbd 100644
--- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/BinaryFormatWriterTests.cs
+++ b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/BinaryFormatWriterTests.cs
@@ -6,6 +6,7 @@
using System.Runtime.Serialization.Formatters.Binary;
using PresentationCore.Tests.TestUtilities;
using PresentationCore.Tests.FluentAssertions;
+using System.Formats.Nrbf;
namespace PresentationCore.Tests.BinaryFormat;
@@ -71,7 +72,7 @@ public void BinaryFormatWriter_TryWriteObject_SupportedObjects_RoundTrip(object
BinaryFormatWriter.TryWriteFrameworkObject(stream, value).Should().BeTrue();
stream.Position = 0;
- BinaryFormattedObject format = new(stream);
+ SerializationRecord format = NrbfDecoder.Decode(stream);
format.TryGetFrameworkObject(out object? deserialized).Should().BeTrue();
if (value is Hashtable hashtable)
@@ -115,4 +116,4 @@ public void BinaryFormatWriter_TryWriteObject_UnsupportedObjects_RoundTrip(objec
ListTests.Lists_UnsupportedTestData).Concat(
ListTests.ArrayLists_UnsupportedTestData).Concat(
ArrayTests.Array_UnsupportedTestData);
-}
\ No newline at end of file
+}
diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/BinaryFormattedObjectTests.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/BinaryFormattedObjectTests.cs
deleted file mode 100644
index 041168b5fff..00000000000
--- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/BinaryFormattedObjectTests.cs
+++ /dev/null
@@ -1,414 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-using System.Collections;
-using PresentationCore.Tests.TestUtilities;
-
-namespace PresentationCore.Tests.BinaryFormat;
-
-public class BinaryFormattedObjectTests
-{
- [Fact]
- public void ReadHeader()
- {
- BinaryFormattedObject format = "Hello World.".SerializeAndParse();
- SerializationHeader header = (SerializationHeader)format[0];
- header.MajorVersion.Should().Be(1);
- header.MinorVersion.Should().Be(0);
- header.RootId.Should().Be(1);
- header.HeaderId.Should().Be(-1);
- }
-
- [Theory]
- [InlineData("Hello there.")]
- [InlineData("")]
- [InlineData("Embedded\0 Null.")]
- public void ReadBinaryObjectString(string testString)
- {
- BinaryFormattedObject format = testString.SerializeAndParse();
- BinaryObjectString stringRecord = (BinaryObjectString)format[1];
- stringRecord.ObjectId.Should().Be(1);
- stringRecord.Value.Should().Be(testString);
- }
-
- [Fact]
- public void ReadEmptyHashTable()
- {
- BinaryFormattedObject format = new Hashtable().SerializeAndParse();
-
- SystemClassWithMembersAndTypes systemClass = (SystemClassWithMembersAndTypes)format[1];
- systemClass.ObjectId.Should().Be(1);
- systemClass.Name.Should().Be("System.Collections.Hashtable");
- systemClass.MemberNames.Should().BeEquivalentTo(new[]
- {
- "LoadFactor",
- "Version",
- "Comparer",
- "HashCodeProvider",
- "HashSize",
- "Keys",
- "Values"
- });
-
- systemClass.MemberTypeInfo.Should().BeEquivalentTo(new (BinaryType Type, object? Info)[]
- {
- (BinaryType.Primitive, PrimitiveType.Single),
- (BinaryType.Primitive, PrimitiveType.Int32),
- (BinaryType.SystemClass, "System.Collections.IComparer"),
- (BinaryType.SystemClass, "System.Collections.IHashCodeProvider"),
- (BinaryType.Primitive, PrimitiveType.Int32),
- (BinaryType.ObjectArray, null),
- (BinaryType.ObjectArray, null)
- });
-
- systemClass.MemberValues.Should().BeEquivalentTo(new object?[]
- {
- 0.72f,
- 0,
- ObjectNull.Instance,
- ObjectNull.Instance,
- 3,
- new MemberReference(2),
- new MemberReference(3)
- });
-
- ArraySingleObject array = (ArraySingleObject)format[2];
- array.ArrayInfo.ObjectId.Should().Be(2);
- array.ArrayInfo.Length.Should().Be(0);
-
- array = (ArraySingleObject)format[3];
- array.ArrayInfo.ObjectId.Should().Be(3);
- array.ArrayInfo.Length.Should().Be(0);
- }
-
- [Fact]
- public void ReadHashTableWithStringPair()
- {
- BinaryFormattedObject format = new Hashtable()
- {
- { "This", "That" }
- }.SerializeAndParse();
-
- SystemClassWithMembersAndTypes systemClass = (SystemClassWithMembersAndTypes)format[1];
-
- systemClass.MemberTypeInfo.Should().BeEquivalentTo(new (BinaryType Type, object? Info)[]
- {
- (BinaryType.Primitive, PrimitiveType.Single),
- (BinaryType.Primitive, PrimitiveType.Int32),
- (BinaryType.SystemClass, "System.Collections.IComparer"),
- (BinaryType.SystemClass, "System.Collections.IHashCodeProvider"),
- (BinaryType.Primitive, PrimitiveType.Int32),
- (BinaryType.ObjectArray, null),
- (BinaryType.ObjectArray, null)
- });
-
- systemClass.MemberValues.Should().BeEquivalentTo(new object?[]
- {
- 0.72f,
- 1,
- ObjectNull.Instance,
- ObjectNull.Instance,
- 3,
- new MemberReference(2),
- new MemberReference(3)
- });
-
- ArraySingleObject array = (ArraySingleObject)format[2];
- array.ArrayInfo.ObjectId.Should().Be(2);
- array.ArrayInfo.Length.Should().Be(1);
- BinaryObjectString value = (BinaryObjectString)array.ArrayObjects[0];
- value.ObjectId.Should().Be(4);
- value.Value.Should().Be("This");
-
- array = (ArraySingleObject)format[3];
- array.ArrayInfo.ObjectId.Should().Be(3);
- array.ArrayInfo.Length.Should().Be(1);
- value = (BinaryObjectString)array.ArrayObjects[0];
- value.ObjectId.Should().Be(5);
- value.Value.Should().Be("That");
- }
-
- [Fact]
- public void ReadHashTableWithRepeatedStrings()
- {
- BinaryFormattedObject format = new Hashtable()
- {
- { "This", "That" },
- { "TheOther", "This" },
- { "That", "This" }
- }.SerializeAndParse();
-
- // The collections themselves get ids first before the strings do.
- // Everything in the second array is a string reference.
- ArraySingleObject array = (ArraySingleObject)format[3];
- array.ObjectId.Should().Be(3);
- array[0].Should().BeOfType();
- array[1].Should().BeOfType();
- array[2].Should().BeOfType();
- }
-
- [Fact]
- public void ReadHashTableWithNullValues()
- {
- BinaryFormattedObject format = new Hashtable()
- {
- { "Yowza", null },
- { "Youza", null },
- { "Meeza", null }
- }.SerializeAndParse();
-
- SystemClassWithMembersAndTypes systemClass = (SystemClassWithMembersAndTypes)format[1];
-
- systemClass.MemberValues.Should().BeEquivalentTo(new object?[]
- {
- 0.72f,
- 4,
- ObjectNull.Instance,
- ObjectNull.Instance,
- 7,
- new MemberReference(2),
- new MemberReference(3)
- });
-
- ArrayRecord array = (ArrayRecord)format[(MemberReference)systemClass.MemberValues[5]];
-
- array.ArrayInfo.ObjectId.Should().Be(2);
- array.ArrayInfo.Length.Should().Be(3);
- BinaryObjectString value = (BinaryObjectString)array.ArrayObjects[0];
- value.ObjectId.Should().Be(4);
- value.Value.Should().BeOneOf("Yowza", "Youza", "Meeza");
-
- array = (ArrayRecord)format[(MemberReference)systemClass["Values"]];
- array.ArrayInfo.ObjectId.Should().Be(3);
- array.ArrayInfo.Length.Should().Be(3);
- array.ArrayObjects[0].Should().BeOfType();
- }
-
- [Fact]
- public void ReadObject()
- {
- BinaryFormattedObject format = new object().SerializeAndParse();
- format[1].Should().BeOfType();
- }
-
- [Fact]
- public void ReadStruct()
- {
- ValueTuple tuple = new(355);
- BinaryFormattedObject format = tuple.SerializeAndParse();
- format[1].Should().BeOfType();
- }
-
- [Fact]
- public void ReadSimpleSerializableObject()
- {
- BinaryFormattedObject format = new SimpleSerializableObject().SerializeAndParse();
-
- BinaryLibrary library = (BinaryLibrary)format[1];
- library.LibraryName.Should().Be(typeof(BinaryFormattedObjectTests).Assembly.FullName);
- library.LibraryId.Should().Be(2);
-
- ClassWithMembersAndTypes @class = (ClassWithMembersAndTypes)format[2];
- @class.ObjectId.Should().Be(1);
- @class.Name.Should().Be(typeof(SimpleSerializableObject).FullName);
- @class.MemberNames.Should().BeEmpty();
- @class.LibraryId.Should().Be(2);
- @class.MemberTypeInfo.Should().BeEmpty();
-
- format[3].Should().BeOfType();
- }
-
- [Fact]
- public void ReadNestedSerializableObject()
- {
- BinaryFormattedObject format = new NestedSerializableObject().SerializeAndParse();
-
- BinaryLibrary library = (BinaryLibrary)format[1];
- library.LibraryName.Should().Be(typeof(BinaryFormattedObjectTests).Assembly.FullName);
- library.LibraryId.Should().Be(2);
-
- ClassWithMembersAndTypes @class = (ClassWithMembersAndTypes)format[2];
- @class.ObjectId.Should().Be(1);
- @class.Name.Should().Be(typeof(NestedSerializableObject).FullName);
- @class.MemberNames.Should().BeEquivalentTo(new[] { "_object", "_meaning" });
- @class.LibraryId.Should().Be(2);
- @class.MemberTypeInfo.Should().BeEquivalentTo(new (BinaryType Type, object? Info)[]
- {
- (BinaryType.Class, new ClassTypeInfo(typeof(SimpleSerializableObject).FullName!, 2)),
- (BinaryType.Primitive, PrimitiveType.Int32)
- });
- @class.MemberValues.Should().BeEquivalentTo(new object?[]
- {
- new MemberReference(3),
- 42
- });
-
- @class = (ClassWithMembersAndTypes)format[3];
- @class.ObjectId.Should().Be(3);
- @class.Name.Should().Be(typeof(SimpleSerializableObject).FullName);
- @class.MemberNames.Should().BeEmpty();
- @class.LibraryId.Should().Be(2);
- @class.MemberTypeInfo.Should().BeEmpty();
-
- format[4].Should().BeOfType();
- }
-
- [Fact]
- public void ReadTwoIntObject()
- {
- BinaryFormattedObject format = new TwoIntSerializableObject().SerializeAndParse();
-
- BinaryLibrary library = (BinaryLibrary)format[1];
- library.LibraryName.Should().Be(typeof(BinaryFormattedObjectTests).Assembly.FullName);
- library.LibraryId.Should().Be(2);
-
- ClassWithMembersAndTypes @class = (ClassWithMembersAndTypes)format[2];
- @class.ObjectId.Should().Be(1);
- @class.Name.Should().Be(typeof(TwoIntSerializableObject).FullName);
- @class.MemberNames.Should().BeEquivalentTo(new[] { "_value", "_meaning" });
- @class.LibraryId.Should().Be(2);
- @class.MemberTypeInfo.Should().BeEquivalentTo(new (BinaryType Type, object? Info)[]
- {
- (BinaryType.Primitive, PrimitiveType.Int32),
- (BinaryType.Primitive, PrimitiveType.Int32)
- });
-
- @class.MemberValues.Should().BeEquivalentTo(new object?[]
- {
- 1970,
- 42
- });
-
- format[3].Should().BeOfType();
- }
-
- [Fact]
- public void ReadRepeatedNestedObject()
- {
- BinaryFormattedObject format = new RepeatedNestedSerializableObject().SerializeAndParse();
- ClassWithMembersAndTypes firstClass = (ClassWithMembersAndTypes)format[3];
- ClassWithId classWithId = (ClassWithId)format[4];
- classWithId.MetadataId.Should().Be(firstClass.ObjectId);
- classWithId.MemberValues.Should().BeEquivalentTo(new object[] { 1970, 42 });
- }
-
- [Fact]
- public void ReadPrimitiveArray()
- {
- BinaryFormattedObject format = new int[] { 10, 9, 8, 7 }.SerializeAndParse();
-
- ArraySinglePrimitive array = (ArraySinglePrimitive)format[1];
- array.ArrayInfo.Length.Should().Be(4);
- array.PrimitiveType.Should().Be(PrimitiveType.Int32);
- array.ArrayObjects.Should().BeEquivalentTo(new object[] { 10, 9, 8, 7 });
- }
-
- [Fact]
- public void ReadStringArray()
- {
- BinaryFormattedObject format = new string[] { "Monday", "Tuesday", "Wednesday" }.SerializeAndParse();
- ArraySingleString array = (ArraySingleString)format[1];
- array.ArrayInfo.ObjectId.Should().Be(1);
- array.ArrayInfo.Length.Should().Be(3);
- BinaryObjectString value = (BinaryObjectString)array.ArrayObjects[0];
- }
-
- [Fact]
- public void ReadStringArrayWithNulls()
- {
- BinaryFormattedObject format = new string?[] { "Monday", null, "Wednesday", null, null, null }.SerializeAndParse();
- ArraySingleString array = (ArraySingleString)format[1];
- array.ArrayInfo.ObjectId.Should().Be(1);
- array.ArrayInfo.Length.Should().Be(6);
- array.ArrayObjects.Should().BeEquivalentTo(new object?[]
- {
- new BinaryObjectString(2, "Monday"),
- ObjectNull.Instance,
- new BinaryObjectString(3, "Wednesday"),
- ObjectNull.Instance,
- ObjectNull.Instance,
- ObjectNull.Instance
- });
- BinaryObjectString value = (BinaryObjectString)array.ArrayObjects[0];
- }
-
- [Fact]
- public void ReadDuplicatedStringArray()
- {
- BinaryFormattedObject format = new string[] { "Monday", "Tuesday", "Monday" }.SerializeAndParse();
- ArraySingleString array = (ArraySingleString)format[1];
- array.ArrayInfo.ObjectId.Should().Be(1);
- array.ArrayInfo.Length.Should().Be(3);
- BinaryObjectString value = (BinaryObjectString)array.ArrayObjects[0];
- MemberReference reference = (MemberReference)array.ArrayObjects[2];
- reference.IdRef.Should().Be(value.ObjectId);
- }
-
- [Fact]
- public void ReadObjectWithNullableObjects()
- {
- BinaryFormattedObject format = new ObjectWithNullableObjects().SerializeAndParse();
- ClassWithMembersAndTypes classRecord = (ClassWithMembersAndTypes)format[2];
- BinaryLibrary library = (BinaryLibrary)format[classRecord.LibraryId];
- }
-
- [Fact]
- public void ReadNestedObjectWithNullableObjects()
- {
- BinaryFormattedObject format = new NestedObjectWithNullableObjects().SerializeAndParse();
- ClassWithMembersAndTypes classRecord = (ClassWithMembersAndTypes)format[2];
- BinaryLibrary library = (BinaryLibrary)format[classRecord.LibraryId];
- }
-
- [Serializable]
- private class SimpleSerializableObject
- {
- }
-
-#pragma warning disable IDE0052 // Remove unread private members
-#pragma warning disable IDE0051 // Remove unused private members
-#pragma warning disable CS0414 // Field is assigned but its value is never used
-#pragma warning disable CS0649 // Field is never assigned to, and will always have its default value null
-#pragma warning disable CA1823 // Avoid unused private fields
- [Serializable]
- private class ObjectWithNullableObjects
- {
- public object? First;
- public object? Second;
- public object? Third;
- }
-
- [Serializable]
- private class NestedObjectWithNullableObjects
- {
- public ObjectWithNullableObjects? First;
- public ObjectWithNullableObjects? Second;
- public ObjectWithNullableObjects? Third = new();
- }
-
- [Serializable]
- private class NestedSerializableObject
- {
- private readonly SimpleSerializableObject _object = new();
- private readonly int _meaning = 42;
- }
-
- [Serializable]
- private class TwoIntSerializableObject
- {
- private readonly int _value = 1970;
- private readonly int _meaning = 42;
- }
-
- [Serializable]
- private class RepeatedNestedSerializableObject
- {
- private readonly TwoIntSerializableObject _first = new();
- private readonly TwoIntSerializableObject _second = new();
- }
-#pragma warning restore IDE0052 // Remove unread private members
-#pragma warning restore IDE0051 // Remove unused private members
-#pragma warning restore CS0414 // Field is assigned but its value is never used
-#pragma warning restore CS0649 // Field is never assigned to, and will always have its default value null
-#pragma warning restore CA1823 // Avoid unused private fields
-}
\ No newline at end of file
diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/ClassInfoTests.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/ClassInfoTests.cs
deleted file mode 100644
index 3b67a86b265..00000000000
--- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/ClassInfoTests.cs
+++ /dev/null
@@ -1,80 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-using PresentationCore.Tests.TestUtilities;
-using PresentationCore.Tests.FluentAssertions;
-namespace PresentationCore.Tests.BinaryFormat;
-
-public class ClassInfoTests
-{
- private static readonly byte[] s_hashtableClassInfo = new byte[]
- {
- 0x01, 0x00, 0x00, 0x00, 0x1c, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f, 0x6c, 0x6c,
- 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x48, 0x61, 0x73, 0x68, 0x74, 0x61, 0x62, 0x6c,
- 0x65, 0x07, 0x00, 0x00, 0x00, 0x0a, 0x4c, 0x6f, 0x61, 0x64, 0x46, 0x61, 0x63, 0x74, 0x6f, 0x72,
- 0x07, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x08, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
- 0x72, 0x10, 0x48, 0x61, 0x73, 0x68, 0x43, 0x6f, 0x64, 0x65, 0x50, 0x72, 0x6f, 0x76, 0x69, 0x64,
- 0x65, 0x72, 0x08, 0x48, 0x61, 0x73, 0x68, 0x53, 0x69, 0x7a, 0x65, 0x04, 0x4b, 0x65, 0x79, 0x73,
- 0x06, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x73
- };
-
- [Fact]
- public void ClassInfo_ReadHashtable()
- {
- using BinaryReader reader = new(new MemoryStream(s_hashtableClassInfo));
- ClassInfo info = ClassInfo.Parse(reader, out Count memberCount);
-
- memberCount.Should().Be(7);
- info.ObjectId.Should().Be(1);
- info.Name.Should().Be("System.Collections.Hashtable");
- info.MemberNames.Should().BeEquivalentTo(new[]
- {
- "LoadFactor",
- "Version",
- "Comparer",
- "HashCodeProvider",
- "HashSize",
- "Keys",
- "Values"
- });
- }
-
- [Fact]
- public void ClassInfo_Hashtable_RoundTrip()
- {
- using BinaryReader reader = new(new MemoryStream(s_hashtableClassInfo));
- ClassInfo info = ClassInfo.Parse(reader, out Count memberCount);
-
- MemoryStream stream = new();
- BinaryWriter writer = new(stream);
- info.Write(writer);
- stream.Position = 0;
-
- using BinaryReader reader2 = new(stream);
- info = ClassInfo.Parse(reader2, out memberCount);
-
- memberCount.Should().Be(7);
- info.ObjectId.Should().Be(1);
- info.Name.Should().Be("System.Collections.Hashtable");
- info.MemberNames.Should().BeEquivalentTo(new[]
- {
- "LoadFactor",
- "Version",
- "Comparer",
- "HashCodeProvider",
- "HashSize",
- "Keys",
- "Values"
- });
- }
-
- [Fact]
- public void MemberTypeInfo_ReadHashtable_TooShort()
- {
- MemoryStream stream = new(s_hashtableClassInfo);
- stream.SetLength(stream.Length - 1);
- using BinaryReader reader = new(stream);
- Action action = () => ClassInfo.Parse(reader, out _);
- action.Should().Throw();
- }
-}
\ No newline at end of file
diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/ExceptionTests.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/ExceptionTests.cs
deleted file mode 100644
index f2b53b197bf..00000000000
--- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/ExceptionTests.cs
+++ /dev/null
@@ -1,67 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-using System.Collections;
-using PresentationCore.Tests.TestUtilities;
-using PresentationCore.Tests.FluentAssertions;
-
-namespace PresentationCore.Tests.BinaryFormat;
-
-public class ExceptionTests
-{
- [Fact]
- public void NotSupportedException_Parse()
- {
- BinaryFormattedObject format = new NotSupportedException().SerializeAndParse();
- format.RecordCount.Should().Be(3);
- var systemClass = (SystemClassWithMembersAndTypes)format[1];
- systemClass.Name.Should().Be(typeof(NotSupportedException).FullName);
- systemClass.MemberNames.Should().BeEquivalentTo(new string[]
- {
- "ClassName",
- "Message",
- "Data",
- "InnerException",
- "HelpURL",
- "StackTraceString",
- "RemoteStackTraceString",
- "RemoteStackIndex",
- "ExceptionMethod",
- "HResult",
- "Source",
- "WatsonBuckets"
- });
-
- systemClass.MemberTypeInfo.Should().BeEquivalentTo(new (BinaryType, object?)[]
- {
- (BinaryType.String, null),
- (BinaryType.String, null),
- (BinaryType.SystemClass, typeof(IDictionary).FullName),
- (BinaryType.SystemClass, typeof(Exception).FullName),
- (BinaryType.String, null),
- (BinaryType.String, null),
- (BinaryType.String, null),
- (BinaryType.Primitive, PrimitiveType.Int32),
- (BinaryType.String, null),
- (BinaryType.Primitive, PrimitiveType.Int32),
- (BinaryType.String, null),
- (BinaryType.PrimitiveArray, PrimitiveType.Byte)
- });
-
- systemClass.MemberValues.Should().BeEquivalentTo(new object[]
- {
- new BinaryObjectString(2, "System.NotSupportedException"),
- new BinaryObjectString(3, "Specified method is not supported."),
- ObjectNull.Instance,
- ObjectNull.Instance,
- ObjectNull.Instance,
- ObjectNull.Instance,
- ObjectNull.Instance,
- 0,
- ObjectNull.Instance,
- unchecked((int)0x80131515),
- ObjectNull.Instance,
- ObjectNull.Instance
- });
- }
-}
\ No newline at end of file
diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/HashTableTests.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/HashTableTests.cs
index e67fc257935..5113c08be0b 100644
--- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/HashTableTests.cs
+++ b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/HashTableTests.cs
@@ -4,6 +4,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
+using System.Formats.Nrbf;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using PresentationCore.Tests.TestUtilities;
@@ -60,23 +61,6 @@ public void HashTable_GetObjectData()
enumerator.Current.Value.Should().BeEquivalentTo(new object[] { "That" });
}
- [Fact]
- public void HashTable_CustomComparer()
- {
- Hashtable hashtable = new(new CustomHashCodeProvider(), StringComparer.OrdinalIgnoreCase)
- {
- { "This", "That" }
- };
-
- BinaryFormattedObject format = hashtable.SerializeAndParse();
- format[1].Should().BeOfType();
- format[2].Should().BeOfType().Which.Name.Should().Be("System.Collections.Hashtable");
- format[3].Should().BeOfType().Which.Name.Should().Be("System.OrdinalComparer");
- format[4].Should().BeOfType().Which.Name.Should().Be("PresentationCore.Tests.BinaryFormat.HashtableTests+CustomHashCodeProvider");
- format[5].Should().BeOfType();
- format[6].Should().BeOfType();
- }
-
[Fact]
public void HashTable_CustomComparer_DoesNotRead()
{
@@ -85,8 +69,8 @@ public void HashTable_CustomComparer_DoesNotRead()
{ "This", "That" }
};
- BinaryFormattedObject format = hashtable.SerializeAndParse();
- format.TryGetPrimitiveHashtable(out Hashtable? deserialized).Should().BeFalse();
+ SerializationRecord format = hashtable.SerializeAndParse();
+ format.TryGetPrimitiveHashtable(out object? deserialized).Should().BeFalse();
deserialized.Should().BeNull();
}
@@ -144,13 +128,13 @@ public void BinaryFormatWriter_WriteUnsupportedHashtables(Hashtable hashtable)
[MemberData(nameof(Hashtables_TestData))]
public void BinaryFormattedObjectExtensions_TryGetPrimitiveHashtable(Hashtable hashtable)
{
- BinaryFormattedObject format = hashtable.SerializeAndParse();
- format.TryGetPrimitiveHashtable(out Hashtable? deserialized).Should().BeTrue();
+ SerializationRecord format = hashtable.SerializeAndParse();
+ format.TryGetPrimitiveHashtable(out object? deserialized).Should().BeTrue();
- deserialized!.Count.Should().Be(hashtable.Count);
+ ((Hashtable)deserialized!).Count.Should().Be(hashtable.Count);
foreach (object? key in hashtable.Keys)
{
- deserialized[key].Should().Be(hashtable[key]);
+ ((Hashtable)deserialized)[key].Should().Be(hashtable[key]);
}
}
@@ -162,12 +146,12 @@ public void RoundTripHashtables(Hashtable hashtable)
BinaryFormatWriter.WritePrimitiveHashtable(stream, hashtable);
stream.Position = 0;
- BinaryFormattedObject format = new(stream);
- format.TryGetPrimitiveHashtable(out Hashtable? deserialized).Should().BeTrue();
- deserialized!.Count.Should().Be(hashtable.Count);
+ SerializationRecord format = NrbfDecoder.Decode(stream);
+ format.TryGetPrimitiveHashtable(out object? deserialized).Should().BeTrue();
+ ((Hashtable)deserialized!).Count.Should().Be(hashtable.Count);
foreach (object? key in hashtable.Keys)
{
- deserialized[key].Should().Be(hashtable[key]);
+ ((Hashtable)deserialized)[key].Should().Be(hashtable[key]);
}
}
@@ -298,4 +282,4 @@ private static Hashtable MakeRepeatedHashtable(int countOfEntries, object? value
// public uint ToUInt32(object value) => throw new NotImplementedException();
// public ulong ToUInt64(object value) => throw new NotImplementedException();
// }
-}
\ No newline at end of file
+}
diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/ListTests.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/ListTests.cs
index 536fb82a1b4..e8fa948b6b9 100644
--- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/ListTests.cs
+++ b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/ListTests.cs
@@ -9,63 +9,12 @@
using FluentAssertions;
using PresentationCore.Tests.TestUtilities;
using PresentationCore.Tests.FluentAssertions;
+using System.Formats.Nrbf;
namespace PresentationCore.Tests.BinaryFormat;
public class ListTests
{
- [Fact]
- public void BinaryFormattedObject_ParseEmptyArrayList()
- {
- BinaryFormattedObject format = new ArrayList().SerializeAndParse();
- SystemClassWithMembersAndTypes systemClass = (SystemClassWithMembersAndTypes)format[1];
-
- systemClass.Name.Should().Be(typeof(ArrayList).FullName);
- systemClass.MemberNames.Should().BeEquivalentTo(new string[] { "_items", "_size", "_version" });
- systemClass.MemberTypeInfo[0].Should().Be((BinaryType.ObjectArray, null));
-
- format[2].Should().BeOfType();
- }
-
- [Theory]
- [MemberData(nameof(ArrayList_Primitive_Data))]
- public void BinaryFormattedObject_ParsePrimitivesArrayList(object value)
- {
- BinaryFormattedObject format = new ArrayList()
- {
- value
- }.SerializeAndParse();
-
- SystemClassWithMembersAndTypes systemClass = (SystemClassWithMembersAndTypes)format[1];
-
- systemClass.Name.Should().Be(typeof(ArrayList).FullName);
- systemClass.MemberNames.Should().BeEquivalentTo(new string[] { "_items", "_size", "_version" });
- systemClass.MemberTypeInfo[0].Should().Be((BinaryType.ObjectArray, null));
-
- ArraySingleObject array = (ArraySingleObject)format[2];
- MemberPrimitiveTyped primitve = (MemberPrimitiveTyped)array[0];
- primitve.Value.Should().Be(value);
- }
-
- [Fact]
- public void BinaryFormattedObject_ParseStringArrayList()
- {
- BinaryFormattedObject format = new ArrayList()
- {
- "JarJar"
- }.SerializeAndParse();
-
- SystemClassWithMembersAndTypes systemClass = (SystemClassWithMembersAndTypes)format[1];
-
- systemClass.Name.Should().Be(typeof(ArrayList).FullName);
- systemClass.MemberNames.Should().BeEquivalentTo(new string[] { "_items", "_size", "_version" });
- systemClass.MemberTypeInfo[0].Should().Be((BinaryType.ObjectArray, null));
-
- ArraySingleObject array = (ArraySingleObject)format[2];
- BinaryObjectString binaryString = (BinaryObjectString)array[0];
- binaryString.Value.Should().Be("JarJar");
- }
-
public static TheoryData ArrayList_Primitive_Data => new()
{
int.MaxValue,
@@ -123,50 +72,6 @@ public void BinaryFormattedObject_ParseStringArrayList()
}
};
- [Fact]
- public void BinaryFormattedObject_ParseEmptyIntList()
- {
- BinaryFormattedObject format = new List().SerializeAndParse();
- SystemClassWithMembersAndTypes classInfo = (SystemClassWithMembersAndTypes)format[1];
-
- // Note that T types are serialized as the mscorlib type.
- classInfo.Name.Should().Be(
- "System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]");
-
- classInfo.ClassInfo.MemberNames.Should().BeEquivalentTo(new string[]
- {
- "_items",
- // This is something that wouldn't be needed if List implemented ISerializable. If we format
- // we can save any extra unused array spots.
- "_size",
- // It is a bit silly that _version gets serialized, it's only use is as a check to see if
- // the collection is modified while it is being enumerated.
- "_version"
- });
- classInfo.MemberTypeInfo[0].Should().Be((BinaryType.PrimitiveArray, PrimitiveType.Int32));
- classInfo.MemberTypeInfo[1].Should().Be((BinaryType.Primitive, PrimitiveType.Int32));
- classInfo.MemberTypeInfo[2].Should().Be((BinaryType.Primitive, PrimitiveType.Int32));
- classInfo["_items"].Should().BeOfType();
- classInfo["_size"].Should().Be(0);
- classInfo["_version"].Should().Be(0);
-
- ArraySinglePrimitive array = (ArraySinglePrimitive)format[2];
- array.Length.Should().Be(0);
- }
-
- [Fact]
- public void BinaryFormattedObject_ParseEmptyStringList()
- {
- BinaryFormattedObject format = new List().SerializeAndParse();
- SystemClassWithMembersAndTypes classInfo = (SystemClassWithMembersAndTypes)format[1];
- classInfo.ClassInfo.Name.Should().StartWith("System.Collections.Generic.List`1[[System.String,");
- classInfo.MemberTypeInfo[0].Should().Be((BinaryType.StringArray, null));
- classInfo["_items"].Should().BeOfType();
-
- ArraySingleString array = (ArraySingleString)format[2];
- array.Length.Should().Be(0);
- }
-
[Theory]
[MemberData(nameof(PrimitiveLists_TestData))]
public void BinaryFormatWriter_TryWritePrimitiveList(IList list)
@@ -197,7 +102,7 @@ public void BinaryFormatWriter_TryWritePrimitiveList_Unsupported(IList list)
[MemberData(nameof(PrimitiveLists_TestData))]
public void BinaryFormattedObjectExtensions_TryGetPrimitiveList(IList list)
{
- BinaryFormattedObject format = list.SerializeAndParse();
+ SerializationRecord format = list.SerializeAndParse();
format.TryGetPrimitiveList(out object? deserialized).Should().BeTrue();
deserialized.Should().BeEquivalentTo(list);
}
@@ -223,4 +128,4 @@ public void BinaryFormattedObjectExtensions_TryGetPrimitiveList(IList list)
new List(),
new List<(int, int)>()
};
-}
\ No newline at end of file
+}
diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/MemberTypeInfoTests.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/MemberTypeInfoTests.cs
deleted file mode 100644
index bfd882a95fa..00000000000
--- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/MemberTypeInfoTests.cs
+++ /dev/null
@@ -1,71 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-using PresentationCore.Tests.TestUtilities;
-
-namespace PresentationCore.Tests.BinaryFormat;
-
-public class MemberTypeInfoTests
-{
- private static readonly byte[] s_hashtableMemberInfo = new byte[]
- {
- 0x00, 0x00, 0x03, 0x03, 0x00, 0x05, 0x05, 0x0b, 0x08, 0x1c, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d,
- 0x2e, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x49, 0x43, 0x6f,
- 0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x24, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f,
- 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x49, 0x48, 0x61, 0x73, 0x68, 0x43,
- 0x6f, 0x64, 0x65, 0x50, 0x72, 0x6f, 0x76, 0x69, 0x64, 0x65, 0x72, 0x08
- };
-
- [Fact]
- public void MemberTypeInfo_ReadHashtable()
- {
- using BinaryReader reader = new(new MemoryStream(s_hashtableMemberInfo));
- MemberTypeInfo info = MemberTypeInfo.Parse(reader, 7);
-
- info.Should().BeEquivalentTo(new (BinaryType Type, object? Info)[]
- {
- (BinaryType.Primitive, PrimitiveType.Single),
- (BinaryType.Primitive, PrimitiveType.Int32),
- (BinaryType.SystemClass, "System.Collections.IComparer"),
- (BinaryType.SystemClass, "System.Collections.IHashCodeProvider"),
- (BinaryType.Primitive, PrimitiveType.Int32),
- (BinaryType.ObjectArray, null),
- (BinaryType.ObjectArray, null)
- });
- }
-
- [Fact]
- public void MemberTypeInfo_HashtableRoundTrip()
- {
- using BinaryReader reader = new(new MemoryStream(s_hashtableMemberInfo));
- MemberTypeInfo info = MemberTypeInfo.Parse(reader, 7);
-
- MemoryStream stream = new();
- BinaryWriter writer = new(stream);
- info.Write(writer);
- stream.Position = 0;
-
- using BinaryReader reader2 = new(stream);
- info = MemberTypeInfo.Parse(reader2, 7);
- info.Should().BeEquivalentTo(new (BinaryType Type, object? Info)[]
- {
- (BinaryType.Primitive, PrimitiveType.Single),
- (BinaryType.Primitive, PrimitiveType.Int32),
- (BinaryType.SystemClass, "System.Collections.IComparer"),
- (BinaryType.SystemClass, "System.Collections.IHashCodeProvider"),
- (BinaryType.Primitive, PrimitiveType.Int32),
- (BinaryType.ObjectArray, null),
- (BinaryType.ObjectArray, null)
- });
- }
-
- [Fact]
- public void MemberTypeInfo_ReadHashtable_TooShort()
- {
- MemoryStream stream = new(s_hashtableMemberInfo);
- stream.SetLength(stream.Length - 1);
- using BinaryReader reader = new(stream);
- Action action = () => MemberTypeInfo.Parse(reader, 7);
- action.Should().Throw();
- }
-}
\ No newline at end of file
diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/PointFTests.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/PointFTests.cs
deleted file mode 100644
index d80e85862f0..00000000000
--- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/PointFTests.cs
+++ /dev/null
@@ -1,31 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-using System.Drawing;
-using PresentationCore.Tests.TestUtilities;
-
-namespace PresentationCore.Tests.BinaryFormat;
-
-public class PointFTests
-{
- [Fact]
- public void PointF_Parse()
- {
- BinaryFormattedObject format = new PointF().SerializeAndParse();
-
- BinaryLibrary binaryLibrary = (BinaryLibrary)format[1];
- binaryLibrary.LibraryId.Should().Be(2);
- binaryLibrary.LibraryName.Should().Be("System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
-
- ClassWithMembersAndTypes classInfo = (ClassWithMembersAndTypes)format[2];
- classInfo.ObjectId.Should().Be(1);
- classInfo.Name.Should().Be("System.Drawing.PointF");
- classInfo.MemberNames.Should().BeEquivalentTo(new string[] { "x", "y" });
- classInfo.MemberValues.Should().BeEquivalentTo(new object[] { 0.0f, 0.0f });
- classInfo.MemberTypeInfo.Should().BeEquivalentTo(new[]
- {
- (BinaryType.Primitive, PrimitiveType.Single),
- (BinaryType.Primitive, PrimitiveType.Single)
- });
- }
-}
\ No newline at end of file
diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/PrimitiveTypeTests.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/PrimitiveTypeTests.cs
index d9042f33753..da83b1c030f 100644
--- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/PrimitiveTypeTests.cs
+++ b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/PrimitiveTypeTests.cs
@@ -8,6 +8,9 @@
using FluentAssertions;
using System.IO;
using PresentationCore.Tests.TestUtilities;
+using System.Globalization;
+using System.Runtime.Serialization;
+using System.Runtime.CompilerServices;
namespace PresentationCore.Tests.BinaryFormat;
@@ -84,16 +87,6 @@ public void WriteReadPrimitiveValue_RoundTrip(byte type, object value)
{ (byte)PrimitiveType.DateTime, DateTime.MaxValue },
};
- [Theory]
- [MemberData(nameof(Primitive_Data))]
- public void PrimitiveTypeMemberName(object value)
- {
- BinaryFormattedObject format = value.SerializeAndParse();
- SystemClassWithMembersAndTypes systemClass = (SystemClassWithMembersAndTypes)format[1];
- systemClass.MemberNames[0].Should().Be("m_value");
- systemClass.MemberValues.Count.Should().Be(1);
- }
-
[Theory]
[MemberData(nameof(Primitive_Data))]
[MemberData(nameof(Primitive_ExtendedData))]
@@ -111,16 +104,6 @@ public void BinaryFormatWriter_WritePrimitive(object value)
deserialized.Should().Be(value);
}
- [Theory]
- [MemberData(nameof(Primitive_Data))]
- [MemberData(nameof(Primitive_ExtendedData))]
- public void BinaryFormattedObject_ReadPrimitive(object value)
- {
- BinaryFormattedObject formattedObject = value.SerializeAndParse();
- formattedObject.TryGetPrimitiveType(out object? deserialized).Should().BeTrue();
- deserialized.Should().Be(value);
- }
-
public static TheoryData Primitive_Data => new()
{
int.MaxValue,
@@ -153,11 +136,64 @@ public static void WritePrimitiveValue(BinaryWriter writer, PrimitiveType type,
=> WritePrimitiveType(writer, type, value);
public static object ReadPrimitiveValue(BinaryReader reader, PrimitiveType type)
- => ReadPrimitiveType(reader, type);
+ => type switch
+ {
+ PrimitiveType.Boolean => reader.ReadBoolean(),
+ PrimitiveType.Byte => reader.ReadByte(),
+ PrimitiveType.SByte => reader.ReadSByte(),
+ PrimitiveType.Char => reader.ReadChar(),
+ PrimitiveType.Int16 => reader.ReadInt16(),
+ PrimitiveType.UInt16 => reader.ReadUInt16(),
+ PrimitiveType.Int32 => reader.ReadInt32(),
+ PrimitiveType.UInt32 => reader.ReadUInt32(),
+ PrimitiveType.Int64 => reader.ReadInt64(),
+ PrimitiveType.UInt64 => reader.ReadUInt64(),
+ PrimitiveType.Single => reader.ReadSingle(),
+ PrimitiveType.Double => reader.ReadDouble(),
+ PrimitiveType.Decimal => decimal.Parse(reader.ReadString(), CultureInfo.InvariantCulture),
+ PrimitiveType.DateTime => ReadDateTime(reader),
+ PrimitiveType.TimeSpan => new TimeSpan(reader.ReadInt64()),
+ // String is handled with a record, never on it's own
+ _ => throw new SerializationException($"Failure trying to read primitve '{type}'"),
+ };
+
+ ///
+ /// Reads a binary formatted from the given .
+ ///
+ /// The data was invalid.
+ private static unsafe DateTime ReadDateTime(BinaryReader reader)
+ => CreateDateTimeFromData(reader.ReadInt64());
+
+ ///
+ /// Creates a object from raw data with validation.
+ ///
+ /// was invalid.
+ private static DateTime CreateDateTimeFromData(long data)
+ {
+ // Copied from System.Runtime.Serialization.Formatters.Binary.BinaryParser
+
+ // Use DateTime's public constructor to validate the input, but we
+ // can't return that result as it strips off the kind. To address
+ // that, store the value directly into a DateTime via an unsafe cast.
+ // See BinaryFormatterWriter.WriteDateTime for details.
+
+ try
+ {
+ const long TicksMask = 0x3FFFFFFFFFFFFFFF;
+ _ = new DateTime(data & TicksMask);
+ }
+ catch (ArgumentException ex)
+ {
+ // Bad data
+ throw new SerializationException(ex.Message, ex);
+ }
+
+ return Unsafe.As(ref data);
+ }
public override void Write(BinaryWriter writer)
{
throw new NotImplementedException();
}
}
-}
\ No newline at end of file
+}
diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/RecordMapTests.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/RecordMapTests.cs
deleted file mode 100644
index 061e2a7925e..00000000000
--- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/RecordMapTests.cs
+++ /dev/null
@@ -1,30 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-using PresentationCore.Tests.TestUtilities;
-
-namespace PresentationCore.Tests.BinaryFormat;
-
-public class RecordMapTests
-{
- private class Record : IRecord
- {
- void IBinaryWriteable.Write(BinaryWriter writer) { }
- }
-
- [Fact]
- public void RecordMap_CannotAddSameIndexTwice()
- {
- RecordMap map = new();
- Action action = () => map[1] = new Record();
- action();
- action.Should().Throw();
- }
-
- [Fact]
- public void RecordMap_GetMissingThrowsKeyNotFound()
- {
- RecordMap map = new();
- Func func = () => map[0];
- func.Should().Throw();
- }
-}
\ No newline at end of file
diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/SystemDrawingTests.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/SystemDrawingTests.cs
index 123b3577de2..7ef30fc027a 100644
--- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/SystemDrawingTests.cs
+++ b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/BinaryFormat/SystemDrawingTests.cs
@@ -4,6 +4,7 @@
using System.Drawing;
using PresentationCore.Tests.TestUtilities;
using PresentationCore.Tests.FluentAssertions;
+using System.Formats.Nrbf;
namespace PresentationCore.Tests.BinaryFormat;
@@ -12,45 +13,27 @@ public class SystemDrawingTests
[Fact]
public void PointF_Parse()
{
- BinaryFormattedObject format = new PointF().SerializeAndParse();
-
- BinaryLibrary binaryLibrary = (BinaryLibrary)format[1];
- binaryLibrary.LibraryId.Should().Be(2);
- binaryLibrary.LibraryName.ToString().Should().Be("System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
-
- ClassWithMembersAndTypes classInfo = (ClassWithMembersAndTypes)format[2];
- classInfo.ObjectId.Should().Be(1);
- classInfo.Name.Should().Be("System.Drawing.PointF");
- classInfo.MemberNames.Should().BeEquivalentTo(new string[] { "x", "y" });
- classInfo.MemberValues.Should().BeEquivalentTo(new object[] { 0.0f, 0.0f });
- classInfo.MemberTypeInfo.Should().BeEquivalentTo(new[]
- {
- (BinaryType.Primitive, PrimitiveType.Single),
- (BinaryType.Primitive, PrimitiveType.Single)
- });
+ PointF input = new(1.5f, 2.1f);
+ SerializationRecord record = input.SerializeAndParse();
+
+ Assert.True(record.TryGetPointF(out object? read));
+
+ Assert.Equal(input.X, ((PointF)read!).X);
+ Assert.Equal(input.Y, ((PointF)read).Y);
}
[Fact]
public void RectangleF_Parse()
{
- BinaryFormattedObject format = new RectangleF().SerializeAndParse();
-
- BinaryLibrary binaryLibrary = (BinaryLibrary)format[1];
- binaryLibrary.LibraryId.Should().Be(2);
- binaryLibrary.LibraryName.ToString().Should().Be("System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
-
- ClassWithMembersAndTypes classInfo = (ClassWithMembersAndTypes)format[2];
- classInfo.ObjectId.Should().Be(1);
- classInfo.Name.Should().Be("System.Drawing.RectangleF");
- classInfo.MemberNames.Should().BeEquivalentTo(new string[] { "x", "y", "width", "height" });
- classInfo.MemberValues.Should().BeEquivalentTo(new object[] { 0.0f, 0.0f, 0.0f, 0.0f });
- classInfo.MemberTypeInfo.Should().BeEquivalentTo(new[]
- {
- (BinaryType.Primitive, PrimitiveType.Single),
- (BinaryType.Primitive, PrimitiveType.Single),
- (BinaryType.Primitive, PrimitiveType.Single),
- (BinaryType.Primitive, PrimitiveType.Single)
- });
+ RectangleF input = new(1.5f, 2.1f, 100.7f, 15.9f);
+ SerializationRecord record = input.SerializeAndParse();
+
+ Assert.True(record.TryGetRectangleF(out object? read));
+
+ Assert.Equal(input.X, ((RectangleF)read!).X);
+ Assert.Equal(input.Y, ((RectangleF)read).Y);
+ Assert.Equal(input.Width, ((RectangleF)read).Width);
+ Assert.Equal(input.Height, ((RectangleF)read).Height);
}
public static TheoryData SystemDrawing_TestData => new()
@@ -58,4 +41,4 @@ public void RectangleF_Parse()
new PointF(),
new RectangleF()
};
-}
\ No newline at end of file
+}
diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/PresentationCore.Tests.csproj b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/PresentationCore.Tests.csproj
index 1e670caed07..fdff270ee47 100644
--- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/PresentationCore.Tests.csproj
+++ b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/PresentationCore.Tests.csproj
@@ -25,6 +25,9 @@
+
+
+
diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/TestUtilities/BinaryFormatTestExtensions.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/TestUtilities/BinaryFormatTestExtensions.cs
index 7927e10225f..06ff94533e3 100644
--- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/TestUtilities/BinaryFormatTestExtensions.cs
+++ b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/TestUtilities/BinaryFormatTestExtensions.cs
@@ -7,6 +7,7 @@
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows;
+using System.Formats.Nrbf;
namespace PresentationCore.Tests.TestUtilities;
@@ -15,7 +16,7 @@ internal static class BinaryFormatTestExtensions
///
/// Serializes the object using the and reads it into a .
///
- public static BinaryFormattedObject SerializeAndParse(this object source) => new(source.Serialize());
+ public static SerializationRecord SerializeAndParse(this object source) => NrbfDecoder.Decode(source.Serialize());
///
/// Serializes the object using the .
@@ -62,4 +63,4 @@ public static bool IsBinaryFormatted(this Type type)
return !((converter.CanConvertFrom(typeof(string)) && converter.CanConvertTo(typeof(string)))
|| (converter.CanConvertFrom(typeof(byte[])) && converter.CanConvertTo(typeof(byte[]))));
}
-}
\ No newline at end of file
+}