Skip to content

Commit 1e18589

Browse files
authored
test: missing predicate tests (#3983)
1 parent 6fc1ec4 commit 1e18589

6 files changed

Lines changed: 232 additions & 1 deletion

File tree

predicates/auth/headersha256_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ func TestHeaderSHA256Args(t *testing.T) {
1919
{
2020
args: []interface{}{"X-Secret", "00112233"},
2121
},
22+
{
23+
args: []interface{}{5, "00112233"},
24+
},
25+
{
26+
args: []interface{}{"X-Secret", 5},
27+
},
2228
{
2329
args: []interface{}{"X-Secret", "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff", "AA"},
2430
},
@@ -31,6 +37,9 @@ func TestHeaderSHA256Args(t *testing.T) {
3137

3238
func TestHeaderSHA256Match(t *testing.T) {
3339
s := NewHeaderSHA256()
40+
if s.Name() == "" {
41+
t.Fatal("predicate should have a name")
42+
}
3443
p, err := s.Create([]interface{}{
3544
"X-Secret",
3645
"2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b", // "secret"

predicates/auth/jwt_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"reflect"
77
"regexp"
88
"testing"
9+
"testing/synctest"
10+
"time"
911

1012
"github.com/stretchr/testify/require"
1113
"github.com/zalando/skipper/eskip"
@@ -157,6 +159,12 @@ func Test_spec_Create(t *testing.T) {
157159
matchBehavior: matchBehaviorAny,
158160
},
159161
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,
160168
}, {
161169
name: "many kv pair of args, one missing",
162170
spec: NewJWTPayloadAllKV(),
@@ -619,6 +627,69 @@ s: JWTPayloadAnyKVRegexp("https://identity.zalando.com/managed-id", "^ssz") -> s
619627

620628
}
621629

630+
func TestPredicateCacheClean(t *testing.T) {
631+
synctest.Test(t, func(t *testing.T) {
632+
reg := &registry{
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+
622693
func BenchmarkJWTPayloadAnyKV(b *testing.B) {
623694
sp := NewJWTPayloadAnyKV()
624695
p, err := sp.Create([]interface{}{"https://identity.zalando.com/managed-id", "foo", "iss", "https://identity.zalando.com"})

predicates/content/content_length_between_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"net/http"
66
"testing"
7+
8+
"github.com/zalando/skipper/predicates"
79
)
810

911
func TestContentLengthCreate(t *testing.T) {
@@ -15,6 +17,32 @@ func TestContentLengthCreate(t *testing.T) {
1517
t.Fatal("expected error here, lower bound equals upper bound")
1618
}
1719
}
20+
func TestContentLengthCreateError(t *testing.T) {
21+
spec := NewContentLengthBetween()
22+
if spec.Name() == "" {
23+
t.Fatal("predicate spec should have a name")
24+
}
25+
26+
_, err := spec.Create([]any{})
27+
if err != predicates.ErrInvalidPredicateParameters {
28+
t.Fatalf("Failed to get expected error, got: %v", err)
29+
}
30+
31+
_, err = spec.Create([]any{"5.0", "8.2"})
32+
if err != predicates.ErrInvalidPredicateParameters {
33+
t.Fatalf("Failed to get expected error, got: %v", err)
34+
}
35+
36+
_, err = spec.Create([]any{5.0, "8.2"})
37+
if err != predicates.ErrInvalidPredicateParameters {
38+
t.Fatalf("Failed to get expected error, got: %v", err)
39+
}
40+
41+
_, err = spec.Create([]any{5, 1})
42+
if err != predicates.ErrInvalidPredicateParameters {
43+
t.Fatalf("Failed to get expected error, got: %v", err)
44+
}
45+
}
1846

1947
func TestContentLengthMatch(t *testing.T) {
2048
s := NewContentLengthBetween()

predicates/forwarded/forwarded_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,54 @@ import (
44
"net/http"
55
"net/url"
66
"testing"
7+
8+
"github.com/zalando/skipper/predicates"
79
)
810

11+
func TestForwardedHostCreateError(t *testing.T) {
12+
spec := NewForwardedHost()
13+
if spec.Name() == "" {
14+
t.Fatal("predicate spec should have a name")
15+
}
16+
17+
_, err := spec.Create([]any{})
18+
if err != predicates.ErrInvalidPredicateParameters {
19+
t.Fatalf("Failed to get expected error, got: %v", err)
20+
}
21+
22+
_, err = spec.Create([]any{5})
23+
if err != predicates.ErrInvalidPredicateParameters {
24+
t.Fatalf("Failed to get expected error, got: %v", err)
25+
}
26+
27+
_, err = spec.Create([]any{"^(?!4)"})
28+
if err == nil || err == predicates.ErrInvalidPredicateParameters {
29+
t.Fatalf("regexp is not valid RE2")
30+
}
31+
}
32+
33+
func TestForwardedProtoCreateError(t *testing.T) {
34+
spec := NewForwardedProto()
35+
if spec.Name() == "" {
36+
t.Fatal("predicate spec should have a name")
37+
}
38+
39+
_, err := spec.Create([]any{})
40+
if err != predicates.ErrInvalidPredicateParameters {
41+
t.Fatalf("Failed to get expected error, got: %v", err)
42+
}
43+
44+
_, err = spec.Create([]any{5})
45+
if err != predicates.ErrInvalidPredicateParameters {
46+
t.Fatalf("Failed to get expected error, got: %v", err)
47+
}
48+
49+
_, err = spec.Create([]any{"foo"})
50+
if err != predicates.ErrInvalidPredicateParameters {
51+
t.Fatalf("Failed to get expected error, got: %v", err)
52+
}
53+
}
54+
955
type request struct {
1056
url string
1157
headers http.Header

predicates/primitive/shutdown_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
func TestShutdown(t *testing.T) {
1111
s, sigs := newShutdown()
12-
p, err := s.Create([]interface{}{})
12+
p, err := s.Create([]any{})
1313
if err != nil {
1414
t.Fatal(err)
1515
}
@@ -25,4 +25,16 @@ func TestShutdown(t *testing.T) {
2525
if !p.Match(req) {
2626
t.Error("expected shutdown")
2727
}
28+
29+
if s.Name() == "" {
30+
t.Fatal("predicate should have a name")
31+
}
32+
}
33+
34+
func TestShutdownErrors(t *testing.T) {
35+
s := NewShutdown()
36+
_, err := s.Create([]any{"wrong args"})
37+
if err == nil {
38+
t.Fatal("Failed to get err")
39+
}
2840
}

predicates/tee/tee_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package tee
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/zalando/skipper/predicates"
8+
)
9+
10+
func TestTee(t *testing.T) {
11+
p := New()
12+
13+
for _, tc := range []struct {
14+
name string
15+
args []any
16+
match bool
17+
err error
18+
}{
19+
{
20+
name: "no args",
21+
args: []any{},
22+
err: predicates.ErrInvalidPredicateParameters,
23+
},
24+
{
25+
name: "wrong args",
26+
args: []any{1.2},
27+
err: predicates.ErrInvalidPredicateParameters,
28+
},
29+
{
30+
name: "match",
31+
args: []any{"goodmatch"},
32+
match: true,
33+
},
34+
{
35+
name: "no match",
36+
args: []any{"nomatch"},
37+
match: false,
38+
},
39+
} {
40+
t.Run(tc.name, func(t *testing.T) {
41+
pred, err := p.Create(tc.args)
42+
if err != nil {
43+
if err == tc.err {
44+
return
45+
}
46+
t.Fatalf("Failed with not expected err want: %v, got: %v", tc.err, err)
47+
} else {
48+
if tc.err != nil {
49+
t.Fatalf("Failed to get expected error: %v", tc.err)
50+
}
51+
}
52+
req, _ := http.NewRequest("GET", "http://my-app.test", nil)
53+
req.Header.Add(HeaderKey, "goodmatch")
54+
res := pred.Match(req)
55+
if res != tc.match {
56+
t.Fatalf("Failed to get expected result, want %v, got %v", tc.match, res)
57+
}
58+
59+
if p.Name() == "" {
60+
t.Fatal("predicate name is empty")
61+
}
62+
})
63+
}
64+
65+
}

0 commit comments

Comments
 (0)