auth_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package registry
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. )
  7. func TestEncodeAuth(t *testing.T) {
  8. newAuthConfig := &AuthConfig{Username: "ken", Password: "test", Email: "test@example.com"}
  9. authStr := encodeAuth(newAuthConfig)
  10. decAuthConfig := &AuthConfig{}
  11. var err error
  12. decAuthConfig.Username, decAuthConfig.Password, err = decodeAuth(authStr)
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. if newAuthConfig.Username != decAuthConfig.Username {
  17. t.Fatal("Encode Username doesn't match decoded Username")
  18. }
  19. if newAuthConfig.Password != decAuthConfig.Password {
  20. t.Fatal("Encode Password doesn't match decoded Password")
  21. }
  22. if authStr != "a2VuOnRlc3Q=" {
  23. t.Fatal("AuthString encoding isn't correct.")
  24. }
  25. }
  26. func setupTempConfigFile() (*ConfigFile, error) {
  27. root, err := ioutil.TempDir("", "docker-test-auth")
  28. if err != nil {
  29. return nil, err
  30. }
  31. configFile := &ConfigFile{
  32. rootPath: root,
  33. Configs: make(map[string]AuthConfig),
  34. }
  35. for _, registry := range []string{"testIndex", IndexServerAddress()} {
  36. configFile.Configs[registry] = AuthConfig{
  37. Username: "docker-user",
  38. Password: "docker-pass",
  39. Email: "docker@docker.io",
  40. }
  41. }
  42. return configFile, nil
  43. }
  44. func TestSameAuthDataPostSave(t *testing.T) {
  45. configFile, err := setupTempConfigFile()
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. defer os.RemoveAll(configFile.rootPath)
  50. err = SaveConfig(configFile)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. authConfig := configFile.Configs["testIndex"]
  55. if authConfig.Username != "docker-user" {
  56. t.Fail()
  57. }
  58. if authConfig.Password != "docker-pass" {
  59. t.Fail()
  60. }
  61. if authConfig.Email != "docker@docker.io" {
  62. t.Fail()
  63. }
  64. if authConfig.Auth != "" {
  65. t.Fail()
  66. }
  67. }
  68. func TestResolveAuthConfigIndexServer(t *testing.T) {
  69. configFile, err := setupTempConfigFile()
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. defer os.RemoveAll(configFile.rootPath)
  74. for _, registry := range []string{"", IndexServerAddress()} {
  75. resolved := configFile.ResolveAuthConfig(registry)
  76. if resolved != configFile.Configs[IndexServerAddress()] {
  77. t.Fail()
  78. }
  79. }
  80. }
  81. func TestResolveAuthConfigFullURL(t *testing.T) {
  82. configFile, err := setupTempConfigFile()
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. defer os.RemoveAll(configFile.rootPath)
  87. registryAuth := AuthConfig{
  88. Username: "foo-user",
  89. Password: "foo-pass",
  90. Email: "foo@example.com",
  91. }
  92. localAuth := AuthConfig{
  93. Username: "bar-user",
  94. Password: "bar-pass",
  95. Email: "bar@example.com",
  96. }
  97. configFile.Configs["https://registry.example.com/v1/"] = registryAuth
  98. configFile.Configs["http://localhost:8000/v1/"] = localAuth
  99. configFile.Configs["registry.com"] = registryAuth
  100. validRegistries := map[string][]string{
  101. "https://registry.example.com/v1/": {
  102. "https://registry.example.com/v1/",
  103. "http://registry.example.com/v1/",
  104. "registry.example.com",
  105. "registry.example.com/v1/",
  106. },
  107. "http://localhost:8000/v1/": {
  108. "https://localhost:8000/v1/",
  109. "http://localhost:8000/v1/",
  110. "localhost:8000",
  111. "localhost:8000/v1/",
  112. },
  113. "registry.com": {
  114. "https://registry.com/v1/",
  115. "http://registry.com/v1/",
  116. "registry.com",
  117. "registry.com/v1/",
  118. },
  119. }
  120. for configKey, registries := range validRegistries {
  121. for _, registry := range registries {
  122. var (
  123. configured AuthConfig
  124. ok bool
  125. )
  126. resolved := configFile.ResolveAuthConfig(registry)
  127. if configured, ok = configFile.Configs[configKey]; !ok {
  128. t.Fail()
  129. }
  130. if resolved.Email != configured.Email {
  131. t.Errorf("%s -> %q != %q\n", registry, resolved.Email, configured.Email)
  132. }
  133. }
  134. }
  135. }