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 user4
47 reject
$1 http path.tld user
48 reject
$1 https timeout.tld user
49 reject
$1 https sso.tld
57 ) | git
-c credential.helper
=$1 credential reject
63 test_expect_success
"helper ($HELPER) has no existing data" '
64 check fill $HELPER <<-\EOF
70 username=askpass-username
71 password=askpass-password
73 askpass: Username for '\''https://example.com'\'':
74 askpass: Password for '\''https://askpass-username@example.com'\'':
78 test_expect_success
"helper ($HELPER) stores password" '
79 check approve $HELPER <<-\EOF
87 test_expect_success
"helper ($HELPER) can retrieve password" '
88 check fill $HELPER <<-\EOF
100 test_expect_success
"helper ($HELPER) requires matching protocol" '
101 check fill $HELPER <<-\EOF
107 username=askpass-username
108 password=askpass-password
110 askpass: Username for '\''http://example.com'\'':
111 askpass: Password for '\''http://askpass-username@example.com'\'':
115 test_expect_success
"helper ($HELPER) requires matching host" '
116 check fill $HELPER <<-\EOF
122 username=askpass-username
123 password=askpass-password
125 askpass: Username for '\''https://other.tld'\'':
126 askpass: Password for '\''https://askpass-username@other.tld'\'':
130 test_expect_success
"helper ($HELPER) requires matching username" '
131 check fill $HELPER <<-\EOF
139 password=askpass-password
141 askpass: Password for '\''https://other@example.com'\'':
145 test_expect_success
"helper ($HELPER) requires matching path" '
146 test_config credential.usehttppath true &&
147 check approve $HELPER <<-\EOF &&
154 check fill $HELPER <<-\EOF
162 username=askpass-username
163 password=askpass-password
165 askpass: Username for '\''http://path.tld/bar.git'\'':
166 askpass: Password for '\''http://askpass-username@path.tld/bar.git'\'':
170 test_expect_success
"helper ($HELPER) can forget host" '
171 check reject $HELPER <<-\EOF &&
175 check fill $HELPER <<-\EOF
181 username=askpass-username
182 password=askpass-password
184 askpass: Username for '\''https://example.com'\'':
185 askpass: Password for '\''https://askpass-username@example.com'\'':
189 test_expect_success
"helper ($HELPER) can store multiple users" '
190 check approve $HELPER <<-\EOF &&
196 check approve $HELPER <<-\EOF &&
202 check fill $HELPER <<-\EOF &&
212 check fill $HELPER <<-\EOF
224 test_expect_success
"helper ($HELPER) can forget user" '
225 check reject $HELPER <<-\EOF &&
230 check fill $HELPER <<-\EOF
238 password=askpass-password
240 askpass: Password for '\''https://user1@example.com'\'':
244 test_expect_success
"helper ($HELPER) remembers other user" '
245 check fill $HELPER <<-\EOF
257 test_expect_success
"helper ($HELPER) can store empty username" '
258 check approve $HELPER <<-\EOF &&
264 check fill $HELPER <<-\EOF
275 : ${GIT_TEST_LONG_CRED_BUFFER:=1024}
276 # 23 bytes accounts for "wwwauth[]=basic realm=" plus NUL
277 LONG_VALUE_LEN
=$
((GIT_TEST_LONG_CRED_BUFFER
- 23))
278 LONG_VALUE
=$
(perl
-e 'print "a" x shift' $LONG_VALUE_LEN)
280 test_expect_success
"helper ($HELPER) not confused by long header" '
281 check approve $HELPER <<-\EOF &&
283 host=victim.example.com
285 password=to-be-stolen
288 check fill $HELPER <<-EOF
290 host=badguy.example.com
291 wwwauth[]=basic realm=${LONG_VALUE}host=victim.example.com
294 host=badguy.example.com
295 username=askpass-username
296 password=askpass-password
297 wwwauth[]=basic realm=${LONG_VALUE}host=victim.example.com
299 askpass: Username for '\''https://badguy.example.com'\'':
300 askpass: Password for '\''https://askpass-username@badguy.example.com'\'':
305 helper_test_timeout
() {
308 test_expect_success
"helper ($HELPER) times out" '
309 check approve "$HELPER" <<-\EOF &&
316 check fill "$HELPER" <<-\EOF
322 username=askpass-username
323 password=askpass-password
325 askpass: Username for '\''https://timeout.tld'\'':
326 askpass: Password for '\''https://askpass-username@timeout.tld'\'':
331 helper_test_oauth_refresh_token
() {
334 test_expect_success
"helper ($HELPER) stores oauth_refresh_token" '
335 check approve $HELPER <<-\EOF
340 oauth_refresh_token=xyzzy
344 test_expect_success
"helper ($HELPER) gets oauth_refresh_token" '
345 check fill $HELPER <<-\EOF
354 oauth_refresh_token=xyzzy
360 write_script askpass
<<\EOF
362 what
=$
(echo $1 | cut
-d" " -f1 |
tr A-Z a-z |
tr -cd a-z
)
365 GIT_ASKPASS
="$PWD/askpass"