PageRenderTime 23ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/UnitTests/Tools/PEVerifier.cs

http://github.com/philiplaureano/LinFu
C# | 112 lines | 86 code | 23 blank | 3 comment | 7 complexity | 74c2b9e100d1f9528f14830cc3179a31 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using LinFu.AOP.Cecil.Interfaces;
  7. using LinFu.Reflection.Emit;
  8. using Mono.Cecil;
  9. using Xunit;
  10. namespace LinFu.UnitTests.Tools
  11. {
  12. internal class PeVerifier : IVerifier
  13. {
  14. private readonly string _location = string.Empty;
  15. private bool _failed;
  16. private string _filename = string.Empty;
  17. internal PeVerifier(string filename)
  18. {
  19. var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
  20. _location = Path.Combine(baseDirectory, filename);
  21. _filename = filename;
  22. }
  23. public void Verify(AssemblyDefinition assembly)
  24. {
  25. // Save the assembly to the temporary file and verify it
  26. assembly.Save(_location);
  27. PeVerify(_location);
  28. }
  29. ~PeVerifier()
  30. {
  31. try
  32. {
  33. if (File.Exists(_location) && !_failed)
  34. File.Delete(_location);
  35. }
  36. catch
  37. {
  38. // Do nothing
  39. }
  40. }
  41. private void PeVerify(string assemblyLocation)
  42. {
  43. var pathKeys = new[]
  44. {
  45. "sdkDir",
  46. "x86SdkDir",
  47. "sdkDirUnderVista"
  48. };
  49. var process = new Process();
  50. var peVerifyLocation = string.Empty;
  51. peVerifyLocation = GetPEVerifyLocation(pathKeys, peVerifyLocation);
  52. // Use PEVerify.exe if it exists
  53. if (!File.Exists(peVerifyLocation))
  54. {
  55. Console.WriteLine("Warning: PEVerify.exe could not be found. Skipping test.");
  56. return;
  57. }
  58. process.StartInfo.FileName = peVerifyLocation;
  59. process.StartInfo.RedirectStandardOutput = true;
  60. process.StartInfo.UseShellExecute = false;
  61. process.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
  62. process.StartInfo.Arguments = "\"" + assemblyLocation + "\" /VERBOSE";
  63. process.StartInfo.CreateNoWindow = true;
  64. process.Start();
  65. var processOutput = process.StandardOutput.ReadToEnd();
  66. process.WaitForExit();
  67. var result = string.Format("PEVerify Exit Code: {0}", process.ExitCode);
  68. Console.WriteLine(GetType().FullName + ": " + result);
  69. if (process.ExitCode == 0)
  70. return;
  71. Console.WriteLine(processOutput);
  72. _failed = true;
  73. Assert.True(false, $"PEVerify output: {Environment.NewLine}{processOutput}{result}");
  74. }
  75. private static string GetPEVerifyLocation(IEnumerable<string> pathKeys, string peVerifyLocation)
  76. {
  77. foreach (var key in pathKeys)
  78. {
  79. var directory = ConfigurationManager.AppSettings[key];
  80. if (string.IsNullOrEmpty(directory))
  81. continue;
  82. peVerifyLocation = Path.Combine(directory, "peverify.exe");
  83. if (File.Exists(peVerifyLocation))
  84. break;
  85. }
  86. return peVerifyLocation;
  87. }
  88. }
  89. }