-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
BinaryFormatterEventSourceTests.cs
216 lines (181 loc) · 7.82 KB
/
BinaryFormatterEventSourceTests.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
// 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.Generic;
using System.Diagnostics.Tracing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using Xunit;
namespace System.Runtime.Serialization.Formatters.Tests
{
[ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.IsBinaryFormatterSupported))]
public static class BinaryFormatterEventSourceTests
{
private const string BinaryFormatterEventSourceName = "System.Runtime.Serialization.Formatters.Binary.BinaryFormatterEventSource";
[Fact]
public static void RecordsSerialization()
{
using LoggingEventListener listener = new LoggingEventListener();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(Stream.Null, CreatePerson());
string[] capturedLog = listener.CaptureLog();
string[] expected = new string[]
{
"SerializationStart [Start, 00000001]: <no payload>",
"SerializingObject [Info, 00000001]: " + typeof(Person).AssemblyQualifiedName,
"SerializingObject [Info, 00000001]: " + typeof(Address).AssemblyQualifiedName,
"SerializationStop [Stop, 00000001]: <no payload>",
};
Assert.Equal(expected, capturedLog);
}
[Fact]
public static void RecordsDeserialization()
{
MemoryStream ms = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, CreatePerson());
ms.Position = 0;
using LoggingEventListener listener = new LoggingEventListener();
formatter.Deserialize(ms);
string[] capturedLog = listener.CaptureLog();
string[] expected = new string[]
{
"DeserializationStart [Start, 00000002]: <no payload>",
"DeserializingObject [Info, 00000002]: " + typeof(Person).AssemblyQualifiedName,
"DeserializingObject [Info, 00000002]: " + typeof(Address).AssemblyQualifiedName,
"DeserializationStop [Stop, 00000002]: <no payload>",
};
Assert.Equal(expected, capturedLog);
}
[Fact]
public static void RecordsNestedSerializationCalls()
{
// First, serialization
using LoggingEventListener listener = new LoggingEventListener();
MemoryStream ms = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, new ClassWithNestedDeserialization());
string[] capturedLog = listener.CaptureLog();
ms.Position = 0;
string[] expected = new string[]
{
"SerializationStart [Start, 00000001]: <no payload>",
"SerializingObject [Info, 00000001]: " + typeof(ClassWithNestedDeserialization).AssemblyQualifiedName,
"SerializationStart [Start, 00000001]: <no payload>",
"SerializingObject [Info, 00000001]: " + typeof(Address).AssemblyQualifiedName,
"SerializationStop [Stop, 00000001]: <no payload>",
"SerializationStop [Stop, 00000001]: <no payload>",
};
Assert.Equal(expected, capturedLog);
listener.ClearLog();
// Then, deserialization
ms.Position = 0;
formatter.Deserialize(ms);
capturedLog = listener.CaptureLog();
expected = new string[]
{
"DeserializationStart [Start, 00000002]: <no payload>",
"DeserializingObject [Info, 00000002]: " + typeof(ClassWithNestedDeserialization).AssemblyQualifiedName,
"DeserializationStart [Start, 00000002]: <no payload>",
"DeserializingObject [Info, 00000002]: " + typeof(Address).AssemblyQualifiedName,
"DeserializationStop [Stop, 00000002]: <no payload>",
"DeserializationStop [Stop, 00000002]: <no payload>",
};
Assert.Equal(expected, capturedLog);
}
private static Person CreatePerson()
{
return new Person()
{
Name = "Some Chap",
HomeAddress = new Address()
{
Street = "123 Anywhere Ln",
City = "Anywhere ST 00000 United States"
}
};
}
private sealed class LoggingEventListener : EventListener
{
private readonly Thread _activeThread = Thread.CurrentThread;
private readonly List<string> _log = new List<string>();
private void AddToLog(FormattableString message)
{
_log.Add(FormattableString.Invariant(message));
}
// Captures the current log
public string[] CaptureLog()
{
return _log.ToArray();
}
public void ClearLog()
{
_log.Clear();
}
protected override void OnEventSourceCreated(EventSource eventSource)
{
if (eventSource.Name == BinaryFormatterEventSourceName)
{
EnableEvents(eventSource, EventLevel.Verbose);
}
base.OnEventSourceCreated(eventSource);
}
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
// The test project is parallelized. We want to filter to only events that fired
// on the current thread, otherwise we could throw off the test results.
if (Thread.CurrentThread != _activeThread)
{
return;
}
AddToLog($"{eventData.EventName} [{eventData.Opcode}, {(int)eventData.Keywords & int.MaxValue:X8}]: {ParsePayload(eventData.Payload)}");
base.OnEventWritten(eventData);
}
private static string ParsePayload(IReadOnlyCollection<object> collection)
{
if (collection?.Count > 0)
{
return string.Join("; ", collection.Select(o => o?.ToString() ?? "<null>"));
}
else
{
return "<no payload>";
}
}
}
[Serializable]
private class Person
{
public string Name { get; set; }
public Address HomeAddress { get; set; }
}
[Serializable]
private class Address
{
public string Street { get; set; }
public string City { get; set; }
}
[Serializable]
public class ClassWithNestedDeserialization : ISerializable
{
public ClassWithNestedDeserialization()
{
}
protected ClassWithNestedDeserialization(SerializationInfo info, StreamingContext context)
{
byte[] serializedData = (byte[])info.GetValue("SomeField", typeof(byte[]));
MemoryStream ms = new MemoryStream(serializedData);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Deserialize(ms); // should deserialize an 'Address' instance
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, new Address());
info.AddValue("SomeField", ms.ToArray());
}
}
}
}