| 1 |
// Copyright 2024 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package auth |
| 6 |
|
| 7 |
import ( |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
func TestParseGitAuth(t *testing.T) { |
| 12 |
testCases := []struct { |
| 13 |
gitauth string // contents of 'git credential fill' |
| 14 |
wantPrefix string |
| 15 |
wantUsername string |
| 16 |
wantPassword string |
| 17 |
}{ |
| 18 |
{ // Standard case. |
| 19 |
gitauth: ` |
| 20 |
protocol=https |
| 21 |
host=example.com |
| 22 |
username=bob |
| 23 |
password=secr3t |
| 24 |
`, |
| 25 |
wantPrefix: "https://example.com", |
| 26 |
wantUsername: "bob", |
| 27 |
wantPassword: "secr3t", |
| 28 |
}, |
| 29 |
{ // Should not use an invalid url. |
| 30 |
gitauth: ` |
| 31 |
protocol=https |
| 32 |
host=example.com |
| 33 |
username=bob |
| 34 |
password=secr3t |
| 35 |
url=invalid |
| 36 |
`, |
| 37 |
wantPrefix: "https://example.com", |
| 38 |
wantUsername: "bob", |
| 39 |
wantPassword: "secr3t", |
| 40 |
}, |
| 41 |
{ // Should use the new url. |
| 42 |
gitauth: ` |
| 43 |
protocol=https |
| 44 |
host=example.com |
| 45 |
username=bob |
| 46 |
password=secr3t |
| 47 |
url=https://go.dev |
| 48 |
`, |
| 49 |
wantPrefix: "https://go.dev", |
| 50 |
wantUsername: "bob", |
| 51 |
wantPassword: "secr3t", |
| 52 |
}, |
| 53 |
{ // Empty data. |
| 54 |
gitauth: ` |
| 55 |
`, |
| 56 |
wantPrefix: "", |
| 57 |
wantUsername: "", |
| 58 |
wantPassword: "", |
| 59 |
}, |
| 60 |
{ // Does not follow the '=' format. |
| 61 |
gitauth: ` |
| 62 |
protocol:https |
| 63 |
host:example.com |
| 64 |
username:bob |
| 65 |
password:secr3t |
| 66 |
`, |
| 67 |
wantPrefix: "", |
| 68 |
wantUsername: "", |
| 69 |
wantPassword: "", |
| 70 |
}, |
| 71 |
} |
| 72 |
for _, tc := range testCases { |
| 73 |
parsedPrefix, username, password := parseGitAuth([]byte(tc.gitauth)) |
| 74 |
if parsedPrefix != tc.wantPrefix { |
| 75 |
t.Errorf("parseGitAuth(%s):\nhave %q\nwant %q", tc.gitauth, parsedPrefix, tc.wantPrefix) |
| 76 |
} |
| 77 |
if username != tc.wantUsername { |
| 78 |
t.Errorf("parseGitAuth(%s):\nhave %q\nwant %q", tc.gitauth, username, tc.wantUsername) |
| 79 |
} |
| 80 |
if password != tc.wantPassword { |
| 81 |
t.Errorf("parseGitAuth(%s):\nhave %q\nwant %q", tc.gitauth, password, tc.wantPassword) |
| 82 |
} |
| 83 |
} |
| 84 |
} |