|
@@ -11,17 +11,80 @@ func TestRedact(t *testing.T) {
|
|
|
redact: true,
|
|
redact: true,
|
|
|
findings: []Finding{
|
|
findings: []Finding{
|
|
|
{
|
|
{
|
|
|
- Secret: "line containing secret",
|
|
|
|
|
- Match: "secret",
|
|
|
|
|
|
|
+ Match: "line containing secret",
|
|
|
|
|
+ Secret: "secret",
|
|
|
},
|
|
},
|
|
|
}},
|
|
}},
|
|
|
}
|
|
}
|
|
|
for _, test := range tests {
|
|
for _, test := range tests {
|
|
|
for _, f := range test.findings {
|
|
for _, f := range test.findings {
|
|
|
- f.Redact()
|
|
|
|
|
|
|
+ f.Redact(100)
|
|
|
if f.Secret != "REDACTED" {
|
|
if f.Secret != "REDACTED" {
|
|
|
t.Error("redact not redacting: ", f.Secret)
|
|
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)
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+}
|