1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/log/log_test.go
Googlom 33b8c489f9
fix dependencies with security issues (#4790)
* remove vault interaction from unit tests

* go mod tidy

* update some dependency minor versions

* update github.com/getsentry/sentry-go

* fix vault dependency

* update google.golang.org/api and cloud.google.com/go/storage

* fix unit test

---------

Co-authored-by: Gulom Alimov <gulomjon.alimov@sap.com>
2024-01-24 15:19:23 +05:00

61 lines
1.4 KiB
Go

//go:build unit
// +build unit
package log
import (
"bytes"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSecrets(t *testing.T) {
t.Run("should log", func(t *testing.T) {
secret := "password"
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.", secret)
assert.NotContains(t, buffer.String(), secret)
})
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)
})
}
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)
})
}