-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
auth_test.go
344 lines (319 loc) · 10.1 KB
/
auth_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package cos
import (
"context"
"fmt"
"net/http"
"net/url"
"reflect"
"testing"
"time"
)
func TestNewAuthorization(t *testing.T) {
expectAuthorization := `q-sign-algorithm=sha1&q-ak=QmFzZTY0IGlzIGEgZ2VuZXJp&q-sign-time=1480932292;1481012292&q-key-time=1480932292;1481012292&q-header-list=host;x-cos-content-sha1;x-cos-stroage-class&q-url-param-list=&q-signature=91f7814df035319aa08d47e5a7a66ea989d57301`
secretID := "QmFzZTY0IGlzIGEgZ2VuZXJp"
secretKey := "AKIDZfbOA78asKUYBcXFrJD0a1ICvR98JM"
host := "testbucket-125000000.cos.ap-beijing-1.myqcloud.com"
uri := "https://testbucket-125000000.cos.ap-beijing-1.myqcloud.com/testfile2"
startTime := time.Unix(int64(1480932292), 0)
endTime := time.Unix(int64(1481012292), 0)
req, _ := http.NewRequest("PUT", uri, nil)
req.Header.Add("Host", host)
req.Header.Add("x-cos-content-sha1", "db8ac1c259eb89d4a131b253bacfca5f319d54f2")
req.Header.Add("x-cos-stroage-class", "nearline")
authTime := &AuthTime{
SignStartTime: startTime,
SignEndTime: endTime,
KeyStartTime: startTime,
KeyEndTime: endTime,
}
auth := newAuthorization(Auth{
SecretID: secretID,
SecretKey: secretKey,
}, req, *authTime)
if auth != expectAuthorization {
t.Errorf("NewAuthorization returned \n%#v, want \n%#v", auth, expectAuthorization)
}
}
func TestAuthorizationTransport(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
if auth == "" {
t.Error("AuthorizationTransport didn't add Authorization header")
}
})
(client.Sender).(*DefaultSender).Transport = &AuthorizationTransport{}
req, _ := http.NewRequest("GET", client.BaseURL.BucketURL.String(), nil)
req.Header.Set("X-Testing", "0")
client.doAPI(context.Background(), Caller{}, req, nil, true)
}
func TestAuthorizationTransportWithSessionToken(t *testing.T) {
setup()
defer teardown()
sessionToken := "CxQQbwSzzX5obZm23yEcyQtpROuDB0Q60d322a47737c8241991d12dc4b8387c7J6NL50eH1BYN6VnFYB_Ml6oPZzUxz5wxDGVvvgxZXr1m-4HvmkvmMH4YB02XdVPapKp7oGnrMous2jsSTALo4iU2fuRclbVw-czYwggSxuNxXAwmqcT1HpD3h3zc3e24sryIhJKqzSOczQZjtGrxSSQ4K23o9Mx8VHgrosliU0aIiI2KFhxJhij03SzDDOQcBAwpFZyM0NvpOdN6b14yJbrt9bAzYGNjX-PeU3MXfi0"
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
if auth == "" {
t.Error("AuthorizationTransport didn't add Authorization header")
}
token := r.Header.Get("x-cos-security-token")
if token == "" {
t.Error("AuthorizationTransport didn't add x-cos-security-token header")
}
if token != sessionToken {
t.Errorf("AuthorizationTransport didn't add expected x-cos-security-token header, expected: %s, got: %s", sessionToken, token)
}
})
(client.Sender).(*DefaultSender).Transport = &AuthorizationTransport{
SecretID: "233",
SecretKey: "666",
SessionToken: sessionToken,
}
req, _ := http.NewRequest("GET", client.BaseURL.BucketURL.String(), nil)
req.Header.Set("X-Testing", "0")
client.doAPI(context.Background(), Caller{}, req, nil, true)
}
func TestAuthorizationTransport_skip_PresignedURL(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, exist := r.Header["Authorization"]
if exist {
t.Error("AuthorizationTransport add Authorization header when use PresignedURL")
}
})
(client.Sender).(*DefaultSender).Transport = &AuthorizationTransport{}
sign := "q-sign-algorithm=sha1&q-ak=QmFzZTY0IGlzIGEgZ2VuZXJp&q-sign-time=1480932292;1481012292&q-key-time=1480932292;1481012292&q-header-list=&q-url-param-list=&q-signature=a5de76b0734f084a7ea24413f7168b4bdbe5676c"
u := fmt.Sprintf("%s?sign=%s", client.BaseURL.BucketURL.String(), sign)
req, _ := http.NewRequest("GET", u, nil)
client.doAPI(context.Background(), Caller{}, req, nil, true)
}
func TestAuthorizationTransport_with_another_transport(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
if auth == "" {
t.Error("AuthorizationTransport didn't add Authorization header")
}
})
tr := &testingTransport{}
(client.Sender).(*DefaultSender).Transport = &AuthorizationTransport{
Transport: tr,
}
req, _ := http.NewRequest("GET", client.BaseURL.BucketURL.String(), nil)
req.Header.Set("X-Testing", "0")
client.doAPI(context.Background(), Caller{}, req, nil, true)
if tr.called != 1 {
t.Error("AuthorizationTransport not call another Transport")
}
}
type testingTransport struct {
called int
}
func (t *testingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
t.called++
return http.DefaultTransport.RoundTrip(req)
}
func Test_camSafeURLEncode(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want string
}{
{
name: "no replace",
args: args{"1234 +abc0AB#@"},
want: "1234%20%2Babc0AB%23%40",
},
{
name: "replace",
args: args{"1234 +abc0AB#@,!'()*"},
want: "1234%20%2Babc0AB%23%40%2C%21%27%28%29%2A",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := camSafeURLEncode(tt.args.s); got != tt.want {
t.Errorf("camSafeURLEncode() = %v, want %v", got, tt.want)
}
})
}
}
func Test_valuesForSign_Encode(t *testing.T) {
tests := []struct {
name string
vs valuesForSign
want string
}{
{
name: "test escape",
vs: valuesForSign{
"test+233": {"value 666"},
"test+234": {"value 667"},
},
want: "test%2B233=value%20666&test%2B234=value%20667",
},
{
name: "test order",
vs: valuesForSign{
"test_233": {"value_666"},
"233": {"value_2"},
"test_666": {"value_123"},
},
want: "233=value_2&test_233=value_666&test_666=value_123",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.vs.Encode(); got != tt.want {
t.Errorf("valuesForSign.Encode() = %v, want %v", got, tt.want)
}
})
}
}
func Test_valuesForSign_Add(t *testing.T) {
type args struct {
key string
value string
}
tests := []struct {
name string
vs valuesForSign
args args
want valuesForSign
}{
{
name: "add new key",
vs: valuesForSign{},
args: args{"test_key", "value_233"},
want: valuesForSign{"test_key": {"value_233"}},
},
{
name: "extend key",
vs: valuesForSign{"test_key": {"value_233"}},
args: args{"test_key", "value_666"},
want: valuesForSign{"test_key": {"value_233", "value_666"}},
},
{
name: "key to lower(add)",
vs: valuesForSign{},
args: args{"TEST_KEY", "value_233"},
want: valuesForSign{"test_key": {"value_233"}},
},
{
name: "key to lower(extend)",
vs: valuesForSign{"test_key": {"value_233"}},
args: args{"TEST_KEY", "value_666"},
want: valuesForSign{"test_key": {"value_233", "value_666"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.vs.Add(tt.args.key, tt.args.value)
if !reflect.DeepEqual(tt.vs, tt.want) {
t.Errorf("%v, want %v", tt.vs, tt.want)
}
})
}
}
func Test_genFormatParameters(t *testing.T) {
type args struct {
parameters url.Values
}
tests := []struct {
name string
args args
wantFormatParameters string
wantSignedParameterList []string
}{
{
name: "test order",
args: args{url.Values{
"test_key_233": {"666"},
"233": {"222"},
"test_key_2": {"value"},
}},
wantFormatParameters: "233=222&test_key_2=value&test_key_233=666",
wantSignedParameterList: []string{"233", "test_key_2", "test_key_233"},
},
{
name: "test escape",
args: args{url.Values{
"Test+key": {"666 value"},
"233 666": {"22+2"},
}},
wantFormatParameters: "233%20666=22%2B2&test%2Bkey=666%20value",
wantSignedParameterList: []string{"233 666", "test+key"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotFormatParameters, gotSignedParameterList := genFormatParameters(tt.args.parameters)
if gotFormatParameters != tt.wantFormatParameters {
t.Errorf("genFormatParameters() gotFormatParameters = %v, want %v", gotFormatParameters, tt.wantFormatParameters)
}
if !reflect.DeepEqual(gotSignedParameterList, tt.wantSignedParameterList) {
t.Errorf("genFormatParameters() gotSignedParameterList = %v, want %v", gotSignedParameterList, tt.wantSignedParameterList)
}
})
}
}
func Test_genFormatHeaders(t *testing.T) {
type args struct {
headers http.Header
}
tests := []struct {
name string
args args
wantFormatHeaders string
wantSignedHeaderList []string
}{
{
name: "test order",
args: args{http.Header{
"host": {"example.com"},
"content-length": {"22"},
"content-md5": {"xxx222"},
}},
wantFormatHeaders: "content-length=22&content-md5=xxx222&host=example.com",
wantSignedHeaderList: []string{"content-length", "content-md5", "host"},
},
{
name: "test escape",
args: args{http.Header{
"host": {"example.com"},
"content-length": {"22"},
"Content-Disposition": {"attachment; filename=hello - world!(+).go"},
}},
wantFormatHeaders: "content-disposition=attachment%3B%20filename%3Dhello%20-%20world%21%28%2B%29.go&content-length=22&host=example.com",
wantSignedHeaderList: []string{"content-disposition", "content-length", "host"},
},
{
name: "test skip key",
args: args{http.Header{
"Host": {"example.com"},
"content-length": {"22"},
"x-cos-xyz": {"lala"},
"Content-Type": {"text/html"},
}},
wantFormatHeaders: "content-length=22&host=example.com&x-cos-xyz=lala",
wantSignedHeaderList: []string{"content-length", "host", "x-cos-xyz"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotFormatHeaders, gotSignedHeaderList := genFormatHeaders(tt.args.headers)
if gotFormatHeaders != tt.wantFormatHeaders {
t.Errorf("genFormatHeaders() gotFormatHeaders = %v, want %v", gotFormatHeaders, tt.wantFormatHeaders)
}
if !reflect.DeepEqual(gotSignedHeaderList, tt.wantSignedHeaderList) {
t.Errorf("genFormatHeaders() gotSignedHeaderList = %v, want %v", gotSignedHeaderList, tt.wantSignedHeaderList)
}
})
}
}