-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathRuleTestShells.cs
91 lines (83 loc) · 3.68 KB
/
RuleTestShells.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.CodeAnalysis.IL.Sdk;
using Microsoft.CodeAnalysis.Sarif;
using Microsoft.CodeAnalysis.Sarif.Driver;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.CodeAnalysis.IL.Rules
{
public class RuleTests
{
[Fact]
public void BAXXXX_RULEFRIENDLYNAME_Fail()
{
// This example is for a PDB parsing rule, which cannot run on
// *nix due to lack of interoperability with msdia140.dll. This
// conditional check (and the false branch) can be deleted
// entirely if this scenario isn't in play.
if (BinaryParsers.PlatformSpecificHelpers.RunningOnWindows())
{
// Every PDB parsing rule should return an error if a PDB can't be located.
// Be sure to delete this code (and remove passing the 'failureConditions`
// arguments to 'VerifyFail' if not implementing a PDB crawling check.
var failureConditions = new HashSet<string>
{
MetadataConditions.CouldNotLoadPdb
};
this.VerifyFail(
new RULEFRIENDLYNAME(),
this.GetTestFilesMatchingConditions(failureConditions),
useDefaultPolicy: true);
}
else
{
this.VerifyThrows<PlatformNotSupportedException>(new RULEFRIENDLYNAME(), useDefaultPolicy: true);
}
}
[Fact]
public void BAXXXX_RULEFRIENDLYNAME_Pass()
{
// This conditional check (and the false branch) are only required for PDB reading rules.
// Delete the conditional and unnecessary branch for checks that don't crawl PDBS.
if (BinaryParsers.PlatformSpecificHelpers.RunningOnWindows())
{
this.VerifyPass(new RULEFRIENDLYNAME(), useDefaultPolicy: true);
}
else
{
this.VerifyThrows<PlatformNotSupportedException>(new RULEFRIENDLYNAME(), useDefaultPolicy: true);
}
}
[Fact]
public void BAXXXX_NotApplicable()
{
// Be sure to add an exhaustive list of metadata conditions here
// that apply to the check (i.e., all the conditions that are
// referenced in the check CanAnalyze implementation).
var notApplicableTo = new HashSet<string>
{
MetadataConditions.ImageIsResourceOnlyBinary,
MetadataConditions.ImageIsILOnlyAssembly
};
this.VerifyNotApplicable(new RULEFRIENDLYNAME(), notApplicableTo);
// This conditional check (and the false branch) are only required for PDB reading rules.
// Delete the conditional and unnecessary branch for checks that don't crawl PDBS.
// Note that this code is explicitly detecting that the rule positively identifies
// 64-bit binaries as scan target candidates.
if (BinaryParsers.PlatformSpecificHelpers.RunningOnWindows())
{
var applicableTo = new HashSet<string> { MetadataConditions.ImageIs64BitBinary };
this.VerifyNotApplicable(new RULEFRIENDLYNAME(), applicableTo, AnalysisApplicability.ApplicableToSpecifiedTarget);
}
else
{
this.VerifyThrows<PlatformNotSupportedException>(new RULEFRIENDLYNAME(), useDefaultPolicy: true);
}
}
}
}