|
11 | 11 | using System.Collections.Generic;
|
12 | 12 | using System.Collections.Immutable;
|
13 | 13 | using System.IO;
|
| 14 | +using System.Reflection; |
| 15 | +using System.Reflection.Emit; |
| 16 | +using System.Text; |
| 17 | +using FluentAssertions; |
| 18 | +using Hyperion.Extensions; |
14 | 19 | using Xunit;
|
15 | 20 |
|
16 | 21 | namespace Hyperion.Tests
|
@@ -85,6 +90,69 @@ public void CanSerializeMessageWithByte()
|
85 | 90 | var res = serializer.Deserialize(stream);
|
86 | 91 | }
|
87 | 92 |
|
| 93 | + /// <summary> |
| 94 | + /// Fix for https://github.com/akkadotnet/Hyperion/issues/144 |
| 95 | + /// </summary> |
| 96 | + [Fact] |
| 97 | + public void CanFindTypeByManifest_WhenManifestContainsUnknownAssemblyVersion() |
| 98 | + { |
| 99 | + var serializer = new Serializer(new SerializerOptions(versionTolerance: true, preserveObjectReferences: true)); |
| 100 | + var type = typeof(ByteMessage); |
| 101 | + |
| 102 | + MemoryStream GetStreamForManifest(string manifest) |
| 103 | + { |
| 104 | + var stream = new MemoryStream(); |
| 105 | + stream.WriteLengthEncodedByteArray(manifest.ToUtf8Bytes(), serializer.GetSerializerSession()); |
| 106 | + stream.Position = 0; |
| 107 | + return stream; |
| 108 | + } |
| 109 | + |
| 110 | + // This is used in serialized manifest, should be something like 'Hyperion.Tests.Bugs+ByteMessage, Hyperion.Tests' |
| 111 | + var shortName = type.GetShortAssemblyQualifiedName(); |
| 112 | + var shortNameStream = GetStreamForManifest(shortName); |
| 113 | + // Something like 'Hyperion.Tests.Bugs+ByteMessage, Hyperion.Tests, Version=0.9.11.0, Culture=neutral, PublicKeyToken=null' |
| 114 | + var fullName = type.AssemblyQualifiedName; |
| 115 | + var fullNameStream = GetStreamForManifest(fullName); |
| 116 | + // Set bad assembly version to make deserialization fail |
| 117 | + var fullNameWithUnknownVersion = fullName.Remove(fullName.IndexOf(", Version=")) + ", Version=999999, Culture=neutral, PublicKeyToken=null"; |
| 118 | + var fullNameWithUnknownVersionStream = GetStreamForManifest(fullNameWithUnknownVersion); |
| 119 | + |
| 120 | + this.Invoking(_ => TypeEx.GetTypeFromManifestFull(shortNameStream, serializer.GetDeserializerSession())) |
| 121 | + .Should().NotThrow("When assembly short name is specified in manifest, should work"); |
| 122 | + this.Invoking(_ => TypeEx.GetTypeFromManifestFull(fullNameStream, serializer.GetDeserializerSession())) |
| 123 | + .Should().NotThrow("When assembly fully qualified name specified and name is correct, should work even before fix"); |
| 124 | + // This one was initially failing |
| 125 | + this.Invoking(_ => TypeEx.GetTypeFromManifestFull(fullNameWithUnknownVersionStream, serializer.GetDeserializerSession())) |
| 126 | + .Should().NotThrow("When assembly fully qualified name specified and unknown/wrong, type should be detected anyway"); |
| 127 | + } |
| 128 | + |
| 129 | + [Fact] |
| 130 | + public void TypeEx_ToQualifiedAssemblyName_should_strip_version_correctly_for_mscorlib_substitution() |
| 131 | + { |
| 132 | + var version = TypeEx.ToQualifiedAssemblyName( |
| 133 | + "System.Collections.Immutable.ImmutableDictionary`2[[System.String, mscorlib,%core%],[System.Int32, mscorlib,%core%]]," + |
| 134 | + " System.Collections.Immutable, Version=1.2.1.0, PublicKeyToken=b03f5f7f11d50a3a", |
| 135 | + ignoreAssemblyVersion: true); |
| 136 | + |
| 137 | + var coreAssemblyName = typeof(TypeEx).GetField("CoreAssemblyName", BindingFlags.Static | BindingFlags.NonPublic)?.GetValue(null); |
| 138 | + if (coreAssemblyName == null) |
| 139 | + throw new Exception($"CoreAssemblyName private static field does not exist in {nameof(TypeEx)} class anymore"); |
| 140 | + |
| 141 | + version.Should().Be("System.Collections.Immutable.ImmutableDictionary`2" + |
| 142 | + $"[[System.String, mscorlib{coreAssemblyName}],[System.Int32, mscorlib{coreAssemblyName}]], System.Collections.Immutable"); |
| 143 | + } |
| 144 | + |
| 145 | + [Fact] |
| 146 | + public void TypeEx_ToQualifiedAssemblyName_should_strip_version_correctly_for_multiple_versions_specified() |
| 147 | + { |
| 148 | + var version = TypeEx.ToQualifiedAssemblyName( |
| 149 | + "System.Collections.Immutable.ImmutableList`1[[Foo.Bar, Foo, Version=2019.12.10.1]], " + |
| 150 | + "System.Collections.Immutable, Version=1.2.2.0, PublicKeyToken=b03f5f7f11d50a3a", |
| 151 | + ignoreAssemblyVersion: true); |
| 152 | + |
| 153 | + version.Should().Be("System.Collections.Immutable.ImmutableList`1[[Foo.Bar, Foo]], System.Collections.Immutable"); |
| 154 | + } |
| 155 | + |
88 | 156 | [Fact]
|
89 | 157 | public void CanSerialieCustomType_bug()
|
90 | 158 | {
|
|
0 commit comments