Browse Source

Add support for strong-name keys in WholeProjectDecompiler and RoundtripAssembly tests.

pull/1633/head
Siegfried Pammer 6 years ago
parent
commit
e1bc205d3a
  1. 22
      ICSharpCode.Decompiler.Tests/Helpers/Tester.cs
  2. 11
      ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs
  3. 14
      ICSharpCode.Decompiler/CSharp/WholeProjectDecompiler.cs

22
ICSharpCode.Decompiler.Tests/Helpers/Tester.cs

@ -534,5 +534,27 @@ namespace ICSharpCode.Decompiler.Tests.Helpers @@ -534,5 +534,27 @@ namespace ICSharpCode.Decompiler.Tests.Helpers
// If the last try still fails, don't catch the exception
action();
}
public static void SignAssembly(string assemblyPath, string keyFilePath)
{
string snPath = SdkUtility.GetSdkPath("sn.exe");
ProcessStartInfo info = new ProcessStartInfo(snPath);
info.Arguments = $"-R \"{assemblyPath}\" \"{keyFilePath}\"";
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
Process process = Process.Start(info);
var outputTask = process.StandardOutput.ReadToEndAsync();
var errorTask = process.StandardError.ReadToEndAsync();
Task.WaitAll(outputTask, errorTask);
process.WaitForExit();
Console.WriteLine("output: " + outputTask.Result);
Console.WriteLine("errors: " + errorTask.Result);
}
}
}

11
ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs

@ -102,11 +102,11 @@ namespace ICSharpCode.Decompiler.Tests @@ -102,11 +102,11 @@ namespace ICSharpCode.Decompiler.Tests
RunWithOutput("Random Tests\\TestCases", "TestCase-1.exe");
}
void RunWithTest(string dir, string fileToRoundtrip, string fileToTest)
void RunWithTest(string dir, string fileToRoundtrip, string fileToTest, string keyFile = null)
{
RunInternal(dir, fileToRoundtrip, outputDir => RunTest(outputDir, fileToTest));
RunInternal(dir, fileToRoundtrip, outputDir => RunTest(outputDir, fileToTest), keyFile);
}
void RunWithOutput(string dir, string fileToRoundtrip)
{
string inputDir = Path.Combine(TestDir, dir);
@ -114,7 +114,7 @@ namespace ICSharpCode.Decompiler.Tests @@ -114,7 +114,7 @@ namespace ICSharpCode.Decompiler.Tests
outputDir => Tester.RunAndCompareOutput(fileToRoundtrip, Path.Combine(inputDir, fileToRoundtrip), Path.Combine(outputDir, fileToRoundtrip)));
}
void RunInternal(string dir, string fileToRoundtrip, Action<string> testAction)
void RunInternal(string dir, string fileToRoundtrip, Action<string> testAction, string snkFilePath = null)
{
if (!Directory.Exists(TestDir)) {
Assert.Ignore($"Assembly-roundtrip test ignored: test directory '{TestDir}' needs to be checked out separately." + Environment.NewLine +
@ -152,6 +152,9 @@ namespace ICSharpCode.Decompiler.Tests @@ -152,6 +152,9 @@ namespace ICSharpCode.Decompiler.Tests
decompiler.Settings = new DecompilerSettings(LanguageVersion.CSharp7_3);
// use a fixed GUID so that we can diff the output between different ILSpy runs without spurious changes
decompiler.ProjectGuid = Guid.Parse("{127C83E4-4587-4CF9-ADCA-799875F3DFE6}");
if (snkFilePath != null) {
decompiler.StrongNameKeyFile = Path.Combine(inputDir, snkFilePath);
}
decompiler.DecompileProject(module, decompiledDir);
Console.WriteLine($"Decompiled {fileToRoundtrip} in {w.Elapsed.TotalSeconds:f2}");
projectFile = Path.Combine(decompiledDir, module.Name + ".csproj");

14
ICSharpCode.Decompiler/CSharp/WholeProjectDecompiler.cs

@ -79,6 +79,12 @@ namespace ICSharpCode.Decompiler.CSharp @@ -79,6 +79,12 @@ namespace ICSharpCode.Decompiler.CSharp
/// </summary>
public Guid? ProjectGuid { get; set; }
/// <summary>
/// Path to the snk file to use for signing.
/// <c>null</c> to not sign.
/// </summary>
public string StrongNameKeyFile { get; set; }
public int MaxDegreeOfParallelism { get; set; } = Environment.ProcessorCount;
#endregion
@ -111,6 +117,9 @@ namespace ICSharpCode.Decompiler.CSharp @@ -111,6 +117,9 @@ namespace ICSharpCode.Decompiler.CSharp
directories.Clear();
var files = WriteCodeFilesInProject(moduleDefinition, cancellationToken).ToList();
files.AddRange(WriteResourceFilesInProject(moduleDefinition));
if (StrongNameKeyFile != null) {
File.Copy(StrongNameKeyFile, Path.Combine(targetDirectory, Path.GetFileName(StrongNameKeyFile)));
}
return WriteProjectFile(projectFileWriter, files, moduleDefinition);
}
@ -215,6 +224,11 @@ namespace ICSharpCode.Decompiler.CSharp @@ -215,6 +224,11 @@ namespace ICSharpCode.Decompiler.CSharp
}
w.WriteElementString("WarningLevel", "4");
w.WriteElementString("AllowUnsafeBlocks", "True");
if (StrongNameKeyFile != null) {
w.WriteElementString("SignAssembly", "True");
w.WriteElementString("AssemblyOriginatorKeyFile", Path.GetFileName(StrongNameKeyFile));
}
w.WriteEndElement(); // </PropertyGroup>

Loading…
Cancel
Save