-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
FormatterServicesTests.cs
322 lines (275 loc) · 14.7 KB
/
FormatterServicesTests.cs
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using Xunit;
namespace System.Runtime.Serialization.Formatters.Tests
{
public partial class FormatterServicesTests
{
[Fact]
public void CheckTypeSecurity_Nop()
{
FormatterServices.CheckTypeSecurity(typeof(int), TypeFilterLevel.Full);
FormatterServices.CheckTypeSecurity(typeof(int), TypeFilterLevel.Low);
}
[Fact]
public void GetSerializableMembers_InvalidArguments_ThrowsException()
{
AssertExtensions.Throws<ArgumentNullException>("type", () => FormatterServices.GetSerializableMembers(null));
}
[Fact]
public void GetSerializableMembers_Interface()
{
Assert.Equal<MemberInfo>(new MemberInfo[0], FormatterServices.GetSerializableMembers(typeof(IDisposable)));
}
[Fact]
public void GetUninitializedObject_NullType_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("type", () => FormatterServices.GetUninitializedObject(null));
AssertExtensions.Throws<ArgumentNullException>("type", () => FormatterServices.GetSafeUninitializedObject(null));
}
[Fact]
public void GetUninitializedObject_NonRuntimeType_ThrowsSerializationException()
{
Assert.Throws<SerializationException>(() => FormatterServices.GetUninitializedObject(new NonRuntimeType()));
Assert.Throws<SerializationException>(() => FormatterServices.GetSafeUninitializedObject(new NonRuntimeType()));
}
public static IEnumerable<object[]> GetUninitializedObject_NotSupportedType_TestData()
{
yield return new object[] { typeof(int[]) };
yield return new object[] { typeof(int[,]) };
yield return new object[] { typeof(int).MakePointerType() };
yield return new object[] { typeof(int).MakeByRefType() };
yield return new object[] { typeof(GenericClass<>).GetTypeInfo().GenericTypeParameters[0] };
}
[Theory]
[ActiveIssue("https://github.com/mono/mono/issues/15113", TestRuntimes.Mono)]
[MemberData(nameof(GetUninitializedObject_NotSupportedType_TestData))]
public void GetUninitializedObject_NotSupportedType_ThrowsArgumentException(Type type)
{
AssertExtensions.Throws<ArgumentException>(null, () => FormatterServices.GetUninitializedObject(type));
AssertExtensions.Throws<ArgumentException>(null, () => FormatterServices.GetSafeUninitializedObject(type));
}
[Theory]
[InlineData(typeof(AbstractClass))]
[InlineData(typeof(StaticClass))]
[InlineData(typeof(Interface))]
[InlineData(typeof(Array))]
public void GetUninitializedObject_AbstractClass_ThrowsMemberAccessException(Type type)
{
Assert.Throws<MemberAccessException>(() => FormatterServices.GetUninitializedObject(type));
Assert.Throws<MemberAccessException>(() => FormatterServices.GetSafeUninitializedObject(type));
}
public abstract class AbstractClass { }
public static class StaticClass { }
public interface Interface { }
public static IEnumerable<object[]> GetUninitializedObject_OpenGenericClass_TestData()
{
yield return new object[] { typeof(GenericClass<>) };
yield return new object[] { typeof(GenericClass<>).MakeGenericType(typeof(GenericClass<>)) };
}
[Theory]
[ActiveIssue("https://github.com/mono/mono/issues/15111", TestRuntimes.Mono)]
[MemberData(nameof(GetUninitializedObject_OpenGenericClass_TestData))]
public void GetUninitializedObject_OpenGenericClass_ThrowsMemberAccessException(Type type)
{
Assert.Throws<MemberAccessException>(() => FormatterServices.GetUninitializedObject(type));
Assert.Throws<MemberAccessException>(() => FormatterServices.GetSafeUninitializedObject(type));
}
public interface IGenericClass
{
int Value { get; set; }
}
public class GenericClass<T> : IGenericClass
{
public int Value { get; set; }
}
public static IEnumerable<object[]> GetUninitializedObject_ByRefLikeType_TestData()
{
yield return new object[] { typeof(TypedReference) };
yield return new object[] { typeof(RuntimeArgumentHandle) };
// .NET Standard 2.0 doesn't have ArgIterator, but .NET Core 2.0 does
Type argIterator = typeof(object).Assembly.GetType("System.ArgIterator");
if (argIterator != null)
{
yield return new object[] { argIterator };
}
}
public static IEnumerable<object[]> GetUninitializedObject_ByRefLikeType_NetCore_TestData()
{
yield return new object[] { typeof(Span<int>) };
yield return new object[] { typeof(ReadOnlySpan<int>) };
yield return new object[] { typeof(StructWithSpanField) };
}
#pragma warning disable 0169 // The private field 'class member' is never used
private ref struct StructWithSpanField
{
Span<byte> _bytes;
int _position;
}
#pragma warning restore 0169
[Theory]
[MemberData(nameof(GetUninitializedObject_ByRefLikeType_NetCore_TestData))]
[SkipOnTargetFramework(~TargetFrameworkMonikers.Netcoreapp, "Some runtimes don't support or recognise Span<T> or ReadOnlySpan<T> as ref types.")]
public void GetUninitializedObject_ByRefLikeType_NetCore_ThrowsNotSupportedException(Type type)
{
Assert.Throws<NotSupportedException>(() => FormatterServices.GetUninitializedObject(type));
Assert.Throws<NotSupportedException>(() => FormatterServices.GetSafeUninitializedObject(type));
}
[Theory]
[MemberData(nameof(GetUninitializedObject_ByRefLikeType_TestData))]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, ".NET Framework has bug that allows allocating instances of byref-like types.")]
public void GetUninitializedObject_ByRefLikeType_NonNetfx_ThrowsNotSupportedException(Type type)
{
Assert.Throws<NotSupportedException>(() => FormatterServices.GetUninitializedObject(type));
Assert.Throws<NotSupportedException>(() => FormatterServices.GetSafeUninitializedObject(type));
}
[Fact]
[SkipOnTargetFramework(~TargetFrameworkMonikers.Netcoreapp, "The coreclr doesn't support GetUninitializedObject for shared generic instances")]
[SkipOnMonoAttribute("System.__Canon doesn't exist on Mono")]
public void GetUninitializedObject_SharedGenericInstance_NetCore_ThrowsNotSupportedException()
{
Type canonType = Type.GetType("System.__Canon");
Assert.NotNull(canonType);
Type sharedGenericInstance = typeof(GenericClass<>).MakeGenericType(canonType);
Assert.Throws<NotSupportedException>(() => FormatterServices.GetUninitializedObject(sharedGenericInstance));
Assert.Throws<NotSupportedException>(() => FormatterServices.GetSafeUninitializedObject(sharedGenericInstance));
}
[Fact]
public void GetUninitializedObject_NullableType_InitializesValue()
{
int? nullableUnsafe = (int?)FormatterServices.GetUninitializedObject(typeof(int?));
Assert.True(nullableUnsafe.HasValue);
Assert.Equal(0, nullableUnsafe.Value);
int? nullableSafe = (int?)FormatterServices.GetSafeUninitializedObject(typeof(int?));
Assert.True(nullableSafe.HasValue);
Assert.Equal(0, nullableSafe.Value);
}
[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "The .NET Framework doesn't support GetUninitializedObject for subclasses of ContextBoundObject")]
public void GetUninitializedObject_ContextBoundObjectSubclass_NetCore_InitializesValue()
{
Assert.Equal(0, ((ContextBoundSubclass)FormatterServices.GetUninitializedObject(typeof(ContextBoundSubclass))).Value);
Assert.Equal(0, ((ContextBoundSubclass)FormatterServices.GetSafeUninitializedObject(typeof(ContextBoundSubclass))).Value);
}
public class ContextBoundSubclass : ContextBoundObject
{
public int Value { get; set; }
}
[Fact]
public void GetUninitializedObject_TypeHasDefaultConstructor_DoesNotRunConstructor()
{
Assert.Equal(42, new ObjectWithDefaultConstructor().Value);
Assert.Equal(0, ((ObjectWithDefaultConstructor)FormatterServices.GetUninitializedObject(typeof(ObjectWithDefaultConstructor))).Value);
Assert.Equal(0, ((ObjectWithDefaultConstructor)FormatterServices.GetSafeUninitializedObject(typeof(ObjectWithDefaultConstructor))).Value);
}
private class ObjectWithDefaultConstructor
{
public ObjectWithDefaultConstructor()
{
Value = 42;
}
public int Value;
}
[Fact]
public void GetUninitializedObject_StaticConstructor_CallsStaticConstructor()
{
Assert.Equal(2, ((ObjectWithStaticConstructor)FormatterServices.GetUninitializedObject(typeof(ObjectWithStaticConstructor))).GetValue());
}
private class ObjectWithStaticConstructor
{
private static int s_value = 1;
static ObjectWithStaticConstructor()
{
s_value = 2;
}
public int GetValue() => s_value;
}
[Fact]
public void GetUninitializedObject_StaticField_InitializesStaticFields()
{
Assert.Equal(1, ((ObjectWithStaticField)FormatterServices.GetUninitializedObject(typeof(ObjectWithStaticField))).GetValue());
}
private class ObjectWithStaticField
{
private static int s_value = 1;
public int GetValue() => s_value;
}
[Fact]
[ActiveIssue("https://github.com/mono/mono/issues/15114", TestRuntimes.Mono)]
public void GetUninitializedObject_StaticConstructorThrows_ThrowsTypeInitializationException()
{
TypeInitializationException ex = Assert.Throws<TypeInitializationException>(() => FormatterServices.GetUninitializedObject(typeof(StaticConstructorThrows)));
Assert.IsType<DivideByZeroException>(ex.InnerException);
}
private class StaticConstructorThrows
{
static StaticConstructorThrows()
{
throw new DivideByZeroException();
}
}
[Fact]
public void GetUninitializedObject_ClassFieldWithDefaultValue_DefaultValueIgnored()
{
Assert.Equal(42, new ObjectWithStructDefaultField().Value);
Assert.Null(((ObjectWithClassDefaultField)FormatterServices.GetUninitializedObject(typeof(ObjectWithClassDefaultField))).Value);
Assert.Null(((ObjectWithClassDefaultField)FormatterServices.GetSafeUninitializedObject(typeof(ObjectWithClassDefaultField))).Value);
}
private class ObjectWithClassDefaultField
{
public string Value = "abc";
}
[Fact]
public void GetUninitializedObject_StructFieldWithDefaultValue_DefaultValueIgnored()
{
Assert.Equal(42, new ObjectWithStructDefaultField().Value);
Assert.Equal(0, ((ObjectWithStructDefaultField)FormatterServices.GetUninitializedObject(typeof(ObjectWithStructDefaultField))).Value);
Assert.Equal(0, ((ObjectWithStructDefaultField)FormatterServices.GetSafeUninitializedObject(typeof(ObjectWithStructDefaultField))).Value);
}
private class ObjectWithStructDefaultField
{
public int Value = 42;
}
[Fact]
public void PopulateObjectMembers_InvalidArguments_ThrowsException()
{
AssertExtensions.Throws<ArgumentNullException>("obj", () => FormatterServices.PopulateObjectMembers(null, new MemberInfo[0], new object[0]));
AssertExtensions.Throws<ArgumentNullException>("members", () => FormatterServices.PopulateObjectMembers(new object(), null, new object[0]));
AssertExtensions.Throws<ArgumentNullException>("data", () => FormatterServices.PopulateObjectMembers(new object(), new MemberInfo[0], null));
AssertExtensions.Throws<ArgumentException>(null, () => FormatterServices.PopulateObjectMembers(new object(), new MemberInfo[1], new object[2]));
AssertExtensions.Throws<ArgumentNullException>("members", () => FormatterServices.PopulateObjectMembers(new object(), new MemberInfo[1], new object[1]));
Assert.Throws<SerializationException>(() => FormatterServices.PopulateObjectMembers(new object(), new MemberInfo[] { typeof(object).GetMethod("GetHashCode") }, new object[] { new object() }));
}
[Fact]
public void GetObjectData_InvalidArguments_ThrowsException()
{
AssertExtensions.Throws<ArgumentNullException>("obj", () => FormatterServices.GetObjectData(null, new MemberInfo[0]));
AssertExtensions.Throws<ArgumentNullException>("members", () => FormatterServices.GetObjectData(new object(), null));
AssertExtensions.Throws<ArgumentNullException>("members", () => FormatterServices.GetObjectData(new object(), new MemberInfo[1]));
Assert.Throws<SerializationException>(() => FormatterServices.GetObjectData(new object(), new MethodInfo[] { typeof(object).GetMethod("GetHashCode") }));
}
[Fact]
public void GetSurrogateForCyclicalReference_InvalidArguments_ThrowsException()
{
AssertExtensions.Throws<ArgumentNullException>("innerSurrogate", () => FormatterServices.GetSurrogateForCyclicalReference(null));
}
[Fact]
public void GetSurrogateForCyclicalReference_ValidSurrogate_GetsObject()
{
var surrogate = new NonSerializablePairSurrogate();
ISerializationSurrogate newSurrogate = FormatterServices.GetSurrogateForCyclicalReference(surrogate);
Assert.NotNull(newSurrogate);
Assert.NotSame(surrogate, newSurrogate);
}
[Fact]
public void GetTypeFromAssembly_InvalidArguments_ThrowsException()
{
AssertExtensions.Throws<ArgumentNullException>("assem", () => FormatterServices.GetTypeFromAssembly(null, "name"));
Assert.Null(FormatterServices.GetTypeFromAssembly(GetType().Assembly, Guid.NewGuid().ToString("N"))); // non-existing type doesn't throw
}
}
}