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 http path.tld user
54 reject
$1 https timeout.tld user
55 reject
$1 https sso.tld
63 ) | git
-c credential.helper
=$1 credential reject
69 test_expect_success
"helper ($HELPER) has no existing data" '
70 check fill $HELPER <<-\EOF
76 username=askpass-username
77 password=askpass-password
79 askpass: Username for '\''https://example.com'\'':
80 askpass: Password for '\''https://askpass-username@example.com'\'':
84 test_expect_success
"helper ($HELPER) stores password" '
85 check approve $HELPER <<-\EOF
93 test_expect_success
"helper ($HELPER) can retrieve password" '
94 check fill $HELPER <<-\EOF
106 test_expect_success
"helper ($HELPER) requires matching protocol" '
107 check fill $HELPER <<-\EOF
113 username=askpass-username
114 password=askpass-password
116 askpass: Username for '\''http://example.com'\'':
117 askpass: Password for '\''http://askpass-username@example.com'\'':
121 test_expect_success
"helper ($HELPER) requires matching host" '
122 check fill $HELPER <<-\EOF
128 username=askpass-username
129 password=askpass-password
131 askpass: Username for '\''https://other.tld'\'':
132 askpass: Password for '\''https://askpass-username@other.tld'\'':
136 test_expect_success
"helper ($HELPER) requires matching username" '
137 check fill $HELPER <<-\EOF
145 password=askpass-password
147 askpass: Password for '\''https://other@example.com'\'':
151 test_expect_success
"helper ($HELPER) requires matching path" '
152 test_config credential.usehttppath true &&
153 check approve $HELPER <<-\EOF &&
160 check fill $HELPER <<-\EOF
168 username=askpass-username
169 password=askpass-password
171 askpass: Username for '\''http://path.tld/bar.git'\'':
172 askpass: Password for '\''http://askpass-username@path.tld/bar.git'\'':
176 test_expect_success
"helper ($HELPER) overwrites on store" '
177 check approve $HELPER <<-\EOF &&
180 username=user-overwrite
183 check approve $HELPER <<-\EOF &&
186 username=user-overwrite
189 check fill $HELPER <<-\EOF &&
192 username=user-overwrite
196 username=user-overwrite
199 check reject $HELPER <<-\EOF &&
202 username=user-overwrite
205 check fill $HELPER <<-\EOF
208 username=user-overwrite
212 username=user-overwrite
213 password=askpass-password
215 askpass: Password for '\''https://user-overwrite@example.com'\'':
219 test_expect_success
"helper ($HELPER) can forget host" '
220 check reject $HELPER <<-\EOF &&
224 check fill $HELPER <<-\EOF
230 username=askpass-username
231 password=askpass-password
233 askpass: Username for '\''https://example.com'\'':
234 askpass: Password for '\''https://askpass-username@example.com'\'':
238 test_expect_success
"helper ($HELPER) can store multiple users" '
239 check approve $HELPER <<-\EOF &&
245 check approve $HELPER <<-\EOF &&
251 check fill $HELPER <<-\EOF &&
261 check fill $HELPER <<-\EOF
273 test_expect_success
"helper ($HELPER) does not erase a password distinct from input" '
274 check approve $HELPER <<-\EOF &&
277 username=user-distinct-pass
280 check reject $HELPER <<-\EOF &&
283 username=user-distinct-pass
286 check fill $HELPER <<-\EOF
289 username=user-distinct-pass
293 username=user-distinct-pass
298 test_expect_success
"helper ($HELPER) can forget user" '
299 check reject $HELPER <<-\EOF &&
304 check fill $HELPER <<-\EOF
312 password=askpass-password
314 askpass: Password for '\''https://user1@example.com'\'':
318 test_expect_success
"helper ($HELPER) remembers other user" '
319 check fill $HELPER <<-\EOF
331 test_expect_success
"helper ($HELPER) can store empty username" '
332 check approve $HELPER <<-\EOF &&
338 check fill $HELPER <<-\EOF
349 test_expect_success
"helper ($HELPER) erases all matching credentials" '
350 check approve $HELPER <<-\EOF &&
356 check approve $HELPER <<-\EOF &&
362 check reject $HELPER <<-\EOF &&
366 check fill $HELPER <<-\EOF
372 username=askpass-username
373 password=askpass-password
375 askpass: Username for '\''https://example.com'\'':
376 askpass: Password for '\''https://askpass-username@example.com'\'':
380 : ${GIT_TEST_LONG_CRED_BUFFER:=1024}
381 # 23 bytes accounts for "wwwauth[]=basic realm=" plus NUL
382 LONG_VALUE_LEN
=$
((GIT_TEST_LONG_CRED_BUFFER
- 23))
383 LONG_VALUE
=$
(perl
-e 'print "a" x shift' $LONG_VALUE_LEN)
385 test_expect_success
"helper ($HELPER) not confused by long header" '
386 check approve $HELPER <<-\EOF &&
388 host=victim.example.com
390 password=to-be-stolen
393 check fill $HELPER <<-EOF
395 host=badguy.example.com
396 wwwauth[]=basic realm=${LONG_VALUE}host=victim.example.com
399 host=badguy.example.com
400 username=askpass-username
401 password=askpass-password
402 wwwauth[]=basic realm=${LONG_VALUE}host=victim.example.com
404 askpass: Username for '\''https://badguy.example.com'\'':
405 askpass: Password for '\''https://askpass-username@badguy.example.com'\'':
410 helper_test_timeout
() {
413 test_expect_success
"helper ($HELPER) times out" '
414 check approve "$HELPER" <<-\EOF &&
421 check fill "$HELPER" <<-\EOF
427 username=askpass-username
428 password=askpass-password
430 askpass: Username for '\''https://timeout.tld'\'':
431 askpass: Password for '\''https://askpass-username@timeout.tld'\'':
436 helper_test_password_expiry_utc
() {
439 test_expect_success
"helper ($HELPER) stores password_expiry_utc" '
440 check approve $HELPER <<-\EOF
445 password_expiry_utc=9999999999
449 test_expect_success
"helper ($HELPER) gets password_expiry_utc" '
450 check fill $HELPER <<-\EOF
459 password_expiry_utc=9999999999
464 test_expect_success
"helper ($HELPER) overwrites when password_expiry_utc changes" '
465 check approve $HELPER <<-\EOF &&
468 username=user-expiry-overwrite
470 password_expiry_utc=9999999998
472 check approve $HELPER <<-\EOF &&
475 username=user-expiry-overwrite
477 password_expiry_utc=9999999999
479 check fill $HELPER <<-\EOF &&
482 username=user-expiry-overwrite
486 username=user-expiry-overwrite
488 password_expiry_utc=9999999999
490 check reject $HELPER <<-\EOF &&
493 username=user-expiry-overwrite
496 check fill $HELPER <<-\EOF
499 username=user-expiry-overwrite
503 username=user-expiry-overwrite
504 password=askpass-password
506 askpass: Password for '\''https://user-expiry-overwrite@example.com'\'':
511 helper_test_oauth_refresh_token
() {
514 test_expect_success
"helper ($HELPER) stores oauth_refresh_token" '
515 check approve $HELPER <<-\EOF
520 oauth_refresh_token=xyzzy
524 test_expect_success
"helper ($HELPER) gets oauth_refresh_token" '
525 check fill $HELPER <<-\EOF
534 oauth_refresh_token=xyzzy
540 write_script askpass
<<\EOF
542 what
=$
(echo $1 | cut
-d" " -f1 |
tr A-Z a-z |
tr -cd a-z
)
545 GIT_ASKPASS
="$PWD/askpass"