forked from oras-project/oras-credentials-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnative_store_test.go
385 lines (366 loc) · 12.1 KB
/
native_store_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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package credentials
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"strings"
"testing"
"github.com/oras-project/oras-credentials-go/trace"
"oras.land/oras-go/v2/registry/remote/auth"
)
const (
basicAuthHost = "localhost:2333"
bearerAuthHost = "localhost:666"
exeErrorHost = "localhost:500/exeError"
jsonErrorHost = "localhost:500/jsonError"
noCredentialsHost = "localhost:404"
traceHost = "localhost:808"
testUsername = "test_username"
testPassword = "test_password"
testRefreshToken = "test_token"
)
var (
errCommandExited = fmt.Errorf("exited with error")
errExecute = fmt.Errorf("Execute failed")
errCredentialsNotFound = fmt.Errorf(errCredentialsNotFoundMessage)
)
// testExecuter implements the Executer interface for testing purpose.
// It simulates interactions between the docker client and a remote
// credentials helper.
type testExecuter struct{}
// Execute mocks the behavior of a credential helper binary. It returns responses
// and errors based on the input.
func (e *testExecuter) Execute(ctx context.Context, input io.Reader, action string) ([]byte, error) {
in, err := io.ReadAll(input)
if err != nil {
return nil, err
}
inS := string(in)
switch action {
case "get":
switch inS {
case basicAuthHost:
return []byte(`{"Username": "test_username", "Secret": "test_password"}`), nil
case bearerAuthHost:
return []byte(`{"Username": "<token>", "Secret": "test_token"}`), nil
case exeErrorHost:
return []byte("Execute failed"), errExecute
case jsonErrorHost:
return []byte("json.Unmarshal failed"), nil
case noCredentialsHost:
return []byte("credentials not found"), errCredentialsNotFound
case traceHost:
traceHook := trace.ContextExecutableTrace(ctx)
if traceHook != nil {
if traceHook.ExecuteStart != nil {
traceHook.ExecuteStart("testExecuter", "get")
}
if traceHook.ExecuteDone != nil {
traceHook.ExecuteDone("testExecuter", "get", nil)
}
}
return []byte(`{"Username": "test_username", "Secret": "test_password"}`), nil
default:
return []byte("program failed"), errCommandExited
}
case "store":
var c dockerCredentials
err := json.NewDecoder(strings.NewReader(inS)).Decode(&c)
if err != nil {
return []byte("program failed"), errCommandExited
}
switch c.ServerURL {
case basicAuthHost, bearerAuthHost, exeErrorHost:
return nil, nil
case traceHost:
traceHook := trace.ContextExecutableTrace(ctx)
if traceHook != nil {
if traceHook.ExecuteStart != nil {
traceHook.ExecuteStart("testExecuter", "store")
}
if traceHook.ExecuteDone != nil {
traceHook.ExecuteDone("testExecuter", "store", nil)
}
}
return nil, nil
default:
return []byte("program failed"), errCommandExited
}
case "erase":
switch inS {
case basicAuthHost, bearerAuthHost:
return nil, nil
case traceHost:
traceHook := trace.ContextExecutableTrace(ctx)
if traceHook != nil {
if traceHook.ExecuteStart != nil {
traceHook.ExecuteStart("testExecuter", "erase")
}
if traceHook.ExecuteDone != nil {
traceHook.ExecuteDone("testExecuter", "erase", nil)
}
}
return nil, nil
default:
return []byte("program failed"), errCommandExited
}
}
return []byte(fmt.Sprintf("unknown argument %q with %q", action, inS)), errCommandExited
}
func TestNativeStore_interface(t *testing.T) {
var ns interface{} = &nativeStore{}
if _, ok := ns.(Store); !ok {
t.Error("&NativeStore{} does not conform Store")
}
}
func TestNativeStore_basicAuth(t *testing.T) {
ns := &nativeStore{
&testExecuter{},
}
// Put
err := ns.Put(context.Background(), basicAuthHost, auth.Credential{Username: testUsername, Password: testPassword})
if err != nil {
t.Fatalf("basic auth test ns.Put fails: %v", err)
}
// Get
cred, err := ns.Get(context.Background(), basicAuthHost)
if err != nil {
t.Fatalf("basic auth test ns.Get fails: %v", err)
}
if cred.Username != testUsername {
t.Fatal("incorrect username")
}
if cred.Password != testPassword {
t.Fatal("incorrect password")
}
// Delete
err = ns.Delete(context.Background(), basicAuthHost)
if err != nil {
t.Fatalf("basic auth test ns.Delete fails: %v", err)
}
}
func TestNativeStore_refreshToken(t *testing.T) {
ns := &nativeStore{
&testExecuter{},
}
// Put
err := ns.Put(context.Background(), bearerAuthHost, auth.Credential{RefreshToken: testRefreshToken})
if err != nil {
t.Fatalf("refresh token test ns.Put fails: %v", err)
}
// Get
cred, err := ns.Get(context.Background(), bearerAuthHost)
if err != nil {
t.Fatalf("refresh token test ns.Get fails: %v", err)
}
if cred.Username != "" {
t.Fatalf("expect username to be empty, got %s", cred.Username)
}
if cred.RefreshToken != testRefreshToken {
t.Fatal("incorrect refresh token")
}
// Delete
err = ns.Delete(context.Background(), basicAuthHost)
if err != nil {
t.Fatalf("refresh token test ns.Delete fails: %v", err)
}
}
func TestNativeStore_errorHandling(t *testing.T) {
ns := &nativeStore{
&testExecuter{},
}
// Get Error: Execute error
_, err := ns.Get(context.Background(), exeErrorHost)
if err != errExecute {
t.Fatalf("got error: %v, should get exeErr", err)
}
// Get Error: json.Unmarshal
_, err = ns.Get(context.Background(), jsonErrorHost)
if err == nil {
t.Fatalf("should get error from json.Unmarshal")
}
// Get: Should not return error when credentials are not found
_, err = ns.Get(context.Background(), noCredentialsHost)
if err != nil {
t.Fatalf("should not get error when no credentials are found")
}
}
func TestNewDefaultNativeStore(t *testing.T) {
defaultHelper := getDefaultHelperSuffix()
wantOK := (defaultHelper != "")
if _, ok := NewDefaultNativeStore(); ok != wantOK {
t.Errorf("NewDefaultNativeStore() = %v, want %v", ok, wantOK)
}
}
func TestNativeStore_trace(t *testing.T) {
ns := &nativeStore{
&testExecuter{},
}
// create trace hooks that write to buffer
buffer := bytes.Buffer{}
traceHook := &trace.ExecutableTrace{
ExecuteStart: func(executableName string, action string) {
buffer.WriteString(fmt.Sprintf("test trace, start the execution of executable %s with action %s ", executableName, action))
},
ExecuteDone: func(executableName string, action string, err error) {
buffer.WriteString(fmt.Sprintf("test trace, completed the execution of executable %s with action %s and got err %v", executableName, action, err))
},
}
ctx := trace.WithExecutableTrace(context.Background(), traceHook)
// Test ns.Put trace
err := ns.Put(ctx, traceHost, auth.Credential{Username: testUsername, Password: testPassword})
if err != nil {
t.Fatalf("trace test ns.Put fails: %v", err)
}
bufferContent := buffer.String()
if bufferContent != "test trace, start the execution of executable testExecuter with action store test trace, completed the execution of executable testExecuter with action store and got err <nil>" {
t.Fatalf("incorrect buffer content: %s", bufferContent)
}
buffer.Reset()
// Test ns.Get trace
_, err = ns.Get(ctx, traceHost)
if err != nil {
t.Fatalf("trace test ns.Get fails: %v", err)
}
bufferContent = buffer.String()
if bufferContent != "test trace, start the execution of executable testExecuter with action get test trace, completed the execution of executable testExecuter with action get and got err <nil>" {
t.Fatalf("incorrect buffer content: %s", bufferContent)
}
buffer.Reset()
// Test ns.Delete trace
err = ns.Delete(ctx, traceHost)
if err != nil {
t.Fatalf("trace test ns.Delete fails: %v", err)
}
bufferContent = buffer.String()
if bufferContent != "test trace, start the execution of executable testExecuter with action erase test trace, completed the execution of executable testExecuter with action erase and got err <nil>" {
t.Fatalf("incorrect buffer content: %s", bufferContent)
}
}
// This test ensures that a nil trace will not cause an error.
func TestNativeStore_noTrace(t *testing.T) {
ns := &nativeStore{
&testExecuter{},
}
// Put
err := ns.Put(context.Background(), traceHost, auth.Credential{Username: testUsername, Password: testPassword})
if err != nil {
t.Fatalf("basic auth test ns.Put fails: %v", err)
}
// Get
cred, err := ns.Get(context.Background(), traceHost)
if err != nil {
t.Fatalf("basic auth test ns.Get fails: %v", err)
}
if cred.Username != testUsername {
t.Fatal("incorrect username")
}
if cred.Password != testPassword {
t.Fatal("incorrect password")
}
// Delete
err = ns.Delete(context.Background(), traceHost)
if err != nil {
t.Fatalf("basic auth test ns.Delete fails: %v", err)
}
}
// This test ensures that an empty trace will not cause an error.
func TestNativeStore_emptyTrace(t *testing.T) {
ns := &nativeStore{
&testExecuter{},
}
traceHook := &trace.ExecutableTrace{}
ctx := trace.WithExecutableTrace(context.Background(), traceHook)
// Put
err := ns.Put(ctx, traceHost, auth.Credential{Username: testUsername, Password: testPassword})
if err != nil {
t.Fatalf("basic auth test ns.Put fails: %v", err)
}
// Get
cred, err := ns.Get(ctx, traceHost)
if err != nil {
t.Fatalf("basic auth test ns.Get fails: %v", err)
}
if cred.Username != testUsername {
t.Fatal("incorrect username")
}
if cred.Password != testPassword {
t.Fatal("incorrect password")
}
// Delete
err = ns.Delete(ctx, traceHost)
if err != nil {
t.Fatalf("basic auth test ns.Delete fails: %v", err)
}
}
func TestNativeStore_multipleTrace(t *testing.T) {
ns := &nativeStore{
&testExecuter{},
}
// create trace hooks that write to buffer
buffer := bytes.Buffer{}
trace1 := &trace.ExecutableTrace{
ExecuteStart: func(executableName string, action string) {
buffer.WriteString(fmt.Sprintf("trace 1 start %s, %s ", executableName, action))
},
ExecuteDone: func(executableName string, action string, err error) {
buffer.WriteString(fmt.Sprintf("trace 1 done %s, %s, %v ", executableName, action, err))
},
}
ctx := context.Background()
ctx = trace.WithExecutableTrace(ctx, trace1)
trace2 := &trace.ExecutableTrace{
ExecuteStart: func(executableName string, action string) {
buffer.WriteString(fmt.Sprintf("trace 2 start %s, %s ", executableName, action))
},
ExecuteDone: func(executableName string, action string, err error) {
buffer.WriteString(fmt.Sprintf("trace 2 done %s, %s, %v ", executableName, action, err))
},
}
ctx = trace.WithExecutableTrace(ctx, trace2)
trace3 := &trace.ExecutableTrace{}
ctx = trace.WithExecutableTrace(ctx, trace3)
// Test ns.Put trace
err := ns.Put(ctx, traceHost, auth.Credential{Username: testUsername, Password: testPassword})
if err != nil {
t.Fatalf("trace test ns.Put fails: %v", err)
}
bufferContent := buffer.String()
if bufferContent != "trace 2 start testExecuter, store trace 1 start testExecuter, store trace 2 done testExecuter, store, <nil> trace 1 done testExecuter, store, <nil> " {
t.Fatalf("incorrect buffer content: %s", bufferContent)
}
buffer.Reset()
// Test ns.Get trace
_, err = ns.Get(ctx, traceHost)
if err != nil {
t.Fatalf("trace test ns.Get fails: %v", err)
}
bufferContent = buffer.String()
if bufferContent != "trace 2 start testExecuter, get trace 1 start testExecuter, get trace 2 done testExecuter, get, <nil> trace 1 done testExecuter, get, <nil> " {
t.Fatalf("incorrect buffer content: %s", bufferContent)
}
buffer.Reset()
// Test ns.Delete trace
err = ns.Delete(ctx, traceHost)
if err != nil {
t.Fatalf("trace test ns.Delete fails: %v", err)
}
bufferContent = buffer.String()
if bufferContent != "trace 2 start testExecuter, erase trace 1 start testExecuter, erase trace 2 done testExecuter, erase, <nil> trace 1 done testExecuter, erase, <nil> " {
t.Fatalf("incorrect buffer content: %s", bufferContent)
}
}