-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathGet-RuleForPath.ps1
84 lines (70 loc) · 3.51 KB
/
Get-RuleForPath.ps1
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
# takes paths like this:
# c/cert/src/rules/DCL39-C/InformationLeakageAcrossTrustBoundariesC.ql
# c/common/test/rules/informationleakageacrossboundaries/InformationLeakageAcrossBoundaries.expected
# c/common/test/rules/informationleakageacrossboundaries/InformationLeakageAcrossBoundaries.ql
# c/common/test/rules/informationleakageacrossboundaries/arrays.c
# c/common/test/rules/informationleakageacrossboundaries/interprocedural.c
# c/common/test/rules/informationleakageacrossboundaries/multilayer.c
# c/common/test/rules/informationleakageacrossboundaries/test.c
# c/misra/src/rules/RULE-18-8/VariableLengthArrayTypesUsed.ql
# c/misra/src/rules/RULE-8-12/ValueImplicitEnumerationConstantNotUnique.ql
# c/misra/test/rules/RULE-18-8/VariableLengthArrayTypesUsed.expected
# c/misra/test/rules/RULE-18-8/test.c
# c/misra/test/rules/RULE-8-12/ValueImplicitEnumerationConstantNotUnique.expected
# c/misra/test/rules/RULE-8-12/test.c
# cpp/cert/src/rules/DCL55-CPP/InformationLeakageAcrossTrustBoundaries.ql
# cpp/common/test/rules/informationleakageacrossboundaries/InformationLeakageAcrossBoundaries.expected
# cpp/common/test/rules/informationleakageacrossboundaries/InformationLeakageAcrossBoundaries.ql
# cpp/common/test/rules/informationleakageacrossboundaries/InformationLeakageAcrossTrustBoundaries.expected
# cpp/common/test/rules/informationleakageacrossboundaries/arrays.cpp
# cpp/common/test/rules/informationleakageacrossboundaries/inheritance.cpp
# cpp/common/test/rules/informationleakageacrossboundaries/interprocedural.cpp
# cpp/common/test/rules/informationleakageacrossboundaries/multilayer.cpp
# cpp/common/test/rules/informationleakageacrossboundaries/test.cpp
# And produces one or more rules for it. It does this by loading every rule
# and computing the test directory for it. This test directory is then
# used to see if a) it is a substring of the supplied path or if b) it
# is a substring of the path once the substitution `/src/` -> `/test/` is
# applied
function Get-RuleForPath {
param([Parameter(Mandatory)]
[string]
$Path,
[ValidateSet('c', 'cpp')]
[string]
$Language
)
# load all the queries for all languages
$allQueries = @()
$queriesToCheck = @()
foreach ($s in $AVAILABLE_SUITES) {
$allQueries += Get-RulesInSuite -Suite $s -Language $Language
}
$modifiedPathWithReplacement = Join-Path (Resolve-Path . -Relative) $Path
# replace "src" with "test" to make it match up
$sep = [IO.Path]::DirectorySeparatorChar
$modifiedPathWithReplacement = $modifiedPathWithReplacement.Replace( ($sep + "src" + $sep + "rules"), ($sep + "test" + $sep + "rules"))
$modifiedPath = Join-Path (Resolve-Path . -Relative) $Path
$matchingRules = @()
# for each query, create the test directory
foreach($q in $allQueries){
# get test directory
$testDirs = (Get-ATestDirectory -RuleObject $q -Language $Language)
foreach($testDirectory in $testDirs){
# resolve path to be compatible
$testPath = (Join-Path (Resolve-Path . -Relative) $testDirectory)
if((Split-Path $modifiedPath -Parent) -eq $testPath){
$matchingRules += $q
continue
}
if((Split-Path $modifiedPathWithReplacement -Parent) -eq $testPath){
$matchingRules += $q
continue
}
}
}
if($matchingRules.Count -gt 0){
return $matchingRules
}
throw "Path does not appear to be part of a rule."
}