Skip to content
This repository was archived by the owner on Mar 19, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Added bad keyfile diagnostic handling
  • Loading branch information
jnm2 committed Feb 15, 2017
commit a9703a21e08bf324b395cfaab9e6aa6a8eb027ed
4 changes: 4 additions & 0 deletions ILMerge.Tests/Helpers/TempFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ namespace ILMerging.Tests.Helpers
[DebuggerDisplay("{ToString(),nq}")]
public sealed class TempFile : IDisposable
{
public TempFile() : this(System.IO.Path.GetTempFileName())
{
}

public TempFile(string path)
{
this.path = path;
Expand Down
5 changes: 5 additions & 0 deletions ILMerge.Tests/ILMerge.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<Compile Include="Helpers\TempFile.cs" />
<Compile Include="KeyTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestFiles.cs" />
</ItemGroup>
<ItemGroup>
<None Include="project.json" />
Expand All @@ -73,6 +74,10 @@
<Project>{84b41834-efde-4e52-a8ce-b51dfcb1d1b2}</Project>
<Name>ILMerge</Name>
</ProjectReference>
<ProjectReference Include="..\System.Compiler\System.Compiler.csproj">
<Project>{D7E16B38-3893-4EEF-847F-A3BE807E9546}</Project>
<Name>System.Compiler</Name>
</ProjectReference>
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
Expand Down
3 changes: 1 addition & 2 deletions ILMerge.Tests/Integration/ConsoleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ public void No_DLL_load_crashes_when_given_PFX(bool withMscorsnInPath)
{
var ilMergeExePath = typeof(ILMerge).Assembly.Location;
var inputAssembly = Assembly.GetExecutingAssembly();
var keyPath = "test.pfx";

using (var outputFile = TempFile.WithExtension(".dll"))
{
var startInfo = new ProcessStartInfo(
ilMergeExePath,
$"{ShadowCopyUtils.GenerateILMergeLibCliSwitches(inputAssembly)} /keyfile:\"{keyPath}\" /out:\"{outputFile}\" \"{inputAssembly.Location}\"")
$"{ShadowCopyUtils.GenerateILMergeLibCliSwitches(inputAssembly)} /keyfile:\"{TestFiles.TestPfx}\" /out:\"{outputFile}\" \"{inputAssembly.Location}\"")
{
WorkingDirectory = Path.GetDirectoryName(inputAssembly.Location)
};
Expand Down
29 changes: 25 additions & 4 deletions ILMerge.Tests/KeyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,41 @@ namespace ILMerging.Tests
[TestFixture]
public sealed class KeyTests
{
private const string KeyFile = "test.snk";

[Test]
public void Can_sign_using_keyfile()
{
using (var outputFile = TempFile.WithExtension(".dll"))
{
var ilMerge = new ILMerge { KeyFile = KeyFile, OutputFile = outputFile };
var ilMerge = new ILMerge { KeyFile = TestFiles.TestSnk, OutputFile = outputFile };
ilMerge.SetUpInputAssemblyForTest(Assembly.GetExecutingAssembly());
ilMerge.Merge();

Assert.That(
AssemblyName.GetAssemblyName(outputFile).GetPublicKey(),
Is.EqualTo(new StrongNameKeyPair(File.ReadAllBytes(KeyFile)).PublicKey));
Is.EqualTo(new StrongNameKeyPair(File.ReadAllBytes(TestFiles.TestSnk)).PublicKey));
}
}

[Test]
public void Bad_keyfile_gives_diagnostic_warning()
{
using (var logFile = new TempFile())
using (var outputFile = TempFile.WithExtension(".dll"))
{
var ilMerge = new ILMerge
{
KeyFile = TestFiles.TestPfx,
OutputFile = outputFile,
LogFile = logFile
};
ilMerge.SetUpInputAssemblyForTest(Assembly.GetExecutingAssembly());

ilMerge.Merge();

var logText = File.ReadAllText(logFile);
Assert.That(logText, Contains.Substring("Unable to obtain public key for StrongNameKeyPair."));
Assert.That(logText, Contains.Substring("PFX"));
Assert.That(logText, Contains.Substring("key container"));
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions ILMerge.Tests/TestFiles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ILMerging.Tests
{
internal static class TestFiles
{
public const string TestSnk = "test.snk";
public const string TestPfx = "test.pfx";
}
}
7 changes: 5 additions & 2 deletions ILMerge/ILMerge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2316,8 +2316,11 @@ public void Merge() {
WriteToLog("ILMerge: Signed assembly '{0}' with a strong name.", outputFileName);
}
}
catch (AssemblyCouldNotBeSignedException) {
WriteToLog("ILMerge error: The target assembly was not able to be strongly named (did you forget to use the /delaysign option?).");
catch (AssemblyCouldNotBeSignedException ex) {
if (ex.Message == AssemblyCouldNotBeSignedException.DefaultMessage)
WriteToLog("ILMerge error: The target assembly was not able to be strongly named (did you forget to use the /delaysign option?).");
else
WriteToLog("ILMerge error: The target assembly was not able to be strongly named. " + ex.Message);
}
}
#endregion
Expand Down
20 changes: 16 additions & 4 deletions System.Compiler/Writer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5299,13 +5299,17 @@ public class KeyFileNotFoundException : System.ArgumentException{}

public class AssemblyCouldNotBeSignedException : System.ApplicationException
{
private const string AssemblyCouldNotBeSignedMessage = "Assembly could not be signed.";
public const string DefaultMessage = "Assembly could not be signed.";

public AssemblyCouldNotBeSignedException() : base(AssemblyCouldNotBeSignedMessage)
public AssemblyCouldNotBeSignedException() : base(DefaultMessage)
{
}

public AssemblyCouldNotBeSignedException(Exception innerException) : base(AssemblyCouldNotBeSignedMessage, innerException)
public AssemblyCouldNotBeSignedException(Exception innerException) : base(DefaultMessage, innerException)
{
}

public AssemblyCouldNotBeSignedException(string message, Exception innerException) : base(message, innerException)
{
}
}
Expand Down Expand Up @@ -5349,7 +5353,15 @@ private static void WritePE(string/*!*/ location, bool writeDebugSymbols, Module
}else
keyFileNameDoesNotExist = true;
}
assem.PublicKeyOrToken = Writer.GetPublicKey(assem);
try
{
assem.PublicKeyOrToken = Writer.GetPublicKey(assem);
}
catch (ArgumentException ex)
{
throw assem.KeyBlob != null ? new AssemblyCouldNotBeSignedException(ex.Message + " (If you are trying to use a PFX, use the VS_KEY_* key container instead of the key file.)", ex) :
new AssemblyCouldNotBeSignedException(ex);
}
}
using (FileStream exeFstream = new FileStream(location, FileMode.Create, FileAccess.Write, FileShare.None)){
string debugSymbolsLocation = writeDebugSymbols ? Path.ChangeExtension(location, "pdb") : null;
Expand Down