2020-04-16 14:37:45 +02:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-11-02 11:08:34 +02:00
|
|
|
"net/url"
|
2020-04-16 14:37:45 +02:00
|
|
|
"testing"
|
2020-11-02 11:08:34 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-04-16 14:37:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSecrets(t *testing.T) {
|
|
|
|
t.Run("should log", func(t *testing.T) {
|
|
|
|
secret := "password"
|
|
|
|
|
2020-05-06 13:35:40 +02:00
|
|
|
outWriter := Entry().Logger.Out
|
2020-04-16 14:37:45 +02:00
|
|
|
var buffer bytes.Buffer
|
|
|
|
Entry().Logger.SetOutput(&buffer)
|
2020-05-06 13:35:40 +02:00
|
|
|
defer func() { Entry().Logger.SetOutput(outWriter) }()
|
|
|
|
|
2020-04-16 14:37:45 +02:00
|
|
|
Entry().Infof("My secret is %s.", secret)
|
|
|
|
assert.Contains(t, buffer.String(), secret)
|
|
|
|
|
|
|
|
buffer.Reset()
|
|
|
|
RegisterSecret(secret)
|
|
|
|
Entry().Infof("My secret is %s.", secret)
|
|
|
|
assert.NotContains(t, buffer.String(), secret)
|
|
|
|
})
|
2020-11-02 11:08:34 +02:00
|
|
|
|
|
|
|
t.Run("should log url encoded", func(t *testing.T) {
|
|
|
|
secret := "secret-token!0"
|
|
|
|
encodedSecret := url.QueryEscape(secret)
|
|
|
|
|
|
|
|
outWriter := Entry().Logger.Out
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
Entry().Logger.SetOutput(&buffer)
|
|
|
|
defer func() { Entry().Logger.SetOutput(outWriter) }()
|
|
|
|
|
|
|
|
Entry().Infof("My secret is %s.", secret)
|
|
|
|
assert.Contains(t, buffer.String(), secret)
|
|
|
|
|
|
|
|
buffer.Reset()
|
|
|
|
RegisterSecret(secret)
|
|
|
|
Entry().Infof("My secret is %s.", encodedSecret)
|
|
|
|
assert.NotContains(t, buffer.String(), encodedSecret)
|
|
|
|
})
|
2020-04-16 14:37:45 +02:00
|
|
|
}
|
2020-05-06 13:35:40 +02:00
|
|
|
|
|
|
|
func TestWriteLargeBuffer(t *testing.T) {
|
|
|
|
t.Run("should log large buffer without linebreaks via Writer()", func(t *testing.T) {
|
|
|
|
b := []byte("a")
|
|
|
|
size := 131072
|
|
|
|
b = bytes.Repeat(b, size)
|
|
|
|
written, err := Writer().Write(b)
|
|
|
|
assert.Equal(t, size, written)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("fails writing large buffer without linebreaks via Entry().Writer()", func(t *testing.T) {
|
|
|
|
// NOTE: If this test starts failing, then the logrus issue has been fixed and
|
|
|
|
// the work-around with Writer() can be removed.
|
|
|
|
b := []byte("a")
|
|
|
|
size := 131072
|
|
|
|
b = bytes.Repeat(b, size)
|
|
|
|
written, err := Entry().Writer().Write(b)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, size != written)
|
|
|
|
})
|
|
|
|
}
|