registry/auth_test.go
8d88ea0c
 package registry
be20f3c5
 
 import (
0fc11699
 	"io/ioutil"
20a57f15
 	"os"
18c9b6c6
 	"path/filepath"
be20f3c5
 	"testing"
bb9da6ba
 
 	"github.com/docker/docker/cliconfig"
be20f3c5
 )
 
 func TestEncodeAuth(t *testing.T) {
bb9da6ba
 	newAuthConfig := &cliconfig.AuthConfig{Username: "ken", Password: "test", Email: "test@example.com"}
 	authStr := cliconfig.EncodeAuth(newAuthConfig)
 	decAuthConfig := &cliconfig.AuthConfig{}
f4b41e1a
 	var err error
bb9da6ba
 	decAuthConfig.Username, decAuthConfig.Password, err = cliconfig.DecodeAuth(authStr)
be20f3c5
 	if err != nil {
 		t.Fatal(err)
 	}
 	if newAuthConfig.Username != decAuthConfig.Username {
 		t.Fatal("Encode Username doesn't match decoded Username")
 	}
 	if newAuthConfig.Password != decAuthConfig.Password {
 		t.Fatal("Encode Password doesn't match decoded Password")
 	}
 	if authStr != "a2VuOnRlc3Q=" {
 		t.Fatal("AuthString encoding isn't correct.")
 	}
 }
20a57f15
 
bb9da6ba
 func setupTempConfigFile() (*cliconfig.ConfigFile, error) {
5c175357
 	root, err := ioutil.TempDir("", "docker-test-auth")
0fc11699
 	if err != nil {
edde4f55
 		return nil, err
0fc11699
 	}
dea49b74
 	root = filepath.Join(root, cliconfig.ConfigFileName)
bb9da6ba
 	configFile := cliconfig.NewConfigFile(root)
0fc11699
 
4fcb9ac4
 	for _, registry := range []string{"testIndex", IndexServer} {
bb9da6ba
 		configFile.AuthConfigs[registry] = cliconfig.AuthConfig{
edde4f55
 			Username: "docker-user",
 			Password: "docker-pass",
 			Email:    "docker@docker.io",
 		}
 	}
 
 	return configFile, nil
 }
 
 func TestSameAuthDataPostSave(t *testing.T) {
 	configFile, err := setupTempConfigFile()
 	if err != nil {
 		t.Fatal(err)
0fc11699
 	}
bb9da6ba
 	defer os.RemoveAll(configFile.Filename())
0fc11699
 
18c9b6c6
 	err = configFile.Save()
0fc11699
 	if err != nil {
 		t.Fatal(err)
 	}
 
18c9b6c6
 	authConfig := configFile.AuthConfigs["testIndex"]
0fc11699
 	if authConfig.Username != "docker-user" {
 		t.Fail()
 	}
 	if authConfig.Password != "docker-pass" {
 		t.Fail()
 	}
 	if authConfig.Email != "docker@docker.io" {
 		t.Fail()
 	}
 	if authConfig.Auth != "" {
 		t.Fail()
 	}
 }
edde4f55
 
 func TestResolveAuthConfigIndexServer(t *testing.T) {
 	configFile, err := setupTempConfigFile()
 	if err != nil {
 		t.Fatal(err)
 	}
bb9da6ba
 	defer os.RemoveAll(configFile.Filename())
edde4f55
 
4fcb9ac4
 	indexConfig := configFile.AuthConfigs[IndexServer]
568f86eb
 
 	officialIndex := &IndexInfo{
 		Official: true,
 	}
 	privateIndex := &IndexInfo{
 		Official: false,
edde4f55
 	}
568f86eb
 
bb9da6ba
 	resolved := ResolveAuthConfig(configFile, officialIndex)
4fcb9ac4
 	assertEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServer")
568f86eb
 
bb9da6ba
 	resolved = ResolveAuthConfig(configFile, privateIndex)
4fcb9ac4
 	assertNotEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to not return IndexServer")
edde4f55
 }
 
 func TestResolveAuthConfigFullURL(t *testing.T) {
 	configFile, err := setupTempConfigFile()
 	if err != nil {
 		t.Fatal(err)
 	}
bb9da6ba
 	defer os.RemoveAll(configFile.Filename())
edde4f55
 
bb9da6ba
 	registryAuth := cliconfig.AuthConfig{
edde4f55
 		Username: "foo-user",
 		Password: "foo-pass",
 		Email:    "foo@example.com",
 	}
bb9da6ba
 	localAuth := cliconfig.AuthConfig{
edde4f55
 		Username: "bar-user",
 		Password: "bar-pass",
 		Email:    "bar@example.com",
 	}
bb9da6ba
 	officialAuth := cliconfig.AuthConfig{
568f86eb
 		Username: "baz-user",
 		Password: "baz-pass",
 		Email:    "baz@example.com",
 	}
4fcb9ac4
 	configFile.AuthConfigs[IndexServer] = officialAuth
568f86eb
 
bb9da6ba
 	expectedAuths := map[string]cliconfig.AuthConfig{
568f86eb
 		"registry.example.com": registryAuth,
 		"localhost:8000":       localAuth,
 		"registry.com":         localAuth,
 	}
edde4f55
 
 	validRegistries := map[string][]string{
568f86eb
 		"registry.example.com": {
edde4f55
 			"https://registry.example.com/v1/",
 			"http://registry.example.com/v1/",
 			"registry.example.com",
 			"registry.example.com/v1/",
 		},
568f86eb
 		"localhost:8000": {
edde4f55
 			"https://localhost:8000/v1/",
 			"http://localhost:8000/v1/",
 			"localhost:8000",
 			"localhost:8000/v1/",
 		},
90b0cce0
 		"registry.com": {
 			"https://registry.com/v1/",
 			"http://registry.com/v1/",
 			"registry.com",
 			"registry.com/v1/",
 		},
edde4f55
 	}
 
 	for configKey, registries := range validRegistries {
568f86eb
 		configured, ok := expectedAuths[configKey]
 		if !ok || configured.Email == "" {
a75b02fe
 			t.Fail()
568f86eb
 		}
 		index := &IndexInfo{
 			Name: configKey,
 		}
edde4f55
 		for _, registry := range registries {
18c9b6c6
 			configFile.AuthConfigs[registry] = configured
bb9da6ba
 			resolved := ResolveAuthConfig(configFile, index)
edde4f55
 			if resolved.Email != configured.Email {
 				t.Errorf("%s -> %q != %q\n", registry, resolved.Email, configured.Email)
 			}
18c9b6c6
 			delete(configFile.AuthConfigs, registry)
bb9da6ba
 			resolved = ResolveAuthConfig(configFile, index)
568f86eb
 			if resolved.Email == configured.Email {
 				t.Errorf("%s -> %q == %q\n", registry, resolved.Email, configured.Email)
 			}
edde4f55
 		}
 	}
 }