t030[02]: work around CR/LF issue
[git/dscho.git] / t / lib-credential.sh
blob8a2c7cc6fffb06115c3fda319b571786495ac688
1 #!/bin/sh
3 # Try a set of credential helpers; the expected stdin,
4 # stdout and stderr should be provided on stdin,
5 # separated by "--".
6 check() {
7 credential_opts=
8 credential_cmd=$1
9 shift
10 for arg in "$@"; do
11 credential_opts="$credential_opts -c credential.helper='$arg'"
12 done
13 read_chunk >stdin &&
14 read_chunk >expect-stdout &&
15 read_chunk >expect-stderr &&
16 if ! eval "git $credential_opts credential $credential_cmd <stdin >stdout 2>stderr"; then
17 echo "git credential failed with code $?" &&
18 cat stderr &&
19 false
20 fi &&
21 if test_have_prereq MINGW
22 then
23 dos2unix -q stderr
24 fi &&
25 test_cmp expect-stdout stdout &&
26 if test_have_prereq MINGW
27 then
28 dos2unix stderr
29 fi &&
30 test_cmp expect-stderr stderr
33 read_chunk() {
34 while read line; do
35 case "$line" in
36 --) break ;;
37 *) echo "$line" ;;
38 esac
39 done
42 # Clear any residual data from previous tests. We only
43 # need this when testing third-party helpers which read and
44 # write outside of our trash-directory sandbox.
46 # Don't bother checking for success here, as it is
47 # outside the scope of tests and represents a best effort to
48 # clean up after ourselves.
49 helper_test_clean() {
50 reject $1 https example.com store-user
51 reject $1 https example.com user1
52 reject $1 https example.com user2
53 reject $1 http path.tld user
54 reject $1 https timeout.tld user
57 reject() {
59 echo protocol=$2
60 echo host=$3
61 echo username=$4
62 ) | git -c credential.helper=$1 credential reject
65 helper_test() {
66 HELPER=$1
68 test_expect_success "helper ($HELPER) has no existing data" '
69 check fill $HELPER <<-\EOF
70 protocol=https
71 host=example.com
73 protocol=https
74 host=example.com
75 username=askpass-username
76 password=askpass-password
78 askpass: Username for '\''https://example.com'\'':
79 askpass: Password for '\''https://askpass-username@example.com'\'':
80 EOF
83 test_expect_success "helper ($HELPER) stores password" '
84 check approve $HELPER <<-\EOF
85 protocol=https
86 host=example.com
87 username=store-user
88 password=store-pass
89 EOF
92 test_expect_success "helper ($HELPER) can retrieve password" '
93 check fill $HELPER <<-\EOF
94 protocol=https
95 host=example.com
97 protocol=https
98 host=example.com
99 username=store-user
100 password=store-pass
105 test_expect_success "helper ($HELPER) requires matching protocol" '
106 check fill $HELPER <<-\EOF
107 protocol=http
108 host=example.com
110 protocol=http
111 host=example.com
112 username=askpass-username
113 password=askpass-password
115 askpass: Username for '\''http://example.com'\'':
116 askpass: Password for '\''http://askpass-username@example.com'\'':
120 test_expect_success "helper ($HELPER) requires matching host" '
121 check fill $HELPER <<-\EOF
122 protocol=https
123 host=other.tld
125 protocol=https
126 host=other.tld
127 username=askpass-username
128 password=askpass-password
130 askpass: Username for '\''https://other.tld'\'':
131 askpass: Password for '\''https://askpass-username@other.tld'\'':
135 test_expect_success "helper ($HELPER) requires matching username" '
136 check fill $HELPER <<-\EOF
137 protocol=https
138 host=example.com
139 username=other
141 protocol=https
142 host=example.com
143 username=other
144 password=askpass-password
146 askpass: Password for '\''https://other@example.com'\'':
150 test_expect_success "helper ($HELPER) requires matching path" '
151 test_config credential.usehttppath true &&
152 check approve $HELPER <<-\EOF &&
153 protocol=http
154 host=path.tld
155 path=foo.git
156 username=user
157 password=pass
159 check fill $HELPER <<-\EOF
160 protocol=http
161 host=path.tld
162 path=bar.git
164 protocol=http
165 host=path.tld
166 path=bar.git
167 username=askpass-username
168 password=askpass-password
170 askpass: Username for '\''http://path.tld/bar.git'\'':
171 askpass: Password for '\''http://askpass-username@path.tld/bar.git'\'':
175 test_expect_success "helper ($HELPER) can forget host" '
176 check reject $HELPER <<-\EOF &&
177 protocol=https
178 host=example.com
180 check fill $HELPER <<-\EOF
181 protocol=https
182 host=example.com
184 protocol=https
185 host=example.com
186 username=askpass-username
187 password=askpass-password
189 askpass: Username for '\''https://example.com'\'':
190 askpass: Password for '\''https://askpass-username@example.com'\'':
194 test_expect_success "helper ($HELPER) can store multiple users" '
195 check approve $HELPER <<-\EOF &&
196 protocol=https
197 host=example.com
198 username=user1
199 password=pass1
201 check approve $HELPER <<-\EOF &&
202 protocol=https
203 host=example.com
204 username=user2
205 password=pass2
207 check fill $HELPER <<-\EOF &&
208 protocol=https
209 host=example.com
210 username=user1
212 protocol=https
213 host=example.com
214 username=user1
215 password=pass1
217 check fill $HELPER <<-\EOF
218 protocol=https
219 host=example.com
220 username=user2
222 protocol=https
223 host=example.com
224 username=user2
225 password=pass2
229 test_expect_success "helper ($HELPER) can forget user" '
230 check reject $HELPER <<-\EOF &&
231 protocol=https
232 host=example.com
233 username=user1
235 check fill $HELPER <<-\EOF
236 protocol=https
237 host=example.com
238 username=user1
240 protocol=https
241 host=example.com
242 username=user1
243 password=askpass-password
245 askpass: Password for '\''https://user1@example.com'\'':
249 test_expect_success "helper ($HELPER) remembers other user" '
250 check fill $HELPER <<-\EOF
251 protocol=https
252 host=example.com
253 username=user2
255 protocol=https
256 host=example.com
257 username=user2
258 password=pass2
263 helper_test_timeout() {
264 HELPER="$*"
266 test_expect_success "helper ($HELPER) times out" '
267 check approve "$HELPER" <<-\EOF &&
268 protocol=https
269 host=timeout.tld
270 username=user
271 password=pass
273 sleep 2 &&
274 check fill "$HELPER" <<-\EOF
275 protocol=https
276 host=timeout.tld
278 protocol=https
279 host=timeout.tld
280 username=askpass-username
281 password=askpass-password
283 askpass: Username for '\''https://timeout.tld'\'':
284 askpass: Password for '\''https://askpass-username@timeout.tld'\'':
289 cat >askpass <<\EOF
290 #!/bin/sh
291 echo >&2 askpass: $*
292 what=`echo $1 | cut -d" " -f1 | tr A-Z a-z | tr -cd a-z`
293 echo "askpass-$what"
295 chmod +x askpass
296 GIT_ASKPASS="$PWD/askpass"
297 export GIT_ASKPASS