diff --git a/ICSharpCode.Decompiler.Tests/Helpers/Tester.cs b/ICSharpCode.Decompiler.Tests/Helpers/Tester.cs index dfe0d7cb2..1ad55293d 100644 --- a/ICSharpCode.Decompiler.Tests/Helpers/Tester.cs +++ b/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 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); + } } } diff --git a/ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs b/ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs index 975c92511..210a45d13 100644 --- a/ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs +++ b/ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs @@ -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 outputDir => Tester.RunAndCompareOutput(fileToRoundtrip, Path.Combine(inputDir, fileToRoundtrip), Path.Combine(outputDir, fileToRoundtrip))); } - void RunInternal(string dir, string fileToRoundtrip, Action testAction) + void RunInternal(string dir, string fileToRoundtrip, Action 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 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"); diff --git a/ICSharpCode.Decompiler/CSharp/WholeProjectDecompiler.cs b/ICSharpCode.Decompiler/CSharp/WholeProjectDecompiler.cs index 377a617d5..a757e2921 100644 --- a/ICSharpCode.Decompiler/CSharp/WholeProjectDecompiler.cs +++ b/ICSharpCode.Decompiler/CSharp/WholeProjectDecompiler.cs @@ -79,6 +79,12 @@ namespace ICSharpCode.Decompiler.CSharp /// public Guid? ProjectGuid { get; set; } + /// + /// Path to the snk file to use for signing. + /// null to not sign. + /// + public string StrongNameKeyFile { get; set; } + public int MaxDegreeOfParallelism { get; set; } = Environment.ProcessorCount; #endregion @@ -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 } w.WriteElementString("WarningLevel", "4"); w.WriteElementString("AllowUnsafeBlocks", "True"); + + if (StrongNameKeyFile != null) { + w.WriteElementString("SignAssembly", "True"); + w.WriteElementString("AssemblyOriginatorKeyFile", Path.GetFileName(StrongNameKeyFile)); + } w.WriteEndElement(); //