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