Files

55 lines
2.0 KiB
Go

package main
import (
"strings"
"testing"
)
// Mutant (13a): put "--no-git" back into historyArgs -> this test fails.
func TestHistoryScansEveryCommitNotTheTree(t *testing.T) {
args := strings.Join(historyArgs("/tmp/r.json"), " ")
if strings.Contains(args, "--no-git") {
t.Fatalf("the history scan must read git history, got %q", args)
}
for _, want := range []string{"--log-opts=--all", "--redact", "--exit-code 0", "--report-format json"} {
if !strings.Contains(args, want) {
t.Errorf("history argv lacks %q: %q", want, args)
}
}
}
func TestHistoryReportKeepsNoSecret(t *testing.T) {
report := `[{"RuleID":"aws-access-token","File":"old/config.py","StartLine":3,
"Commit":"0123456789abcdef0123456789abcdef01234567","Secret":"AKIAREALSECRET",
"Match":"key = AKIAREALSECRET","Fingerprint":"0123:old/config.py:aws-access-token:3"}]`
got, err := parseHistoryReport(report)
if err != nil || len(got) != 1 {
t.Fatalf("got %v, %v", got, err)
}
if got[0].File != "old/config.py" || got[0].Line != 3 || got[0].Rule != "aws-access-token" {
t.Errorf("location lost: %+v", got[0])
}
if strings.Contains(strings.Join([]string{got[0].Fingerprint, got[0].File, got[0].Rule, got[0].Commit}, " "), "AKIAREALSECRET") {
t.Errorf("a secret value leaked into the finding: %+v", got[0])
}
}
func TestHistoryReportFallsBackToALocationFingerprint(t *testing.T) {
got, err := parseHistoryReport(`[{"RuleID":"r","File":"f","StartLine":9,"Commit":"c"}]`)
if err != nil || got[0].Fingerprint != "c:f:r:9" {
t.Fatalf("got %v, %v", got, err)
}
}
func TestAnEmptyHistoryIsNoFindingAndJunkIsAnError(t *testing.T) {
if got, err := parseHistoryReport(""); err != nil || len(got) != 0 {
t.Errorf("empty: %v, %v", got, err)
}
if got, err := parseHistoryReport("[]"); err != nil || len(got) != 0 {
t.Errorf("[]: %v, %v", got, err)
}
if _, err := parseHistoryReport("gitleaks: fatal"); err == nil {
t.Error("an unreadable report must be an error (could_not_run), not zero findings")
}
}