From e2263836141e9d6daa03765a3f4c5ccf39a429b9 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Tue, 15 Dec 2015 13:36:47 -0500 Subject: [PATCH] Move the TestEncodeAuth test to the correct package. Also make EncodeAuth and DecodeAuth private because they're only used by cliconfig. Signed-off-by: Daniel Nephin --- cliconfig/config.go | 16 ++++++++-------- cliconfig/config_test.go | 23 ++++++++++++++++++++++- registry/auth_test.go | 21 --------------------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/cliconfig/config.go b/cliconfig/config.go index 61ad1fa414..d3d109d122 100644 --- a/cliconfig/config.go +++ b/cliconfig/config.go @@ -80,7 +80,7 @@ func (configFile *ConfigFile) LegacyLoadFromReader(configData io.Reader) error { if len(origAuth) != 2 { return fmt.Errorf("Invalid Auth config file") } - authConfig.Username, authConfig.Password, err = DecodeAuth(origAuth[1]) + authConfig.Username, authConfig.Password, err = decodeAuth(origAuth[1]) if err != nil { return err } @@ -93,7 +93,7 @@ func (configFile *ConfigFile) LegacyLoadFromReader(configData io.Reader) error { configFile.AuthConfigs[defaultIndexserver] = authConfig } else { for k, authConfig := range configFile.AuthConfigs { - authConfig.Username, authConfig.Password, err = DecodeAuth(authConfig.Auth) + authConfig.Username, authConfig.Password, err = decodeAuth(authConfig.Auth) if err != nil { return err } @@ -113,7 +113,7 @@ func (configFile *ConfigFile) LoadFromReader(configData io.Reader) error { } var err error for addr, ac := range configFile.AuthConfigs { - ac.Username, ac.Password, err = DecodeAuth(ac.Auth) + ac.Username, ac.Password, err = decodeAuth(ac.Auth) if err != nil { return err } @@ -201,7 +201,7 @@ func (configFile *ConfigFile) SaveToWriter(writer io.Writer) error { for k, authConfig := range configFile.AuthConfigs { authCopy := authConfig // encode and save the authstring, while blanking out the original fields - authCopy.Auth = EncodeAuth(&authCopy) + authCopy.Auth = encodeAuth(&authCopy) authCopy.Username = "" authCopy.Password = "" authCopy.ServerAddress = "" @@ -242,8 +242,8 @@ func (configFile *ConfigFile) Filename() string { return configFile.filename } -// EncodeAuth creates a base64 encoded string to containing authorization information -func EncodeAuth(authConfig *types.AuthConfig) string { +// encodeAuth creates a base64 encoded string to containing authorization information +func encodeAuth(authConfig *types.AuthConfig) string { authStr := authConfig.Username + ":" + authConfig.Password msg := []byte(authStr) encoded := make([]byte, base64.StdEncoding.EncodedLen(len(msg))) @@ -251,8 +251,8 @@ func EncodeAuth(authConfig *types.AuthConfig) string { return string(encoded) } -// DecodeAuth decodes a base64 encoded string and returns username and password -func DecodeAuth(authStr string) (string, string, error) { +// decodeAuth decodes a base64 encoded string and returns username and password +func decodeAuth(authStr string) (string, string, error) { decLen := base64.StdEncoding.DecodedLen(len(authStr)) decoded := make([]byte, decLen) authByte := []byte(authStr) diff --git a/cliconfig/config_test.go b/cliconfig/config_test.go index dcf368c552..b6c8160001 100644 --- a/cliconfig/config_test.go +++ b/cliconfig/config_test.go @@ -7,6 +7,7 @@ import ( "strings" "testing" + "github.com/docker/docker/api/types" "github.com/docker/docker/pkg/homedir" ) @@ -427,8 +428,8 @@ func TestJsonSaveWithNoFile(t *testing.T) { !strings.Contains(string(buf), "user@example.com") { t.Fatalf("Should have save in new form: %s", string(buf)) } - } + func TestLegacyJsonSaveWithNoFile(t *testing.T) { js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}` @@ -456,3 +457,23 @@ func TestLegacyJsonSaveWithNoFile(t *testing.T) { t.Fatalf("Should have save in new form: %s", string(buf)) } } + +func TestEncodeAuth(t *testing.T) { + newAuthConfig := &types.AuthConfig{Username: "ken", Password: "test", Email: "test@example.com"} + authStr := encodeAuth(newAuthConfig) + decAuthConfig := &types.AuthConfig{} + var err error + decAuthConfig.Username, decAuthConfig.Password, err = decodeAuth(authStr) + 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.") + } +} diff --git a/registry/auth_test.go b/registry/auth_test.go index a2c5c804c9..ff1bd54715 100644 --- a/registry/auth_test.go +++ b/registry/auth_test.go @@ -5,29 +5,8 @@ import ( "github.com/docker/docker/api/types" registrytypes "github.com/docker/docker/api/types/registry" - "github.com/docker/docker/cliconfig" ) -func TestEncodeAuth(t *testing.T) { - newAuthConfig := &types.AuthConfig{Username: "ken", Password: "test", Email: "test@example.com"} - authStr := cliconfig.EncodeAuth(newAuthConfig) - decAuthConfig := &types.AuthConfig{} - var err error - decAuthConfig.Username, decAuthConfig.Password, err = cliconfig.DecodeAuth(authStr) - 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.") - } -} - func buildAuthConfigs() map[string]types.AuthConfig { authConfigs := map[string]types.AuthConfig{}