You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

340 lines
12 KiB

  1. // Copyright (c) 2016 Daniel Grunwald
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Diagnostics;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Reflection.PortableExecutable;
  23. using System.Text.RegularExpressions;
  24. using System.Threading;
  25. using ICSharpCode.Decompiler.CSharp;
  26. using ICSharpCode.Decompiler.CSharp.ProjectDecompiler;
  27. using ICSharpCode.Decompiler.Metadata;
  28. using ICSharpCode.Decompiler.Tests.Helpers;
  29. using Microsoft.Build.Locator;
  30. using NUnit.Framework;
  31. namespace ICSharpCode.Decompiler.Tests
  32. {
  33. [TestFixture, Parallelizable(ParallelScope.All)]
  34. public class RoundtripAssembly
  35. {
  36. public static readonly string TestDir = Path.GetFullPath(Path.Combine(Tester.TestCasePath, "../../ILSpy-tests"));
  37. static readonly string nunit = Path.Combine(TestDir, "nunit", "nunit3-console.exe");
  38. [Test]
  39. public void Cecil_net45()
  40. {
  41. RunWithTest("Mono.Cecil-net45", "Mono.Cecil.dll", "Mono.Cecil.Tests.dll");
  42. }
  43. [Test]
  44. public void NewtonsoftJson_net45()
  45. {
  46. RunWithTest("Newtonsoft.Json-net45", "Newtonsoft.Json.dll", "Newtonsoft.Json.Tests.dll");
  47. }
  48. [Test]
  49. public void NewtonsoftJson_pcl_debug()
  50. {
  51. try
  52. {
  53. RunWithTest("Newtonsoft.Json-pcl-debug", "Newtonsoft.Json.dll", "Newtonsoft.Json.Tests.dll", useOldProjectFormat: true);
  54. }
  55. catch (CompilationFailedException)
  56. {
  57. Assert.Ignore("Cannot yet re-compile PCL projects.");
  58. }
  59. }
  60. [Test]
  61. public void NRefactory_CSharp()
  62. {
  63. RunWithTest("NRefactory", "ICSharpCode.NRefactory.CSharp.dll", "ICSharpCode.NRefactory.Tests.dll");
  64. }
  65. [Test]
  66. public void ICSharpCode_Decompiler()
  67. {
  68. RunOnly("ICSharpCode.Decompiler", "ICSharpCode.Decompiler.dll");
  69. }
  70. [Test]
  71. public void ImplicitConversions()
  72. {
  73. RunWithOutput("Random Tests\\TestCases", "ImplicitConversions.exe");
  74. }
  75. [Test]
  76. public void ImplicitConversions_32()
  77. {
  78. RunWithOutput("Random Tests\\TestCases", "ImplicitConversions_32.exe");
  79. }
  80. [Test]
  81. public void ExplicitConversions()
  82. {
  83. RunWithOutput("Random Tests\\TestCases", "ExplicitConversions.exe", LanguageVersion.CSharp8_0);
  84. }
  85. [Test]
  86. public void ExplicitConversions_32()
  87. {
  88. RunWithOutput("Random Tests\\TestCases", "ExplicitConversions_32.exe", LanguageVersion.CSharp8_0);
  89. }
  90. [Test]
  91. [Ignore("Waiting for https://github.com/dotnet/roslyn/issues/45929")]
  92. public void ExplicitConversions_With_NativeInts()
  93. {
  94. RunWithOutput("Random Tests\\TestCases", "ExplicitConversions.exe", LanguageVersion.Preview);
  95. }
  96. [Test]
  97. [Ignore("Waiting for https://github.com/dotnet/roslyn/issues/45929")]
  98. public void ExplicitConversions_32_With_NativeInts()
  99. {
  100. RunWithOutput("Random Tests\\TestCases", "ExplicitConversions_32.exe", LanguageVersion.Preview);
  101. }
  102. [Test]
  103. public void Random_TestCase_1()
  104. {
  105. RunWithOutput("Random Tests\\TestCases", "TestCase-1.exe", LanguageVersion.CSharp8_0);
  106. }
  107. [Test]
  108. [Ignore("Waiting for https://github.com/dotnet/roslyn/issues/45929")]
  109. public void Random_TestCase_1_With_NativeInts()
  110. {
  111. RunWithOutput("Random Tests\\TestCases", "TestCase-1.exe", LanguageVersion.Preview);
  112. }
  113. // Let's limit the roundtrip tests to C# 8.0 for now; because 9.0 is still in preview
  114. // and the generated project doesn't build as-is.
  115. const LanguageVersion defaultLanguageVersion = LanguageVersion.CSharp8_0;
  116. void RunWithTest(string dir, string fileToRoundtrip, string fileToTest, LanguageVersion languageVersion = defaultLanguageVersion, string keyFile = null, bool useOldProjectFormat = false)
  117. {
  118. RunInternal(dir, fileToRoundtrip, outputDir => RunTest(outputDir, fileToTest), languageVersion, snkFilePath: keyFile, useOldProjectFormat: useOldProjectFormat);
  119. }
  120. void RunWithOutput(string dir, string fileToRoundtrip, LanguageVersion languageVersion = defaultLanguageVersion)
  121. {
  122. string inputDir = Path.Combine(TestDir, dir);
  123. RunInternal(dir, fileToRoundtrip,
  124. outputDir => Tester.RunAndCompareOutput(fileToRoundtrip, Path.Combine(inputDir, fileToRoundtrip), Path.Combine(outputDir, fileToRoundtrip)),
  125. languageVersion);
  126. }
  127. void RunOnly(string dir, string fileToRoundtrip, LanguageVersion languageVersion = defaultLanguageVersion)
  128. {
  129. RunInternal(dir, fileToRoundtrip, outputDir => { }, languageVersion);
  130. }
  131. void RunInternal(string dir, string fileToRoundtrip, Action<string> testAction, LanguageVersion languageVersion, string snkFilePath = null, bool useOldProjectFormat = false)
  132. {
  133. if (!Directory.Exists(TestDir))
  134. {
  135. Assert.Ignore($"Assembly-roundtrip test ignored: test directory '{TestDir}' needs to be checked out separately." + Environment.NewLine +
  136. $"git clone https://github.com/icsharpcode/ILSpy-tests \"{TestDir}\"");
  137. }
  138. string inputDir = Path.Combine(TestDir, dir);
  139. string decompiledDir = inputDir + "-decompiled";
  140. string outputDir = inputDir + "-output";
  141. if (inputDir.EndsWith("TestCases"))
  142. {
  143. // make sure output dir names are unique so that we don't get trouble due to parallel test execution
  144. decompiledDir += Path.GetFileNameWithoutExtension(fileToRoundtrip) + "_" + languageVersion.ToString();
  145. outputDir += Path.GetFileNameWithoutExtension(fileToRoundtrip) + "_" + languageVersion.ToString();
  146. }
  147. ClearDirectory(decompiledDir);
  148. ClearDirectory(outputDir);
  149. string projectFile = null;
  150. foreach (string file in Directory.EnumerateFiles(inputDir, "*", SearchOption.AllDirectories))
  151. {
  152. if (!file.StartsWith(inputDir + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
  153. {
  154. Assert.Fail($"Unexpected file name: {file}");
  155. }
  156. string relFile = file.Substring(inputDir.Length + 1);
  157. Directory.CreateDirectory(Path.Combine(outputDir, Path.GetDirectoryName(relFile)));
  158. if (relFile.Equals(fileToRoundtrip, StringComparison.OrdinalIgnoreCase))
  159. {
  160. Console.WriteLine($"Decompiling {fileToRoundtrip}...");
  161. Stopwatch w = Stopwatch.StartNew();
  162. using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
  163. {
  164. PEFile module = new PEFile(file, fileStream, PEStreamOptions.PrefetchEntireImage);
  165. var resolver = new TestAssemblyResolver(file, inputDir, module.Reader.DetectTargetFrameworkId());
  166. resolver.AddSearchDirectory(inputDir);
  167. resolver.RemoveSearchDirectory(".");
  168. // use a fixed GUID so that we can diff the output between different ILSpy runs without spurious changes
  169. var projectGuid = Guid.Parse("{127C83E4-4587-4CF9-ADCA-799875F3DFE6}");
  170. var settings = new DecompilerSettings(languageVersion);
  171. if (useOldProjectFormat)
  172. {
  173. settings.UseSdkStyleProjectFormat = false;
  174. }
  175. var decompiler = new TestProjectDecompiler(projectGuid, resolver, resolver, settings);
  176. if (snkFilePath != null)
  177. {
  178. decompiler.StrongNameKeyFile = Path.Combine(inputDir, snkFilePath);
  179. }
  180. decompiler.DecompileProject(module, decompiledDir);
  181. Console.WriteLine($"Decompiled {fileToRoundtrip} in {w.Elapsed.TotalSeconds:f2}");
  182. projectFile = Path.Combine(decompiledDir, module.Name + ".csproj");
  183. }
  184. }
  185. else
  186. {
  187. File.Copy(file, Path.Combine(outputDir, relFile));
  188. }
  189. }
  190. Assert.IsNotNull(projectFile, $"Could not find {fileToRoundtrip}");
  191. Compile(projectFile, outputDir);
  192. testAction(outputDir);
  193. }
  194. static void ClearDirectory(string dir)
  195. {
  196. Directory.CreateDirectory(dir);
  197. foreach (string subdir in Directory.EnumerateDirectories(dir))
  198. {
  199. for (int attempt = 0; ; attempt++)
  200. {
  201. try
  202. {
  203. Directory.Delete(subdir, true);
  204. break;
  205. }
  206. catch (IOException)
  207. {
  208. if (attempt >= 10)
  209. throw;
  210. Thread.Sleep(100);
  211. }
  212. }
  213. }
  214. foreach (string file in Directory.EnumerateFiles(dir))
  215. {
  216. File.Delete(file);
  217. }
  218. }
  219. static string FindMSBuild()
  220. {
  221. string vsPath = MSBuildLocator.QueryVisualStudioInstances(new VisualStudioInstanceQueryOptions { DiscoveryTypes = DiscoveryType.VisualStudioSetup })
  222. .OrderByDescending(i => i.Version)
  223. .FirstOrDefault()
  224. ?.MSBuildPath;
  225. if (vsPath == null)
  226. throw new InvalidOperationException("Could not find MSBuild");
  227. return Path.Combine(vsPath, "msbuild.exe");
  228. }
  229. static void Compile(string projectFile, string outputDir)
  230. {
  231. var info = new ProcessStartInfo(FindMSBuild());
  232. info.Arguments = $"/nologo /v:minimal /restore /p:OutputPath=\"{outputDir}\" \"{projectFile}\"";
  233. info.CreateNoWindow = true;
  234. info.UseShellExecute = false;
  235. info.RedirectStandardOutput = true;
  236. // Don't let environment variables (e.g. set by AppVeyor) influence the build.
  237. info.EnvironmentVariables.Remove("Configuration");
  238. info.EnvironmentVariables.Remove("Platform");
  239. Console.WriteLine($"\"{info.FileName}\" {info.Arguments}");
  240. using (var p = Process.Start(info))
  241. {
  242. Regex errorRegex = new Regex(@"^[\w\d.\\-]+\(\d+,\d+\):");
  243. string suffix = $" [{projectFile}]";
  244. string line;
  245. while ((line = p.StandardOutput.ReadLine()) != null)
  246. {
  247. if (line.EndsWith(suffix, StringComparison.OrdinalIgnoreCase))
  248. {
  249. line = line.Substring(0, line.Length - suffix.Length);
  250. }
  251. Match m = errorRegex.Match(line);
  252. if (m.Success)
  253. {
  254. // Make path absolute so that it gets hyperlinked
  255. line = Path.GetDirectoryName(projectFile) + Path.DirectorySeparatorChar + line;
  256. }
  257. Console.WriteLine(line);
  258. }
  259. p.WaitForExit();
  260. if (p.ExitCode != 0)
  261. throw new CompilationFailedException($"Compilation of {Path.GetFileName(projectFile)} failed");
  262. }
  263. }
  264. static void RunTest(string outputDir, string fileToTest)
  265. {
  266. var info = new ProcessStartInfo(nunit);
  267. info.WorkingDirectory = outputDir;
  268. info.Arguments = $"\"{fileToTest}\"";
  269. info.CreateNoWindow = true;
  270. info.UseShellExecute = false;
  271. info.RedirectStandardOutput = true;
  272. Console.WriteLine($"\"{info.FileName}\" {info.Arguments}");
  273. using (var p = Process.Start(info))
  274. {
  275. string line;
  276. while ((line = p.StandardOutput.ReadLine()) != null)
  277. {
  278. Console.WriteLine(line);
  279. }
  280. p.WaitForExit();
  281. if (p.ExitCode != 0)
  282. throw new TestRunFailedException($"Test execution of {Path.GetFileName(fileToTest)} failed");
  283. }
  284. }
  285. class TestProjectDecompiler : WholeProjectDecompiler
  286. {
  287. public TestProjectDecompiler(Guid projecGuid, IAssemblyResolver resolver, AssemblyReferenceClassifier assemblyReferenceClassifier, DecompilerSettings settings)
  288. : base(settings, projecGuid, resolver, assemblyReferenceClassifier, debugInfoProvider: null)
  289. {
  290. }
  291. }
  292. class CompilationFailedException : Exception
  293. {
  294. public CompilationFailedException(string message) : base(message)
  295. {
  296. }
  297. }
  298. class TestRunFailedException : Exception
  299. {
  300. public TestRunFailedException(string message) : base(message)
  301. {
  302. }
  303. }
  304. }
  305. }