Skip to content

Commit d4d4d1a

Browse files
committed
Move configtest package
Signed-off-by: Terry Howe <terrylhowe@gmail.com>
1 parent 7a4a43a commit d4d4d1a

16 files changed

+250
-422
lines changed

registry/remote/credentials/example_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010
See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
13-
// Package remote_test includes all the testable examples for remote repository type
1413

1514
package credentials_test
1615

registry/remote/credentials/file_store_test.go

Lines changed: 111 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"reflect"
2525
"testing"
2626

27-
"oras.land/oras-go/v2/registry/remote/credentials/configtest"
27+
"oras.land/oras-go/v2/registry/remote/internal/configuration/configtest"
2828
)
2929

3030
func TestNewFileStore_badPath(t *testing.T) {
@@ -65,17 +65,17 @@ func TestNewFileStore_badFormat(t *testing.T) {
6565
}{
6666
{
6767
name: "Bad JSON format",
68-
configPath: "testdata/bad_config",
68+
configPath: "../internal/configuration/testdata/bad_config",
6969
wantErr: true,
7070
},
7171
{
7272
name: "Invalid auths format",
73-
configPath: "testdata/invalid_auths_config.json",
73+
configPath: "../internal/configuration/testdata/invalid_auths_config.json",
7474
wantErr: true,
7575
},
7676
{
7777
name: "No auths field",
78-
configPath: "testdata/no_auths_config.json",
78+
configPath: "../internal/configuration/testdata/no_auths_config.json",
7979
wantErr: false,
8080
},
8181
}
@@ -92,7 +92,7 @@ func TestNewFileStore_badFormat(t *testing.T) {
9292

9393
func TestFileStore_Get_validConfig(t *testing.T) {
9494
ctx := context.Background()
95-
fs, err := NewFileStore("testdata/valid_auths_config.json")
95+
fs, err := NewFileStore("../internal/configuration/testdata/valid_auths_config.json")
9696
if err != nil {
9797
t.Fatal("NewFileStore() error =", err)
9898
}
@@ -183,7 +183,7 @@ func TestFileStore_Get_validConfig(t *testing.T) {
183183

184184
func TestFileStore_Get_invalidConfig(t *testing.T) {
185185
ctx := context.Background()
186-
fs, err := NewFileStore("testdata/invalid_auths_entry_config.json")
186+
fs, err := NewFileStore("../internal/configuration/testdata/invalid_auths_entry_config.json")
187187
if err != nil {
188188
t.Fatal("NewFileStore() error =", err)
189189
}
@@ -229,7 +229,7 @@ func TestFileStore_Get_invalidConfig(t *testing.T) {
229229

230230
func TestFileStore_Get_emptyConfig(t *testing.T) {
231231
ctx := context.Background()
232-
fs, err := NewFileStore("testdata/empty_config.json")
232+
fs, err := NewFileStore("../internal/configuration/testdata/empty.json")
233233
if err != nil {
234234
t.Fatal("NewFileStore() error =", err)
235235
}
@@ -907,3 +907,107 @@ func Test_validateCredentialFormat(t *testing.T) {
907907
})
908908
}
909909
}
910+
911+
func Test_encodeAuth(t *testing.T) {
912+
tests := []struct {
913+
name string
914+
username string
915+
password string
916+
want string
917+
}{
918+
{
919+
name: "Username and password",
920+
username: "username",
921+
password: "password",
922+
want: "dXNlcm5hbWU6cGFzc3dvcmQ=",
923+
},
924+
{
925+
name: "Username only",
926+
username: "username",
927+
password: "",
928+
want: "dXNlcm5hbWU6",
929+
},
930+
{
931+
name: "Password only",
932+
username: "",
933+
password: "password",
934+
want: "OnBhc3N3b3Jk",
935+
},
936+
{
937+
name: "Empty username and empty password",
938+
username: "",
939+
password: "",
940+
want: "",
941+
},
942+
}
943+
for _, tt := range tests {
944+
t.Run(tt.name, func(t *testing.T) {
945+
if got := encodeAuth(tt.username, tt.password); got != tt.want {
946+
t.Errorf("encodeAuth() = %v, want %v", got, tt.want)
947+
}
948+
})
949+
}
950+
}
951+
952+
func Test_decodeAuth(t *testing.T) {
953+
tests := []struct {
954+
name string
955+
authStr string
956+
username string
957+
password string
958+
wantErr bool
959+
}{
960+
{
961+
name: "Valid base64",
962+
authStr: "dXNlcm5hbWU6cGFzc3dvcmQ=", // username:password
963+
username: "username",
964+
password: "password",
965+
},
966+
{
967+
name: "Valid base64, username only",
968+
authStr: "dXNlcm5hbWU6", // username:
969+
username: "username",
970+
},
971+
{
972+
name: "Valid base64, password only",
973+
authStr: "OnBhc3N3b3Jk", // :password
974+
password: "password",
975+
},
976+
{
977+
name: "Valid base64, bad format",
978+
authStr: "d2hhdGV2ZXI=", // whatever
979+
username: "",
980+
password: "",
981+
wantErr: true,
982+
},
983+
{
984+
name: "Invalid base64",
985+
authStr: "whatever",
986+
username: "",
987+
password: "",
988+
wantErr: true,
989+
},
990+
{
991+
name: "Empty string",
992+
authStr: "",
993+
username: "",
994+
password: "",
995+
wantErr: false,
996+
},
997+
}
998+
for _, tt := range tests {
999+
t.Run(tt.name, func(t *testing.T) {
1000+
gotUsername, gotPassword, err := decodeAuth(tt.authStr)
1001+
if (err != nil) != tt.wantErr {
1002+
t.Errorf("decodeAuth() error = %v, wantErr %v", err, tt.wantErr)
1003+
return
1004+
}
1005+
if gotUsername != tt.username {
1006+
t.Errorf("decodeAuth() got = %v, want %v", gotUsername, tt.username)
1007+
}
1008+
if gotPassword != tt.password {
1009+
t.Errorf("decodeAuth() got1 = %v, want %v", gotPassword, tt.password)
1010+
}
1011+
})
1012+
}
1013+
}

registry/remote/credentials/memory_store_from_config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
)
2727

2828
func TestMemoryStore_Create_fromInvalidConfig(t *testing.T) {
29-
f, err := os.ReadFile("testdata/invalid_auths_entry_config.json")
29+
f, err := os.ReadFile("../internal/configuration/testdata/invalid_auths_entry_config.json")
3030
if err != nil {
3131
t.Fatalf("failed to read file: %v", err)
3232
}
@@ -38,7 +38,7 @@ func TestMemoryStore_Create_fromInvalidConfig(t *testing.T) {
3838

3939
func TestMemoryStore_Get_validConfig(t *testing.T) {
4040
ctx := context.Background()
41-
f, err := os.ReadFile("testdata/valid_auths_config.json")
41+
f, err := os.ReadFile("../internal/configuration/testdata/valid_auths_config.json")
4242
if err != nil {
4343
t.Fatalf("failed to read file: %v", err)
4444
}

registry/remote/credentials/store_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"reflect"
2525
"testing"
2626

27-
"oras.land/oras-go/v2/registry/remote/credentials/configtest"
27+
"oras.land/oras-go/v2/registry/remote/internal/configuration/configtest"
2828
)
2929

3030
// testStore implements the Store interface, used for testing purpose.
@@ -585,37 +585,37 @@ func Test_DynamicStore_getHelperSuffix(t *testing.T) {
585585
}{
586586
{
587587
name: "Get cred helper: registry_helper1",
588-
configPath: "testdata/credHelpers_config.json",
588+
configPath: "../internal/configuration/testdata/credHelpers_config.json",
589589
serverAddress: "registry1.example.com",
590590
want: "registry1-helper",
591591
},
592592
{
593593
name: "Get cred helper: registry_helper2",
594-
configPath: "testdata/credHelpers_config.json",
594+
configPath: "../internal/configuration/testdata/credHelpers_config.json",
595595
serverAddress: "registry2.example.com",
596596
want: "registry2-helper",
597597
},
598598
{
599599
name: "Empty cred helper configured",
600-
configPath: "testdata/credHelpers_config.json",
600+
configPath: "../internal/configuration/testdata/credHelpers_config.json",
601601
serverAddress: "registry3.example.com",
602602
want: "",
603603
},
604604
{
605605
name: "No cred helper and creds store configured",
606-
configPath: "testdata/credHelpers_config.json",
606+
configPath: "../internal/configuration/testdata/credHelpers_config.json",
607607
serverAddress: "whatever.example.com",
608608
want: "",
609609
},
610610
{
611611
name: "Choose cred helper over creds store",
612-
configPath: "testdata/credsStore_config.json",
612+
configPath: "../internal/configuration/testdata/credsStore_config.json",
613613
serverAddress: "test.example.com",
614614
want: "test-helper",
615615
},
616616
{
617617
name: "No cred helper configured, choose cred store",
618-
configPath: "testdata/credsStore_config.json",
618+
configPath: "../internal/configuration/testdata/credsStore_config.json",
619619
serverAddress: "whatever.example.com",
620620
want: "teststore",
621621
},
@@ -634,7 +634,7 @@ func Test_DynamicStore_getHelperSuffix(t *testing.T) {
634634
}
635635

636636
func Test_DynamicStore_ConfigPath(t *testing.T) {
637-
path := "../../testdata/credsStore_config.json"
637+
path := "../internal/configuration/testdata/credsStore_config.json"
638638
var err error
639639
store, err := NewStore(path, StoreOptions{})
640640
if err != nil {
@@ -654,22 +654,22 @@ func Test_DynamicStore_getStore_nativeStore(t *testing.T) {
654654
}{
655655
{
656656
name: "Cred helper configured for registry1.example.com",
657-
configPath: "testdata/credHelpers_config.json",
657+
configPath: "../internal/configuration/testdata/credHelpers_config.json",
658658
serverAddress: "registry1.example.com",
659659
},
660660
{
661661
name: "Cred helper configured for registry2.example.com",
662-
configPath: "testdata/credHelpers_config.json",
662+
configPath: "../internal/configuration/testdata/credHelpers_config.json",
663663
serverAddress: "registry2.example.com",
664664
},
665665
{
666666
name: "Cred helper configured for test.example.com",
667-
configPath: "testdata/credsStore_config.json",
667+
configPath: "../internal/configuration/testdata/credsStore_config.json",
668668
serverAddress: "test.example.com",
669669
},
670670
{
671671
name: "No cred helper configured, use creds store",
672-
configPath: "testdata/credsStore_config.json",
672+
configPath: "../internal/configuration/testdata/credsStore_config.json",
673673
serverAddress: "whaterver.example.com",
674674
},
675675
}
@@ -695,12 +695,12 @@ func Test_DynamicStore_getStore_fileStore(t *testing.T) {
695695
}{
696696
{
697697
name: "Empty cred helper configured for registry3.example.com",
698-
configPath: "testdata/credHelpers_config.json",
698+
configPath: "../internal/configuration/testdata/credHelpers_config.json",
699699
serverAddress: "registry3.example.com",
700700
},
701701
{
702702
name: "No cred helper configured",
703-
configPath: "testdata/credHelpers_config.json",
703+
configPath: "../internal/configuration/testdata/credHelpers_config.json",
704704
serverAddress: "whatever.example.com",
705705
},
706706
}

0 commit comments

Comments
 (0)