Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test where the manifest for collections of a known type aren't … #346

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 58 additions & 28 deletions src/Hyperion.Tests/Bugs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Bugs(ITestOutputHelper output)
{
_output = output;
}

#region issue 58

public enum TrustLevel { Unknown, Suspicious, Partial, Fully }
Expand Down Expand Up @@ -109,15 +109,15 @@ 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);
Expand Down Expand Up @@ -148,11 +148,11 @@ public void TypeEx_ToQualifiedAssemblyName_should_strip_version_correctly_for_ms
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()
{
Expand Down Expand Up @@ -280,16 +280,17 @@ class Temp : IEquatable<Temp>
public int[,] IntIntArray { get; set; }
public Poco Poco { get; set; }
public string String { get; set; }
public Dictionary<int,string> Dictionary { 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 List<Poco> PocoList { get; set; }

public bool Equals(Temp other)
{
if (other == null)
if (other == null)
throw new Exception("Equals failed.");
if (ReferenceEquals(this, other))
throw new Exception("Equals failed.");
Expand All @@ -305,19 +306,19 @@ public bool Equals(Temp other)
}
}

if (Exception.GetType() != other.Exception.GetType())
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
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];
Expand All @@ -326,7 +327,8 @@ public bool Equals(Temp other)
if (!arr.GetValue(j).Equals(oArr.GetValue(j)))
throw new Exception("Equals failed.");
}
} else if (!SubArray[i].Equals(other.SubArray[i]))
}
else if (!SubArray[i].Equals(other.SubArray[i]))
throw new Exception("Equals failed.");
}

Expand All @@ -345,15 +347,17 @@ public bool Equals(Temp other)
if (other.Delegate(2, 2) != 4)
throw new Exception("Equals failed.");

if(!IntArray.SequenceEqual(other.IntArray))
if (!IntArray.SequenceEqual(other.IntArray))
throw new Exception("Equals failed.");
if(!Equals(Poco, other.Poco))
if (!Equals(Poco, other.Poco))
throw new Exception("Equals failed.");
if (String != other.String)
throw new Exception("Equals failed.");
if(!TestEnum.SequenceEqual(other.TestEnum))
if (!TestEnum.SequenceEqual(other.TestEnum))
throw new Exception("Equals failed.");
if(!ImmutableList.SequenceEqual(other.ImmutableList))
if (!ImmutableList.SequenceEqual(other.ImmutableList))
throw new Exception("Equals failed.");
if (!PocoList.SequenceEqual(other.PocoList))
throw new Exception("Equals failed.");

return true;
Expand All @@ -364,7 +368,7 @@ 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);
return Equals((Temp)obj);
}

public override int GetHashCode()
Expand All @@ -381,6 +385,7 @@ public override int GetHashCode()
hashCode = (hashCode * 397) ^ (TestEnum != null ? TestEnum.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Exception != null ? Exception.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (ImmutableList != null ? ImmutableList.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (PocoList != null ? PocoList.GetHashCode() : 0);
return hashCode;
}
}
Expand All @@ -402,13 +407,13 @@ public Poco(int intValue, string stringValue)

public bool Equals(Poco other)
{
if (ReferenceEquals(null, other))
if (ReferenceEquals(null, other))
throw new Exception("Equals failed.");
if (ReferenceEquals(this, other))
if (ReferenceEquals(this, other))
throw new Exception("Equals failed.");
if(Int != other.Int)
if (Int != other.Int)
throw new Exception("Equals failed.");
if(String != other.String)
if (String != other.String)
throw new Exception("Equals failed.");
return true;
}
Expand All @@ -418,7 +423,7 @@ 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);
return Equals((Poco)obj);
}

public override int GetHashCode()
Expand All @@ -437,20 +442,20 @@ public void WritesManifestEvenIfKnown()
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}},
IntArray = new[] { 1, 2, 3, 4, 5 },
IntIntArray = new[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } },
Poco = new Poco(999, "666"),
String = "huhu",
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},
TestEnum = new[] { 4, 8, 9, 3, 2 },
Exception = new ArgumentException("Test Exception", new IndexOutOfRangeException("-999")),
ImmutableList = new [] {9, 4, 6, 2, 5}.ToImmutableList(),
ImmutableList = new[] { 9, 4, 6, 2, 5 }.ToImmutableList(),
ImmutableDictionary = new Dictionary<int, string>
{
{ 666, "b" },
Expand All @@ -466,7 +471,7 @@ public void WritesManifestEvenIfKnown()
typeof(Dictionary<int, string>),
typeof(DictionaryEntry),
typeof(KeyValuePair<int,string>),
typeof(Temp),
typeof(Temp),
typeof(TestDelegate),
typeof(Enumerable),
typeof(IEnumerable<int>),
Expand Down Expand Up @@ -507,5 +512,30 @@ public void ShouldOrderFieldsByOrdinal()
}

#endregion

[Fact]
public void DontWriteManifestForCollectionOfKnownTypes()
{
var stream = new MemoryStream();
var msg = new Temp
{
PocoList = new List<Poco>() {
new Poco(999, "666"),
},
};
var serializer = new Serializer(new SerializerOptions(knownTypes: new[]
{
typeof(Temp),
typeof(Poco),
}));
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.List", text);
Assert.Equal(msg, res);
}
}
}