|
| 1 | +//go:build !windows |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2025 The Tekton Authors |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package main |
| 20 | + |
| 21 | +import ( |
| 22 | + "bytes" |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + "testing" |
| 26 | +) |
| 27 | + |
| 28 | +func TestMaskingWriter(t *testing.T) { |
| 29 | + testCases := []struct { |
| 30 | + name string |
| 31 | + secrets []string |
| 32 | + input string |
| 33 | + expected string |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "no secrets", |
| 37 | + secrets: nil, |
| 38 | + input: "hello world", |
| 39 | + expected: "hello world", |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "single secret", |
| 43 | + secrets: []string{"password123"}, |
| 44 | + input: "The password is password123!", |
| 45 | + expected: "The password is ***!", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "multiple secrets", |
| 49 | + secrets: []string{"secret1", "secret2"}, |
| 50 | + input: "secret1 and secret2 are here", |
| 51 | + expected: "*** and *** are here", |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "short secret skipped", |
| 55 | + secrets: []string{"ab"}, |
| 56 | + input: "ab ab ab", |
| 57 | + expected: "ab ab ab", |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "repeated secret", |
| 61 | + secrets: []string{"token"}, |
| 62 | + input: "token token token", |
| 63 | + expected: "*** *** ***", |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + for _, tc := range testCases { |
| 68 | + t.Run(tc.name, func(t *testing.T) { |
| 69 | + var buf bytes.Buffer |
| 70 | + w := newMaskingWriter(&buf, tc.secrets) |
| 71 | + if _, err := w.Write([]byte(tc.input)); err != nil { |
| 72 | + t.Fatalf("unexpected error: %v", err) |
| 73 | + } |
| 74 | + if got := buf.String(); got != tc.expected { |
| 75 | + t.Errorf("got %q, want %q", got, tc.expected) |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestLoadSecretsForMasking(t *testing.T) { |
| 82 | + dir := t.TempDir() |
| 83 | + filePath := filepath.Join(dir, "secrets") |
| 84 | + content := "c2VjcmV0MQ==\ncGFzc3dvcmQ=\n" |
| 85 | + if err := os.WriteFile(filePath, []byte(content), 0644); err != nil { |
| 86 | + t.Fatalf("failed to write test file: %v", err) |
| 87 | + } |
| 88 | + |
| 89 | + got, err := loadSecretsForMasking(filePath) |
| 90 | + if err != nil { |
| 91 | + t.Fatalf("unexpected error: %v", err) |
| 92 | + } |
| 93 | + |
| 94 | + expected := map[string]bool{"secret1": true, "password": true} |
| 95 | + for _, s := range got { |
| 96 | + if !expected[s] { |
| 97 | + t.Fatalf("unexpected secret: %s", s) |
| 98 | + } |
| 99 | + delete(expected, s) |
| 100 | + } |
| 101 | + if len(expected) != 0 { |
| 102 | + t.Fatalf("missing secrets: %v", expected) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func TestLoadSecretsForMaskingInvalidLine(t *testing.T) { |
| 107 | + dir := t.TempDir() |
| 108 | + filePath := filepath.Join(dir, "secrets") |
| 109 | + if err := os.WriteFile(filePath, []byte("not-base64\n"), 0644); err != nil { |
| 110 | + t.Fatalf("failed to write test file: %v", err) |
| 111 | + } |
| 112 | + |
| 113 | + if _, err := loadSecretsForMasking(filePath); err == nil { |
| 114 | + t.Fatalf("expected error") |
| 115 | + } |
| 116 | +} |
0 commit comments