From 252db15d3d99d274f791e9b705252b3fb7eb127f Mon Sep 17 00:00:00 2001 From: Tom Meschter Date: Mon, 4 May 2015 15:59:48 -0700 Subject: [PATCH] Add a method to check for strong-named assemblies Add a utility method to check for strong-named assemblies. `AssemblyUtilities.HasStrongName` uses the metadata reader to check if the assembly at a given path has a strong name or not. This commit includes the code itself and a couple of unit tests. A later commit will wire it into the compilers/VS. --- .../AssemblyUtilitiesTests.cs | 18 ++++++++++++++ .../Core/Portable/AssemblyUtilities.cs | 24 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/Compilers/Core/CodeAnalysisTest/AssemblyUtilitiesTests.cs b/src/Compilers/Core/CodeAnalysisTest/AssemblyUtilitiesTests.cs index d441178c11d..658a1080045 100644 --- a/src/Compilers/Core/CodeAnalysisTest/AssemblyUtilitiesTests.cs +++ b/src/Compilers/Core/CodeAnalysisTest/AssemblyUtilitiesTests.cs @@ -213,5 +213,23 @@ public void IdentifyMissingDependencies_MultipleMissing() Assert.Contains("mscorlib", results); Assert.Contains("Gamma", results); } + + [Fact] + public void HasStrongName_False() + { + var directory = Temp.CreateDirectory(); + + var alphaDll = directory.CreateFile("Alpha.dll").WriteAllBytes(TestResources.AssemblyLoadTests.AssemblyLoadTests.Alpha); + + Assert.False(AssemblyUtilities.HasStrongName(alphaDll.Path)); + } + + [Fact] + public void HasStrongName_True() + { + string path = Assembly.GetExecutingAssembly().Location; + + Assert.True(AssemblyUtilities.HasStrongName(path)); + } } } diff --git a/src/Compilers/Core/Portable/AssemblyUtilities.cs b/src/Compilers/Core/Portable/AssemblyUtilities.cs index 89dbf2970b1..61ffa0369e3 100644 --- a/src/Compilers/Core/Portable/AssemblyUtilities.cs +++ b/src/Compilers/Core/Portable/AssemblyUtilities.cs @@ -130,6 +130,10 @@ public static ImmutableArray FindSatelliteAssemblies(string filePath) return builder.ToImmutable(); } + /// + /// Given a path to an assembly and a set of paths to possible dependencies, + /// identifies which of the assembly's references are missing. + /// public static ImmutableArray IdentifyMissingDependencies(string assemblyPath, IEnumerable assemblySet) { if (assemblyPath == null) @@ -168,5 +172,25 @@ public static ImmutableArray IdentifyMissingDependencies(strin return ImmutableArray.CreateRange(assemblyReferences); } + + /// + /// Given a path to an assembly, returns true if the assembly has a strong + /// name (either a full key or a token) or false otherwise. + /// + public static bool HasStrongName(string assemblyPath) + { + if (assemblyPath == null) + { + throw new ArgumentNullException(nameof(assemblyPath)); + } + + using (var reader = new PEReader(FileUtilities.OpenRead(assemblyPath))) + { + var metadataReader = reader.GetMetadataReader(); + var assemblyDefinition = metadataReader.ReadAssemblyIdentityOrThrow(); + + return assemblyDefinition.IsStrongName; + } + } } } -- GitLab