config_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package credentialprovider
  14. import (
  15. "encoding/json"
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "reflect"
  20. "testing"
  21. )
  22. func TestReadDockerConfigFile(t *testing.T) {
  23. configJsonFileName := "config.json"
  24. var fileInfo *os.File
  25. preferredPaths := []string{}
  26. //test dockerconfig json
  27. inputDockerconfigJsonFile := "{ \"auths\": { \"http://foo.example.com\":{\"auth\":\"Zm9vOmJhcgo=\",\"email\":\"foo@example.com\"}}}"
  28. preferredPath, err := ioutil.TempDir("", "test_foo_bar_dockerconfigjson_")
  29. if err != nil {
  30. t.Fatalf("Creating tmp dir fail: %v", err)
  31. return
  32. }
  33. defer os.RemoveAll(preferredPath)
  34. preferredPaths = append(preferredPaths, preferredPath)
  35. absDockerConfigFileLocation, err := filepath.Abs(filepath.Join(preferredPath, configJsonFileName))
  36. if err != nil {
  37. t.Fatalf("While trying to canonicalize %s: %v", preferredPath, err)
  38. }
  39. if _, err := os.Stat(absDockerConfigFileLocation); os.IsNotExist(err) {
  40. //create test cfg file
  41. fileInfo, err = os.OpenFile(absDockerConfigFileLocation, os.O_CREATE|os.O_RDWR, 0664)
  42. if err != nil {
  43. t.Fatalf("While trying to create file %s: %v", absDockerConfigFileLocation, err)
  44. }
  45. defer fileInfo.Close()
  46. }
  47. fileInfo.WriteString(inputDockerconfigJsonFile)
  48. orgPreferredPath := GetPreferredDockercfgPath()
  49. SetPreferredDockercfgPath(preferredPath)
  50. defer SetPreferredDockercfgPath(orgPreferredPath)
  51. if _, err := ReadDockerConfigFile(); err != nil {
  52. t.Errorf("Getting docker config file fail : %v preferredPath : %q", err, preferredPath)
  53. }
  54. }
  55. func TestDockerConfigJsonJSONDecode(t *testing.T) {
  56. input := []byte(`{"auths": {"http://foo.example.com":{"username": "foo", "password": "bar", "email": "foo@example.com"}, "http://bar.example.com":{"username": "bar", "password": "baz", "email": "bar@example.com"}}}`)
  57. expect := DockerConfigJson{
  58. Auths: DockerConfig(map[string]DockerConfigEntry{
  59. "http://foo.example.com": {
  60. Username: "foo",
  61. Password: "bar",
  62. Email: "foo@example.com",
  63. },
  64. "http://bar.example.com": {
  65. Username: "bar",
  66. Password: "baz",
  67. Email: "bar@example.com",
  68. },
  69. }),
  70. }
  71. var output DockerConfigJson
  72. err := json.Unmarshal(input, &output)
  73. if err != nil {
  74. t.Errorf("Received unexpected error: %v", err)
  75. }
  76. if !reflect.DeepEqual(expect, output) {
  77. t.Errorf("Received unexpected output. Expected %#v, got %#v", expect, output)
  78. }
  79. }
  80. func TestDockerConfigJSONDecode(t *testing.T) {
  81. input := []byte(`{"http://foo.example.com":{"username": "foo", "password": "bar", "email": "foo@example.com"}, "http://bar.example.com":{"username": "bar", "password": "baz", "email": "bar@example.com"}}`)
  82. expect := DockerConfig(map[string]DockerConfigEntry{
  83. "http://foo.example.com": {
  84. Username: "foo",
  85. Password: "bar",
  86. Email: "foo@example.com",
  87. },
  88. "http://bar.example.com": {
  89. Username: "bar",
  90. Password: "baz",
  91. Email: "bar@example.com",
  92. },
  93. })
  94. var output DockerConfig
  95. err := json.Unmarshal(input, &output)
  96. if err != nil {
  97. t.Errorf("Received unexpected error: %v", err)
  98. }
  99. if !reflect.DeepEqual(expect, output) {
  100. t.Errorf("Received unexpected output. Expected %#v, got %#v", expect, output)
  101. }
  102. }
  103. func TestDockerConfigEntryJSONDecode(t *testing.T) {
  104. tests := []struct {
  105. input []byte
  106. expect DockerConfigEntry
  107. fail bool
  108. }{
  109. // simple case, just decode the fields
  110. {
  111. input: []byte(`{"username": "foo", "password": "bar", "email": "foo@example.com"}`),
  112. expect: DockerConfigEntry{
  113. Username: "foo",
  114. Password: "bar",
  115. Email: "foo@example.com",
  116. },
  117. fail: false,
  118. },
  119. // auth field decodes to username & password
  120. {
  121. input: []byte(`{"auth": "Zm9vOmJhcg==", "email": "foo@example.com"}`),
  122. expect: DockerConfigEntry{
  123. Username: "foo",
  124. Password: "bar",
  125. Email: "foo@example.com",
  126. },
  127. fail: false,
  128. },
  129. // auth field overrides username & password
  130. {
  131. input: []byte(`{"username": "foo", "password": "bar", "auth": "cGluZzpwb25n", "email": "foo@example.com"}`),
  132. expect: DockerConfigEntry{
  133. Username: "ping",
  134. Password: "pong",
  135. Email: "foo@example.com",
  136. },
  137. fail: false,
  138. },
  139. // poorly-formatted auth causes failure
  140. {
  141. input: []byte(`{"auth": "pants", "email": "foo@example.com"}`),
  142. expect: DockerConfigEntry{
  143. Username: "",
  144. Password: "",
  145. Email: "foo@example.com",
  146. },
  147. fail: true,
  148. },
  149. // invalid JSON causes failure
  150. {
  151. input: []byte(`{"email": false}`),
  152. expect: DockerConfigEntry{
  153. Username: "",
  154. Password: "",
  155. Email: "",
  156. },
  157. fail: true,
  158. },
  159. }
  160. for i, tt := range tests {
  161. var output DockerConfigEntry
  162. err := json.Unmarshal(tt.input, &output)
  163. if (err != nil) != tt.fail {
  164. t.Errorf("case %d: expected fail=%t, got err=%v", i, tt.fail, err)
  165. }
  166. if !reflect.DeepEqual(tt.expect, output) {
  167. t.Errorf("case %d: expected output %#v, got %#v", i, tt.expect, output)
  168. }
  169. }
  170. }
  171. func TestDecodeDockerConfigFieldAuth(t *testing.T) {
  172. tests := []struct {
  173. input string
  174. username string
  175. password string
  176. fail bool
  177. }{
  178. // auth field decodes to username & password
  179. {
  180. input: "Zm9vOmJhcg==",
  181. username: "foo",
  182. password: "bar",
  183. },
  184. // good base64 data, but no colon separating username & password
  185. {
  186. input: "cGFudHM=",
  187. fail: true,
  188. },
  189. // bad base64 data
  190. {
  191. input: "pants",
  192. fail: true,
  193. },
  194. }
  195. for i, tt := range tests {
  196. username, password, err := decodeDockerConfigFieldAuth(tt.input)
  197. if (err != nil) != tt.fail {
  198. t.Errorf("case %d: expected fail=%t, got err=%v", i, tt.fail, err)
  199. }
  200. if tt.username != username {
  201. t.Errorf("case %d: expected username %q, got %q", i, tt.username, username)
  202. }
  203. if tt.password != password {
  204. t.Errorf("case %d: expected password %q, got %q", i, tt.password, password)
  205. }
  206. }
  207. }
  208. func TestDockerConfigEntryJSONCompatibleEncode(t *testing.T) {
  209. tests := []struct {
  210. input DockerConfigEntry
  211. expect []byte
  212. }{
  213. // simple case, just decode the fields
  214. {
  215. expect: []byte(`{"username":"foo","password":"bar","email":"foo@example.com","auth":"Zm9vOmJhcg=="}`),
  216. input: DockerConfigEntry{
  217. Username: "foo",
  218. Password: "bar",
  219. Email: "foo@example.com",
  220. },
  221. },
  222. }
  223. for i, tt := range tests {
  224. actual, err := json.Marshal(tt.input)
  225. if err != nil {
  226. t.Errorf("case %d: unexpected error: %v", i, err)
  227. }
  228. if string(tt.expect) != string(actual) {
  229. t.Errorf("case %d: expected %v, got %v", i, string(tt.expect), string(actual))
  230. }
  231. }
  232. }