|
6 | 6 | "reflect" |
7 | 7 | "regexp" |
8 | 8 | "testing" |
| 9 | + "testing/synctest" |
| 10 | + "time" |
9 | 11 |
|
10 | 12 | "github.com/stretchr/testify/require" |
11 | 13 | "github.com/zalando/skipper/eskip" |
@@ -157,6 +159,12 @@ func Test_spec_Create(t *testing.T) { |
157 | 159 | matchBehavior: matchBehaviorAny, |
158 | 160 | }, |
159 | 161 | wantErr: false, |
| 162 | + }, { |
| 163 | + name: "many kv pair of args, one regexp error", |
| 164 | + spec: NewJWTPayloadAnyKVRegexp(), |
| 165 | + args: []interface{}{"uid", "^(?!4)", "claim1", "claimValue1"}, |
| 166 | + want: nil, |
| 167 | + wantErr: true, |
160 | 168 | }, { |
161 | 169 | name: "many kv pair of args, one missing", |
162 | 170 | spec: NewJWTPayloadAllKV(), |
@@ -619,6 +627,69 @@ s: JWTPayloadAnyKVRegexp("https://identity.zalando.com/managed-id", "^ssz") -> s |
619 | 627 |
|
620 | 628 | } |
621 | 629 |
|
| 630 | +func TestPredicateCacheClean(t *testing.T) { |
| 631 | + synctest.Test(t, func(t *testing.T) { |
| 632 | + reg := ®istry{ |
| 633 | + quit: make(chan struct{}), |
| 634 | + predicateMap: make(map[string]*predicate), |
| 635 | + } |
| 636 | + go reg.clean() |
| 637 | + defer reg.Close() |
| 638 | + |
| 639 | + spec := &spec{ |
| 640 | + name: predicates.JWTPayloadAllKVRegexpName, |
| 641 | + matchBehavior: matchBehaviorAll, |
| 642 | + matchMode: matchModeRegexp, |
| 643 | + reg: reg, |
| 644 | + } |
| 645 | + |
| 646 | + p, err := spec.Create([]any{"https://identity.zalando.com/managed-id", "^ssz", "https://identity.zalando.com/token", "^Bear"}) |
| 647 | + if err != nil { |
| 648 | + t.Fatalf("Failed to create predicate: %v", err) |
| 649 | + } |
| 650 | + |
| 651 | + token := "eyJraWQiOiJwbGF0Zm9ybS1pYW0tdmNlaHloajYiLCJhbGciOiJFUzI1NiJ9.eyJzdWIiOiJjNGRkZmU5ZC1hMGQzLTRhZmItYmYyNi0yNGI5NTg4NzMxYTAiLCJodHRwczovL2lkZW50aXR5LnphbGFuZG8uY29tL3JlYWxtIjoidXNlcnMiLCJodHRwczovL2lkZW50aXR5LnphbGFuZG8uY29tL3Rva2VuIjoiQmVhcmVyIiwiaHR0cHM6Ly9pZGVudGl0eS56YWxhbmRvLmNvbS9tYW5hZ2VkLWlkIjoic3N6dWVjcyIsImF6cCI6Inp0b2tlbiIsImh0dHBzOi8vaWRlbnRpdHkuemFsYW5kby5jb20vYnAiOiI4MTBkMWQwMC00MzEyLTQzZTUtYmQzMS1kODM3M2ZkZDI0YzciLCJhdXRoX3RpbWUiOjE1MjMyNTk0NjgsImlzcyI6Imh0dHBzOi8vaWRlbnRpdHkuemFsYW5kby5jb20iLCJleHAiOjE1MjUwMjQyODUsImlhdCI6MTUyNTAyMDY3NX0.uxHcC7DJrkP-_G81Jmiba5liVP0LJOmkpal4wsUr7CmtMlE23P1bptIMxnJLv5EMSN1NFn-BJe9hcEB2A3LarA" |
| 652 | + |
| 653 | + r := &http.Request{ |
| 654 | + Header: http.Header{ |
| 655 | + authHeaderName: []string{"Bearer " + token}, |
| 656 | + }, |
| 657 | + } |
| 658 | + if !p.Match(r) { |
| 659 | + t.Fatal("Failed to match") |
| 660 | + } |
| 661 | + |
| 662 | + if l := len(reg.predicateMap); l != 1 { |
| 663 | + t.Fatalf("Failed to get predicateMap of len 1, got: %d", l) |
| 664 | + } |
| 665 | + |
| 666 | + ok := false |
| 667 | + for _, p := range reg.predicateMap { |
| 668 | + p.cache.Range(func(k, v any) bool { |
| 669 | + ok = true |
| 670 | + return true |
| 671 | + }) |
| 672 | + } |
| 673 | + if !ok { |
| 674 | + t.Fatal("Failed to get cache filled") |
| 675 | + } |
| 676 | + |
| 677 | + time.Sleep(time.Hour + time.Minute) |
| 678 | + |
| 679 | + ok = true |
| 680 | + for _, p := range reg.predicateMap { |
| 681 | + p.cache.Range(func(k, v any) bool { |
| 682 | + ok = false |
| 683 | + return true |
| 684 | + }) |
| 685 | + } |
| 686 | + if !ok { |
| 687 | + t.Fatal("Failed to get cache cleaned") |
| 688 | + } |
| 689 | + |
| 690 | + }) |
| 691 | +} |
| 692 | + |
622 | 693 | func BenchmarkJWTPayloadAnyKV(b *testing.B) { |
623 | 694 | sp := NewJWTPayloadAnyKV() |
624 | 695 | p, err := sp.Create([]interface{}{"https://identity.zalando.com/managed-id", "foo", "iss", "https://identity.zalando.com"}) |
|
0 commit comments