30 lines
757 B
Go
30 lines
757 B
Go
package securelog
|
|
|
|
import "testing"
|
|
|
|
func TestRedactJSON(t *testing.T) {
|
|
in := `{"email":"a@b.com","password":"Secret12!@#a","password_hash":"$2a$10$abc","new_password":"x","ok":true}`
|
|
out := RedactJSON(in)
|
|
if contains(out, "Secret12") || contains(out, "$2a$10$abc") || contains(out, `"x"`) {
|
|
t.Fatalf("still has secret: %s", out)
|
|
}
|
|
if !contains(out, "[REDACTED]") {
|
|
t.Fatalf("expected redacted: %s", out)
|
|
}
|
|
if !contains(out, "a@b.com") {
|
|
t.Fatalf("email should remain: %s", out)
|
|
}
|
|
}
|
|
|
|
func contains(s, sub string) bool {
|
|
return len(s) >= len(sub) && (s == sub || len(sub) == 0 ||
|
|
(len(s) > 0 && (func() bool {
|
|
for i := 0; i+len(sub) <= len(s); i++ {
|
|
if s[i:i+len(sub)] == sub {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
})()))
|
|
}
|