|
1 | 1 | package osxkeychain |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
4 | 6 | "github.com/docker/docker-credential-helpers/credentials" |
5 | 7 | "testing" |
6 | 8 | ) |
@@ -54,6 +56,194 @@ func TestOSXKeychainHelper(t *testing.T) { |
54 | 56 | } |
55 | 57 | } |
56 | 58 |
|
| 59 | +// TestOSXKeychainHelperParseURL verifies that a // "scheme" is added to URLs, |
| 60 | +// and that invalid URLs produce an error. |
| 61 | +func TestOSXKeychainHelperParseURL(t *testing.T) { |
| 62 | + tests := []struct { |
| 63 | + url string |
| 64 | + expectedURL string |
| 65 | + err error |
| 66 | + }{ |
| 67 | + {url: "foobar.docker.io", expectedURL: "//foobar.docker.io"}, |
| 68 | + {url: "foobar.docker.io:2376", expectedURL: "//foobar.docker.io:2376"}, |
| 69 | + {url: "//foobar.docker.io:2376", expectedURL: "//foobar.docker.io:2376"}, |
| 70 | + {url: "http://foobar.docker.io:2376", expectedURL: "http://foobar.docker.io:2376"}, |
| 71 | + {url: "https://foobar.docker.io:2376", expectedURL: "https://foobar.docker.io:2376"}, |
| 72 | + {url: "https://foobar.docker.io:2376/some/path", expectedURL: "https://foobar.docker.io:2376/some/path"}, |
| 73 | + {url: "https://foobar.docker.io:2376/some/other/path?foo=bar", expectedURL: "https://foobar.docker.io:2376/some/other/path"}, |
| 74 | + {url: "/foobar.docker.io", err: errors.New("no hostname in URL")}, |
| 75 | + {url: "ftp://foobar.docker.io:2376", err: errors.New("unsupported scheme: ftp")}, |
| 76 | + } |
| 77 | + |
| 78 | + for _, te := range tests { |
| 79 | + u, err := parseURL(te.url) |
| 80 | + |
| 81 | + if te.err == nil && err != nil { |
| 82 | + t.Errorf("Error: failed to parse URL %q: %s", te.url, err) |
| 83 | + continue |
| 84 | + } |
| 85 | + if te.err != nil && err == nil { |
| 86 | + t.Errorf("Error: expected error %q, got none when parsing URL %q", te.err, te.url) |
| 87 | + continue |
| 88 | + } |
| 89 | + if te.err != nil && err.Error() != te.err.Error() { |
| 90 | + t.Errorf("Error: expected error %q, got %q when parsing URL %q", te.err, err, te.url) |
| 91 | + continue |
| 92 | + } |
| 93 | + if u != nil && u.String() != te.expectedURL { |
| 94 | + t.Errorf("Error: expected URL: %q, but got %q for URL: %q", te.expectedURL, u.String(), te.url) |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// TestOSXKeychainHelperRetrieveAliases verifies that secrets can be accessed |
| 100 | +// through variations on the URL |
| 101 | +func TestOSXKeychainHelperRetrieveAliases(t *testing.T) { |
| 102 | + tests := []struct { |
| 103 | + storeURL string |
| 104 | + readURL string |
| 105 | + }{ |
| 106 | + // stored with port, retrieved without |
| 107 | + {"https://foobar.docker.io:2376", "https://foobar.docker.io"}, |
| 108 | + |
| 109 | + // stored as https, retrieved without scheme |
| 110 | + {"https://foobar.docker.io:2376", "foobar.docker.io"}, |
| 111 | + |
| 112 | + // stored with path, retrieved without |
| 113 | + {"https://foobar.docker.io:1234/one/two", "https://foobar.docker.io:1234"}, |
| 114 | + } |
| 115 | + |
| 116 | + helper := Osxkeychain{} |
| 117 | + defer func() { |
| 118 | + for _, te := range tests { |
| 119 | + helper.Delete(te.storeURL) |
| 120 | + } |
| 121 | + }() |
| 122 | + |
| 123 | + // Clean store before testing. |
| 124 | + for _, te := range tests { |
| 125 | + helper.Delete(te.storeURL) |
| 126 | + } |
| 127 | + |
| 128 | + for _, te := range tests { |
| 129 | + c := &credentials.Credentials{ServerURL: te.storeURL, Username: "hello", Secret: "world"} |
| 130 | + if err := helper.Add(c); err != nil { |
| 131 | + t.Errorf("Error: failed to store secret for URL %q: %s", te.storeURL, err) |
| 132 | + continue |
| 133 | + } |
| 134 | + if _, _, err := helper.Get(te.readURL); err != nil { |
| 135 | + t.Errorf("Error: failed to read secret for URL %q using %q", te.storeURL, te.readURL) |
| 136 | + } |
| 137 | + helper.Delete(te.storeURL) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// TestOSXKeychainHelperRetrieveStrict verifies that only matching secrets are |
| 142 | +// returned. |
| 143 | +func TestOSXKeychainHelperRetrieveStrict(t *testing.T) { |
| 144 | + tests := []struct { |
| 145 | + storeURL string |
| 146 | + readURL string |
| 147 | + }{ |
| 148 | + // stored as https, retrieved using http |
| 149 | + {"https://foobar.docker.io:2376", "http://foobar.docker.io:2376"}, |
| 150 | + |
| 151 | + // stored as http, retrieved using https |
| 152 | + {"http://foobar.docker.io:2376", "https://foobar.docker.io:2376"}, |
| 153 | + |
| 154 | + // same: stored as http, retrieved without a scheme specified (hence, using the default https://) |
| 155 | + {"http://foobar.docker.io", "foobar.docker.io:5678"}, |
| 156 | + |
| 157 | + // non-matching ports |
| 158 | + {"https://foobar.docker.io:1234", "https://foobar.docker.io:5678"}, |
| 159 | + |
| 160 | + // non-matching ports TODO is this desired behavior? The other way round does work |
| 161 | + //{"https://foobar.docker.io", "https://foobar.docker.io:5678"}, |
| 162 | + |
| 163 | + // non-matching paths |
| 164 | + {"https://foobar.docker.io:1234/one/two", "https://foobar.docker.io:1234/five/six"}, |
| 165 | + } |
| 166 | + |
| 167 | + helper := Osxkeychain{} |
| 168 | + defer func() { |
| 169 | + for _, te := range tests { |
| 170 | + helper.Delete(te.storeURL) |
| 171 | + } |
| 172 | + }() |
| 173 | + |
| 174 | + // Clean store before testing. |
| 175 | + for _, te := range tests { |
| 176 | + helper.Delete(te.storeURL) |
| 177 | + } |
| 178 | + |
| 179 | + for _, te := range tests { |
| 180 | + c := &credentials.Credentials{ServerURL: te.storeURL, Username: "hello", Secret: "world"} |
| 181 | + if err := helper.Add(c); err != nil { |
| 182 | + t.Errorf("Error: failed to store secret for URL %q: %s", te.storeURL, err) |
| 183 | + continue |
| 184 | + } |
| 185 | + if _, _, err := helper.Get(te.readURL); err == nil { |
| 186 | + t.Errorf("Error: managed to read secret for URL %q using %q, but should not be able to", te.storeURL, te.readURL) |
| 187 | + } |
| 188 | + helper.Delete(te.storeURL) |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +// TestOSXKeychainHelperStoreRetrieve verifies that secrets stored in the |
| 193 | +// the keychain can be read back using the URL that was used to store them. |
| 194 | +func TestOSXKeychainHelperStoreRetrieve(t *testing.T) { |
| 195 | + tests := []struct { |
| 196 | + url string |
| 197 | + }{ |
| 198 | + {url: "foobar.docker.io"}, |
| 199 | + {url: "foobar.docker.io:2376"}, |
| 200 | + {url: "//foobar.docker.io:2376"}, |
| 201 | + {url: "https://foobar.docker.io:2376"}, |
| 202 | + {url: "http://foobar.docker.io:2376"}, |
| 203 | + {url: "https://foobar.docker.io:2376/some/path"}, |
| 204 | + {url: "https://foobar.docker.io:2376/some/other/path"}, |
| 205 | + {url: "https://foobar.docker.io:2376/some/other/path?foo=bar"}, |
| 206 | + } |
| 207 | + |
| 208 | + helper := Osxkeychain{} |
| 209 | + defer func() { |
| 210 | + for _, te := range tests { |
| 211 | + helper.Delete(te.url) |
| 212 | + } |
| 213 | + }() |
| 214 | + |
| 215 | + // Clean store before testing. |
| 216 | + for _, te := range tests { |
| 217 | + helper.Delete(te.url) |
| 218 | + } |
| 219 | + |
| 220 | + // Note that we don't delete between individual tests here, to verify that |
| 221 | + // subsequent stores/overwrites don't affect storing / retrieving secrets. |
| 222 | + for i, te := range tests { |
| 223 | + c := &credentials.Credentials{ |
| 224 | + ServerURL: te.url, |
| 225 | + Username: fmt.Sprintf("user-%d", i), |
| 226 | + Secret: fmt.Sprintf("secret-%d", i), |
| 227 | + } |
| 228 | + |
| 229 | + if err := helper.Add(c); err != nil { |
| 230 | + t.Errorf("Error: failed to store secret for URL: %s: %s", te.url, err) |
| 231 | + continue |
| 232 | + } |
| 233 | + user, secret, err := helper.Get(te.url) |
| 234 | + if err != nil { |
| 235 | + t.Errorf("Error: failed to read secret for URL %q: %s", te.url, err) |
| 236 | + continue |
| 237 | + } |
| 238 | + if user != c.Username { |
| 239 | + t.Errorf("Error: expected username %s, got username %s for URL: %s", c.Username, user, te.url) |
| 240 | + } |
| 241 | + if secret != c.Secret { |
| 242 | + t.Errorf("Error: expected secret %s, got secret %s for URL: %s", c.Secret, secret, te.url) |
| 243 | + } |
| 244 | + } |
| 245 | +} |
| 246 | + |
57 | 247 | func TestMissingCredentials(t *testing.T) { |
58 | 248 | helper := Osxkeychain{} |
59 | 249 | _, _, err := helper.Get("https://adsfasdf.wrewerwer.com/asdfsdddd") |
|
0 commit comments