github.com/stchris/docker@v1.4.2-0.20150106053530-1510a324dbd5/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  	for _, registry := range []string{"", IndexServerAddress()} {
    85  		resolved := configFile.ResolveAuthConfig(registry)
    86  		if resolved != configFile.Configs[IndexServerAddress()] {
    87  			t.Fail()
    88  		}
    89  	}
    90  }
    91  
    92  func TestResolveAuthConfigFullURL(t *testing.T) {
    93  	configFile, err := setupTempConfigFile()
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  	defer os.RemoveAll(configFile.rootPath)
    98  
    99  	registryAuth := AuthConfig{
   100  		Username: "foo-user",
   101  		Password: "foo-pass",
   102  		Email:    "foo@example.com",
   103  	}
   104  	localAuth := AuthConfig{
   105  		Username: "bar-user",
   106  		Password: "bar-pass",
   107  		Email:    "bar@example.com",
   108  	}
   109  	configFile.Configs["https://registry.example.com/v1/"] = registryAuth
   110  	configFile.Configs["http://localhost:8000/v1/"] = localAuth
   111  	configFile.Configs["registry.com"] = registryAuth
   112  
   113  	validRegistries := map[string][]string{
   114  		"https://registry.example.com/v1/": {
   115  			"https://registry.example.com/v1/",
   116  			"http://registry.example.com/v1/",
   117  			"registry.example.com",
   118  			"registry.example.com/v1/",
   119  		},
   120  		"http://localhost:8000/v1/": {
   121  			"https://localhost:8000/v1/",
   122  			"http://localhost:8000/v1/",
   123  			"localhost:8000",
   124  			"localhost:8000/v1/",
   125  		},
   126  		"registry.com": {
   127  			"https://registry.com/v1/",
   128  			"http://registry.com/v1/",
   129  			"registry.com",
   130  			"registry.com/v1/",
   131  		},
   132  	}
   133  
   134  	for configKey, registries := range validRegistries {
   135  		for _, registry := range registries {
   136  			var (
   137  				configured AuthConfig
   138  				ok         bool
   139  			)
   140  			resolved := configFile.ResolveAuthConfig(registry)
   141  			if configured, ok = configFile.Configs[configKey]; !ok {
   142  				t.Fail()
   143  			}
   144  			if resolved.Email != configured.Email {
   145  				t.Errorf("%s -> %q != %q\n", registry, resolved.Email, configured.Email)
   146  			}
   147  		}
   148  	}
   149  }