github.com/dougm/docker@v1.5.0/registry/auth_test.go (about) 1 package registry 2 3 import ( 4 "io/ioutil" 5 "os" 6 "testing" 7 ) 8 9 func TestEncodeAuth(t *testing.T) { 10 newAuthConfig := &AuthConfig{Username: "ken", Password: "test", Email: "test@example.com"} 11 authStr := encodeAuth(newAuthConfig) 12 decAuthConfig := &AuthConfig{} 13 var err error 14 decAuthConfig.Username, decAuthConfig.Password, err = decodeAuth(authStr) 15 if err != nil { 16 t.Fatal(err) 17 } 18 if newAuthConfig.Username != decAuthConfig.Username { 19 t.Fatal("Encode Username doesn't match decoded Username") 20 } 21 if newAuthConfig.Password != decAuthConfig.Password { 22 t.Fatal("Encode Password doesn't match decoded Password") 23 } 24 if authStr != "a2VuOnRlc3Q=" { 25 t.Fatal("AuthString encoding isn't correct.") 26 } 27 } 28 29 func setupTempConfigFile() (*ConfigFile, error) { 30 root, err := ioutil.TempDir("", "docker-test-auth") 31 if err != nil { 32 return nil, err 33 } 34 configFile := &ConfigFile{ 35 rootPath: root, 36 Configs: make(map[string]AuthConfig), 37 } 38 39 for _, registry := range []string{"testIndex", IndexServerAddress()} { 40 configFile.Configs[registry] = AuthConfig{ 41 Username: "docker-user", 42 Password: "docker-pass", 43 Email: "docker@docker.io", 44 } 45 } 46 47 return configFile, nil 48 } 49 50 func TestSameAuthDataPostSave(t *testing.T) { 51 configFile, err := setupTempConfigFile() 52 if err != nil { 53 t.Fatal(err) 54 } 55 defer os.RemoveAll(configFile.rootPath) 56 57 err = SaveConfig(configFile) 58 if err != nil { 59 t.Fatal(err) 60 } 61 62 authConfig := configFile.Configs["testIndex"] 63 if authConfig.Username != "docker-user" { 64 t.Fail() 65 } 66 if authConfig.Password != "docker-pass" { 67 t.Fail() 68 } 69 if authConfig.Email != "docker@docker.io" { 70 t.Fail() 71 } 72 if authConfig.Auth != "" { 73 t.Fail() 74 } 75 } 76 77 func TestResolveAuthConfigIndexServer(t *testing.T) { 78 configFile, err := setupTempConfigFile() 79 if err != nil { 80 t.Fatal(err) 81 } 82 defer os.RemoveAll(configFile.rootPath) 83 84 indexConfig := configFile.Configs[IndexServerAddress()] 85 86 officialIndex := &IndexInfo{ 87 Official: true, 88 } 89 privateIndex := &IndexInfo{ 90 Official: false, 91 } 92 93 resolved := configFile.ResolveAuthConfig(officialIndex) 94 assertEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServerAddress()") 95 96 resolved = configFile.ResolveAuthConfig(privateIndex) 97 assertNotEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to not return IndexServerAddress()") 98 } 99 100 func TestResolveAuthConfigFullURL(t *testing.T) { 101 configFile, err := setupTempConfigFile() 102 if err != nil { 103 t.Fatal(err) 104 } 105 defer os.RemoveAll(configFile.rootPath) 106 107 registryAuth := AuthConfig{ 108 Username: "foo-user", 109 Password: "foo-pass", 110 Email: "foo@example.com", 111 } 112 localAuth := AuthConfig{ 113 Username: "bar-user", 114 Password: "bar-pass", 115 Email: "bar@example.com", 116 } 117 officialAuth := AuthConfig{ 118 Username: "baz-user", 119 Password: "baz-pass", 120 Email: "baz@example.com", 121 } 122 configFile.Configs[IndexServerAddress()] = officialAuth 123 124 expectedAuths := map[string]AuthConfig{ 125 "registry.example.com": registryAuth, 126 "localhost:8000": localAuth, 127 "registry.com": localAuth, 128 } 129 130 validRegistries := map[string][]string{ 131 "registry.example.com": { 132 "https://registry.example.com/v1/", 133 "http://registry.example.com/v1/", 134 "registry.example.com", 135 "registry.example.com/v1/", 136 }, 137 "localhost:8000": { 138 "https://localhost:8000/v1/", 139 "http://localhost:8000/v1/", 140 "localhost:8000", 141 "localhost:8000/v1/", 142 }, 143 "registry.com": { 144 "https://registry.com/v1/", 145 "http://registry.com/v1/", 146 "registry.com", 147 "registry.com/v1/", 148 }, 149 } 150 151 for configKey, registries := range validRegistries { 152 configured, ok := expectedAuths[configKey] 153 if !ok || configured.Email == "" { 154 t.Fail() 155 } 156 index := &IndexInfo{ 157 Name: configKey, 158 } 159 for _, registry := range registries { 160 configFile.Configs[registry] = configured 161 resolved := configFile.ResolveAuthConfig(index) 162 if resolved.Email != configured.Email { 163 t.Errorf("%s -> %q != %q\n", registry, resolved.Email, configured.Email) 164 } 165 delete(configFile.Configs, registry) 166 resolved = configFile.ResolveAuthConfig(index) 167 if resolved.Email == configured.Email { 168 t.Errorf("%s -> %q == %q\n", registry, resolved.Email, configured.Email) 169 } 170 } 171 } 172 }