github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/moby/registry/auth_test.go (about) 1 package registry // import "github.com/docker/docker/registry" 2 3 import ( 4 "testing" 5 6 "github.com/docker/docker/api/types" 7 registrytypes "github.com/docker/docker/api/types/registry" 8 ) 9 10 func buildAuthConfigs() map[string]types.AuthConfig { 11 authConfigs := map[string]types.AuthConfig{} 12 13 for _, registry := range []string{"testIndex", IndexServer} { 14 authConfigs[registry] = types.AuthConfig{ 15 Username: "docker-user", 16 Password: "docker-pass", 17 } 18 } 19 20 return authConfigs 21 } 22 23 func TestResolveAuthConfigIndexServer(t *testing.T) { 24 authConfigs := buildAuthConfigs() 25 indexConfig := authConfigs[IndexServer] 26 27 officialIndex := ®istrytypes.IndexInfo{ 28 Official: true, 29 } 30 privateIndex := ®istrytypes.IndexInfo{ 31 Official: false, 32 } 33 34 resolved := ResolveAuthConfig(authConfigs, officialIndex) 35 assertEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServer") 36 37 resolved = ResolveAuthConfig(authConfigs, privateIndex) 38 assertNotEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to not return IndexServer") 39 } 40 41 func TestResolveAuthConfigFullURL(t *testing.T) { 42 authConfigs := buildAuthConfigs() 43 44 registryAuth := types.AuthConfig{ 45 Username: "foo-user", 46 Password: "foo-pass", 47 } 48 localAuth := types.AuthConfig{ 49 Username: "bar-user", 50 Password: "bar-pass", 51 } 52 officialAuth := types.AuthConfig{ 53 Username: "baz-user", 54 Password: "baz-pass", 55 } 56 authConfigs[IndexServer] = officialAuth 57 58 expectedAuths := map[string]types.AuthConfig{ 59 "registry.example.com": registryAuth, 60 "localhost:8000": localAuth, 61 "registry.com": localAuth, 62 } 63 64 validRegistries := map[string][]string{ 65 "registry.example.com": { 66 "https://registry.example.com/v1/", 67 "http://registry.example.com/v1/", 68 "registry.example.com", 69 "registry.example.com/v1/", 70 }, 71 "localhost:8000": { 72 "https://localhost:8000/v1/", 73 "http://localhost:8000/v1/", 74 "localhost:8000", 75 "localhost:8000/v1/", 76 }, 77 "registry.com": { 78 "https://registry.com/v1/", 79 "http://registry.com/v1/", 80 "registry.com", 81 "registry.com/v1/", 82 }, 83 } 84 85 for configKey, registries := range validRegistries { 86 configured, ok := expectedAuths[configKey] 87 if !ok { 88 t.Fail() 89 } 90 index := ®istrytypes.IndexInfo{ 91 Name: configKey, 92 } 93 for _, registry := range registries { 94 authConfigs[registry] = configured 95 resolved := ResolveAuthConfig(authConfigs, index) 96 if resolved.Username != configured.Username || resolved.Password != configured.Password { 97 t.Errorf("%s -> %v != %v\n", registry, resolved, configured) 98 } 99 delete(authConfigs, registry) 100 resolved = ResolveAuthConfig(authConfigs, index) 101 if resolved.Username == configured.Username || resolved.Password == configured.Password { 102 t.Errorf("%s -> %v == %v\n", registry, resolved, configured) 103 } 104 } 105 } 106 }