/src/UnitTests/Reflection/BasePEVerifyTestCase.cs

http://github.com/philiplaureano/LinFu · C# · 138 lines · 91 code · 29 blank · 18 comment · 8 complexity · c454ee4d7afc9f624b424acad51a34e2 MD5 · raw file

  1. // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // LEGAL NOTICE: The original BasePEVerifyTestCase code was taken from Castle
  15. // and modified to suit LinFu's purposes. The following code
  16. // is still subject to the terms of the Apache License and retains
  17. // the rights of its original owners.
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Configuration;
  21. using System.Diagnostics;
  22. using System.IO;
  23. using Xunit;
  24. namespace LinFu.UnitTests.Reflection
  25. {
  26. public abstract class BasePEVerifyTestCase : BaseTestFixture
  27. {
  28. private static readonly List<string> _disposalList = new List<string>();
  29. protected override void Init()
  30. {
  31. OnInit();
  32. }
  33. protected override void Term()
  34. {
  35. OnTerm();
  36. lock (_disposalList)
  37. {
  38. // Delete the files tagged for removal
  39. foreach (var file in _disposalList)
  40. {
  41. if (!File.Exists(file))
  42. continue;
  43. File.Delete(file);
  44. }
  45. _disposalList.Clear();
  46. }
  47. }
  48. protected virtual void OnInit()
  49. {
  50. }
  51. protected virtual void OnTerm()
  52. {
  53. }
  54. protected static void AutoDelete(string filename)
  55. {
  56. lock (_disposalList)
  57. {
  58. if (_disposalList.Contains(filename) || !File.Exists(filename))
  59. return;
  60. _disposalList.Add(filename);
  61. }
  62. }
  63. protected void PEVerify(string assemblyLocation)
  64. {
  65. var pathKeys = new[]
  66. {
  67. "sdkDir",
  68. "x86SdkDir",
  69. "sdkDirUnderVista"
  70. };
  71. var process = new Process();
  72. var peVerifyLocation = string.Empty;
  73. peVerifyLocation = GetPEVerifyLocation(pathKeys, peVerifyLocation);
  74. if (!File.Exists(peVerifyLocation))
  75. {
  76. Console.WriteLine("Warning: PEVerify.exe could not be found. Skipping test.");
  77. return;
  78. }
  79. process.StartInfo.FileName = peVerifyLocation;
  80. process.StartInfo.RedirectStandardOutput = true;
  81. process.StartInfo.UseShellExecute = false;
  82. process.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
  83. process.StartInfo.Arguments = "\"" + assemblyLocation + "\" /VERBOSE";
  84. process.StartInfo.CreateNoWindow = true;
  85. process.Start();
  86. var processOutput = process.StandardOutput.ReadToEnd();
  87. process.WaitForExit();
  88. var result = string.Format("PEVerify Exit Code: {0}", process.ExitCode);
  89. Console.WriteLine(GetType().FullName + ": " + result);
  90. if (process.ExitCode == 0)
  91. return;
  92. Console.WriteLine(processOutput);
  93. Assert.True(false, $"PEVerify output: {Environment.NewLine}{processOutput}{result}");
  94. }
  95. private static string GetPEVerifyLocation(IEnumerable<string> pathKeys, string peVerifyLocation)
  96. {
  97. foreach (var key in pathKeys)
  98. {
  99. var directory = ConfigurationManager.AppSettings[key];
  100. if (string.IsNullOrEmpty(directory))
  101. continue;
  102. peVerifyLocation = Path.Combine(directory, "peverify.exe");
  103. if (File.Exists(peVerifyLocation))
  104. break;
  105. }
  106. return peVerifyLocation;
  107. }
  108. }
  109. }