Data scrubbing options for protecting sensitive data https://godoc.org/maze.io/x/scrub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.3 KiB

package scrub
import "testing"
var testEntropy = []struct {
Test, Want string
}{
{"", ""},
{"1223334444", "1223334444"},
{"password123", Replacement},
{"Tr0ub4dour&3", Replacement},
{"test correcthorsebatterystaple", "test correcthorsebatterystaple"},
{"test coRrecth0rseba++ery9.23.2007staple$", "test " + Replacement},
{"rWibMFACxAUGZmxhVncy test", Replacement + " test"},
{"test Ba9ZyWABu99[BK#6MBgbH88Tofv)vs$ test", "test " + Replacement + " test"},
}
func TestEntropy(t *testing.T) {
for _, test := range testEntropy {
t.Run(test.Test, func(it *testing.T) {
if v := Entropy(test.Test); v != test.Want {
it.Errorf("expected %q to return %q, got %q", test.Test, test.Want, v)
}
})
}
}
func TestEntropyScrubber_Scrub(t *testing.T) {
var scrubber EntropyScrubber // using defaults
for _, test := range testEntropy {
t.Run(test.Test, func(it *testing.T) {
if v := scrubber.Scrub(test.Test); v != test.Want {
it.Errorf("expected %q to return %q, got %q", test.Test, test.Want, v)
}
})
}
}
func TestEntropyScrubber(t *testing.T) {
scrubber := EntropyScrubber{
Whitespace: []rune(DefaultWhitespace),
Threshold: DefaultEntropyThreshold,
}
tests := []struct {
Test, Want string
}{
// No scrubbing:
{"ps axufwww", "ps axufwww"},
// Secret scrubbing:
{"chpasswd root coRrecth0rseba++ery9.23.2007staple$", "chpasswd root *redacted*"},
}
for _, test := range tests {
t.Run(test.Test, func(it *testing.T) {
if v := scrubber.Scrub(test.Test); v != test.Want {
t.Errorf("expected %q to return %q, got %q", test.Test, test.Want, v)
}
})
}
}
func TestEntropyShannon(t *testing.T) {
var tests = []struct {
Test string
Want float64
}{
{"", 0},
{"1223334444", 1.8464393446710152},
{"password123", 3.2776134368191157},
{"Tr0ub4dour&3", 3.2516291673878226},
{"correcthorsebatterystaple", 3.363856189774724},
{"coRrecth0rseba++ery9.23.2007staple$", 4.229003731107054},
{"rWibMFACxAUGZmxhVncy", 4.1219280948873624},
{"Ba9ZyWABu99[BK#6MBgbH88Tofv)vs$", 4.413716068381602},
}
for _, test := range tests {
t.Run(test.Test, func(it *testing.T) {
if v := entropy(test.Test); !almostEqual(v, test.Want) {
it.Errorf("expected %q to return %g, got %g", test.Test, test.Want, v)
}
})
}
}
const epsilon = 0.00000001
func almostEqual(a, b float64) bool {
return (a-b) < epsilon && (b-a) < epsilon
}