1#!/bin/sh
2
3# Try a set of credential helpers; the expected stdin,
4# stdout and stderr should be provided on stdin,
5# separated by "--".
6check() {
7 read_chunk >stdin &&
8 read_chunk >expect-stdout &&
9 read_chunk >expect-stderr &&
10 test-credential "$@" <stdin >stdout 2>stderr &&
11 test_cmp expect-stdout stdout &&
12 test_cmp expect-stderr stderr
13}
14
15read_chunk() {
16 while read line; do
17 case "$line" in
18 --) break ;;
19 *) echo "$line" ;;
20 esac
21 done
22}
23
24# Clear any residual data from previous tests. We only
25# need this when testing third-party helpers which read and
26# write outside of our trash-directory sandbox.
27#
28# Don't bother checking for success here, as it is
29# outside the scope of tests and represents a best effort to
30# clean up after ourselves.
31helper_test_clean() {
32 reject $1 https example.com store-user
33 reject $1 https example.com user1
34 reject $1 https example.com user2
35 reject $1 http path.tld user
36 reject $1 https timeout.tld user
37}
38
39reject() {
40 (
41 echo protocol=$2
42 echo host=$3
43 echo username=$4
44 ) | test-credential reject $1
45}
46
47helper_test() {
48 HELPER=$1
49
50 test_expect_success "helper ($HELPER) has no existing data" '
51 check fill $HELPER <<-\EOF
52 protocol=https
53 host=example.com
54 --
55 username=askpass-username
56 password=askpass-password
57 --
58 askpass: Username for '\''https://example.com'\'':
59 askpass: Password for '\''https://askpass-username@example.com'\'':
60 EOF
61 '
62
63 test_expect_success "helper ($HELPER) stores password" '
64 check approve $HELPER <<-\EOF
65 protocol=https
66 host=example.com
67 username=store-user
68 password=store-pass
69 EOF
70 '
71
72 test_expect_success "helper ($HELPER) can retrieve password" '
73 check fill $HELPER <<-\EOF
74 protocol=https
75 host=example.com
76 --
77 username=store-user
78 password=store-pass
79 --
80 EOF
81 '
82
83 test_expect_success "helper ($HELPER) requires matching protocol" '
84 check fill $HELPER <<-\EOF
85 protocol=http
86 host=example.com
87 --
88 username=askpass-username
89 password=askpass-password
90 --
91 askpass: Username for '\''http://example.com'\'':
92 askpass: Password for '\''http://askpass-username@example.com'\'':
93 EOF
94 '
95
96 test_expect_success "helper ($HELPER) requires matching host" '
97 check fill $HELPER <<-\EOF
98 protocol=https
99 host=other.tld
100 --
101 username=askpass-username
102 password=askpass-password
103 --
104 askpass: Username for '\''https://other.tld'\'':
105 askpass: Password for '\''https://askpass-username@other.tld'\'':
106 EOF
107 '
108
109 test_expect_success "helper ($HELPER) requires matching username" '
110 check fill $HELPER <<-\EOF
111 protocol=https
112 host=example.com
113 username=other
114 --
115 username=other
116 password=askpass-password
117 --
118 askpass: Password for '\''https://other@example.com'\'':
119 EOF
120 '
121
122 test_expect_success "helper ($HELPER) requires matching path" '
123 test_config credential.usehttppath true &&
124 check approve $HELPER <<-\EOF &&
125 protocol=http
126 host=path.tld
127 path=foo.git
128 username=user
129 password=pass
130 EOF
131 check fill $HELPER <<-\EOF
132 protocol=http
133 host=path.tld
134 path=bar.git
135 --
136 username=askpass-username
137 password=askpass-password
138 --
139 askpass: Username for '\''http://path.tld/bar.git'\'':
140 askpass: Password for '\''http://askpass-username@path.tld/bar.git'\'':
141 EOF
142 '
143
144 test_expect_success "helper ($HELPER) can forget host" '
145 check reject $HELPER <<-\EOF &&
146 protocol=https
147 host=example.com
148 EOF
149 check fill $HELPER <<-\EOF
150 protocol=https
151 host=example.com
152 --
153 username=askpass-username
154 password=askpass-password
155 --
156 askpass: Username for '\''https://example.com'\'':
157 askpass: Password for '\''https://askpass-username@example.com'\'':
158 EOF
159 '
160
161 test_expect_success "helper ($HELPER) can store multiple users" '
162 check approve $HELPER <<-\EOF &&
163 protocol=https
164 host=example.com
165 username=user1
166 password=pass1
167 EOF
168 check approve $HELPER <<-\EOF &&
169 protocol=https
170 host=example.com
171 username=user2
172 password=pass2
173 EOF
174 check fill $HELPER <<-\EOF &&
175 protocol=https
176 host=example.com
177 username=user1
178 --
179 username=user1
180 password=pass1
181 EOF
182 check fill $HELPER <<-\EOF
183 protocol=https
184 host=example.com
185 username=user2
186 --
187 username=user2
188 password=pass2
189 EOF
190 '
191
192 test_expect_success "helper ($HELPER) can forget user" '
193 check reject $HELPER <<-\EOF &&
194 protocol=https
195 host=example.com
196 username=user1
197 EOF
198 check fill $HELPER <<-\EOF
199 protocol=https
200 host=example.com
201 username=user1
202 --
203 username=user1
204 password=askpass-password
205 --
206 askpass: Password for '\''https://user1@example.com'\'':
207 EOF
208 '
209
210 test_expect_success "helper ($HELPER) remembers other user" '
211 check fill $HELPER <<-\EOF
212 protocol=https
213 host=example.com
214 username=user2
215 --
216 username=user2
217 password=pass2
218 EOF
219 '
220}
221
222helper_test_timeout() {
223 HELPER="$*"
224
225 test_expect_success "helper ($HELPER) times out" '
226 check approve "$HELPER" <<-\EOF &&
227 protocol=https
228 host=timeout.tld
229 username=user
230 password=pass
231 EOF
232 sleep 2 &&
233 check fill "$HELPER" <<-\EOF
234 protocol=https
235 host=timeout.tld
236 --
237 username=askpass-username
238 password=askpass-password
239 --
240 askpass: Username for '\''https://timeout.tld'\'':
241 askpass: Password for '\''https://askpass-username@timeout.tld'\'':
242 EOF
243 '
244}
245
246cat >askpass <<\EOF
247#!/bin/sh
248echo >&2 askpass: $*
249what=`echo $1 | cut -d" " -f1 | tr A-Z a-z | tr -cd a-z`
250echo "askpass-$what"
251EOF
252chmod +x askpass
253GIT_ASKPASS="$PWD/askpass"
254export GIT_ASKPASS