forked from k0sproject/rig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader_test.go
More file actions
65 lines (61 loc) · 2.18 KB
/
Copy pathreader_test.go
File metadata and controls
65 lines (61 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package redact_test
import (
"bytes"
"errors"
"fmt"
"io"
"testing"
"github.com/k0sproject/rig/v2/redact"
)
func TestRedactReader(t *testing.T) {
testCases := []struct {
name string
buffer []byte
match []string
mask string
expectedOut []byte
}{
{"no matches", []byte("Hello, World"), []string{"ZZZ"}, "*", []byte("Hello, World")},
{"single full match", []byte("Hello, World"), []string{"ll"}, "??", []byte("He??o, World")},
{"two full matches", []byte("Hello, World"), []string{"l"}, ".", []byte("He..o, Wor.d")},
{"two full matches, long mask", []byte("Hello, World"), []string{"l"}, "REDACTED", []byte("HeREDACTEDREDACTEDo, WorREDACTEDd")},
{"non-completing partial match", []byte("Hello, World"), []string{"World!"}, "*", []byte("Hello, World")},
{"completing partial match", []byte("Hello, World!"), []string{"World"}, "[REDACTED]", []byte("Hello, [REDACTED]!")},
{"match is a subset of the mask", []byte("Hello, World!"), []string{","}, ",,", []byte("Hello,, World!")},
{"nothing but match", []byte("Hello, World!"), []string{"Hello, World!"}, ".", []byte(".")},
}
for _, tc := range testCases {
for _, bufSize := range []int{1, 2, 5, 100, 1000} {
t.Run(fmt.Sprintf("%s bufsize %d", tc.name, bufSize), func(t *testing.T) {
out := &bytes.Buffer{}
buf := make([]byte, bufSize)
redactReader := redact.Reader(bytes.NewReader(tc.buffer), tc.mask, tc.match...)
for {
n, err := redactReader.Read(buf)
if n > bufSize {
t.Fatalf("Read more bytes than the buffer size")
}
if n > 0 {
_, err = out.Write(buf[:n])
if err != nil {
t.Fatalf("unexpected error while copying buffers: %v", err)
}
}
if err != nil {
if errors.Is(err, io.EOF) {
break
}
t.Fatalf("unexpected error: %v", err)
}
}
outBytes := out.Bytes()
if len(outBytes) != len(tc.expectedOut) {
t.Errorf("Expected %d bytes, but got %d", len(tc.expectedOut), len(outBytes))
}
if !bytes.Equal(outBytes, tc.expectedOut) {
t.Errorf("Output not what expected!\n\tTest parameters: %+v\n\tExpected %s, but got %s", tc, string(tc.expectedOut), string(outBytes))
}
})
}
}
}