sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/secretutil/censor_test.go (about) 1 /* 2 Copyright 2021 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package secretutil 18 19 import ( 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 ) 24 25 func TestReloadingCensorer(t *testing.T) { 26 text := func() []byte { 27 return []byte("secret SECRET c2VjcmV0 sEcReT") 28 } 29 var testCases = []struct { 30 name string 31 mutation func(c *ReloadingCensorer) 32 expected []byte 33 }{ 34 { 35 name: "no registered secrets", 36 mutation: func(c *ReloadingCensorer) {}, 37 expected: text(), 38 }, 39 { 40 name: "registered strings", 41 mutation: func(c *ReloadingCensorer) { 42 c.Refresh("secret") 43 }, 44 expected: []byte("XXXXXX SECRET XXXXXXXX sEcReT"), 45 }, 46 { 47 name: "registered strings with padding", 48 mutation: func(c *ReloadingCensorer) { 49 c.Refresh(" secret ") 50 }, 51 expected: []byte("XXXXXX SECRET XXXXXXXX sEcReT"), 52 }, 53 { 54 name: "registered strings only padding", 55 mutation: func(c *ReloadingCensorer) { 56 c.Refresh(" ") 57 }, 58 expected: text(), 59 }, 60 { 61 name: "registered multiple strings", 62 mutation: func(c *ReloadingCensorer) { 63 c.Refresh("secret", "SECRET", "sEcReT") 64 }, 65 expected: []byte("XXXXXX XXXXXX XXXXXXXX XXXXXX"), 66 }, 67 { 68 name: "registered bytes", 69 mutation: func(c *ReloadingCensorer) { 70 c.RefreshBytes([]byte("secret")) 71 }, 72 expected: []byte("XXXXXX SECRET XXXXXXXX sEcReT"), 73 }, 74 } 75 76 for _, testCase := range testCases { 77 t.Run(testCase.name, func(t *testing.T) { 78 censorer := NewCensorer() 79 testCase.mutation(censorer) 80 input := text() 81 censorer.Censor(&input) 82 if len(input) != len(text()) { 83 t.Errorf("%s: length of input changed from %d to %d", testCase.name, len(text()), len(input)) 84 } 85 if diff := cmp.Diff(string(testCase.expected), string(input)); diff != "" { 86 t.Errorf("%s: got incorrect text after censor: %v", testCase.name, diff) 87 } 88 }) 89 } 90 } 91 92 func TestBooleanNotHidden(t *testing.T) { 93 var testCases = []struct { 94 name string 95 mutation func(c *ReloadingCensorer) 96 text func() []byte 97 expected []byte 98 }{ 99 { 100 name: "true skipped", 101 mutation: func(c *ReloadingCensorer) { 102 c.Refresh("true", "True", "TRUE", " TRUE", "tRue") 103 }, 104 text: func() []byte { 105 return []byte("true True TRUE tRUE should stay") 106 }, 107 expected: []byte("true True TRUE tRUE should stay"), 108 }, 109 { 110 name: "false skipped", 111 mutation: func(c *ReloadingCensorer) { 112 c.Refresh("false", "False", "FALSE", " FALSE", "fAlse") 113 }, 114 text: func() []byte { 115 return []byte("false False FALSE fALse should stay") 116 }, 117 expected: []byte("false False FALSE fALse should stay"), 118 }, 119 { 120 name: "true bytes skipped", 121 mutation: func(c *ReloadingCensorer) { 122 c.RefreshBytes([]byte("true"), []byte("True"), []byte("TRUE"), []byte(" TRUE"), []byte("tRue")) 123 }, 124 text: func() []byte { 125 return []byte("true True TRUE tRUE should stay") 126 }, 127 expected: []byte("true True TRUE tRUE should stay"), 128 }, 129 { 130 name: "false bytes skipped", 131 mutation: func(c *ReloadingCensorer) { 132 c.RefreshBytes([]byte("false"), []byte("False"), []byte("FALSE"), []byte(" FALSE"), []byte("fAlse")) 133 }, 134 text: func() []byte { 135 return []byte("false False FALSE fALse should stay") 136 }, 137 expected: []byte("false False FALSE fALse should stay"), 138 }, 139 } 140 141 for _, testCase := range testCases { 142 t.Run(testCase.name, func(t *testing.T) { 143 censorer := NewCensorer() 144 testCase.mutation(censorer) 145 input := testCase.text() 146 censorer.Censor(&input) 147 if diff := cmp.Diff(string(testCase.expected), string(input)); diff != "" { 148 t.Errorf("%s: got incorrect text after censor: %v", testCase.name, diff) 149 } 150 }) 151 } 152 }