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. 9
      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
// If the last try still fails, don't catch the exception // If the last try still fails, don't catch the exception
action(); 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);
}
} }
} }

9
ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs

@ -102,9 +102,9 @@ namespace ICSharpCode.Decompiler.Tests
RunWithOutput("Random Tests\\TestCases", "TestCase-1.exe"); 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) void RunWithOutput(string dir, string fileToRoundtrip)
@ -114,7 +114,7 @@ namespace ICSharpCode.Decompiler.Tests
outputDir => Tester.RunAndCompareOutput(fileToRoundtrip, Path.Combine(inputDir, fileToRoundtrip), Path.Combine(outputDir, fileToRoundtrip))); 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)) { if (!Directory.Exists(TestDir)) {
Assert.Ignore($"Assembly-roundtrip test ignored: test directory '{TestDir}' needs to be checked out separately." + Environment.NewLine + 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
decompiler.Settings = new DecompilerSettings(LanguageVersion.CSharp7_3); 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 // 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}"); decompiler.ProjectGuid = Guid.Parse("{127C83E4-4587-4CF9-ADCA-799875F3DFE6}");
if (snkFilePath != null) {
decompiler.StrongNameKeyFile = Path.Combine(inputDir, snkFilePath);
}
decompiler.DecompileProject(module, decompiledDir); decompiler.DecompileProject(module, decompiledDir);
Console.WriteLine($"Decompiled {fileToRoundtrip} in {w.Elapsed.TotalSeconds:f2}"); Console.WriteLine($"Decompiled {fileToRoundtrip} in {w.Elapsed.TotalSeconds:f2}");
projectFile = Path.Combine(decompiledDir, module.Name + ".csproj"); projectFile = Path.Combine(decompiledDir, module.Name + ".csproj");

14
ICSharpCode.Decompiler/CSharp/WholeProjectDecompiler.cs

@ -79,6 +79,12 @@ namespace ICSharpCode.Decompiler.CSharp
/// </summary> /// </summary>
public Guid? ProjectGuid { get; set; } 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; public int MaxDegreeOfParallelism { get; set; } = Environment.ProcessorCount;
#endregion #endregion
@ -111,6 +117,9 @@ namespace ICSharpCode.Decompiler.CSharp
directories.Clear(); directories.Clear();
var files = WriteCodeFilesInProject(moduleDefinition, cancellationToken).ToList(); var files = WriteCodeFilesInProject(moduleDefinition, cancellationToken).ToList();
files.AddRange(WriteResourceFilesInProject(moduleDefinition)); files.AddRange(WriteResourceFilesInProject(moduleDefinition));
if (StrongNameKeyFile != null) {
File.Copy(StrongNameKeyFile, Path.Combine(targetDirectory, Path.GetFileName(StrongNameKeyFile)));
}
return WriteProjectFile(projectFileWriter, files, moduleDefinition); return WriteProjectFile(projectFileWriter, files, moduleDefinition);
} }
@ -216,6 +225,11 @@ namespace ICSharpCode.Decompiler.CSharp
w.WriteElementString("WarningLevel", "4"); w.WriteElementString("WarningLevel", "4");
w.WriteElementString("AllowUnsafeBlocks", "True"); w.WriteElementString("AllowUnsafeBlocks", "True");
if (StrongNameKeyFile != null) {
w.WriteElementString("SignAssembly", "True");
w.WriteElementString("AssemblyOriginatorKeyFile", Path.GetFileName(StrongNameKeyFile));
}
w.WriteEndElement(); // </PropertyGroup> w.WriteEndElement(); // </PropertyGroup>
w.WriteStartElement("PropertyGroup"); // platform-specific w.WriteStartElement("PropertyGroup"); // platform-specific

Loading…
Cancel
Save