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

Exclude AssemblyDescriptor::Version in hashcode computation when relaxing version comparisons #429

Merged
merged 3 commits into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ public int GetHashCode(AssemblyDescriptor obj)
int hashCode = obj.Name is null ? 0 : obj.Name.GetHashCode();
hashCode = (hashCode * 397) ^ (obj.Culture is not null ? obj.Culture.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (int) TableIndex.AssemblyRef;
hashCode = (hashCode * 397) ^ obj.Version.GetHashCode();

if (!AcceptNewerAssemblyVersionNumbers && !AcceptOlderAssemblyVersionNumbers)
hashCode = (hashCode * 397) ^ obj.Version.GetHashCode();

hashCode = (hashCode * 397) ^ (int) obj.Attributes;

byte[]? publicKeyToken = obj.GetPublicKeyToken();
Expand Down
6 changes: 3 additions & 3 deletions test/AsmResolver.DotNet.Tests/Bundles/BundleManifestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,21 @@ public void SameManifestContentsShouldResultInSameBundleID()
Assert.Equal(manifest.BundleID, newManifest.GenerateDeterministicBundleID());
}

[Fact]
[SkippableFact]
public void PatchAndRepackageExistingBundleV1()
{
Skip.IfNot(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
AssertPatchAndRepackageChangesOutput(Properties.Resources.HelloWorld_SingleFile_V1);
}

[Fact]
[SkippableFact]
public void PatchAndRepackageExistingBundleV2()
{
Skip.IfNot(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
AssertPatchAndRepackageChangesOutput(Properties.Resources.HelloWorld_SingleFile_V2);
}

[Fact]
[SkippableFact]
public void PatchAndRepackageExistingBundleV6()
{
Skip.IfNot(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
Expand Down
40 changes: 40 additions & 0 deletions test/AsmResolver.DotNet.Tests/Signatures/SignatureComparerTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AsmResolver.DotNet.Signatures;
using AsmResolver.DotNet.Signatures.Types;
Expand Down Expand Up @@ -165,6 +166,45 @@ public void MatchForwardedNestedTypes()
Assert.NotEqual(type2, resolvedType1, _comparer); // Fails
}

[Fact]
public void AssemblyHashCodeStrict()
{
var assembly1 = new AssemblyReference("SomeAssembly", new Version(1, 2, 3, 4));
var assembly2 = new AssemblyReference("SomeAssembly", new Version(1, 2, 3, 4));

Assert.Equal(
_comparer.GetHashCode((AssemblyDescriptor) assembly1),
_comparer.GetHashCode((AssemblyDescriptor) assembly2));
}

[Fact]
public void AssemblyHashCodeVersionAgnostic()
{
var assembly1 = new AssemblyReference("SomeAssembly", new Version(1, 2, 3, 4));
var assembly2 = new AssemblyReference("SomeAssembly", new Version(5, 6, 7, 8));

var comparer = new SignatureComparer(SignatureComparisonFlags.VersionAgnostic);
Assert.Equal(
comparer.GetHashCode((AssemblyDescriptor) assembly1),
comparer.GetHashCode((AssemblyDescriptor) assembly2));
}

[Fact]
public void CorlibComparison()
{
// https://github.com/Washi1337/AsmResolver/issues/427

var comparer = new SignatureComparer(SignatureComparisonFlags.VersionAgnostic);

var reference1 = KnownCorLibs.SystemRuntime_v5_0_0_0;
var reference2 = KnownCorLibs.SystemRuntime_v6_0_0_0;
Assert.Equal(reference1, reference2, comparer);

var set = new HashSet<AssemblyReference>(comparer);
Assert.True(set.Add(reference1));
Assert.False(set.Add(reference2));
}

private class NestedTypes
{
public class FirstType
Expand Down