-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathEdgeCaseTests.cs
More file actions
194 lines (164 loc) · 7.59 KB
/
EdgeCaseTests.cs
File metadata and controls
194 lines (164 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;
namespace System.Formats.Nrbf.Tests;
public class EdgeCaseTests : ReadTests
{
[Fact]
public void SurrogatesGetNoSpecialHandling()
{
#if NET
// Type is [Serializable] only on Full .NET Framework.
// So here we use a Base64 representation of serialized typeof(object)
const string serializedWithFullFramework = "AAEAAAD/////AQAAAAAAAAAEAQAAAB9TeXN0ZW0uVW5pdHlTZXJpYWxpemF0aW9uSG9sZGVyAwAAAAREYXRhCVVuaXR5VHlwZQxBc3NlbWJseU5hbWUBAAEIBgIAAAANU3lzdGVtLk9iamVjdAQAAAAGAwAAAEttc2NvcmxpYiwgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkL";
using MemoryStream stream = new(Convert.FromBase64String(serializedWithFullFramework));
#else
using MemoryStream stream = Serialize(typeof(object));
#endif
ClassRecord classRecord = (ClassRecord)NrbfDecoder.Decode(stream);
// It's a surrogate, so there is no type match.
Assert.False(classRecord.TypeNameMatches(typeof(Type)));
Assert.Equal("System.UnitySerializationHolder", classRecord.TypeName.FullName);
Assert.Equal("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", classRecord.GetString("AssemblyName"));
}
[Theory]
[InlineData(FormatterTypeStyle.TypesAlways)]
[InlineData(FormatterTypeStyle.TypesAlways | FormatterTypeStyle.XsdString)]
public void ArraysOfStringsCanContainMemberReferences(FormatterTypeStyle typeFormat)
{
// it has to be the same object, not just the same value
const string same = "same";
string[] input = { same, same };
using MemoryStream stream = new();
BinaryFormatter binaryFormatter = new()
{
TypeFormat = typeFormat
};
binaryFormatter.Serialize(stream, input);
stream.Position = 0;
string?[] ouput = ((SZArrayRecord<string>)NrbfDecoder.Decode(stream)).GetArray();
Assert.Equal(input, ouput);
if ((typeFormat & FormatterTypeStyle.XsdString) == 0)
{
Assert.Same(ouput[0], ouput[1]);
}
else
{
Assert.NotSame(ouput[0], ouput[1]);
}
}
[ConditionalTheory]
[InlineData(100)]
[InlineData(64_001)]
[InlineData(127_000)]
[InlineData(2147483591)] // Array.MaxLength
public void CanReadArrayOfAnySize(int length)
{
if (length == 2147483591 && (!PlatformDetection.Is64BitProcess || !PlatformDetection.IsReleaseRuntime || PlatformDetection.IsMonoRuntime))
{
throw new SkipTestException("It would take too much time to execute.");
}
try
{
byte[] input = new byte[length];
new Random().NextBytes(input);
// MemoryStream can not handle large array payloads as it's backed by an array.
using FileStream stream = SerializeToFile(input);
byte[] output = ((SZArrayRecord<byte>)NrbfDecoder.Decode(stream)).GetArray();
Assert.Equal(input, output);
}
catch (OutOfMemoryException) when (length == 2147483591)
{
throw new SkipTestException("Not enough memory available to test max array size support");
}
}
#pragma warning disable SYSLIB0011 // Type or member is obsolete
[Theory]
[InlineData(FormatterTypeStyle.TypesWhenNeeded)]
[InlineData(FormatterTypeStyle.XsdString)]
public void FormatterTypeStyleOtherThanTypesAlwaysAreNotSupportedByDesign(FormatterTypeStyle typeFormat)
{
using MemoryStream ms = new();
BinaryFormatter binaryFormatter = new()
{
TypeFormat = typeFormat
};
#pragma warning restore SYSLIB0011 // Type or member is obsolete
binaryFormatter.Serialize(ms, true);
ms.Position = 0;
Assert.Throws<NotSupportedException>(() => NrbfDecoder.Decode(ms));
}
public static IEnumerable<object[]> CanReadAllKindsOfDateTimes_Arguments
{
get
{
yield return new object[] { new DateTime(1990, 11, 24, 0, 0, 0, DateTimeKind.Local) };
yield return new object[] { new DateTime(1990, 11, 25, 0, 0, 0, DateTimeKind.Utc) };
yield return new object[] { new DateTime(1990, 11, 26, 0, 0, 0, DateTimeKind.Unspecified) };
}
}
[Theory]
[MemberData(nameof(CanReadAllKindsOfDateTimes_Arguments))]
public void CanReadAllKindsOfDateTimes_DateTimeIsTheRootRecord(DateTime input)
{
using MemoryStream stream = Serialize(input);
PrimitiveTypeRecord<DateTime> dateTimeRecord = (PrimitiveTypeRecord<DateTime>)NrbfDecoder.Decode(stream);
Assert.Equal(input.Ticks, dateTimeRecord.Value.Ticks);
Assert.Equal(input.Kind, dateTimeRecord.Value.Kind);
}
[Serializable]
public class ClassWithDateTime
{
public DateTime Value;
}
[Theory]
[MemberData(nameof(CanReadAllKindsOfDateTimes_Arguments))]
public void CanReadAllKindsOfDateTimes_DateTimeIsMemberOfTheRootRecord(DateTime input)
{
using MemoryStream stream = Serialize(new ClassWithDateTime() { Value = input });
ClassRecord classRecord = NrbfDecoder.DecodeClassRecord(stream);
Assert.Equal(input.Ticks, classRecord.GetDateTime(nameof(ClassWithDateTime.Value)).Ticks);
Assert.Equal(input.Kind, classRecord.GetDateTime(nameof(ClassWithDateTime.Value)).Kind);
}
[Fact]
public void CanReadUserClassStoredAsSystemClass()
{
// For the following data, BinaryFormatter serializes the ClassWithNullableStructField class
// as a record with a single field called "NullableField" with BinaryType.SystemClass (!!!)
// and TypeName being System.Nullable`1[[SampleStruct, $AssemblyName]].
// It most likely does so, because it's System.Nullable<$NonSystemStruct>.
// But later it serializes the SampleStruct as a ClassWithMembersAndTypes record,
// not SystemClassWithMembersAndTypes.
// It does so, only when the payload contains at least one class with the nullable field being null.
using MemoryStream stream = Serialize(
new ClassWithNullableStructField[]
{
new ClassWithNullableStructField() { NullableField = null }, // having a null here is crucial for the test
new ClassWithNullableStructField() { NullableField = new ClassWithNullableStructField.SampleStruct() { Value = 42 } }
}
);
SZArrayRecord<SerializationRecord> arrayRecord = (SZArrayRecord<SerializationRecord>)NrbfDecoder.Decode(stream);
SerializationRecord[] records = arrayRecord.GetArray();
Assert.Equal(2, arrayRecord.Length);
Assert.All(records, record => Assert.True(record.TypeNameMatches(typeof(ClassWithNullableStructField))));
Assert.Null(((ClassRecord)records[0]).GetClassRecord(nameof(ClassWithNullableStructField.NullableField)));
ClassRecord? notNullRecord = ((ClassRecord)records[1]).GetClassRecord(nameof(ClassWithNullableStructField.NullableField));
Assert.NotNull(notNullRecord);
Assert.Equal(42, notNullRecord.GetInt32(nameof(ClassWithNullableStructField.SampleStruct.Value)));
}
[Serializable]
public class ClassWithNullableStructField
{
#pragma warning disable IDE0001 // Simplify names
public System.Nullable<SampleStruct> NullableField;
#pragma warning restore IDE0001
[Serializable]
public struct SampleStruct
{
public int Value;
}
}
}