|
| 1 | +package linecontent |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +const ( |
| 9 | + dummySecret = "DummySecret" |
| 10 | +) |
| 11 | + |
| 12 | +func TestGetLineContent(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + line string |
| 16 | + secret string |
| 17 | + expected string |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "Empty line", |
| 21 | + line: "", |
| 22 | + secret: dummySecret, |
| 23 | + expected: "", |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "Empty secret", |
| 27 | + line: "line", |
| 28 | + secret: "", |
| 29 | + expected: "", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "Secret not found with line size smaller than the parse limit", |
| 33 | + line: "Dummy content line", |
| 34 | + secret: dummySecret, |
| 35 | + expected: "Dummy content line", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "Secret not found with secret present and line size larger than the parse limit", |
| 39 | + line: "This is the start of a big line content" + strings.Repeat("A", lineContentMaxParseSize) + dummySecret, |
| 40 | + secret: dummySecret, |
| 41 | + expected: "This is the start of a big line content" + strings.Repeat("A", contextLeftSizeLimit+contextRightSizeLimit-len("This is the start of a big line content")), |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "Secret larger than the line", |
| 45 | + line: strings.Repeat("B", contextLeftSizeLimit) + strings.Repeat("A", contextRightSizeLimit), |
| 46 | + secret: "large secret" + strings.Repeat("B", contextRightSizeLimit+contextLeftSizeLimit+100), |
| 47 | + expected: strings.Repeat("B", contextLeftSizeLimit) + strings.Repeat("A", contextRightSizeLimit), |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "Secret at the beginning with line size smaller than the parse limit", |
| 51 | + line: "start:" + dummySecret + strings.Repeat("A", lineContentMaxParseSize/2), |
| 52 | + secret: dummySecret, |
| 53 | + expected: "start:" + dummySecret + strings.Repeat("A", contextRightSizeLimit), |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "Secret found in middle with line size smaller than the parse limit", |
| 57 | + line: "start" + strings.Repeat("A", contextLeftSizeLimit) + dummySecret + strings.Repeat("A", contextRightSizeLimit) + "end", |
| 58 | + secret: dummySecret, |
| 59 | + expected: strings.Repeat("A", contextLeftSizeLimit) + dummySecret + strings.Repeat("A", contextRightSizeLimit), |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "Secret at the end with line size smaller than the parse limit", |
| 63 | + line: strings.Repeat("A", lineContentMaxParseSize/2) + dummySecret + ":end", |
| 64 | + secret: dummySecret, |
| 65 | + expected: strings.Repeat("A", contextLeftSizeLimit) + dummySecret + ":end", |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "Secret at the beginning with line size larger than the parse limit", |
| 69 | + line: "start:" + dummySecret + strings.Repeat("A", lineContentMaxParseSize), |
| 70 | + secret: dummySecret, |
| 71 | + expected: "start:" + dummySecret + strings.Repeat("A", contextRightSizeLimit), |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "Secret found in middle with line size larger than the parse limit", |
| 75 | + line: "start" + strings.Repeat("A", contextLeftSizeLimit) + dummySecret + strings.Repeat("A", lineContentMaxParseSize) + "end", |
| 76 | + secret: dummySecret, |
| 77 | + expected: strings.Repeat("A", contextLeftSizeLimit) + dummySecret + strings.Repeat("A", contextRightSizeLimit), |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "Secret at the end with line size larger than the parse limit", |
| 81 | + line: strings.Repeat("A", lineContentMaxParseSize-100) + dummySecret + strings.Repeat("A", lineContentMaxParseSize), |
| 82 | + secret: dummySecret, |
| 83 | + expected: strings.Repeat("A", contextLeftSizeLimit) + dummySecret + strings.Repeat("A", calculateRepeatForSecretAtTheEndWithLargerThanParseLimit(100, 1, len(dummySecret))), |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "Secret at the beginning with line containing 2 byte chars and size smaller than the parse limit", |
| 87 | + line: "start:" + dummySecret + strings.Repeat("é", lineContentMaxParseSize/4), |
| 88 | + secret: dummySecret, |
| 89 | + expected: "start:" + dummySecret + strings.Repeat("é", contextRightSizeLimit), |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "Secret found in middle with line containing 2 byte chars and size smaller than the parse limit", |
| 93 | + line: "start" + strings.Repeat("é", contextLeftSizeLimit) + dummySecret + strings.Repeat("é", contextRightSizeLimit) + "end", |
| 94 | + secret: dummySecret, |
| 95 | + expected: strings.Repeat("é", contextLeftSizeLimit) + dummySecret + strings.Repeat("é", contextRightSizeLimit), |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "Secret at the end with line containing 2 byte chars and size smaller than the parse limit", |
| 99 | + line: strings.Repeat("é", lineContentMaxParseSize/4) + dummySecret + ":end", |
| 100 | + secret: dummySecret, |
| 101 | + expected: strings.Repeat("é", contextLeftSizeLimit) + dummySecret + ":end", |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "Secret at the beginning with line containing 2 byte chars and size larger than the parse limit", |
| 105 | + line: "start:" + dummySecret + strings.Repeat("é", lineContentMaxParseSize/2), |
| 106 | + secret: dummySecret, |
| 107 | + expected: "start:" + dummySecret + strings.Repeat("é", contextRightSizeLimit), |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "Secret found in middle with line containing 2 byte chars and size larger than the parse limit", |
| 111 | + line: "start" + strings.Repeat("é", contextLeftSizeLimit) + dummySecret + strings.Repeat("é", lineContentMaxParseSize/2) + "end", |
| 112 | + secret: dummySecret, |
| 113 | + expected: strings.Repeat("é", contextLeftSizeLimit) + dummySecret + strings.Repeat("é", contextRightSizeLimit), |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "Secret at the end with line containing 2 byte chars and size larger than the parse limit", |
| 117 | + line: strings.Repeat("é", lineContentMaxParseSize/2-100) + dummySecret + strings.Repeat("é", lineContentMaxParseSize/2), |
| 118 | + secret: dummySecret, |
| 119 | + expected: strings.Repeat("é", contextLeftSizeLimit) + dummySecret + strings.Repeat("é", calculateRepeatForSecretAtTheEndWithLargerThanParseLimit(100, 2, len(dummySecret))), |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "Secret at the beginning with line containing 3 byte chars and size smaller than the parse limit", |
| 123 | + line: "start:" + dummySecret + strings.Repeat("ࠚ", lineContentMaxParseSize/6), |
| 124 | + secret: dummySecret, |
| 125 | + expected: "start:" + dummySecret + strings.Repeat("ࠚ", contextRightSizeLimit), |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "Secret found in middle with line containing 3 byte chars and size smaller than the parse limit", |
| 129 | + line: "start" + strings.Repeat("ࠚ", contextLeftSizeLimit) + dummySecret + strings.Repeat("ࠚ", contextRightSizeLimit) + "end", |
| 130 | + secret: dummySecret, |
| 131 | + expected: strings.Repeat("ࠚ", contextLeftSizeLimit) + dummySecret + strings.Repeat("ࠚ", contextRightSizeLimit), |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "Secret at the end with line containing 3 byte chars and size smaller than the parse limit", |
| 135 | + line: strings.Repeat("ࠚ", lineContentMaxParseSize/6) + dummySecret + ":end", |
| 136 | + secret: dummySecret, |
| 137 | + expected: strings.Repeat("ࠚ", contextLeftSizeLimit) + dummySecret + ":end", |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "Secret at the beginning with line containing 3 byte chars and size larger than the parse limit", |
| 141 | + line: "start:" + dummySecret + strings.Repeat("ࠚ", lineContentMaxParseSize/3), |
| 142 | + secret: dummySecret, |
| 143 | + expected: "start:" + dummySecret + strings.Repeat("ࠚ", contextRightSizeLimit), |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "Secret found in middle with line containing 3 byte chars and size larger than the parse limit", |
| 147 | + line: "start" + strings.Repeat("ࠚ", contextLeftSizeLimit) + dummySecret + strings.Repeat("ࠚ", lineContentMaxParseSize/3) + "end", |
| 148 | + secret: dummySecret, |
| 149 | + expected: strings.Repeat("ࠚ", contextLeftSizeLimit) + dummySecret + strings.Repeat("ࠚ", contextRightSizeLimit), |
| 150 | + }, |
| 151 | + { |
| 152 | + name: "Secret at the end with line containing 3 byte chars and size larger than the parse limit", |
| 153 | + line: strings.Repeat("ࠚ", lineContentMaxParseSize/3-100) + dummySecret + strings.Repeat("ࠚ", lineContentMaxParseSize/3), |
| 154 | + secret: dummySecret, |
| 155 | + expected: strings.Repeat("ࠚ", contextLeftSizeLimit) + dummySecret + strings.Repeat("ࠚ", calculateRepeatForSecretAtTheEndWithLargerThanParseLimit(100, 3, len(dummySecret))), |
| 156 | + }, |
| 157 | + { |
| 158 | + name: "Secret at the beginning with line containing 4 byte chars and size smaller than the parse limit", |
| 159 | + line: "start:" + dummySecret + strings.Repeat("𝄞", lineContentMaxParseSize/8), |
| 160 | + secret: dummySecret, |
| 161 | + expected: "start:" + dummySecret + strings.Repeat("𝄞", contextRightSizeLimit), |
| 162 | + }, |
| 163 | + { |
| 164 | + name: "Secret found in middle with line containing 4 byte chars and size smaller than the parse limit", |
| 165 | + line: "start" + strings.Repeat("𝄞", contextLeftSizeLimit) + dummySecret + strings.Repeat("𝄞", contextRightSizeLimit) + "end", |
| 166 | + secret: dummySecret, |
| 167 | + expected: strings.Repeat("𝄞", contextLeftSizeLimit) + dummySecret + strings.Repeat("𝄞", contextRightSizeLimit), |
| 168 | + }, |
| 169 | + { |
| 170 | + name: "Secret at the end with line containing 4 byte chars and size smaller than the parse limit", |
| 171 | + line: strings.Repeat("𝄞", lineContentMaxParseSize/8) + dummySecret + ":end", |
| 172 | + secret: dummySecret, |
| 173 | + expected: strings.Repeat("𝄞", contextLeftSizeLimit) + dummySecret + ":end", |
| 174 | + }, |
| 175 | + { |
| 176 | + name: "Secret at the beginning with line containing 4 byte chars and size larger than the parse limit", |
| 177 | + line: "start:" + dummySecret + strings.Repeat("𝄞", lineContentMaxParseSize/4), |
| 178 | + secret: dummySecret, |
| 179 | + expected: "start:" + dummySecret + strings.Repeat("𝄞", contextRightSizeLimit), |
| 180 | + }, |
| 181 | + { |
| 182 | + name: "Secret found in middle with line containing 4 byte chars and size larger than the parse limit", |
| 183 | + line: "start" + strings.Repeat("𝄞", contextLeftSizeLimit) + dummySecret + strings.Repeat("𝄞", lineContentMaxParseSize/4) + "end", |
| 184 | + secret: dummySecret, |
| 185 | + expected: strings.Repeat("𝄞", contextLeftSizeLimit) + dummySecret + strings.Repeat("𝄞", contextRightSizeLimit), |
| 186 | + }, |
| 187 | + { |
| 188 | + name: "Secret at the end with line containing 4 byte chars and size larger than the parse limit", |
| 189 | + line: strings.Repeat("𝄞", lineContentMaxParseSize/4-100) + dummySecret + strings.Repeat("𝄞", lineContentMaxParseSize/4), |
| 190 | + secret: dummySecret, |
| 191 | + expected: strings.Repeat("𝄞", contextLeftSizeLimit) + dummySecret + strings.Repeat("𝄞", calculateRepeatForSecretAtTheEndWithLargerThanParseLimit(100, 4, len(dummySecret))), |
| 192 | + }, |
| 193 | + } |
| 194 | + |
| 195 | + for _, tt := range tests { |
| 196 | + t.Run(tt.name, func(t *testing.T) { |
| 197 | + got := GetLineContent(tt.line, tt.secret) |
| 198 | + if got != tt.expected { |
| 199 | + t.Errorf("GetLineContent() = %v, want %v", got, tt.expected) |
| 200 | + } |
| 201 | + }) |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +func calculateRepeatForSecretAtTheEndWithLargerThanParseLimit(offset, bytes, secretLength int) int { |
| 206 | + remainingSize := lineContentMaxParseSize - ((lineContentMaxParseSize/bytes - offset) * bytes) - secretLength |
| 207 | + return (remainingSize - ((3 - ((remainingSize) % bytes)) * bytes)) / bytes |
| 208 | +} |
0 commit comments