/
PeVerifierTests.cs
76 lines (67 loc) · 2.42 KB
/
PeVerifierTests.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
69
70
71
72
73
74
75
76
using System;
using System.IO;
using ApprovalTests;
using Fody;
using Mono.Cecil;
using Xunit;
using Xunit.Abstractions;
// ReSharper disable UnusedVariable
public class PeVerifierTests :
XunitApprovalBase
{
string assemblyPath = "FodyHelpers.Tests.dll";
[Fact]
public void StaticPathResolution()
{
Assert.True(PeVerifier.FoundPeVerify);
}
[Fact]
public void Should_verify_current_assembly()
{
var verify = PeVerifier.Verify(assemblyPath, GetIgnoreCodes(), out var output);
Assert.True(verify);
Assert.NotNull(output);
}
[Fact]
public void Same_assembly_should_not_throw()
{
Directory.CreateDirectory("temp");
var newAssemblyPath = Path.GetFullPath("temp/temp.dll");
File.Copy(assemblyPath, newAssemblyPath, true);
PeVerifier.ThrowIfDifferent(assemblyPath, newAssemblyPath,
ignoreCodes: GetIgnoreCodes());
File.Delete(newAssemblyPath);
}
static string[] GetIgnoreCodes()
{
return new[] {"0x80070002", "0x80131869"};
}
[Fact]
public void TrimLineNumbers()
{
var text = PeVerifier.TrimLineNumbers(@"
[IL]: Error: [C:\Code\net452\AssemblyToProcess.dll : UnsafeClass::MethodWithAmp][offset 0x00000002][found Native Int][expected unmanaged pointer] Unexpected type on the stack.
[IL]: Error: [C:\Code\net452\AssemblyToProcess.dll : UnsafeClass::get_NullProperty][offset 0x00000006][found unmanaged pointer][expected unmanaged pointer] Unexpected type on the stack.
[IL]: Error: [C:\Code\net452\AssemblyToProcess.dll : UnsafeClass::set_NullProperty][offset 0x00000001] Unmanaged pointers are not a verifiable type.
3 Error(s) Verifying C:\Code\Fody\net452\AssemblyToProcess.dll");
Approvals.Verify(text);
}
[Fact]
public void Invalid_assembly_should_throw()
{
Directory.CreateDirectory("temp");
var newAssemblyPath = Path.GetFullPath("temp/temp.dll");
File.Copy(assemblyPath, newAssemblyPath, true);
using (var moduleDefinition = ModuleDefinition.ReadModule(assemblyPath))
{
moduleDefinition.AssemblyReferences.Clear();
moduleDefinition.Write(newAssemblyPath);
}
Assert.Throws<Exception>(() => PeVerifier.ThrowIfDifferent(assemblyPath, newAssemblyPath));
File.Delete(newAssemblyPath);
}
public PeVerifierTests(ITestOutputHelper output) :
base(output)
{
}
}