Skip to content

Commit

Permalink
Merge pull request #429 from Washi1337/issue/hashcode-version-exclusion
Browse files Browse the repository at this point in the history
Exclude AssemblyDescriptor::Version in hashcode computation when relaxing version comparisons
  • Loading branch information
Washi1337 authored Apr 29, 2023
2 parents c81797d + 64acd1c commit 0945321
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
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

0 comments on commit 0945321

Please sign in to comment.