3 test_description='basic credential helper tests'
5 . "$TEST_DIRECTORY"/lib-credential.sh
7 test_expect_success 'setup helper scripts' '
9 whoami=$(echo $0 | sed s/.*git-credential-//)
10 echo >&2 "$whoami: $*"
13 while read key value; do
14 echo >&2 "$whoami: $key=$value"
20 write_script git-credential-useless <<-\EOF &&
25 write_script git-credential-quit <<-\EOF &&
30 write_script git-credential-verbatim <<-\EOF &&
34 test -z "$user" || echo username=$user
35 test -z "$pass" || echo password=$pass
41 test_expect_success 'credential_fill invokes helper' '
42 check fill "verbatim foo bar" <<-\EOF
52 verbatim: protocol=http
53 verbatim: host=example.com
57 test_expect_success 'credential_fill invokes multiple helpers' '
58 check fill useless "verbatim foo bar" <<-\EOF
68 useless: protocol=http
69 useless: host=example.com
71 verbatim: protocol=http
72 verbatim: host=example.com
76 test_expect_success 'credential_fill stops when we get a full response' '
77 check fill "verbatim one two" "verbatim three four" <<-\EOF
87 verbatim: protocol=http
88 verbatim: host=example.com
92 test_expect_success 'credential_fill continues through partial response' '
93 check fill "verbatim one \"\"" "verbatim two three" <<-\EOF
103 verbatim: protocol=http
104 verbatim: host=example.com
106 verbatim: protocol=http
107 verbatim: host=example.com
108 verbatim: username=one
112 test_expect_success 'credential_fill passes along metadata' '
113 check fill "verbatim one two" <<-\EOF
125 verbatim: protocol=ftp
126 verbatim: host=example.com
127 verbatim: path=foo.git
131 test_expect_success 'credential_approve calls all helpers' '
132 check approve useless "verbatim one two" <<-\EOF
140 useless: protocol=http
141 useless: host=example.com
142 useless: username=foo
143 useless: password=bar
145 verbatim: protocol=http
146 verbatim: host=example.com
147 verbatim: username=foo
148 verbatim: password=bar
152 test_expect_success 'do not bother storing password-less credential' '
153 check approve useless <<-\EOF
163 test_expect_success 'credential_reject calls all helpers' '
164 check reject useless "verbatim one two" <<-\EOF
172 useless: protocol=http
173 useless: host=example.com
174 useless: username=foo
175 useless: password=bar
177 verbatim: protocol=http
178 verbatim: host=example.com
179 verbatim: username=foo
180 verbatim: password=bar
184 test_expect_success 'usernames can be preserved' '
185 check fill "verbatim \"\" three" <<-\EOF
196 verbatim: protocol=http
197 verbatim: host=example.com
198 verbatim: username=one
202 test_expect_success 'usernames can be overridden' '
203 check fill "verbatim two three" <<-\EOF
214 verbatim: protocol=http
215 verbatim: host=example.com
216 verbatim: username=one
220 test_expect_success 'do not bother completing already-full credential' '
221 check fill "verbatim three four" <<-\EOF
235 # We can't test the basic terminal password prompt here because
236 # getpass() tries too hard to find the real terminal. But if our
237 # askpass helper is run, we know the internal getpass is working.
238 test_expect_success 'empty helper list falls back to internal getpass' '
245 username=askpass-username
246 password=askpass-password
248 askpass: Username for '\''http://example.com'\'':
249 askpass: Password for '\''http://askpass-username@example.com'\'':
253 test_expect_success 'internal getpass does not ask for known username' '
262 password=askpass-password
264 askpass: Password for '\''http://foo@example.com'\'':
273 test_expect_success 'respect configured credentials' '
274 test_config credential.helper "$HELPER" &&
287 test_expect_success 'match configured credential' '
288 test_config credential.https://example.com.helper "$HELPER" &&
302 test_expect_success 'do not match configured credential' '
303 test_config credential.https://foo.helper "$HELPER" &&
310 username=askpass-username
311 password=askpass-password
313 askpass: Username for '\''https://bar'\'':
314 askpass: Password for '\''https://askpass-username@bar'\'':
318 test_expect_success 'pull username from config' '
319 test_config credential.https://example.com.username foo &&
327 password=askpass-password
329 askpass: Password for '\''https://foo@example.com'\'':
333 test_expect_success 'http paths can be part of context' '
334 check fill "verbatim foo bar" <<-\EOF &&
345 verbatim: protocol=https
346 verbatim: host=example.com
348 test_config credential.https://example.com.useHttpPath true &&
349 check fill "verbatim foo bar" <<-\EOF
361 verbatim: protocol=https
362 verbatim: host=example.com
363 verbatim: path=foo.git
367 test_expect_success 'helpers can abort the process' '
369 -c credential.helper=quit \
370 -c credential.helper="verbatim foo bar" \
371 credential fill >stdout 2>stderr <<-\EOF &&
376 test_cmp expect stdout &&
377 cat >expect <<-\EOF &&
380 quit: host=example.com
381 fatal: credential helper '\''quit'\'' told us to quit
383 test_i18ncmp expect stderr
386 test_expect_success 'empty helper spec resets helper list' '
387 test_config credential.helper "verbatim file file" &&
388 check fill "" "verbatim cmdline cmdline" <<-\EOF
398 verbatim: protocol=http
399 verbatim: host=example.com
403 test_expect_success 'url parser rejects embedded newlines' '
404 test_must_fail git credential fill 2>stderr <<-\EOF &&
405 url=https://one.example.com?%0ahost=two.example.com/
407 cat >expect <<-\EOF &&
408 warning: url contains a newline in its host component: https://one.example.com?%0ahost=two.example.com/
409 fatal: credential url cannot be parsed: https://one.example.com?%0ahost=two.example.com/
411 test_i18ncmp expect stderr
414 test_expect_success 'host-less URLs are parsed as empty host' '
415 check fill "verbatim foo bar" <<-\EOF
416 url=cert:///path/to/cert.pem
420 path=path/to/cert.pem
425 verbatim: protocol=cert
427 verbatim: path=path/to/cert.pem
431 test_expect_success 'credential system refuses to work with missing host' '
432 test_must_fail git credential fill 2>stderr <<-\EOF &&
435 cat >expect <<-\EOF &&
436 fatal: refusing to work with credential missing host field
438 test_i18ncmp expect stderr
441 test_expect_success 'credential system refuses to work with missing protocol' '
442 test_must_fail git credential fill 2>stderr <<-\EOF &&
445 cat >expect <<-\EOF &&
446 fatal: refusing to work with credential missing protocol field
448 test_i18ncmp expect stderr