t030[02]: work around CR/LF issue
[git/dscho.git] / t / lib-credential.sh
blob66dc4fd6c9d51901bdc5fafa9ca686be4fa331f9
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 if test_have_prereq MINGW
13 then
14 dos2unix stderr
15 fi &&
16 test_cmp expect-stderr stderr
19 read_chunk() {
20 while read line; do
21 case "$line" in
22 --) break ;;
23 *) echo "$line" ;;
24 esac
25 done
28 # Clear any residual data from previous tests. We only
29 # need this when testing third-party helpers which read and
30 # write outside of our trash-directory sandbox.
32 # Don't bother checking for success here, as it is
33 # outside the scope of tests and represents a best effort to
34 # clean up after ourselves.
35 helper_test_clean() {
36 reject $1 https example.com store-user
37 reject $1 https example.com user1
38 reject $1 https example.com user2
39 reject $1 http path.tld user
40 reject $1 https timeout.tld user
43 reject() {
45 echo protocol=$2
46 echo host=$3
47 echo username=$4
48 ) | test-credential reject $1
51 helper_test() {
52 HELPER=$1
54 test_expect_success "helper ($HELPER) has no existing data" '
55 check fill $HELPER <<-\EOF
56 protocol=https
57 host=example.com
59 username=askpass-username
60 password=askpass-password
62 askpass: Username for '\''https://example.com'\'':
63 askpass: Password for '\''https://askpass-username@example.com'\'':
64 EOF
67 test_expect_success "helper ($HELPER) stores password" '
68 check approve $HELPER <<-\EOF
69 protocol=https
70 host=example.com
71 username=store-user
72 password=store-pass
73 EOF
76 test_expect_success "helper ($HELPER) can retrieve password" '
77 check fill $HELPER <<-\EOF
78 protocol=https
79 host=example.com
81 username=store-user
82 password=store-pass
84 EOF
87 test_expect_success "helper ($HELPER) requires matching protocol" '
88 check fill $HELPER <<-\EOF
89 protocol=http
90 host=example.com
92 username=askpass-username
93 password=askpass-password
95 askpass: Username for '\''http://example.com'\'':
96 askpass: Password for '\''http://askpass-username@example.com'\'':
97 EOF
100 test_expect_success "helper ($HELPER) requires matching host" '
101 check fill $HELPER <<-\EOF
102 protocol=https
103 host=other.tld
105 username=askpass-username
106 password=askpass-password
108 askpass: Username for '\''https://other.tld'\'':
109 askpass: Password for '\''https://askpass-username@other.tld'\'':
113 test_expect_success "helper ($HELPER) requires matching username" '
114 check fill $HELPER <<-\EOF
115 protocol=https
116 host=example.com
117 username=other
119 username=other
120 password=askpass-password
122 askpass: Password for '\''https://other@example.com'\'':
126 test_expect_success "helper ($HELPER) requires matching path" '
127 test_config credential.usehttppath true &&
128 check approve $HELPER <<-\EOF &&
129 protocol=http
130 host=path.tld
131 path=foo.git
132 username=user
133 password=pass
135 check fill $HELPER <<-\EOF
136 protocol=http
137 host=path.tld
138 path=bar.git
140 username=askpass-username
141 password=askpass-password
143 askpass: Username for '\''http://path.tld/bar.git'\'':
144 askpass: Password for '\''http://askpass-username@path.tld/bar.git'\'':
148 test_expect_success "helper ($HELPER) can forget host" '
149 check reject $HELPER <<-\EOF &&
150 protocol=https
151 host=example.com
153 check fill $HELPER <<-\EOF
154 protocol=https
155 host=example.com
157 username=askpass-username
158 password=askpass-password
160 askpass: Username for '\''https://example.com'\'':
161 askpass: Password for '\''https://askpass-username@example.com'\'':
165 test_expect_success "helper ($HELPER) can store multiple users" '
166 check approve $HELPER <<-\EOF &&
167 protocol=https
168 host=example.com
169 username=user1
170 password=pass1
172 check approve $HELPER <<-\EOF &&
173 protocol=https
174 host=example.com
175 username=user2
176 password=pass2
178 check fill $HELPER <<-\EOF &&
179 protocol=https
180 host=example.com
181 username=user1
183 username=user1
184 password=pass1
186 check fill $HELPER <<-\EOF
187 protocol=https
188 host=example.com
189 username=user2
191 username=user2
192 password=pass2
196 test_expect_success "helper ($HELPER) can forget user" '
197 check reject $HELPER <<-\EOF &&
198 protocol=https
199 host=example.com
200 username=user1
202 check fill $HELPER <<-\EOF
203 protocol=https
204 host=example.com
205 username=user1
207 username=user1
208 password=askpass-password
210 askpass: Password for '\''https://user1@example.com'\'':
214 test_expect_success "helper ($HELPER) remembers other user" '
215 check fill $HELPER <<-\EOF
216 protocol=https
217 host=example.com
218 username=user2
220 username=user2
221 password=pass2
226 helper_test_timeout() {
227 HELPER="$*"
229 test_expect_success "helper ($HELPER) times out" '
230 check approve "$HELPER" <<-\EOF &&
231 protocol=https
232 host=timeout.tld
233 username=user
234 password=pass
236 sleep 2 &&
237 check fill "$HELPER" <<-\EOF
238 protocol=https
239 host=timeout.tld
241 username=askpass-username
242 password=askpass-password
244 askpass: Username for '\''https://timeout.tld'\'':
245 askpass: Password for '\''https://askpass-username@timeout.tld'\'':
250 cat >askpass <<\EOF
251 #!/bin/sh
252 echo >&2 askpass: $*
253 what=`echo $1 | cut -d" " -f1 | tr A-Z a-z | tr -cd a-z`
254 echo "askpass-$what"
256 chmod +x askpass
257 GIT_ASKPASS="$PWD/askpass"
258 export GIT_ASKPASS