| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package caddy |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "os" |
| 20 | "path/filepath" |
| 21 | "runtime" |
| 22 | "sync" |
| 23 | "testing" |
| 24 | ) |
| 25 | |
| 26 | func TestReplacer(t *testing.T) { |
| 27 | type testCase struct { |
| 28 | input, expect, empty string |
| 29 | } |
| 30 | |
| 31 | rep := testReplacer() |
| 32 | |
| 33 | // ReplaceAll |
| 34 | for i, tc := range []testCase{ |
| 35 | { |
| 36 | input: "{", |
| 37 | expect: "{", |
| 38 | }, |
| 39 | { |
| 40 | input: `\{`, |
| 41 | expect: `{`, |
| 42 | }, |
| 43 | { |
| 44 | input: "foo{", |
| 45 | expect: "foo{", |
| 46 | }, |
| 47 | { |
| 48 | input: `foo\{`, |
| 49 | expect: `foo{`, |
| 50 | }, |
| 51 | { |
| 52 | input: "foo{bar", |
| 53 | expect: "foo{bar", |
| 54 | }, |
| 55 | { |
| 56 | input: `foo\{bar`, |
| 57 | expect: `foo{bar`, |
| 58 | }, |
| 59 | { |
| 60 | input: "foo{bar}", |
| 61 | expect: "foo", |
| 62 | }, |
| 63 | { |
| 64 | input: `foo\{bar\}`, |
| 65 | expect: `foo{bar}`, |
| 66 | }, |
| 67 | { |
| 68 | input: "}", |
| 69 | expect: "}", |
| 70 | }, |
| 71 | { |
| 72 | input: `\}`, |
| 73 | expect: `}`, |
| 74 | }, |
| 75 | { |
| 76 | input: "{}", |
| 77 | expect: "", |
| 78 | }, |
| 79 | { |
| 80 | input: `\{\}`, |
| 81 | expect: `{}`, |
| 82 | }, |
| 83 | { |
| 84 | input: `{"json": "object"}`, |
| 85 | expect: "", |
| 86 | }, |
| 87 | { |
| 88 | input: `\{"json": "object"}`, |
| 89 | expect: `{"json": "object"}`, |
| 90 | }, |
| 91 | { |
| 92 | input: `\{"json": "object"\}`, |
| 93 | expect: `{"json": "object"}`, |
| 94 | }, |
| 95 | { |
| 96 | input: `\{"json": "object{bar}"\}`, |
| 97 | expect: `{"json": "object"}`, |
| 98 | }, |
| 99 | { |
| 100 | input: `\{"json": \{"nested": "object"\}\}`, |
| 101 | expect: `{"json": {"nested": "object"}}`, |
| 102 | }, |
| 103 | { |
| 104 | input: `\{"json": \{"nested": "{bar}"\}\}`, |
| 105 | expect: `{"json": {"nested": ""}}`, |
| 106 | }, |
| 107 | { |
| 108 | input: `pre \{"json": \{"nested": "{bar}"\}\}`, |
| 109 | expect: `pre {"json": {"nested": ""}}`, |
| 110 | }, |
| 111 | { |
| 112 | input: `\{"json": \{"nested": "{bar}"\}\} post`, |
| 113 | expect: `{"json": {"nested": ""}} post`, |
| 114 | }, |
| 115 | { |
| 116 | input: `pre \{"json": \{"nested": "{bar}"\}\} post`, |
| 117 | expect: `pre {"json": {"nested": ""}} post`, |
| 118 | }, |
| 119 | { |
| 120 | input: `{{`, |
| 121 | expect: "{{", |
| 122 | }, |
| 123 | { |
| 124 | input: `{{}`, |
| 125 | expect: "", |
| 126 | }, |
| 127 | { |
| 128 | input: `{"json": "object"\}`, |
| 129 | expect: "", |
| 130 | }, |
| 131 | { |
| 132 | input: `{unknown}`, |
| 133 | empty: "-", |
| 134 | expect: "-", |
| 135 | }, |
| 136 | { |
| 137 | input: `back\slashes`, |
| 138 | expect: `back\slashes`, |
| 139 | }, |
| 140 | { |
| 141 | input: `double back\\slashes`, |
| 142 | expect: `double back\\slashes`, |
| 143 | }, |
| 144 | { |
| 145 | input: `placeholder {with \{ brace} in name`, |
| 146 | expect: `placeholder in name`, |
| 147 | }, |
| 148 | { |
| 149 | input: `placeholder {with \} brace} in name`, |
| 150 | expect: `placeholder in name`, |
| 151 | }, |
| 152 | { |
| 153 | input: `placeholder {with \} \} braces} in name`, |
| 154 | expect: `placeholder in name`, |
| 155 | }, |
| 156 | { |
| 157 | input: `\{'group':'default','max_age':3600,'endpoints':[\{'url':'https://some.domain.local/a/d/g'\}],'include_subdomains':true\}`, |
| 158 | expect: `{'group':'default','max_age':3600,'endpoints':[{'url':'https://some.domain.local/a/d/g'}],'include_subdomains':true}`, |
| 159 | }, |
| 160 | { |
| 161 | input: `{}{}{}{\\\\}\\\\`, |
| 162 | expect: `{\\\}\\\\`, |
| 163 | }, |
| 164 | { |
| 165 | input: string([]byte{0x26, 0x00, 0x83, 0x7B, 0x84, 0x07, 0x5C, 0x7D, 0x84}), |
| 166 | expect: string([]byte{0x26, 0x00, 0x83, 0x7B, 0x84, 0x07, 0x7D, 0x84}), |
| 167 | }, |
| 168 | { |
| 169 | input: `\\}`, |
| 170 | expect: `\}`, |
| 171 | }, |
| 172 | } { |
| 173 | actual := rep.ReplaceAll(tc.input, tc.empty) |
| 174 | if actual != tc.expect { |
| 175 | t.Errorf("Test %d: '%s': expected '%s' but got '%s'", |
| 176 | i, tc.input, tc.expect, actual) |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | func TestReplacerSet(t *testing.T) { |
| 182 | rep := testReplacer() |
| 183 | |
| 184 | for _, tc := range []struct { |
| 185 | variable string |
| 186 | value any |
| 187 | }{ |
| 188 | { |
| 189 | variable: "test1", |
| 190 | value: "val1", |
| 191 | }, |
| 192 | { |
| 193 | variable: "asdf", |
| 194 | value: "123", |
| 195 | }, |
| 196 | { |
| 197 | variable: "numbers", |
| 198 | value: 123.456, |
| 199 | }, |
| 200 | { |
| 201 | variable: "äöü", |
| 202 | value: "öö_äü", |
| 203 | }, |
| 204 | { |
| 205 | variable: "with space", |
| 206 | value: "space value", |
| 207 | }, |
| 208 | { |
| 209 | variable: "1", |
| 210 | value: "test-123", |
| 211 | }, |
| 212 | { |
| 213 | variable: "mySuper_IP", |
| 214 | value: "1.2.3.4", |
| 215 | }, |
| 216 | { |
| 217 | variable: "testEmpty", |
| 218 | value: "", |
| 219 | }, |
| 220 | } { |
| 221 | rep.Set(tc.variable, tc.value) |
| 222 | |
| 223 | // test if key is added |
| 224 | if val, ok := rep.static[tc.variable]; ok { |
| 225 | if val != tc.value { |
| 226 | t.Errorf("Expected value '%s' for key '%s' got '%s'", tc.value, tc.variable, val) |
| 227 | } |
| 228 | } else { |
| 229 | t.Errorf("Expected existing key '%s' found nothing", tc.variable) |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // test if all keys are still there (by length) |
| 234 | length := len(rep.static) |
| 235 | if len(rep.static) != 8 { |
| 236 | t.Errorf("Expected length '%v' got '%v'", 7, length) |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | func TestReplacerReplaceKnown(t *testing.T) { |
| 241 | rep := Replacer{ |
| 242 | mapMutex: &sync.RWMutex{}, |
| 243 | providers: []replacementProvider{ |
| 244 | // split our possible vars to two functions (to test if both functions are called) |
| 245 | ReplacerFunc(func(key string) (val any, ok bool) { |
| 246 | switch key { |
| 247 | case "test1": |
| 248 | return "val1", true |
| 249 | case "asdf": |
| 250 | return "123", true |
| 251 | case "äöü": |
| 252 | return "öö_äü", true |
| 253 | case "with space": |
| 254 | return "space value", true |
| 255 | default: |
| 256 | return "NOOO", false |
| 257 | } |
| 258 | }), |
| 259 | ReplacerFunc(func(key string) (val any, ok bool) { |
| 260 | switch key { |
| 261 | case "1": |
| 262 | return "test-123", true |
| 263 | case "mySuper_IP": |
| 264 | return "1.2.3.4", true |
| 265 | case "testEmpty": |
| 266 | return "", true |
| 267 | default: |
| 268 | return "NOOO", false |
| 269 | } |
| 270 | }), |
| 271 | }, |
| 272 | } |
| 273 | |
| 274 | for _, tc := range []struct { |
| 275 | testInput string |
| 276 | expected string |
| 277 | }{ |
| 278 | { |
| 279 | // test vars without space |
| 280 | testInput: "{test1}{asdf}{äöü}{1}{with space}{mySuper_IP}", |
| 281 | expected: "val1123öö_äütest-123space value1.2.3.4", |
| 282 | }, |
| 283 | { |
| 284 | // test vars with space |
| 285 | testInput: "{test1} {asdf} {äöü} {1} {with space} {mySuper_IP} ", |
| 286 | expected: "val1 123 öö_äü test-123 space value 1.2.3.4 ", |
| 287 | }, |
| 288 | { |
| 289 | // test with empty val |
| 290 | testInput: "{test1} {testEmpty} {asdf} {1} ", |
| 291 | expected: "val1 EMPTY 123 test-123 ", |
| 292 | }, |
| 293 | { |
| 294 | // test vars with not finished placeholders |
| 295 | testInput: "{te{test1}{as{{df{1}", |
| 296 | expected: "{teval1{as{{dftest-123", |
| 297 | }, |
| 298 | { |
| 299 | // test with non existing vars |
| 300 | testInput: "{test1} {nope} {1} ", |
| 301 | expected: "val1 {nope} test-123 ", |
| 302 | }, |
| 303 | } { |
| 304 | actual := rep.ReplaceKnown(tc.testInput, "EMPTY") |
| 305 | |
| 306 | // test if all are replaced as expected |
| 307 | if actual != tc.expected { |
| 308 | t.Errorf("Expected '%s' got '%s' for '%s'", tc.expected, actual, tc.testInput) |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | func TestReplacerDelete(t *testing.T) { |
| 314 | rep := Replacer{ |
| 315 | mapMutex: &sync.RWMutex{}, |
| 316 | static: map[string]any{ |
| 317 | "key1": "val1", |
| 318 | "key2": "val2", |
| 319 | "key3": "val3", |
| 320 | "key4": "val4", |
| 321 | }, |
| 322 | } |
| 323 | |
| 324 | startLen := len(rep.static) |
| 325 | |
| 326 | toDel := []string{ |
| 327 | "key2", "key4", |
| 328 | } |
| 329 | |
| 330 | for _, key := range toDel { |
| 331 | rep.Delete(key) |
| 332 | |
| 333 | // test if key is removed from static map |
| 334 | if _, ok := rep.static[key]; ok { |
| 335 | t.Errorf("Expected '%s' to be removed. It is still in static map.", key) |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | // check if static slice is smaller |
| 340 | expected := startLen - len(toDel) |
| 341 | actual := len(rep.static) |
| 342 | if len(rep.static) != expected { |
| 343 | t.Errorf("Expected length '%v' got length '%v'", expected, actual) |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | func TestReplacerMap(t *testing.T) { |
| 348 | rep := testReplacer() |
| 349 | |
| 350 | for i, tc := range []ReplacerFunc{ |
| 351 | func(key string) (val any, ok bool) { |
| 352 | return "", false |
| 353 | }, |
| 354 | func(key string) (val any, ok bool) { |
| 355 | return "", false |
| 356 | }, |
| 357 | } { |
| 358 | rep.Map(tc) |
| 359 | |
| 360 | // test if function (which listens on specific key) is added by checking length |
| 361 | if len(rep.providers) == i+1 { |
| 362 | // check if the last function is the one we just added |
| 363 | pTc := fmt.Sprintf("%p", tc) |
| 364 | pRep := fmt.Sprintf("%p", rep.providers[i]) |
| 365 | if pRep != pTc { |
| 366 | t.Errorf("Expected func pointer '%s' got '%s'", pTc, pRep) |
| 367 | } |
| 368 | } else { |
| 369 | t.Errorf("Expected providers length '%v' got length '%v'", i+1, len(rep.providers)) |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | func TestReplacerNew(t *testing.T) { |
| 375 | repl := NewReplacer() |
| 376 | |
| 377 | if len(repl.providers) != 3 { |
| 378 | t.Errorf("Expected providers length '%v' got length '%v'", 3, len(repl.providers)) |
| 379 | } |
| 380 | |
| 381 | // test if default global replacements are added as the first provider |
| 382 | hostname, _ := os.Hostname() |
| 383 | wd, _ := os.Getwd() |
| 384 | os.Setenv("CADDY_REPLACER_TEST", "envtest") |
| 385 | defer os.Setenv("CADDY_REPLACER_TEST", "") |
| 386 | |
| 387 | for _, tc := range []struct { |
| 388 | variable string |
| 389 | value string |
| 390 | }{ |
| 391 | { |
| 392 | variable: "system.hostname", |
| 393 | value: hostname, |
| 394 | }, |
| 395 | { |
| 396 | variable: "system.slash", |
| 397 | value: string(filepath.Separator), |
| 398 | }, |
| 399 | { |
| 400 | variable: "system.os", |
| 401 | value: runtime.GOOS, |
| 402 | }, |
| 403 | { |
| 404 | variable: "system.arch", |
| 405 | value: runtime.GOARCH, |
| 406 | }, |
| 407 | { |
| 408 | variable: "system.wd", |
| 409 | value: wd, |
| 410 | }, |
| 411 | { |
| 412 | variable: "env.CADDY_REPLACER_TEST", |
| 413 | value: "envtest", |
| 414 | }, |
| 415 | } { |
| 416 | if val, ok := repl.providers[0].replace(tc.variable); ok { |
| 417 | if val != tc.value { |
| 418 | t.Errorf("Expected value '%s' for key '%s' got '%s'", tc.value, tc.variable, val) |
| 419 | } |
| 420 | } else { |
| 421 | t.Errorf("Expected key '%s' to be recognized by first provider", tc.variable) |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | // test if file provider is added as the second provider |
| 426 | for _, tc := range []struct { |
| 427 | variable string |
| 428 | value string |
| 429 | }{ |
| 430 | { |
| 431 | variable: "file.caddytest/integration/testdata/foo.txt", |
| 432 | value: "foo", |
| 433 | }, |
| 434 | { |
| 435 | variable: "file.caddytest/integration/testdata/foo_with_trailing_newline.txt", |
| 436 | value: "foo", |
| 437 | }, |
| 438 | { |
| 439 | variable: "file.caddytest/integration/testdata/foo_with_multiple_trailing_newlines.txt", |
| 440 | value: "foo" + getEOL(), |
| 441 | }, |
| 442 | } { |
| 443 | if val, ok := repl.providers[1].replace(tc.variable); ok { |
| 444 | if val != tc.value { |
| 445 | t.Errorf("Expected value '%s' for key '%s' got '%s'", tc.value, tc.variable, val) |
| 446 | } |
| 447 | } else { |
| 448 | t.Errorf("Expected key '%s' to be recognized by second provider", tc.variable) |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | func getEOL() string { |
| 454 | if os.PathSeparator == '\\' { |
| 455 | return "\r\n" // Windows EOL |
| 456 | } |
| 457 | return "\n" // Unix and modern macOS EOL |
| 458 | } |
| 459 | |
| 460 | func TestReplacerNewWithoutFile(t *testing.T) { |
| 461 | repl := NewReplacer().WithoutFile() |
| 462 | |
| 463 | for _, tc := range []struct { |
| 464 | variable string |
| 465 | value string |
| 466 | notFound bool |
| 467 | }{ |
| 468 | { |
| 469 | variable: "file.caddytest/integration/testdata/foo.txt", |
| 470 | notFound: true, |
| 471 | }, |
| 472 | { |
| 473 | variable: "system.os", |
| 474 | value: runtime.GOOS, |
| 475 | }, |
| 476 | } { |
| 477 | if val, ok := repl.Get(tc.variable); ok && !tc.notFound { |
| 478 | if val != tc.value { |
| 479 | t.Errorf("Expected value '%s' for key '%s' got '%s'", tc.value, tc.variable, val) |
| 480 | } |
| 481 | } else if !tc.notFound { |
| 482 | t.Errorf("Expected key '%s' to be recognized", tc.variable) |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | func BenchmarkReplacer(b *testing.B) { |
| 488 | type testCase struct { |
| 489 | name, input, empty string |
| 490 | } |
| 491 | |
| 492 | rep := testReplacer() |
| 493 | rep.Set("str", "a string") |
| 494 | rep.Set("int", 123.456) |
| 495 | |
| 496 | for _, bm := range []testCase{ |
| 497 | { |
| 498 | name: "no placeholder", |
| 499 | input: `simple string`, |
| 500 | }, |
| 501 | { |
| 502 | name: "string replacement", |
| 503 | input: `str={str}`, |
| 504 | }, |
| 505 | { |
| 506 | name: "int replacement", |
| 507 | input: `int={int}`, |
| 508 | }, |
| 509 | { |
| 510 | name: "placeholder", |
| 511 | input: `{"json": "object"}`, |
| 512 | }, |
| 513 | { |
| 514 | name: "escaped placeholder", |
| 515 | input: `\{"json": \{"nested": "{bar}"\}\}`, |
| 516 | }, |
| 517 | } { |
| 518 | b.Run(bm.name, func(b *testing.B) { |
| 519 | for b.Loop() { |
| 520 | rep.ReplaceAll(bm.input, bm.empty) |
| 521 | } |
| 522 | }) |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | func testReplacer() Replacer { |
| 527 | return Replacer{ |
| 528 | providers: make([]replacementProvider, 0), |
| 529 | static: make(map[string]any), |
| 530 | mapMutex: &sync.RWMutex{}, |
| 531 | } |
| 532 | } |