| 1 |
/* |
| 2 |
Copyright The ORAS Authors. |
| 3 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
you may not use this file except in compliance with the License. |
| 5 |
You may obtain a copy of the License at |
| 6 |
|
| 7 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
|
| 9 |
Unless required by applicable law or agreed to in writing, software |
| 10 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
See the License for the specific language governing permissions and |
| 13 |
limitations under the License. |
| 14 |
*/ |
| 15 |
|
| 16 |
package credentials |
| 17 |
|
| 18 |
import ( |
| 19 |
"context" |
| 20 |
"errors" |
| 21 |
"os" |
| 22 |
"reflect" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"oras.land/oras-go/v2/registry/remote/auth" |
| 26 |
"oras.land/oras-go/v2/registry/remote/credentials/internal/config" |
| 27 |
) |
| 28 |
|
| 29 |
func TestMemoryStore_Create_fromInvalidConfig(t *testing.T) { |
| 30 |
f, err := os.ReadFile("testdata/invalid_auths_entry_config.json") |
| 31 |
if err != nil { |
| 32 |
t.Fatalf("failed to read file: %v", err) |
| 33 |
} |
| 34 |
_, err = NewMemoryStoreFromDockerConfig(f) |
| 35 |
if !errors.Is(err, config.ErrInvalidConfigFormat) { |
| 36 |
t.Fatalf("Error: %s is expected", config.ErrInvalidConfigFormat) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func TestMemoryStore_Get_validConfig(t *testing.T) { |
| 41 |
ctx := context.Background() |
| 42 |
f, err := os.ReadFile("testdata/valid_auths_config.json") |
| 43 |
if err != nil { |
| 44 |
t.Fatalf("failed to read file: %v", err) |
| 45 |
} |
| 46 |
cfg, err := NewMemoryStoreFromDockerConfig(f) |
| 47 |
if err != nil { |
| 48 |
t.Fatalf("NewMemoryStoreFromConfig() error = %v", err) |
| 49 |
} |
| 50 |
|
| 51 |
tests := []struct { |
| 52 |
name string |
| 53 |
serverAddress string |
| 54 |
want auth.Credential |
| 55 |
wantErr bool |
| 56 |
}{ |
| 57 |
{ |
| 58 |
name: "Username and password", |
| 59 |
serverAddress: "registry1.example.com", |
| 60 |
want: auth.Credential{ |
| 61 |
Username: "username", |
| 62 |
Password: "password", |
| 63 |
}, |
| 64 |
}, |
| 65 |
{ |
| 66 |
name: "Identity token", |
| 67 |
serverAddress: "registry2.example.com", |
| 68 |
want: auth.Credential{ |
| 69 |
RefreshToken: "identity_token", |
| 70 |
}, |
| 71 |
}, |
| 72 |
{ |
| 73 |
name: "Registry token", |
| 74 |
serverAddress: "registry3.example.com", |
| 75 |
want: auth.Credential{ |
| 76 |
AccessToken: "registry_token", |
| 77 |
}, |
| 78 |
}, |
| 79 |
{ |
| 80 |
name: "Username and password, identity token and registry token", |
| 81 |
serverAddress: "registry4.example.com", |
| 82 |
want: auth.Credential{ |
| 83 |
Username: "username", |
| 84 |
Password: "password", |
| 85 |
RefreshToken: "identity_token", |
| 86 |
AccessToken: "registry_token", |
| 87 |
}, |
| 88 |
}, |
| 89 |
{ |
| 90 |
name: "Empty credential", |
| 91 |
serverAddress: "registry5.example.com", |
| 92 |
want: auth.EmptyCredential, |
| 93 |
}, |
| 94 |
{ |
| 95 |
name: "Username and password, no auth", |
| 96 |
serverAddress: "registry6.example.com", |
| 97 |
want: auth.Credential{ |
| 98 |
Username: "username", |
| 99 |
Password: "password", |
| 100 |
}, |
| 101 |
}, |
| 102 |
{ |
| 103 |
name: "Auth overriding Username and password", |
| 104 |
serverAddress: "registry7.example.com", |
| 105 |
want: auth.Credential{ |
| 106 |
Username: "username", |
| 107 |
Password: "password", |
| 108 |
}, |
| 109 |
}, |
| 110 |
{ |
| 111 |
name: "Not in auths", |
| 112 |
serverAddress: "foo.example.com", |
| 113 |
want: auth.EmptyCredential, |
| 114 |
}, |
| 115 |
{ |
| 116 |
name: "No record", |
| 117 |
serverAddress: "registry999.example.com", |
| 118 |
want: auth.EmptyCredential, |
| 119 |
}, |
| 120 |
} |
| 121 |
for _, tt := range tests { |
| 122 |
t.Run(tt.name+" MemoryStore.Get()", func(t *testing.T) { |
| 123 |
got, err := cfg.Get(ctx, tt.serverAddress) |
| 124 |
if (err != nil) != tt.wantErr { |
| 125 |
t.Errorf("MemoryStore.Get() error = %v, wantErr %v", err, tt.wantErr) |
| 126 |
return |
| 127 |
} |
| 128 |
if !reflect.DeepEqual(got, tt.want) { |
| 129 |
t.Errorf("MemoryStore.Get() = %v, want %v", got, tt.want) |
| 130 |
} |
| 131 |
}) |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
func TestMemoryStore_Get_emptyConfig(t *testing.T) { |
| 136 |
ctx := context.Background() |
| 137 |
emptyValidJson := []byte("{}") |
| 138 |
cfg, err := NewMemoryStoreFromDockerConfig(emptyValidJson) |
| 139 |
if err != nil { |
| 140 |
t.Fatal("NewMemoryStoreFromConfig() error =", err) |
| 141 |
} |
| 142 |
|
| 143 |
tests := []struct { |
| 144 |
name string |
| 145 |
serverAddress string |
| 146 |
want auth.Credential |
| 147 |
wantErr error |
| 148 |
}{ |
| 149 |
{ |
| 150 |
name: "Not found", |
| 151 |
serverAddress: "registry.example.com", |
| 152 |
want: auth.EmptyCredential, |
| 153 |
wantErr: nil, |
| 154 |
}, |
| 155 |
} |
| 156 |
for _, tt := range tests { |
| 157 |
t.Run(tt.name, func(t *testing.T) { |
| 158 |
got, err := cfg.Get(ctx, tt.serverAddress) |
| 159 |
if !errors.Is(err, tt.wantErr) { |
| 160 |
t.Errorf("MemoryStore.Get() error = %v, wantErr %v", err, tt.wantErr) |
| 161 |
return |
| 162 |
} |
| 163 |
if !reflect.DeepEqual(got, tt.want) { |
| 164 |
t.Errorf("MemoryStore.Get() = %v, want %v", got, tt.want) |
| 165 |
} |
| 166 |
}) |
| 167 |
} |
| 168 |
} |