1
15 package auth
16
17 import (
18 "reflect"
19 "testing"
20 )
21
22 func Test_parseChallenge(t *testing.T) {
23 tests := []struct {
24 name string
25 header string
26 wantScheme Scheme
27 wantParams map[string]string
28 }{
29 {
30 name: "empty header",
31 },
32 {
33 name: "unknown scheme",
34 header: "foo bar",
35 wantScheme: SchemeUnknown,
36 },
37 {
38 name: "basic challenge",
39 header: `Basic realm="Test Registry"`,
40 wantScheme: SchemeBasic,
41 },
42 {
43 name: "basic challenge with no parameters",
44 header: "Basic",
45 wantScheme: SchemeBasic,
46 },
47 {
48 name: "basic challenge with no parameters but spaces",
49 header: "Basic ",
50 wantScheme: SchemeBasic,
51 },
52 {
53 name: "bearer challenge",
54 header: `Bearer realm="https://auth.example.io/token",service="registry.example.io",scope="repository:library/hello-world:pull,push"`,
55 wantScheme: SchemeBearer,
56 wantParams: map[string]string{
57 "realm": "https://auth.example.io/token",
58 "service": "registry.example.io",
59 "scope": "repository:library/hello-world:pull,push",
60 },
61 },
62 {
63 name: "bearer challenge with multiple scopes",
64 header: `Bearer realm="https://auth.example.io/token",service="registry.example.io",scope="repository:library/alpine:pull,push repository:ubuntu:pull"`,
65 wantScheme: SchemeBearer,
66 wantParams: map[string]string{
67 "realm": "https://auth.example.io/token",
68 "service": "registry.example.io",
69 "scope": "repository:library/alpine:pull,push repository:ubuntu:pull",
70 },
71 },
72 {
73 name: "bearer challenge with no parameters",
74 header: "Bearer",
75 wantScheme: SchemeBearer,
76 },
77 {
78 name: "bearer challenge with no parameters but spaces",
79 header: "Bearer ",
80 wantScheme: SchemeBearer,
81 },
82 {
83 name: "bearer challenge with white spaces",
84 header: `Bearer realm = "https://auth.example.io/token" ,service=registry.example.io, scope ="repository:library/hello-world:pull,push" `,
85 wantScheme: SchemeBearer,
86 wantParams: map[string]string{
87 "realm": "https://auth.example.io/token",
88 "service": "registry.example.io",
89 "scope": "repository:library/hello-world:pull,push",
90 },
91 },
92 {
93 name: "bad bearer challenge (incomplete parameter with spaces)",
94 header: `Bearer realm="https://auth.example.io/token",service`,
95 wantScheme: SchemeBearer,
96 wantParams: map[string]string{
97 "realm": "https://auth.example.io/token",
98 },
99 },
100 {
101 name: "bad bearer challenge (incomplete parameter with no value)",
102 header: `Bearer realm="https://auth.example.io/token",service=`,
103 wantScheme: SchemeBearer,
104 wantParams: map[string]string{
105 "realm": "https://auth.example.io/token",
106 },
107 },
108 {
109 name: "bad bearer challenge (incomplete parameter with spaces)",
110 header: `Bearer realm="https://auth.example.io/token",service= `,
111 wantScheme: SchemeBearer,
112 wantParams: map[string]string{
113 "realm": "https://auth.example.io/token",
114 },
115 },
116 {
117 name: "bad bearer challenge (incomplete quote)",
118 header: `Bearer realm="https://auth.example.io/token",service="registry`,
119 wantScheme: SchemeBearer,
120 wantParams: map[string]string{
121 "realm": "https://auth.example.io/token",
122 },
123 },
124 {
125 name: "bearer challenge with empty parameter value",
126 header: `Bearer realm="https://auth.example.io/token",empty="",service="registry.example.io",scope="repository:library/hello-world:pull,push"`,
127 wantScheme: SchemeBearer,
128 wantParams: map[string]string{
129 "realm": "https://auth.example.io/token",
130 "empty": "",
131 "service": "registry.example.io",
132 "scope": "repository:library/hello-world:pull,push",
133 },
134 },
135 {
136 name: "bearer challenge with escaping parameter value",
137 header: `Bearer foo="foo\"bar",hello="\"hello world\""`,
138 wantScheme: SchemeBearer,
139 wantParams: map[string]string{
140 "foo": `foo"bar`,
141 "hello": `"hello world"`,
142 },
143 },
144 }
145 for _, tt := range tests {
146 t.Run(tt.name, func(t *testing.T) {
147 gotScheme, gotParams := parseChallenge(tt.header)
148 if gotScheme != tt.wantScheme {
149 t.Errorf("parseChallenge() gotScheme = %v, want %v", gotScheme, tt.wantScheme)
150 }
151 if !reflect.DeepEqual(gotParams, tt.wantParams) {
152 t.Errorf("parseChallenge() gotParams = %v, want %v", gotParams, tt.wantParams)
153 }
154 })
155 }
156 }
157
View as plain text