Skip to content

Commit 2b2999b

Browse files
committed
Add tests for excludes with comments
1 parent 37cada1 commit 2b2999b

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

rules/nosec_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,29 @@ func TestNosecBlockExcludeOne(t *testing.T) {
168168
checkTestResults(t, issues, 0, "None")
169169
}
170170

171+
func TestNosecBlockExcludeOneWithComment(t *testing.T) {
172+
config := map[string]interface{}{"ignoreNosec": false}
173+
analyzer := gas.NewAnalyzer(config, nil)
174+
analyzer.AddRule(NewSubproc("G001", config))
175+
176+
issues := gasTestRunner(
177+
`package main
178+
import (
179+
"os"
180+
"os/exec"
181+
)
182+
183+
func main() {
184+
// #exclude !G001(This rule is bogus)
185+
if true {
186+
cmd := exec.Command("sh", "-c", os.Getenv("BLAH"))
187+
cmd.Run()
188+
}
189+
}`, analyzer)
190+
191+
checkTestResults(t, issues, 0, "None")
192+
}
193+
171194
func TestNosecBlockExcludeOneNoMatch(t *testing.T) {
172195
config := map[string]interface{}{"ignoreNosec": false}
173196
analyzer := gas.NewAnalyzer(config, nil)
@@ -256,3 +279,26 @@ func TestNosecExcludeTwoBothMatch(t *testing.T) {
256279

257280
checkTestResults(t, issues, 0, "No issues")
258281
}
282+
283+
func TestNosecExcludeTwoWithComments(t *testing.T) {
284+
config := map[string]interface{}{"ignoreNosec": false}
285+
analyzer := gas.NewAnalyzer(config, nil)
286+
analyzer.AddRule(NewSubproc("G001", config))
287+
analyzer.AddRule(NewWeakRandCheck("G002", config))
288+
289+
issues := gasTestRunner(
290+
`package main
291+
import (
292+
"math/rand"
293+
"os"
294+
"os/exec"
295+
)
296+
297+
func main() {
298+
// #exclude !G001(The env var is trusted) !G002(Unimportant random number)
299+
cmd := exec.Command("sh", "-c", os.Getenv("BLAH"), string(rand.Int()))
300+
cmd.Run()
301+
}`, analyzer)
302+
303+
checkTestResults(t, issues, 0, "No issues")
304+
}

0 commit comments

Comments
 (0)