-
Notifications
You must be signed in to change notification settings - Fork 62
/
Bugs.cs
493 lines (435 loc) · 20.2 KB
/
Bugs.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#region copyright
// -----------------------------------------------------------------------
// <copyright file="Bugs.cs" company="Akka.NET Team">
// Copyright (C) 2015-2016 AsynkronIT <https://github.com/AsynkronIT>
// Copyright (C) 2016-2016 Akka.NET Team <https://github.com/akkadotnet>
// </copyright>
// -----------------------------------------------------------------------
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using FluentAssertions;
using Hyperion.Extensions;
using Xunit;
using Xunit.Abstractions;
namespace Hyperion.Tests
{
public class Bugs : TestBase
{
private readonly ITestOutputHelper _output;
public Bugs(ITestOutputHelper output)
{
_output = output;
}
#region issue 58
public enum TrustLevel { Unknown, Suspicious, Partial, Fully }
public interface IContainer<out T>
{
T Value { get; }
TrustLevel TrustLevel { get; }
}
public class Container<T> : IContainer<T>
{
public T Value { get; }
public TrustLevel TrustLevel { get; }
public Container(T value, TrustLevel trustLevel)
{
Value = value;
TrustLevel = trustLevel;
}
}
public class ProvisioningResultMessage<T>
{
private T Result { get; }
public ProvisioningResultMessage(T result)
{
Result = result;
}
}
#endregion
public class ByteMessage
{
public DateTime UtcTime { get; }
public long LongValue { get; }
public byte ByteValue { get; }
public ByteMessage(DateTime utcTime, byte byteValue, long longValue)
{
UtcTime = utcTime;
ByteValue = byteValue;
LongValue = longValue;
}
public override bool Equals(object obj)
{
var msg = obj as ByteMessage;
return msg != null && Equals(msg);
}
public bool Equals(ByteMessage other)
{
return UtcTime.Equals(other.UtcTime) && LongValue.Equals(other.LongValue) && ByteValue.Equals(other.ByteValue);
}
}
[Fact]
public void CanSerializeMessageWithByte()
{
var stream = new MemoryStream();
var msg = new ByteMessage(DateTime.UtcNow, 1, 2);
var serializer = new Serializer(new SerializerOptions(versionTolerance: true, preserveObjectReferences: true));
serializer.Serialize(msg, stream);
stream.Position = 0;
var res = serializer.Deserialize(stream);
}
/// <summary>
/// Fix for https://github.com/akkadotnet/Hyperion/issues/144
/// </summary>
[Fact]
public void CanFindTypeByManifest_WhenManifestContainsUnknownAssemblyVersion()
{
var serializer = new Serializer(new SerializerOptions(versionTolerance: true, preserveObjectReferences: true));
var type = typeof(ByteMessage);
MemoryStream GetStreamForManifest(string manifest)
{
var stream = new MemoryStream();
stream.WriteLengthEncodedByteArray(manifest.ToUtf8Bytes(), serializer.GetSerializerSession());
stream.Position = 0;
return stream;
}
// This is used in serialized manifest, should be something like 'Hyperion.Tests.Bugs+ByteMessage, Hyperion.Tests'
var shortName = type.GetShortAssemblyQualifiedName();
var shortNameStream = GetStreamForManifest(shortName);
// Something like 'Hyperion.Tests.Bugs+ByteMessage, Hyperion.Tests, Version=0.9.11.0, Culture=neutral, PublicKeyToken=null'
var fullName = type.AssemblyQualifiedName;
var fullNameStream = GetStreamForManifest(fullName);
// Set bad assembly version to make deserialization fail
var fullNameWithUnknownVersion = fullName.Remove(fullName.IndexOf(", Version=")) + ", Version=999999, Culture=neutral, PublicKeyToken=null";
var fullNameWithUnknownVersionStream = GetStreamForManifest(fullNameWithUnknownVersion);
this.Invoking(_ => TypeEx.GetTypeFromManifestFull(shortNameStream, serializer.GetDeserializerSession()))
.Should().NotThrow("When assembly short name is specified in manifest, should work");
this.Invoking(_ => TypeEx.GetTypeFromManifestFull(fullNameStream, serializer.GetDeserializerSession()))
.Should().NotThrow("When assembly fully qualified name specified and name is correct, should work even before fix");
// This one was initially failing
this.Invoking(_ => TypeEx.GetTypeFromManifestFull(fullNameWithUnknownVersionStream, serializer.GetDeserializerSession()))
.Should().NotThrow("When assembly fully qualified name specified and unknown/wrong, type should be detected anyway");
}
[Fact]
public void TypeEx_ToQualifiedAssemblyName_should_strip_version_correctly_for_mscorlib_substitution()
{
var version = TypeEx.ToQualifiedAssemblyName(
"System.Collections.Immutable.ImmutableDictionary`2[[System.String, mscorlib,%core%],[System.Int32, mscorlib,%core%]]," +
" System.Collections.Immutable, Version=1.2.1.0, PublicKeyToken=b03f5f7f11d50a3a",
ignoreAssemblyVersion: true);
var coreAssemblyName = typeof(TypeEx).GetField("CoreAssemblyName", BindingFlags.Static | BindingFlags.NonPublic)?.GetValue(null);
if (coreAssemblyName == null)
throw new Exception($"CoreAssemblyName private static field does not exist in {nameof(TypeEx)} class anymore");
version.Should().Be("System.Collections.Immutable.ImmutableDictionary`2" +
$"[[System.String, mscorlib{coreAssemblyName}],[System.Int32, mscorlib{coreAssemblyName}]], System.Collections.Immutable");
}
[Fact]
public void TypeEx_ToQualifiedAssemblyName_should_strip_version_correctly_for_multiple_versions_specified()
{
var version = TypeEx.ToQualifiedAssemblyName(
"System.Collections.Immutable.ImmutableList`1[[Foo.Bar, Foo, Version=2019.12.10.1]], " +
"System.Collections.Immutable, Version=1.2.2.0, PublicKeyToken=b03f5f7f11d50a3a",
ignoreAssemblyVersion: true);
version.Should().Be("System.Collections.Immutable.ImmutableList`1[[Foo.Bar, Foo]], System.Collections.Immutable");
}
[Fact]
public void CanSerialieCustomType_bug()
{
var stream = new MemoryStream();
var serializer = new Serializer(new SerializerOptions(versionTolerance: true, preserveObjectReferences: true));
var root = new Recover(SnapshotSelectionCriteria.Latest);
serializer.Serialize(root, stream);
stream.Position = 0;
var actual = serializer.Deserialize<Recover>(stream);
}
[Fact]
public void CanSerializeImmutableGenericInterfaces()
{
var serializer = new Serializer(new SerializerOptions(versionTolerance: true, preserveObjectReferences: true));
var names = new List<Container<string>>
{
new Container<string>("Mr", TrustLevel.Partial),
new Container<string>("Bob", TrustLevel.Partial),
new Container<string>("Smith", TrustLevel.Suspicious),
new Container<string>("Mrs", TrustLevel.Suspicious),
new Container<string>("Jane", TrustLevel.Suspicious),
new Container<string>("Smith", TrustLevel.Suspicious),
new Container<string>("Master", TrustLevel.Fully),
new Container<string>("Fred", TrustLevel.Fully),
new Container<string>("Smith", TrustLevel.Fully),
new Container<string>("Miss", TrustLevel.Partial),
new Container<string>("Sandra", TrustLevel.Partial),
new Container<string>("Smith", TrustLevel.Suspicious)
};
var message = new ProvisioningResultMessage<IEnumerable<Container<string>>>(names.ToImmutableArray());
using (var stream = new MemoryStream())
{
serializer.Serialize(message, stream);
stream.Position = 0;
var actual = serializer.Deserialize(stream);
}
}
[Fact]
public void CanSerializeUri()
{
var stream = new MemoryStream();
var msg = new Uri("http://localhost:9202/", UriKind.RelativeOrAbsolute);
var serializer = new Serializer(new SerializerOptions(preserveObjectReferences: true, versionTolerance: true));
serializer.Serialize(msg, stream);
stream.Position = 0;
var res = serializer.Deserialize(stream);
Assert.Equal(msg, res);
Assert.Equal(stream.Length, stream.Position);
}
#region Issue 117
[Fact]
public void CanSerializeColor()
{
var expected = Color.Aquamarine;
Serialize(expected);
Reset();
var actual = Deserialize<Color>();
Assert.Equal(expected, actual);
}
#endregion
public class SnapshotSelectionCriteria
{
public static SnapshotSelectionCriteria Latest { get; set; } = new SnapshotSelectionCriteria()
{
Foo = "hello",
};
public string Foo { get; set; }
}
#if SERIALIZATION
[Serializable]
#endif
public sealed class Recover
{
public static readonly Recover Default = new Recover(SnapshotSelectionCriteria.Latest);
public Recover(SnapshotSelectionCriteria fromSnapshot, long toSequenceNr = long.MaxValue, long replayMax = long.MaxValue)
{
FromSnapshot = fromSnapshot;
ToSequenceNr = toSequenceNr;
ReplayMax = replayMax;
}
/// <summary>
/// Criteria for selecting a saved snapshot from which recovery should start. Default is del youngest snapshot.
/// </summary>
public SnapshotSelectionCriteria FromSnapshot { get; private set; }
/// <summary>
/// Upper, inclusive sequence number bound. Default is no upper bound.
/// </summary>
public long ToSequenceNr { get; private set; }
/// <summary>
/// Maximum number of messages to replay. Default is no limit.
/// </summary>
public long ReplayMax { get; private set; }
}
delegate int TestDelegate(int x, int y);
class Temp : IEquatable<Temp>
{
public object[] SubArray { get; set; }
public int[] IntArray { get; set; }
public int[,] IntIntArray { get; set; }
public Poco Poco { get; set; }
public string String { get; set; }
public Dictionary<int,string> Dictionary { get; set; }
public TestDelegate Delegate { get; set; }
public IEnumerable<int> TestEnum { get; set; }
public Exception Exception { get; set; }
public ImmutableList<int> ImmutableList { get; set; }
public ImmutableDictionary<int, string> ImmutableDictionary { get; set; }
public bool Equals(Temp other)
{
if (other == null)
throw new Exception("Equals failed.");
if (ReferenceEquals(this, other))
throw new Exception("Equals failed.");
if (IntIntArray.Rank != other.IntIntArray.Rank)
throw new Exception("Equals failed.");
for (var i = 0; i < IntIntArray.Rank; ++i)
{
for (var j = 0; j < IntIntArray.GetLength(i); ++j)
{
if (IntIntArray[j, i] != other.IntIntArray[j, i])
throw new Exception("Equals failed.");
}
}
if (Exception.GetType() != other.Exception.GetType())
throw new Exception("Equals failed.");
if (Exception.Message != other.Exception.Message)
throw new Exception("Equals failed.");
if(Exception.InnerException != null
&& Exception.InnerException.GetType() != other.Exception.InnerException.GetType())
throw new Exception("Equals failed.");
for (var i = 0; i < SubArray.Length; i++)
{
if (SubArray[i].GetType() != other.SubArray[i].GetType())
throw new Exception("Equals failed.");
if (SubArray[i] is Array arr)
{
var oArr = (Array)other.SubArray[i];
for (var j = 0; j < arr.Length; ++j)
{
if (!arr.GetValue(j).Equals(oArr.GetValue(j)))
throw new Exception("Equals failed.");
}
} else if (!SubArray[i].Equals(other.SubArray[i]))
throw new Exception("Equals failed.");
}
foreach (var key in Dictionary.Keys)
{
if (!Dictionary[key].Equals(other.Dictionary[key]))
throw new Exception("Equals failed.");
}
foreach (var key in ImmutableDictionary.Keys)
{
if (!ImmutableDictionary[key].Equals(other.ImmutableDictionary[key]))
throw new Exception("Equals failed.");
}
if (other.Delegate(2, 2) != 4)
throw new Exception("Equals failed.");
if(!IntArray.SequenceEqual(other.IntArray))
throw new Exception("Equals failed.");
if(!Equals(Poco, other.Poco))
throw new Exception("Equals failed.");
if (String != other.String)
throw new Exception("Equals failed.");
if(!TestEnum.SequenceEqual(other.TestEnum))
throw new Exception("Equals failed.");
if(!ImmutableList.SequenceEqual(other.ImmutableList))
throw new Exception("Equals failed.");
return true;
}
public override bool Equals(object obj)
{
if (obj == null) throw new Exception("Equals failed.");
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) throw new Exception("Equals failed.");
return Equals((Temp) obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = (SubArray != null ? SubArray.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (IntArray != null ? IntArray.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (IntIntArray != null ? IntIntArray.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Poco != null ? Poco.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (String != null ? String.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Dictionary != null ? Dictionary.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Delegate != null ? Delegate.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (TestEnum != null ? TestEnum.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Exception != null ? Exception.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (ImmutableList != null ? ImmutableList.GetHashCode() : 0);
return hashCode;
}
}
}
class Poco : IEquatable<Poco>
{
public Poco()
{ }
public Poco(int intValue, string stringValue)
{
Int = intValue;
String = stringValue;
}
public int Int { get; set; }
public string String { get; set; }
public bool Equals(Poco other)
{
if (ReferenceEquals(null, other))
throw new Exception("Equals failed.");
if (ReferenceEquals(this, other))
throw new Exception("Equals failed.");
if(Int != other.Int)
throw new Exception("Equals failed.");
if(String != other.String)
throw new Exception("Equals failed.");
return true;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) throw new Exception("Equals failed.");
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) throw new Exception("Equals failed.");
return Equals((Poco) obj);
}
public override int GetHashCode()
{
unchecked
{
return (Int * 397) ^ (String != null ? String.GetHashCode() : 0);
}
}
}
[Fact]
public void WritesManifestEvenIfKnown()
{
var stream = new MemoryStream();
var msg = new Temp
{
SubArray = new object[] { 1, (byte)2, new object[] { 3 } },
IntArray = new [] {1, 2, 3, 4, 5},
IntIntArray = new [,] {{1, 2}, {3,4}, {5,6}, {7,8}},
Poco = new Poco(999, "666"),
String = "huhu",
Dictionary = new Dictionary<int, string>
{
{ 666, "b" },
{ 999, "testString" },
{ 42, "iMaGiNe" }
},
Delegate = (x, y) => x * y,
TestEnum = new[]{4,8,9,3,2},
Exception = new ArgumentException("Test Exception", new IndexOutOfRangeException("-999")),
ImmutableList = new [] {9, 4, 6, 2, 5}.ToImmutableList(),
ImmutableDictionary = new Dictionary<int, string>
{
{ 666, "b" },
{ 999, "testString" },
{ 42, "iMaGiNe" }
}.ToImmutableDictionary(),
};
var serializer = new Serializer(new SerializerOptions(knownTypes: new[]
{
typeof(object[]),
typeof(int[]),
typeof(int[,]),
typeof(Dictionary<int, string>),
typeof(DictionaryEntry),
typeof(KeyValuePair<int,string>),
typeof(Temp),
typeof(TestDelegate),
typeof(Enumerable),
typeof(IEnumerable<int>),
typeof(Exception),
typeof(ArgumentException),
typeof(IndexOutOfRangeException),
typeof(FieldInfo),
typeof(ImmutableList),
typeof(ImmutableList<int>),
typeof(ImmutableDictionary<int, string>),
typeof(MethodInfo),
typeof(PropertyInfo),
}));
serializer.Serialize(msg, stream);
stream.Position = 0;
var a = stream.ToArray();
var text = string.Join("", a.Select(x => x < 32 || x > 126 ? "" : ((char)x).ToString()));
_output.WriteLine(text);
var res = (Temp)serializer.Deserialize(stream);
Assert.DoesNotContain("System.Collections.Generic.Dictionary", text);
Assert.Equal(msg, res);
}
}
}