Merge branch 'rl/show-empty-prefix' into maint
[git/raj.git] / t / lib-credential.sh
blob4a37cd79e585e86572b5bc6f127bbee33af998b3
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 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
15 read_chunk() {
16 while read line; do
17 case "$line" in
18 --) break ;;
19 *) echo "$line" ;;
20 esac
21 done
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.
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.
31 helper_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
39 reject() {
41 echo protocol=$2
42 echo host=$3
43 echo username=$4
44 ) | test-credential reject $1
47 helper_test() {
48 HELPER=$1
50 test_expect_success "helper ($HELPER) has no existing data" '
51 check fill $HELPER <<-\EOF
52 protocol=https
53 host=example.com
55 username=askpass-username
56 password=askpass-password
58 askpass: Username for '\''https://example.com'\'':
59 askpass: Password for '\''https://askpass-username@example.com'\'':
60 EOF
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
72 test_expect_success "helper ($HELPER) can retrieve password" '
73 check fill $HELPER <<-\EOF
74 protocol=https
75 host=example.com
77 username=store-user
78 password=store-pass
80 EOF
83 test_expect_success "helper ($HELPER) requires matching protocol" '
84 check fill $HELPER <<-\EOF
85 protocol=http
86 host=example.com
88 username=askpass-username
89 password=askpass-password
91 askpass: Username for '\''http://example.com'\'':
92 askpass: Password for '\''http://askpass-username@example.com'\'':
93 EOF
96 test_expect_success "helper ($HELPER) requires matching host" '
97 check fill $HELPER <<-\EOF
98 protocol=https
99 host=other.tld
101 username=askpass-username
102 password=askpass-password
104 askpass: Username for '\''https://other.tld'\'':
105 askpass: Password for '\''https://askpass-username@other.tld'\'':
109 test_expect_success "helper ($HELPER) requires matching username" '
110 check fill $HELPER <<-\EOF
111 protocol=https
112 host=example.com
113 username=other
115 username=other
116 password=askpass-password
118 askpass: Password for '\''https://other@example.com'\'':
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
131 check fill $HELPER <<-\EOF
132 protocol=http
133 host=path.tld
134 path=bar.git
136 username=askpass-username
137 password=askpass-password
139 askpass: Username for '\''http://path.tld/bar.git'\'':
140 askpass: Password for '\''http://askpass-username@path.tld/bar.git'\'':
144 test_expect_success "helper ($HELPER) can forget host" '
145 check reject $HELPER <<-\EOF &&
146 protocol=https
147 host=example.com
149 check fill $HELPER <<-\EOF
150 protocol=https
151 host=example.com
153 username=askpass-username
154 password=askpass-password
156 askpass: Username for '\''https://example.com'\'':
157 askpass: Password for '\''https://askpass-username@example.com'\'':
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
168 check approve $HELPER <<-\EOF &&
169 protocol=https
170 host=example.com
171 username=user2
172 password=pass2
174 check fill $HELPER <<-\EOF &&
175 protocol=https
176 host=example.com
177 username=user1
179 username=user1
180 password=pass1
182 check fill $HELPER <<-\EOF
183 protocol=https
184 host=example.com
185 username=user2
187 username=user2
188 password=pass2
192 test_expect_success "helper ($HELPER) can forget user" '
193 check reject $HELPER <<-\EOF &&
194 protocol=https
195 host=example.com
196 username=user1
198 check fill $HELPER <<-\EOF
199 protocol=https
200 host=example.com
201 username=user1
203 username=user1
204 password=askpass-password
206 askpass: Password for '\''https://user1@example.com'\'':
210 test_expect_success "helper ($HELPER) remembers other user" '
211 check fill $HELPER <<-\EOF
212 protocol=https
213 host=example.com
214 username=user2
216 username=user2
217 password=pass2
222 helper_test_timeout() {
223 HELPER="$*"
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
232 sleep 2 &&
233 check fill "$HELPER" <<-\EOF
234 protocol=https
235 host=timeout.tld
237 username=askpass-username
238 password=askpass-password
240 askpass: Username for '\''https://timeout.tld'\'':
241 askpass: Password for '\''https://askpass-username@timeout.tld'\'':
246 cat >askpass <<\EOF
247 #!/bin/sh
248 echo >&2 askpass: $*
249 what=`echo $1 | cut -d" " -f1 | tr A-Z a-z | tr -cd a-z`
250 echo "askpass-$what"
252 chmod +x askpass
253 GIT_ASKPASS="$PWD/askpass"
254 export GIT_ASKPASS