1 # Shell library for testing credential handling including helpers. See t0302
2 # for an example of testing a specific helper.
4 # Try a set of credential helpers; the expected stdin,
5 # stdout and stderr should be provided on stdin,
12 credential_opts
="$credential_opts -c credential.helper='$arg'"
15 read_chunk
>expect-stdout
&&
16 read_chunk
>expect-stderr
&&
17 if ! eval "git $credential_opts credential $credential_cmd <stdin >stdout 2>stderr"; then
18 echo "git credential failed with code $?" &&
22 test_cmp expect-stdout stdout
&&
23 test_cmp expect-stderr stderr
35 # Clear any residual data from previous tests. We only
36 # need this when testing third-party helpers which read and
37 # write outside of our trash-directory sandbox.
39 # Don't bother checking for success here, as it is
40 # outside the scope of tests and represents a best effort to
41 # clean up after ourselves.
43 reject
$1 https example.com store-user
44 reject
$1 https example.com user1
45 reject
$1 https example.com user2
46 reject
$1 https example.com user-expiry
47 reject
$1 https example.com user-expiry-overwrite
48 reject
$1 https example.com user4
49 reject
$1 https example.com user-distinct-pass
50 reject
$1 https example.com user-overwrite
51 reject
$1 https example.com user-erase1
52 reject
$1 https example.com user-erase2
53 reject
$1 https victim.example.com user
54 reject
$1 http path.tld user
55 reject
$1 https timeout.tld user
56 reject
$1 https sso.tld
64 ) | git
-c credential.helper
=$1 credential reject
70 test_expect_success
"helper ($HELPER) has no existing data" '
71 check fill $HELPER <<-\EOF
77 username=askpass-username
78 password=askpass-password
80 askpass: Username for '\''https://example.com'\'':
81 askpass: Password for '\''https://askpass-username@example.com'\'':
85 test_expect_success
"helper ($HELPER) stores password" '
86 check approve $HELPER <<-\EOF
94 test_expect_success
"helper ($HELPER) can retrieve password" '
95 check fill $HELPER <<-\EOF
107 test_expect_success
"helper ($HELPER) requires matching protocol" '
108 check fill $HELPER <<-\EOF
114 username=askpass-username
115 password=askpass-password
117 askpass: Username for '\''http://example.com'\'':
118 askpass: Password for '\''http://askpass-username@example.com'\'':
122 test_expect_success
"helper ($HELPER) requires matching host" '
123 check fill $HELPER <<-\EOF
129 username=askpass-username
130 password=askpass-password
132 askpass: Username for '\''https://other.tld'\'':
133 askpass: Password for '\''https://askpass-username@other.tld'\'':
137 test_expect_success
"helper ($HELPER) requires matching username" '
138 check fill $HELPER <<-\EOF
146 password=askpass-password
148 askpass: Password for '\''https://other@example.com'\'':
152 test_expect_success
"helper ($HELPER) requires matching path" '
153 test_config credential.usehttppath true &&
154 check approve $HELPER <<-\EOF &&
161 check fill $HELPER <<-\EOF
169 username=askpass-username
170 password=askpass-password
172 askpass: Username for '\''http://path.tld/bar.git'\'':
173 askpass: Password for '\''http://askpass-username@path.tld/bar.git'\'':
177 test_expect_success
"helper ($HELPER) overwrites on store" '
178 check approve $HELPER <<-\EOF &&
181 username=user-overwrite
184 check approve $HELPER <<-\EOF &&
187 username=user-overwrite
190 check fill $HELPER <<-\EOF &&
193 username=user-overwrite
197 username=user-overwrite
200 check reject $HELPER <<-\EOF &&
203 username=user-overwrite
206 check fill $HELPER <<-\EOF
209 username=user-overwrite
213 username=user-overwrite
214 password=askpass-password
216 askpass: Password for '\''https://user-overwrite@example.com'\'':
220 test_expect_success
"helper ($HELPER) can forget host" '
221 check reject $HELPER <<-\EOF &&
225 check fill $HELPER <<-\EOF
231 username=askpass-username
232 password=askpass-password
234 askpass: Username for '\''https://example.com'\'':
235 askpass: Password for '\''https://askpass-username@example.com'\'':
239 test_expect_success
"helper ($HELPER) can store multiple users" '
240 check approve $HELPER <<-\EOF &&
246 check approve $HELPER <<-\EOF &&
252 check fill $HELPER <<-\EOF &&
262 check fill $HELPER <<-\EOF
274 test_expect_success
"helper ($HELPER) does not erase a password distinct from input" '
275 check approve $HELPER <<-\EOF &&
278 username=user-distinct-pass
281 check reject $HELPER <<-\EOF &&
284 username=user-distinct-pass
287 check fill $HELPER <<-\EOF
290 username=user-distinct-pass
294 username=user-distinct-pass
299 test_expect_success
"helper ($HELPER) can forget user" '
300 check reject $HELPER <<-\EOF &&
305 check fill $HELPER <<-\EOF
313 password=askpass-password
315 askpass: Password for '\''https://user1@example.com'\'':
319 test_expect_success
"helper ($HELPER) remembers other user" '
320 check fill $HELPER <<-\EOF
332 test_expect_success
"helper ($HELPER) can store empty username" '
333 check approve $HELPER <<-\EOF &&
339 check fill $HELPER <<-\EOF
350 test_expect_success
"helper ($HELPER) erases all matching credentials" '
351 check approve $HELPER <<-\EOF &&
357 check approve $HELPER <<-\EOF &&
363 check reject $HELPER <<-\EOF &&
367 check fill $HELPER <<-\EOF
373 username=askpass-username
374 password=askpass-password
376 askpass: Username for '\''https://example.com'\'':
377 askpass: Password for '\''https://askpass-username@example.com'\'':
381 : ${GIT_TEST_LONG_CRED_BUFFER:=1024}
382 # 23 bytes accounts for "wwwauth[]=basic realm=" plus NUL
383 LONG_VALUE_LEN
=$
((GIT_TEST_LONG_CRED_BUFFER
- 23))
384 LONG_VALUE
=$
(perl
-e 'print "a" x shift' $LONG_VALUE_LEN)
386 test_expect_success
"helper ($HELPER) not confused by long header" '
387 check approve $HELPER <<-\EOF &&
389 host=victim.example.com
391 password=to-be-stolen
394 check fill $HELPER <<-EOF
396 host=badguy.example.com
397 wwwauth[]=basic realm=${LONG_VALUE}host=victim.example.com
400 host=badguy.example.com
401 username=askpass-username
402 password=askpass-password
403 wwwauth[]=basic realm=${LONG_VALUE}host=victim.example.com
405 askpass: Username for '\''https://badguy.example.com'\'':
406 askpass: Password for '\''https://askpass-username@badguy.example.com'\'':
411 helper_test_timeout
() {
414 test_expect_success
"helper ($HELPER) times out" '
415 check approve "$HELPER" <<-\EOF &&
422 check fill "$HELPER" <<-\EOF
428 username=askpass-username
429 password=askpass-password
431 askpass: Username for '\''https://timeout.tld'\'':
432 askpass: Password for '\''https://askpass-username@timeout.tld'\'':
437 helper_test_password_expiry_utc
() {
440 test_expect_success
"helper ($HELPER) stores password_expiry_utc" '
441 check approve $HELPER <<-\EOF
446 password_expiry_utc=9999999999
450 test_expect_success
"helper ($HELPER) gets password_expiry_utc" '
451 check fill $HELPER <<-\EOF
460 password_expiry_utc=9999999999
465 test_expect_success
"helper ($HELPER) overwrites when password_expiry_utc changes" '
466 check approve $HELPER <<-\EOF &&
469 username=user-expiry-overwrite
471 password_expiry_utc=9999999998
473 check approve $HELPER <<-\EOF &&
476 username=user-expiry-overwrite
478 password_expiry_utc=9999999999
480 check fill $HELPER <<-\EOF &&
483 username=user-expiry-overwrite
487 username=user-expiry-overwrite
489 password_expiry_utc=9999999999
491 check reject $HELPER <<-\EOF &&
494 username=user-expiry-overwrite
497 check fill $HELPER <<-\EOF
500 username=user-expiry-overwrite
504 username=user-expiry-overwrite
505 password=askpass-password
507 askpass: Password for '\''https://user-expiry-overwrite@example.com'\'':
512 helper_test_oauth_refresh_token
() {
515 test_expect_success
"helper ($HELPER) stores oauth_refresh_token" '
516 check approve $HELPER <<-\EOF
521 oauth_refresh_token=xyzzy
525 test_expect_success
"helper ($HELPER) gets oauth_refresh_token" '
526 check fill $HELPER <<-\EOF
535 oauth_refresh_token=xyzzy
541 helper_test_authtype
() {
544 test_expect_success
"helper ($HELPER) stores authtype and credential" '
545 check approve $HELPER <<-\EOF
546 capability[]=authtype
548 credential=random-token
554 test_expect_success
"helper ($HELPER) gets authtype and credential" '
555 check fill $HELPER <<-\EOF
556 capability[]=authtype
560 capability[]=authtype
562 credential=random-token
569 test_expect_success
"helper ($HELPER) stores authtype and credential with username" '
570 check approve $HELPER <<-\EOF
571 capability[]=authtype
573 credential=other-token
580 test_expect_success
"helper ($HELPER) gets authtype and credential with username" '
581 check fill $HELPER <<-\EOF
582 capability[]=authtype
587 capability[]=authtype
589 credential=other-token
597 test_expect_success
"helper ($HELPER) does not get authtype and credential with different username" '
598 check fill $HELPER <<-\EOF
599 capability[]=authtype
607 password=askpass-password
609 askpass: Password for '\''https://barbaz@git.example.com'\'':
613 test_expect_success
"helper ($HELPER) does not store ephemeral authtype and credential" '
614 check approve $HELPER <<-\EOF &&
615 capability[]=authtype
617 credential=git2-token
619 host=git2.example.com
623 check fill $HELPER <<-\EOF
624 capability[]=authtype
626 host=git2.example.com
629 host=git2.example.com
630 username=askpass-username
631 password=askpass-password
633 askpass: Username for '\''https://git2.example.com'\'':
634 askpass: Password for '\''https://askpass-username@git2.example.com'\'':
638 test_expect_success
"helper ($HELPER) does not store ephemeral username and password" '
639 check approve $HELPER <<-\EOF &&
640 capability[]=authtype
642 host=git2.example.com
648 check fill $HELPER <<-\EOF
649 capability[]=authtype
651 host=git2.example.com
654 host=git2.example.com
655 username=askpass-username
656 password=askpass-password
658 askpass: Username for '\''https://git2.example.com'\'':
659 askpass: Password for '\''https://askpass-username@git2.example.com'\'':
664 write_script askpass
<<\EOF
666 what
=$
(echo $1 | cut
-d" " -f1 |
tr A-Z a-z |
tr -cd a-z
)
669 GIT_ASKPASS
="$PWD/askpass"