Files
gitleaks/report/finding_test.go
T
FrankandZachary Rice 30c6117aaa feat: Add optional redaction value, default 100 (#1229)
This commit introduces an optional value for the --redact flag, allowing
users to specify the extent of redaction for secrets. The valid range of
values is from 0 (no redaction) to 100 (full redaction).

If no value is provided, the default redaction value is set to 100,
resulting in complete redaction by replacing the secret with the term
'REDACTED'. This preserves the original behavior of the --redact flag.

Co-authored-by: Zachary Rice <zachary.rice@trufflesec.com>
2023-08-29 11:03:23 -05:00

91 lines
2.0 KiB
Go

package report
import "testing"
func TestRedact(t *testing.T) {
tests := []struct {
findings []Finding
redact bool
}{
{
redact: true,
findings: []Finding{
{
Match: "line containing secret",
Secret: "secret",
},
}},
}
for _, test := range tests {
for _, f := range test.findings {
f.Redact(100)
if f.Secret != "REDACTED" {
t.Error("redact not redacting: ", f.Secret)
}
if f.Match != "line containing REDACTED" {
t.Error("redact not redacting: ", f.Secret)
}
}
}
}
func TestMask(t *testing.T) {
tests := map[string]struct {
finding Finding
percent uint
expect Finding
}{
"normal secret": {
finding: Finding{Match: "line containing secret", Secret: "secret"},
expect: Finding{Match: "line containing se...", Secret: "se..."},
percent: 75,
},
"empty secret": {
finding: Finding{Match: "line containing", Secret: ""},
expect: Finding{Match: "line containing", Secret: ""},
percent: 75,
},
"short secret": {
finding: Finding{Match: "line containing", Secret: "ss"},
expect: Finding{Match: "line containing", Secret: "..."},
percent: 75,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
f := test.finding
e := test.expect
f.Redact(test.percent)
if f.Secret != e.Secret {
t.Error("redact not redacting: ", f.Secret)
}
if f.Match != e.Match {
t.Error("redact not redacting: ", f.Match)
}
})
}
}
func TestMaskSecret(t *testing.T) {
tests := map[string]struct {
secret string
percent uint
expect string
}{
"normal masking": {secret: "secret", percent: 75, expect: "se..."},
"high masking": {secret: "secret", percent: 90, expect: "s..."},
"low masking": {secret: "secret", percent: 10, expect: "secre..."},
"invalid masking": {secret: "secret", percent: 1000, expect: "..."},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got := maskSecret(test.secret, test.percent)
if got != test.expect {
t.Error("redact not redacting: ", got)
}
})
}
}