2023-08-10 09:06:00 +00:00
|
|
|
// Copyright 2023 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-01-08 19:39:52 +00:00
|
|
|
package shared
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewSecretsReplacer(t *testing.T) {
|
|
|
|
tc := []struct {
|
2023-10-31 18:44:03 +00:00
|
|
|
name string
|
2022-01-08 19:39:52 +00:00
|
|
|
log string
|
|
|
|
secrets []string
|
|
|
|
expect string
|
|
|
|
}{{
|
2023-10-31 18:44:03 +00:00
|
|
|
name: "dont replace secrets with less than 3 chars",
|
2022-01-08 19:39:52 +00:00
|
|
|
log: "start log\ndone",
|
2023-10-31 18:44:03 +00:00
|
|
|
secrets: []string{"", "d", "art"},
|
2022-01-08 19:39:52 +00:00
|
|
|
expect: "start log\ndone",
|
|
|
|
}, {
|
2023-10-31 18:44:03 +00:00
|
|
|
name: "single line passwords",
|
2022-01-08 19:39:52 +00:00
|
|
|
log: `this IS secret: password`,
|
|
|
|
secrets: []string{"password", " IS "},
|
2023-10-31 18:44:03 +00:00
|
|
|
expect: `this IS secret: ********`,
|
2022-01-08 19:39:52 +00:00
|
|
|
}, {
|
2023-10-31 18:44:03 +00:00
|
|
|
name: "secret with one newline",
|
2022-01-08 19:39:52 +00:00
|
|
|
log: "start log\ndone\nnow\nan\nmulti line secret!! ;)",
|
|
|
|
secrets: []string{"an\nmulti line secret!!"},
|
|
|
|
expect: "start log\ndone\nnow\n********\n******** ;)",
|
2022-01-16 21:57:37 +00:00
|
|
|
}, {
|
2023-10-31 18:44:03 +00:00
|
|
|
name: "secret with multible lines with no match",
|
2022-01-16 21:57:37 +00:00
|
|
|
log: "start log\ndone\nnow\nan\nmulti line secret!! ;)",
|
|
|
|
secrets: []string{"Test\nwith\n\ntwo new lines"},
|
|
|
|
expect: "start log\ndone\nnow\nan\nmulti line secret!! ;)",
|
|
|
|
}, {
|
2023-10-31 18:44:03 +00:00
|
|
|
name: "secret with multible lines with match",
|
2022-01-16 21:57:37 +00:00
|
|
|
log: "start log\ndone\nnow\nan\nmulti line secret!! ;)\nwith\ntwo\n\nnewlines",
|
|
|
|
secrets: []string{"an\nmulti line secret!!", "two\n\nnewlines"},
|
|
|
|
expect: "start log\ndone\nnow\n********\n******** ;)\nwith\n********\n\n********",
|
2022-01-08 19:39:52 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, c := range tc {
|
2023-10-31 18:44:03 +00:00
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
rep := NewSecretsReplacer(c.secrets)
|
|
|
|
result := rep.Replace(c.log)
|
|
|
|
assert.EqualValues(t, c.expect, result)
|
|
|
|
})
|
2022-01-08 19:39:52 +00:00
|
|
|
}
|
|
|
|
}
|