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 'match multiple configured helpers' '
319 test_config credential.helper "verbatim \"\" \"\"" &&
320 test_config credential.https://example.com.helper "$HELPER" &&
332 verbatim: protocol=https
333 verbatim: host=example.com
337 test_expect_success 'match multiple configured helpers with URLs' '
338 test_config credential.https://example.com/repo.git.helper "verbatim \"\" \"\"" &&
339 test_config credential.https://example.com.helper "$HELPER" &&
351 verbatim: protocol=https
352 verbatim: host=example.com
356 test_expect_success 'match percent-encoded values' '
357 test_config credential.https://example.com/%2566.git.helper "$HELPER" &&
359 url=https://example.com/%2566.git
369 test_expect_success 'match percent-encoded UTF-8 values in path' '
370 test_config credential.https://example.com.useHttpPath true &&
371 test_config credential.https://example.com/perú.git.helper "$HELPER" &&
373 url=https://example.com/per%C3%BA.git
384 test_expect_success 'match percent-encoded values in username' '
385 test_config credential.https://user%2fname@example.com/foo/bar.git.helper "$HELPER" &&
387 url=https://user%2fname@example.com/foo/bar.git
397 test_expect_success 'fetch with multiple path components' '
398 test_unconfig credential.helper &&
399 test_config credential.https://example.com/foo/repo.git.helper "verbatim foo bar" &&
401 url=https://example.com/foo/repo.git
409 verbatim: protocol=https
410 verbatim: host=example.com
414 test_expect_success 'pull username from config' '
415 test_config credential.https://example.com.username foo &&
423 password=askpass-password
425 askpass: Password for '\''https://foo@example.com'\'':
429 test_expect_success 'honors username from URL over helper (URL)' '
430 test_config credential.https://example.com.username bob &&
431 test_config credential.https://example.com.helper "verbatim \"\" bar" &&
433 url=https://alice@example.com
441 verbatim: protocol=https
442 verbatim: host=example.com
443 verbatim: username=alice
447 test_expect_success 'honors username from URL over helper (components)' '
448 test_config credential.https://example.com.username bob &&
449 test_config credential.https://example.com.helper "verbatim \"\" bar" &&
461 verbatim: protocol=https
462 verbatim: host=example.com
463 verbatim: username=alice
467 test_expect_success 'last matching username wins' '
468 test_config credential.https://example.com/path.git.username bob &&
469 test_config credential.https://example.com.username alice &&
470 test_config credential.https://example.com.helper "verbatim \"\" bar" &&
472 url=https://example.com/path.git
480 verbatim: protocol=https
481 verbatim: host=example.com
482 verbatim: username=alice
486 test_expect_success 'http paths can be part of context' '
487 check fill "verbatim foo bar" <<-\EOF &&
498 verbatim: protocol=https
499 verbatim: host=example.com
501 test_config credential.https://example.com.useHttpPath true &&
502 check fill "verbatim foo bar" <<-\EOF
514 verbatim: protocol=https
515 verbatim: host=example.com
516 verbatim: path=foo.git
520 test_expect_success 'context uses urlmatch' '
521 test_config "credential.https://*.org.useHttpPath" true &&
522 check fill "verbatim foo bar" <<-\EOF
534 verbatim: protocol=https
535 verbatim: host=example.org
536 verbatim: path=foo.git
540 test_expect_success 'helpers can abort the process' '
542 -c credential.helper=quit \
543 -c credential.helper="verbatim foo bar" \
544 credential fill >stdout 2>stderr <<-\EOF &&
548 test_must_be_empty stdout &&
549 cat >expect <<-\EOF &&
552 quit: host=example.com
553 fatal: credential helper '\''quit'\'' told us to quit
555 test_i18ncmp expect stderr
558 test_expect_success 'empty helper spec resets helper list' '
559 test_config credential.helper "verbatim file file" &&
560 check fill "" "verbatim cmdline cmdline" <<-\EOF
570 verbatim: protocol=http
571 verbatim: host=example.com
575 test_expect_success 'url parser rejects embedded newlines' '
576 test_must_fail git credential fill 2>stderr <<-\EOF &&
577 url=https://one.example.com?%0ahost=two.example.com/
579 cat >expect <<-\EOF &&
580 warning: url contains a newline in its path component: https://one.example.com?%0ahost=two.example.com/
581 fatal: credential url cannot be parsed: https://one.example.com?%0ahost=two.example.com/
583 test_i18ncmp expect stderr
586 test_expect_success 'host-less URLs are parsed as empty host' '
587 check fill "verbatim foo bar" <<-\EOF
588 url=cert:///path/to/cert.pem
592 path=path/to/cert.pem
597 verbatim: protocol=cert
599 verbatim: path=path/to/cert.pem
603 test_expect_success 'credential system refuses to work with missing host' '
604 test_must_fail git credential fill 2>stderr <<-\EOF &&
607 cat >expect <<-\EOF &&
608 fatal: refusing to work with credential missing host field
610 test_i18ncmp expect stderr
613 test_expect_success 'credential system refuses to work with missing protocol' '
614 test_must_fail git credential fill 2>stderr <<-\EOF &&
617 cat >expect <<-\EOF &&
618 fatal: refusing to work with credential missing protocol field
620 test_i18ncmp expect stderr
623 # usage: check_host_and_path <url> <expected-host> <expected-path>
624 check_host_and_path () {
625 # we always parse the path component, but we need this to make sure it
626 # is passed to the helper
627 test_config credential.useHTTPPath true &&
628 check fill "verbatim user pass" <<-EOF
638 verbatim: protocol=https
644 test_expect_success 'url parser handles bare query marker' '
645 check_host_and_path https://example.com?foo.git example.com ?foo.git
648 test_expect_success 'url parser handles bare fragment marker' '
649 check_host_and_path https://example.com#foo.git example.com "#foo.git"
652 test_expect_success 'url parser not confused by encoded markers' '
653 check_host_and_path https://example.com%23%3f%2f/foo.git \
654 "example.com#?/" foo.git
657 test_expect_success 'credential config with partial URLs' '
658 echo "echo password=yep" | write_script git-credential-yep &&
659 test_write_lines url=https://user@example.com/repo.git >stdin &&
664 https://example.com \
665 https://example.com/ \
666 https://user@example.com \
667 https://user@example.com/ \
668 https://example.com/repo.git \
669 https://user@example.com/repo.git \
672 git -c credential.$partial.helper=yep \
673 credential fill <stdin >stdout &&
683 git -c credential.$partial.helper=yep \
684 credential fill <stdin >stdout &&
689 git -c credential.$partial.helper=yep \
690 -c credential.with%0anewline.username=uh-oh \
691 credential fill <stdin >stdout 2>stderr &&
692 test_i18ngrep "skipping credential lookup for key" stderr