forked from gitleaks/gitleaks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checks_test.go
88 lines (79 loc) · 1.82 KB
/
checks_test.go
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
package main
import (
"bufio"
"fmt"
"os"
"testing"
)
func TestCheckRegex(t *testing.T) {
var results []Leak
opts = &Options{
Concurrency: 10,
B64EntropyCutoff: 70,
HexEntropyCutoff: 40,
Entropy: false,
}
repo := Repo{
url: "someurl",
}
commit := Commit{}
checks := map[string]int{
"aws=\"AKIALALEMEL33243OLIAE": 1,
"aws\"afewafewafewafewaf\"": 0,
}
for k, v := range checks {
results = doChecks(k, commit, &repo)
if v != len(results) {
t.Errorf("regexCheck failed on string %s", k)
}
}
}
func TestExternalRegex(t *testing.T) {
opts, err := defaultOptions()
if err != nil {
t.Error()
}
file, err := os.Create("testregex.txt")
if err != nil {
t.Error()
}
defer file.Close()
w := bufio.NewWriter(file)
fmt.Fprintln(w, "AKIA[0-9A-Z]{16}")
w.Flush()
opts.RegexFile = "testregex.txt"
opts.loadExternalRegex()
leaks := doChecks("aws=\"AKIALALEMEL33243OLIAE",
Commit{}, &Repo{url: "someurl"})
if len(leaks) != 2 {
// leak from default regex, leak from external
t.Error()
}
os.Remove("testregex.txt")
}
func TestEntropy(t *testing.T) {
var enoughEntropy bool
opts := &Options{
Concurrency: 10,
B64EntropyCutoff: 70,
HexEntropyCutoff: 40,
Entropy: false,
}
checks := map[string]bool{
"reddit_api_secret = settings./.http}": false,
"heroku_client_secret = simple": false,
"reddit_api_secret = \"4ok1WFf57-EMswEfAFGewa\"": true,
"aws_secret= \"AKIAIMNOJVGFDXXFE4OA\"": true,
}
for k, v := range checks {
enoughEntropy = checkShannonEntropy(k, opts)
if v != enoughEntropy {
t.Errorf("checkEntropy failed for %s. Expected %t, got %t", k, v, enoughEntropy)
}
}
}
func TestStopWords(t *testing.T) {
if containsStopWords("aws_secret=settings.AWS_SECRET") != true {
t.Errorf("checkStopWords Failed")
}
}