-
Notifications
You must be signed in to change notification settings - Fork 1
/
key_provider_test.go
96 lines (90 loc) · 1.98 KB
/
key_provider_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package asn
import (
"bytes"
"net/http"
"os"
"testing"
"github.com/lestrrat-go/jwx/v2/jws"
)
const (
requestPath = "testdata/request.txt"
invalidRequestPath = "testdata/invalid_request.txt"
notFoundCerPath = "testdata/notFound.cer"
)
func TestVerify(t *testing.T) {
type args struct {
fetchers []RootCAFetcher
opts []jws.VerifyOption
}
tests := []struct {
name string
args args
req string
res string
wantErr bool
}{
{
name: "is not from Apple",
args: args{
fetchers: []RootCAFetcher{
NewFileRootCAFetcher(path(emptyCerPath)),
NewHTTPRootCAFetcher(&http.Client{Transport: &fakeAppleServer{}}, "http://localhost:8080/isAppleFalse"),
NewRawRootCAFetcher([]byte(``)),
},
},
wantErr: true,
},
{
name: "not found",
args: args{
fetchers: []RootCAFetcher{
NewFileRootCAFetcher(path(notFoundCerPath)),
NewHTTPRootCAFetcher(&http.Client{Transport: &fakeAppleServer{}}, "http://localhost:8080/notfound"),
},
},
wantErr: true,
},
{
name: "invalid signature",
args: args{
fetchers: []RootCAFetcher{
NewFileRootCAFetcher(path(cerPath)),
},
},
req: invalidRequestPath,
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
for _, fetcher := range tt.args.fetchers {
kp := NewKeyProvider(fetcher)
opts := append([]jws.VerifyOption{jws.WithKeyProvider(kp)}, tt.args.opts...)
if tt.req == "" {
tt.req = requestPath
}
actual, err := jws.Verify(read(t, path(tt.req)), opts...)
t.Log(err)
if tt.wantErr != (err != nil) {
t.Error(err)
}
if expected := read(t, tt.res); !tt.wantErr && !bytes.Equal(expected, actual) {
t.Errorf("expected: %s, but actual: %s\n", expected, actual)
}
}
})
}
}
func read(t *testing.T, path string) []byte {
t.Helper()
if path == "" {
return nil
}
b, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
return b
}