@@ -11,6 +11,8 @@ import (
1111 "github.com/secrethub/secrethub-go/pkg/randchar"
1212)
1313
14+ var maskString = "<redacted by SecretHub>"
15+
1416func TestMatcher (t * testing.T ) {
1517 tests := []struct {
1618 matchString string
@@ -120,8 +122,6 @@ func TestMatcher(t *testing.T) {
120122}
121123
122124func TestNewMaskedWriter (t * testing.T ) {
123- maskString := "<redacted by SecretHub>"
124-
125125 timeout10s := time .Second * 10
126126 timeout1us := time .Microsecond * 1
127127 timeout0 := time .Second * 0
@@ -271,6 +271,35 @@ func TestNewMaskedWriter(t *testing.T) {
271271 }
272272}
273273
274+ func TestNewMaskedWriter_OnlyFlushingOnTimeoutBug (t * testing.T ) {
275+ // There was a bug in MaskedWriter where it was only flushed on a timeout when a secret was found in the middle
276+ // of write. This test assures this bug is not present by writing a secret in the middle of a Write and
277+ // checking whether Flush() returns before the timeout of the MaskedWriter.
278+
279+ var buf bytes.Buffer
280+
281+ maskStrings := [][]byte {[]byte ("foo" )}
282+
283+ w := NewMaskedWriter (& buf , maskStrings , maskString , time .Second * 10 )
284+
285+ go w .Run ()
286+ _ , err := w .Write ([]byte ("teststring foo more text" ))
287+ assert .OK (t , err )
288+
289+ done := make (chan struct {})
290+ go func () {
291+ err := w .Flush ()
292+ assert .OK (t , err )
293+ done <- struct {}{}
294+ }()
295+
296+ select {
297+ case <- time .After (5 * time .Second ):
298+ t .Error ("MaskedWriter was not flushed before timeout" )
299+ case <- done :
300+ }
301+ }
302+
274303type errWriter struct {
275304 err error
276305}
0 commit comments