forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeVerifier.cs
68 lines (54 loc) · 1.99 KB
/
PeVerifier.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Diagnostics;
using System.IO;
using NUnit.Framework;
namespace NHibernate.Test.ProxyTest
{
// utility class to run PEVerify.exe against a saved-to-disk assembly, similar to:
// http://stackoverflow.com/questions/7290893/is-there-an-api-for-verifying-the-msil-of-a-dynamic-assembly-at-runtime
public class PeVerifier
{
private string _assemlyLocation;
private string _peVerifyPath;
public PeVerifier(string assemblyFileName)
{
var assemblyLocation = Path.Combine(TestContext.CurrentContext.TestDirectory, assemblyFileName);
if (!File.Exists(assemblyLocation))
throw new ArgumentException(string.Format("Could not locate assembly {0}", assemblyLocation), "assemblyLocation");
_assemlyLocation = assemblyLocation;
var dir = Path.GetDirectoryName(_assemlyLocation);
while (!Directory.Exists(Path.Combine(dir, "Tools", "PEVerify")))
{
if (Directory.GetParent(dir) == null)
throw new DirectoryNotFoundException(string.Format("Could not find Tools/PEVerify directory in ancestor of {0}", _assemlyLocation));
dir = Directory.GetParent(dir).FullName;
}
var versionFolder = "4.0";
if (Environment.Version.Major == 2)
versionFolder = "3.5";
_peVerifyPath = Path.Combine(dir, "Tools", "PEVerify", versionFolder, "PEVerify.exe");
if (!File.Exists(_peVerifyPath))
throw new FileNotFoundException(string.Format("Could not find PEVerify.exe at {0}", _peVerifyPath));
}
public void AssertIsValid()
{
var process = new Process
{
StartInfo =
{
FileName = _peVerifyPath,
RedirectStandardOutput = true,
UseShellExecute = false,
Arguments = "\"" + _assemlyLocation + "\" /VERBOSE",
CreateNoWindow = true
}
};
process.Start();
var processOutput = process.StandardOutput.ReadToEnd();
process.WaitForExit();
var result = process.ExitCode + " code ";
if (process.ExitCode != 0)
Assert.Fail("PeVerify reported error(s): " + Environment.NewLine + processOutput, result);
}
}
}