Select Git revision
sensitive_words_test.go
-
Francé Wilke authoredFrancé Wilke authored
sensitive_words_test.go 3.21 KiB
package logs_test
import (
"testing"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs"
)
func TestFilter(t *testing.T) {
tests := []test{
{"Your new OTP is 12345", map[string]bool{"12345": true}, "Your new OTP is ***"}, // match at end of string
{"Your new OTP is 12345.", map[string]bool{"12345": true}, "Your new OTP is ***."}, // match in middle
{"12345 is your new OTP.", map[string]bool{"12345": true}, "*** is your new OTP."}, // match at start
{"Your new OTP is 12345. Do not repeat 12345/123456", map[string]bool{"12345": true}, "Your new OTP is ***. Do not repeat ***/123456"}, // no match on longer word
{"Send 12345\n12345 is the code you should type", map[string]bool{"12345": true}, "Send ***\n*** is the code you should type"}, // match before/after newline
//
{"Your new OTP is 111-11", map[string]bool{"111-11": true}, "Your new OTP is ***"},
{"The secret IP is 1.2.3.4 but 11.2.3.4 and 1.2.3.44 is public", map[string]bool{"1.2.3.4": true}, "The secret IP is *** but 11.2.3.4 and 1.2.3.44 is public"}, // match with delimiters in the word
//
{"1.2.3.4.5.6.7.8", map[string]bool{"1": true}, "***.2.3.4.5.6.7.8"}, // short replace at start
{"1.2.3.4.5.6.7.8", map[string]bool{"4": true}, "1.2.3.***.5.6.7.8"}, // short replace in middle
{"1.2.3.4.5.6.7.8", map[string]bool{"8": true}, "1.2.3.4.5.6.7.***"}, // short replace at end
//
{"11.22.33.44.55.66.77.88", map[string]bool{"11": true}, "***.22.33.44.55.66.77.88"}, // short replace at start
{"11.22.33.44.55.66.77.88", map[string]bool{"44": true}, "11.22.33.***.55.66.77.88"}, // short replace in middle
{"11.22.33.44.55.66.77.88", map[string]bool{"88": true}, "11.22.33.44.55.66.77.***"}, // short replace at end
//
{"111.222.333.444.555.666.777.888", map[string]bool{"111": true}, "***.222.333.444.555.666.777.888"}, // exact replace at start
{"111.222.333.444.555.666.777.888", map[string]bool{"444": true}, "111.222.333.***.555.666.777.888"}, // exact replace in middle
{"111.222.333.444.555.666.777.888", map[string]bool{"888": true}, "111.222.333.444.555.666.777.***"}, // exact replace at end
//
{"1111.2222.3333.4444.5555.6666.7777.8888", map[string]bool{"1111": true}, "***.2222.3333.4444.5555.6666.7777.8888"}, // long replace at start
{"1111.2222.3333.4444.5555.6666.7777.8888", map[string]bool{"4444": true}, "1111.2222.3333.***.5555.6666.7777.8888"}, // long replace in middle
{"1111.2222.3333.4444.5555.6666.7777.8888", map[string]bool{"8888": true}, "1111.2222.3333.4444.5555.6666.7777.***"}, // long replace at end
}
for testNr, test := range tests {
filtered, changed := logs.FilterSensitiveWordsMap(test.Text, test.Words)
if test.Filtered != filtered {
t.Fatalf("test[%d]: %s --%+v--> %s != %s", testNr, test.Text, test.Words, filtered, test.Filtered)
}
if test.Text != test.Filtered && !changed {
t.Fatalf("test[%d]: changed=%v != %v", testNr, changed, test.Text != test.Filtered)
}
t.Logf("test[%d]: %s --%+v--> %s", testNr, test.Text, test.Words, filtered)
}
}
type test struct {
Text string
Words map[string]bool
Filtered string
}