package scrub
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestBuffer(t *testing.T) {
|
|
for _, test := range testAllCases {
|
|
t.Run(strings.SplitN(test.Test, "\n", 2)[0], func(it *testing.T) {
|
|
b := NewBuffer(All)
|
|
defer b.Reset()
|
|
|
|
if _, err := b.WriteString(test.Test); err != nil {
|
|
it.Fatal(err)
|
|
return
|
|
}
|
|
|
|
r, err := ioutil.ReadAll(b)
|
|
if err != nil {
|
|
it.Fatal(err)
|
|
return
|
|
}
|
|
if result := string(r); result != test.Want {
|
|
it.Fatalf("expected %q, got %q", test.Want, result)
|
|
return
|
|
}
|
|
it.Log(string(r))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBuffer_Read(t *testing.T) {
|
|
b := NewBuffer(All)
|
|
|
|
go func(b *Buffer) {
|
|
time.Sleep(time.Millisecond * 10)
|
|
_, _ = b.WriteString("$apr1$71850310$gh9m4xcAn3MGxogwX/ztb.")
|
|
}(b)
|
|
|
|
// Testing Write-before-Read buffer signalling.
|
|
r, err := ioutil.ReadAll(b)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
return
|
|
}
|
|
|
|
want := "$apr1$*redacted*"
|
|
if string(r) != want {
|
|
t.Fatalf("expected %q, got %q", want, string(r))
|
|
}
|
|
}
|
|
|
|
func TestBuffer_ScrubSize(t *testing.T) {
|
|
for _, test := range testAllCases {
|
|
t.Run(strings.SplitN(test.Test, "\n", 2)[0], func(it *testing.T) {
|
|
b := NewBuffer(All)
|
|
defer b.Reset()
|
|
|
|
b.ScrubSize(len(test.Test))
|
|
if _, err := b.WriteString(test.Test); err != nil {
|
|
it.Fatal(err)
|
|
return
|
|
}
|
|
|
|
r, err := ioutil.ReadAll(b)
|
|
if err != nil {
|
|
it.Fatal(err)
|
|
return
|
|
}
|
|
if result := string(r); result != test.Want {
|
|
it.Fatalf("expected %q, got %q", test.Want, result)
|
|
return
|
|
}
|
|
it.Log(string(r))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBuffer_ScrubAfter(t *testing.T) {
|
|
if testing.Short() {
|
|
t.SkipNow()
|
|
}
|
|
for _, test := range testAllCases {
|
|
t.Run(strings.SplitN(test.Test, "\n", 2)[0], func(it *testing.T) {
|
|
b := NewBuffer(All)
|
|
defer b.Reset()
|
|
|
|
c := b.ScrubAfter(time.Millisecond * 5)
|
|
defer c()
|
|
|
|
if _, err := b.WriteString(test.Test); err != nil {
|
|
it.Fatal(err)
|
|
return
|
|
}
|
|
|
|
// Allow the scrub to take place.
|
|
time.Sleep(time.Millisecond * 25)
|
|
|
|
r, err := ioutil.ReadAll(b)
|
|
if err != nil {
|
|
it.Fatal(err)
|
|
return
|
|
}
|
|
if result := string(r); result != test.Want {
|
|
it.Fatalf("expected %q, got %q", test.Want, result)
|
|
return
|
|
}
|
|
it.Log(string(r))
|
|
})
|
|
}
|
|
}
|