-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_test.go
More file actions
175 lines (160 loc) · 3.94 KB
/
Copy pathmain_test.go
File metadata and controls
175 lines (160 loc) · 3.94 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"go/ast"
"go/token"
"testing"
)
func TestCheckHardcodedCredentials(t *testing.T) {
testCases := []struct {
name string
input string
expected bool
}{
{"Hardcoded Password", "\"password123\"", true},
{"API Key", "\"APIKEY12345\"", true},
{"Random String", "\"Hello, World!\"", false},
{"Empty String", "\"\"", false},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
n := &ast.BasicLit{Kind: token.STRING, Value: tc.input}
findings := make(chan finding, 1)
checkHardcodedCredentials(n, "test.go", token.NewFileSet(), findings)
close(findings)
if tc.expected {
if len(findings) == 0 {
t.Errorf("Expected finding but got none")
}
} else {
if len(findings) != 0 {
t.Errorf("Expected no findings but got one")
}
}
})
}
}
func TestCheckInsecureHTTP(t *testing.T) {
testCases := []struct {
name string
input string
expected bool
}{
{"HTTP URL", "\"http://example.com\"", true},
{"HTTPS URL", "\"https://example.com\"", false},
{"Non-URL String", "\"Not a URL\"", false},
{"Empty String", "\"\"", false},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
arg := &ast.BasicLit{Kind: token.STRING, Value: tc.input}
n := &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: ast.NewIdent("http"),
Sel: ast.NewIdent("Get"),
},
Args: []ast.Expr{arg},
}
findings := make(chan finding, 1)
checkInsecureHTTP(n, "test.go", token.NewFileSet(), findings)
close(findings)
if tc.expected {
if len(findings) == 0 {
t.Errorf("Expected finding but got none")
}
} else {
if len(findings) != 0 {
t.Errorf("Expected no findings but got one")
}
}
})
}
}
func TestCheckCommandInjection(t *testing.T) {
testCases := []struct {
name string
function string
expected bool
}{
{"Exec Command", "Command", true},
{"Exec", "Exec", false},
{"Not a Command", "NotACommand", false},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
n := &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: ast.NewIdent("exec"),
Sel: ast.NewIdent(tc.function),
},
}
findings := make(chan finding, 1)
checkCommandInjection(n, "test.go", token.NewFileSet(), findings)
close(findings)
if tc.expected {
if len(findings) == 0 {
t.Errorf("Expected finding but got none")
}
} else {
if len(findings) != 0 {
t.Errorf("Expected no findings but got one")
}
}
})
}
}
func TestIsGoFile(t *testing.T) {
testCases := []struct {
name string
input string
expected bool
}{
{"Go File", "main.go", true},
{"Text File", "main.txt", false},
{"Empty String", "", false},
{"No Extension", "file", false},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := isGoFile(tc.input)
if result != tc.expected {
t.Errorf("Expected %v, got %v", tc.expected, result)
}
})
}
}
func BenchmarkCheckHardcodedCredentials(b *testing.B) {
n := &ast.BasicLit{Kind: token.STRING, Value: "\"password123\""}
for i := 0; i < b.N; i++ {
findings := make(chan finding, 1)
checkHardcodedCredentials(n, "test.go", token.NewFileSet(), findings)
close(findings)
}
}
func BenchmarkCheckInsecureHTTP(b *testing.B) {
arg := &ast.BasicLit{Kind: token.STRING, Value: "\"http://example.com\""}
n := &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: ast.NewIdent("http"),
Sel: ast.NewIdent("Get"),
},
Args: []ast.Expr{arg},
}
for i := 0; i < b.N; i++ {
findings := make(chan finding, 1)
checkInsecureHTTP(n, "test.go", token.NewFileSet(), findings)
close(findings)
}
}
func BenchmarkCheckCommandInjection(b *testing.B) {
for i := 0; i < b.N; i++ {
n := &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: ast.NewIdent("exec"),
Sel: ast.NewIdent("Command"),
},
}
findings := make(chan finding, 1)
checkCommandInjection(n, "test.go", token.NewFileSet(), findings)
close(findings)
}
}