Imported Upstream version 2.5.1
[scm/test.git] / t / t-credentials.sh
1 #!/usr/bin/env bash
2
3 . "$(dirname "$0")/testlib.sh"
4
5 ensure_git_version_isnt $VERSION_LOWER "2.3.0"
6
7 begin_test "credentails with url-specific helper skips askpass"
8 (
9   set -e
10
11   reponame="url-specific-helper"
12   setup_remote_repo "$reponame"
13
14   clone_repo "$reponame" "$reponame"
15   git config credential.useHttpPath false
16   git config credential.helper ""
17   git config credential.$GITSERVER.helper "lfstest"
18
19   git lfs track "*.dat"
20   echo "hello" > a.dat
21
22   git add .gitattributes a.dat
23   git commit -m "initial commit"
24
25   # askpass is skipped
26   GIT_ASKPASS="lfs-bad-cmd" GIT_TRACE=1 git push origin master 2>&1 | tee push.log
27
28   [ "0" -eq "$(grep "filling with GIT_ASKPASS" push.log | wc -l)" ]
29 )
30 end_test
31
32 begin_test "credentials without useHttpPath, with bad path password"
33 (
34   set -e
35
36   reponame="no-httppath-bad-password"
37   setup_remote_repo "$reponame"
38
39   printf "path:wrong" > "$CREDSDIR/127.0.0.1--$reponame"
40
41   clone_repo "$reponame" without-path
42   git config credential.useHttpPath false
43   git checkout -b without-path
44
45   git lfs track "*.dat" 2>&1 | tee track.log
46   grep "Tracking \"\*.dat\"" track.log
47
48   printf "a" > a.dat
49   git add a.dat
50   git add .gitattributes
51   git commit -m "add a.dat"
52
53   GIT_TRACE=1 git push origin without-path 2>&1 | tee push.log
54   grep "Uploading LFS objects: 100% (1/1), 1 B" push.log
55
56   echo "approvals:"
57   [ "1" -eq "$(cat push.log | grep "creds: git credential approve" | wc -l)" ]
58   echo "fills:"
59   [ "1" -eq "$(cat push.log | grep "creds: git credential fill" | wc -l)" ]
60
61   echo "credential calls have no path:"
62   credcalls="$(grep "creds: git credential" push.log)"
63   [ "0" -eq "$(echo "$credcalls" | grep "no-httppath-bad-password" | wc -l)" ]
64   expected="$(echo "$credcalls" | wc -l)"
65   [ "$expected" -eq "$(printf "$credcalls" | grep '", "")' | wc -l)" ]
66 )
67 end_test
68
69 begin_test "credentials with url-specific useHttpPath, with bad path password"
70 (
71   set -e
72
73   reponame="url-specific-httppath-bad-password"
74   setup_remote_repo "$reponame"
75
76   printf "path:wrong" > "$CREDSDIR/127.0.0.1--$reponame"
77
78   clone_repo "$reponame" with-url-specific-path
79   git config credential.$GITSERVER.useHttpPath false
80   git config lfs.locksverify false
81   git checkout -b without-path
82
83   git lfs track "*.dat" 2>&1 | tee track.log
84   grep "Tracking \"\*.dat\"" track.log
85
86   printf "a" > a.dat
87   git add a.dat
88   git add .gitattributes
89   git commit -m "add a.dat"
90
91   GIT_TRACE=1 git push origin without-path 2>&1 | tee push.log
92   grep "Uploading LFS objects: 100% (1/1), 1 B" push.log
93
94   echo "approvals:"
95   [ "1" -eq "$(cat push.log | grep "creds: git credential approve" | wc -l)" ]
96   echo "fills:"
97   [ "1" -eq "$(cat push.log | grep "creds: git credential fill" | wc -l)" ]
98 )
99 end_test
100
101 begin_test "credentials with useHttpPath, with wrong password"
102 (
103   set -e
104
105   reponame="httppath-bad-password"
106   setup_remote_repo "$reponame"
107
108   printf "path:wrong" > "$CREDSDIR/127.0.0.1--$reponame"
109
110   clone_repo "$reponame" with-path-wrong-pass
111   git checkout -b with-path-wrong-pass
112
113   git lfs track "*.dat" 2>&1 | tee track.log
114   grep "Tracking \"\*.dat\"" track.log
115
116   contents="a"
117   contents_oid=$(calc_oid "$contents")
118
119   printf "$contents" > a.dat
120   git add a.dat
121   git add .gitattributes
122   git commit -m "add a.dat"
123
124   GIT_TRACE=1 git push origin with-path-wrong-pass 2>&1 | tee push.log
125   [ "0" = "$(grep -c "Uploading LFS objects: 100% (1/1), 0 B" push.log)" ]
126   echo "approvals:"
127   [ "0" -eq "$(cat push.log | grep "creds: git credential approve" | wc -l)" ]
128   echo "fills:"
129   [ "2" -eq "$(cat push.log | grep "creds: git credential fill" | wc -l)" ]
130 )
131 end_test
132
133 begin_test "credentials with useHttpPath, with correct password"
134 (
135   set -e
136
137   reponame="$(basename "$0" ".sh")"
138   setup_remote_repo "$reponame"
139
140   printf "path:$reponame" > "$CREDSDIR/127.0.0.1--$reponame"
141
142   clone_repo "$reponame" with-path-correct-pass
143   git checkout -b with-path-correct-pass
144
145   git lfs track "*.dat" 2>&1 | tee track.log
146   grep "Tracking \"\*.dat\"" track.log
147
148   # creating new branch does not re-send any objects existing on other
149   # remote branches anymore, generate new object, different from prev tests
150   contents="b"
151   contents_oid=$(calc_oid "$contents")
152
153   printf "$contents" > b.dat
154   git add b.dat
155   git add .gitattributes
156   git commit -m "add b.dat"
157
158   GIT_TRACE=1 git push origin with-path-correct-pass 2>&1 | tee push.log
159   grep "Uploading LFS objects: 100% (1/1), 1 B" push.log
160   echo "approvals:"
161   [ "1" -eq "$(cat push.log | grep "creds: git credential approve" | wc -l)" ]
162   echo "fills:"
163   [ "1" -eq "$(cat push.log | grep "creds: git credential fill" | wc -l)" ]
164   echo "credential calls have path:"
165   credcalls="$(grep "creds: git credential" push.log)"
166   [ "0" -eq "$(echo "$credcalls" | grep '", "")' | wc -l)" ]
167   expected="$(echo "$credcalls" | wc -l)"
168   [ "$expected" -eq "$(printf "$credcalls" | grep "t-credentials" | wc -l)" ]
169 )
170 end_test
171
172 begin_test "git credential"
173 (
174   set -e
175
176   printf "git:server" > "$CREDSDIR/credential-test.com"
177   printf "git:path" > "$CREDSDIR/credential-test.com--some-path"
178
179   mkdir empty
180   cd empty
181   git init
182
183   echo "protocol=http
184 host=credential-test.com
185 path=some/path" | GIT_TERMINAL_PROMPT=0 git credential fill > cred.log
186   cat cred.log
187
188   expected="protocol=http
189 host=credential-test.com
190 path=some/path
191 username=git
192 password=path"
193
194   [ "$expected" = "$(cat cred.log)" ]
195
196   git config credential.useHttpPath false
197
198   echo "protocol=http
199 host=credential-test.com" | GIT_TERMINAL_PROMPT=0 git credential fill > cred.log
200   cat cred.log
201
202   expected="protocol=http
203 host=credential-test.com
204 username=git
205 password=server"
206   [ "$expected" = "$(cat cred.log)" ]
207
208   echo "protocol=http
209 host=credential-test.com
210 path=some/path" | GIT_TERMINAL_PROMPT=0 git credential fill > cred.log
211   cat cred.log
212
213   expected="protocol=http
214 host=credential-test.com
215 username=git
216 password=server"
217
218   [ "$expected" = "$(cat cred.log)" ]
219 )
220 end_test
221
222
223 if [[ $(uname) == *"MINGW"* ]]; then
224   NETRCFILE="$HOME/_netrc"
225 else
226   NETRCFILE="$HOME/.netrc"
227 fi
228
229
230 begin_test "credentials from netrc"
231 (
232   set -e
233
234   printf "machine localhost\nlogin netrcuser\npassword netrcpass\n" >> "$NETRCFILE"
235   echo $HOME
236   echo "GITSERVER $GITSERVER"
237   cat $NETRCFILE
238
239   # prevent prompts on Windows particularly
240   export SSH_ASKPASS=
241
242   reponame="netrctest"
243   setup_remote_repo "$reponame"
244
245   clone_repo "$reponame" repo
246
247   # Need a remote named "localhost" or 127.0.0.1 in netrc will interfere with the other auth
248   git remote add "netrc" "$(echo $GITSERVER | sed s/127.0.0.1/localhost/)/netrctest"
249   git lfs env
250
251   git lfs track "*.dat"
252   echo "push a" > a.dat
253   git add .gitattributes a.dat
254   git commit -m "add a.dat"
255
256   GIT_TRACE=1 git lfs push netrc master 2>&1 | tee push.log
257   grep "Uploading LFS objects: 100% (1/1), 7 B" push.log
258   echo "any git credential calls:"
259   [ "0" -eq "$(cat push.log | grep "git credential" | wc -l)" ]
260 )
261 end_test
262
263 begin_test "credentials from netrc with unknown keyword"
264 (
265   set -e
266
267   printf "machine localhost\nlogin netrcuser\nnot-a-key something\npassword netrcpass\n" >> "$NETRCFILE"
268   echo $HOME
269   echo "GITSERVER $GITSERVER"
270   cat $NETRCFILE
271
272   # prevent prompts on Windows particularly
273   export SSH_ASKPASS=
274
275   reponame="netrctest"
276   setup_remote_repo "$reponame"
277
278   clone_repo "$reponame" repo2
279
280   # Need a remote named "localhost" or 127.0.0.1 in netrc will interfere with the other auth
281   git remote add "netrc" "$(echo $GITSERVER | sed s/127.0.0.1/localhost/)/netrctest"
282   git lfs env
283
284   git lfs track "*.dat"
285   echo "push a" > a.dat
286   git add .gitattributes a.dat
287   git commit -m "add a.dat"
288
289   GIT_TRACE=1 git lfs push netrc master 2>&1 | tee push.log
290   grep "Uploading LFS objects: 100% (1/1), 7 B" push.log
291   echo "any git credential calls:"
292   [ "0" -eq "$(cat push.log | grep "git credential" | wc -l)" ]
293 )
294 end_test
295
296 begin_test "credentials from netrc with bad password"
297 (
298   set -e
299
300   printf "machine localhost\nlogin netrcuser\npassword badpass\n" >> "$NETRCFILE"
301   echo $HOME
302   echo "GITSERVER $GITSERVER"
303   cat $NETRCFILE
304
305   # prevent prompts on Windows particularly
306   export SSH_ASKPASS=
307
308   reponame="netrctest"
309   setup_remote_repo "$reponame"
310
311   clone_repo "$reponame" repo3
312
313   # Need a remote named "localhost" or 127.0.0.1 in netrc will interfere with the other auth
314   git remote add "netrc" "$(echo $GITSERVER | sed s/127.0.0.1/localhost/)/netrctest"
315   git lfs env
316
317   git lfs track "*.dat"
318   echo "push a" > a.dat
319   git add .gitattributes a.dat
320   git commit -m "add a.dat"
321
322   git push netrc master 2>&1 | tee push.log
323   [ "0" = "$(grep -c "Uploading LFS objects: 100% (1/1), 7 B" push.log)" ]
324 )
325 end_test
326
327 begin_test "credentials from lfs.url"
328 (
329   set -e
330
331   reponame="requirecreds-lfsurl"
332   setup_remote_repo "$reponame"
333   clone_repo "$reponame" "$reponame"
334
335   git lfs track "*.dat"
336   echo "push a" > a.dat
337   git add .gitattributes a.dat
338   git commit -m "add a.dat"
339
340   echo "bad push"
341   git lfs env
342   git lfs push origin master 2>&1 | tee push.log
343   grep "Uploading LFS objects:   0% (0/1), 0 B" push.log
344
345   echo "good push"
346   gitserverhost=$(echo "$GITSERVER" | cut -d'/' -f3)
347   git config lfs.url http://requirecreds:pass@$gitserverhost/$reponame.git/info/lfs
348   git lfs env
349   git lfs push origin master 2>&1 | tee push.log
350   grep "Uploading LFS objects:   0% (0/1), 0 B" push.log
351
352   echo "bad fetch"
353   rm -rf .git/lfs/objects
354   git config lfs.url http://$gitserverhost/$reponame.git/info/lfs
355   git lfs env
356   git lfs fetch --all 2>&1 | tee fetch.log
357   grep "Downloading LFS objects:   0% (0/1), 0 B" fetch.log
358
359   echo "good fetch"
360   rm -rf .git/lfs/objects
361   git config lfs.url http://requirecreds:pass@$gitserverhost/$reponame.git/info/lfs
362   git lfs env
363   git lfs fetch --all 2>&1 | tee fetch.log
364   grep "Downloading LFS objects: 100% (1/1), 7 B" fetch.log
365 )
366 end_test
367
368 begin_test "credentials from remote.origin.url"
369 (
370   set -e
371
372   reponame="requirecreds-remoteurl"
373   setup_remote_repo "$reponame"
374   clone_repo "$reponame" "$reponame"
375
376   git lfs track "*.dat"
377   echo "push b" > b.dat
378   git add .gitattributes b.dat
379   git commit -m "add b.dat"
380
381   echo "bad push"
382   git lfs env
383   git lfs push origin master 2>&1 | tee push.log
384   grep "Uploading LFS objects:   0% (0/1), 0 B" push.log
385
386   echo "good push"
387   gitserverhost=$(echo "$GITSERVER" | cut -d'/' -f3)
388   git config remote.origin.url http://requirecreds:pass@$gitserverhost/$reponame.git
389   git lfs env
390   git lfs push origin master 2>&1 | tee push.log
391   grep "Uploading LFS objects: 100% (1/1), 7 B" push.log
392
393   echo "bad fetch"
394   rm -rf .git/lfs/objects
395   git config remote.origin.url http://$gitserverhost/$reponame.git
396   git lfs env
397   git lfs fetch --all 2>&1 | tee fetch.log
398   # Missing authentication causes `git lfs fetch` to fail before the progress
399   # meter is printed to the TTY.
400
401   echo "good fetch"
402   rm -rf .git/lfs/objects
403   git config remote.origin.url http://requirecreds:pass@$gitserverhost/$reponame.git
404   git lfs env
405   git lfs fetch --all 2>&1 | tee fetch.log
406   grep "Downloading LFS objects: 100% (1/1), 7 B" fetch.log
407 )
408 end_test