auth_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. indexConfig := configFile.Configs[IndexServerAddress()]
  75. officialIndex := &IndexInfo{
  76. Official: true,
  77. }
  78. privateIndex := &IndexInfo{
  79. Official: false,
  80. }
  81. resolved := configFile.ResolveAuthConfig(officialIndex)
  82. assertEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServerAddress()")
  83. resolved = configFile.ResolveAuthConfig(privateIndex)
  84. assertNotEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to not return IndexServerAddress()")
  85. }
  86. func TestResolveAuthConfigFullURL(t *testing.T) {
  87. configFile, err := setupTempConfigFile()
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. defer os.RemoveAll(configFile.rootPath)
  92. registryAuth := AuthConfig{
  93. Username: "foo-user",
  94. Password: "foo-pass",
  95. Email: "foo@example.com",
  96. }
  97. localAuth := AuthConfig{
  98. Username: "bar-user",
  99. Password: "bar-pass",
  100. Email: "bar@example.com",
  101. }
  102. officialAuth := AuthConfig{
  103. Username: "baz-user",
  104. Password: "baz-pass",
  105. Email: "baz@example.com",
  106. }
  107. configFile.Configs[IndexServerAddress()] = officialAuth
  108. expectedAuths := map[string]AuthConfig{
  109. "registry.example.com": registryAuth,
  110. "localhost:8000": localAuth,
  111. "registry.com": localAuth,
  112. }
  113. validRegistries := map[string][]string{
  114. "registry.example.com": {
  115. "https://registry.example.com/v1/",
  116. "http://registry.example.com/v1/",
  117. "registry.example.com",
  118. "registry.example.com/v1/",
  119. },
  120. "localhost:8000": {
  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. for configKey, registries := range validRegistries {
  134. configured, ok := expectedAuths[configKey]
  135. if !ok || configured.Email == "" {
  136. t.Fail()
  137. }
  138. index := &IndexInfo{
  139. Name: configKey,
  140. }
  141. for _, registry := range registries {
  142. configFile.Configs[registry] = configured
  143. resolved := configFile.ResolveAuthConfig(index)
  144. if resolved.Email != configured.Email {
  145. t.Errorf("%s -> %q != %q\n", registry, resolved.Email, configured.Email)
  146. }
  147. delete(configFile.Configs, registry)
  148. resolved = configFile.ResolveAuthConfig(index)
  149. if resolved.Email == configured.Email {
  150. t.Errorf("%s -> %q == %q\n", registry, resolved.Email, configured.Email)
  151. }
  152. }
  153. }
  154. }