auth_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package auth
  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. validRegistries := map[string][]string{
  100. "https://registry.example.com/v1/": {
  101. "https://registry.example.com/v1/",
  102. "http://registry.example.com/v1/",
  103. "registry.example.com",
  104. "registry.example.com/v1/",
  105. },
  106. "http://localhost:8000/v1/": {
  107. "https://localhost:8000/v1/",
  108. "http://localhost:8000/v1/",
  109. "localhost:8000",
  110. "localhost:8000/v1/",
  111. },
  112. }
  113. for configKey, registries := range validRegistries {
  114. for _, registry := range registries {
  115. var (
  116. configured AuthConfig
  117. ok bool
  118. )
  119. resolved := configFile.ResolveAuthConfig(registry)
  120. if configured, ok = configFile.Configs[configKey]; !ok {
  121. t.Fail()
  122. }
  123. if resolved.Email != configured.Email {
  124. t.Errorf("%s -> %q != %q\n", registry, resolved.Email, configured.Email)
  125. }
  126. }
  127. }
  128. }