Imported Upstream version 2.4.0
[scm/test.git] / test / test-credentials.sh
1 #!/usr/bin/env bash
2
3 . "test/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 checkout -b without-path
81
82   git lfs track "*.dat" 2>&1 | tee track.log
83   grep "Tracking \"\*.dat\"" track.log
84
85   printf "a" > a.dat
86   git add a.dat
87   git add .gitattributes
88   git commit -m "add a.dat"
89
90   GIT_TRACE=1 git push origin without-path 2>&1 | tee push.log
91   grep "Uploading LFS objects: 100% (1/1), 1 B" push.log
92
93   echo "approvals:"
94   [ "1" -eq "$(cat push.log | grep "creds: git credential approve" | wc -l)" ]
95   echo "fills:"
96   [ "1" -eq "$(cat push.log | grep "creds: git credential fill" | wc -l)" ]
97 )
98 end_test
99
100 begin_test "credentials with useHttpPath, with wrong password"
101 (
102   set -e
103
104   reponame="httppath-bad-password"
105   setup_remote_repo "$reponame"
106
107   printf "path:wrong" > "$CREDSDIR/127.0.0.1--$reponame"
108
109   clone_repo "$reponame" with-path-wrong-pass
110   git checkout -b with-path-wrong-pass
111
112   git lfs track "*.dat" 2>&1 | tee track.log
113   grep "Tracking \"\*.dat\"" track.log
114
115   contents="a"
116   contents_oid=$(calc_oid "$contents")
117
118   printf "$contents" > a.dat
119   git add a.dat
120   git add .gitattributes
121   git commit -m "add a.dat"
122
123   GIT_TRACE=1 git push origin with-path-wrong-pass 2>&1 | tee push.log
124   [ "0" = "$(grep -c "Uploading LFS objects: 100% (1/1), 0 B" push.log)" ]
125   echo "approvals:"
126   [ "0" -eq "$(cat push.log | grep "creds: git credential approve" | wc -l)" ]
127   echo "fills:"
128   [ "2" -eq "$(cat push.log | grep "creds: git credential fill" | wc -l)" ]
129 )
130 end_test
131
132 begin_test "credentials with useHttpPath, with correct password"
133 (
134   set -e
135
136   reponame="$(basename "$0" ".sh")"
137   setup_remote_repo "$reponame"
138
139   printf "path:$reponame" > "$CREDSDIR/127.0.0.1--$reponame"
140
141   clone_repo "$reponame" with-path-correct-pass
142   git checkout -b with-path-correct-pass
143
144   git lfs track "*.dat" 2>&1 | tee track.log
145   grep "Tracking \"\*.dat\"" track.log
146
147   # creating new branch does not re-send any objects existing on other
148   # remote branches anymore, generate new object, different from prev tests
149   contents="b"
150   contents_oid=$(calc_oid "$contents")
151
152   printf "$contents" > b.dat
153   git add b.dat
154   git add .gitattributes
155   git commit -m "add b.dat"
156
157   GIT_TRACE=1 git push origin with-path-correct-pass 2>&1 | tee push.log
158   grep "Uploading LFS objects: 100% (1/1), 1 B" push.log
159   echo "approvals:"
160   [ "1" -eq "$(cat push.log | grep "creds: git credential approve" | wc -l)" ]
161   echo "fills:"
162   [ "1" -eq "$(cat push.log | grep "creds: git credential fill" | wc -l)" ]
163   echo "credential calls have path:"
164   credcalls="$(grep "creds: git credential" push.log)"
165   [ "0" -eq "$(echo "$credcalls" | grep '", "")' | wc -l)" ]
166   expected="$(echo "$credcalls" | wc -l)"
167   [ "$expected" -eq "$(printf "$credcalls" | grep "test-credentials" | wc -l)" ]
168 )
169 end_test
170
171 begin_test "git credential"
172 (
173   set -e
174
175   printf "git:server" > "$CREDSDIR/credential-test.com"
176   printf "git:path" > "$CREDSDIR/credential-test.com--some-path"
177
178   mkdir empty
179   cd empty
180   git init
181
182   echo "protocol=http
183 host=credential-test.com
184 path=some/path" | GIT_TERMINAL_PROMPT=0 git credential fill > cred.log
185   cat cred.log
186
187   expected="protocol=http
188 host=credential-test.com
189 path=some/path
190 username=git
191 password=path"
192
193   [ "$expected" = "$(cat cred.log)" ]
194
195   git config credential.useHttpPath false
196
197   echo "protocol=http
198 host=credential-test.com" | GIT_TERMINAL_PROMPT=0 git credential fill > cred.log
199   cat cred.log
200
201   expected="protocol=http
202 host=credential-test.com
203 username=git
204 password=server"
205   [ "$expected" = "$(cat cred.log)" ]
206
207   echo "protocol=http
208 host=credential-test.com
209 path=some/path" | GIT_TERMINAL_PROMPT=0 git credential fill > cred.log
210   cat cred.log
211
212   expected="protocol=http
213 host=credential-test.com
214 username=git
215 password=server"
216
217   [ "$expected" = "$(cat cred.log)" ]
218 )
219 end_test
220
221
222 if [[ $(uname) == *"MINGW"* ]]; then
223   NETRCFILE="$HOME/_netrc"
224 else
225   NETRCFILE="$HOME/.netrc"
226 fi
227
228
229 begin_test "credentials from netrc"
230 (
231   set -e
232
233   printf "machine localhost\nlogin netrcuser\npassword netrcpass\n" >> "$NETRCFILE"
234   echo $HOME
235   echo "GITSERVER $GITSERVER"
236   cat $NETRCFILE
237
238   # prevent prompts on Windows particularly
239   export SSH_ASKPASS=
240
241   reponame="netrctest"
242   setup_remote_repo "$reponame"
243
244   clone_repo "$reponame" repo
245
246   # Need a remote named "localhost" or 127.0.0.1 in netrc will interfere with the other auth
247   git remote add "netrc" "$(echo $GITSERVER | sed s/127.0.0.1/localhost/)/netrctest"
248   git lfs env
249
250   git lfs track "*.dat"
251   echo "push a" > a.dat
252   git add .gitattributes a.dat
253   git commit -m "add a.dat"
254
255   GIT_TRACE=1 git lfs push netrc master 2>&1 | tee push.log
256   grep "Uploading LFS objects: 100% (1/1), 7 B" push.log
257   echo "any git credential calls:"
258   [ "0" -eq "$(cat push.log | grep "git credential" | wc -l)" ]
259 )
260 end_test
261
262 begin_test "credentials from netrc with bad password"
263 (
264   set -e
265
266   printf "machine localhost\nlogin netrcuser\npassword badpass\n" >> "$NETRCFILE"
267   echo $HOME
268   echo "GITSERVER $GITSERVER"
269   cat $NETRCFILE
270
271   # prevent prompts on Windows particularly
272   export SSH_ASKPASS=
273
274   reponame="netrctest"
275   setup_remote_repo "$reponame"
276
277   clone_repo "$reponame" repo2
278
279   # Need a remote named "localhost" or 127.0.0.1 in netrc will interfere with the other auth
280   git remote add "netrc" "$(echo $GITSERVER | sed s/127.0.0.1/localhost/)/netrctest"
281   git lfs env
282
283   git lfs track "*.dat"
284   echo "push a" > a.dat
285   git add .gitattributes a.dat
286   git commit -m "add a.dat"
287
288   git push netrc master 2>&1 | tee push.log
289   [ "0" = "$(grep -c "Uploading LFS objects: 100% (1/1), 7 B" push.log)" ]
290 )
291 end_test
292
293 begin_test "credentials from lfs.url"
294 (
295   set -e
296
297   reponame="requirecreds"
298   setup_remote_repo "$reponame"
299   clone_repo "$reponame" requirecreds-lfsurl
300
301   git lfs track "*.dat"
302   echo "push a" > a.dat
303   git add .gitattributes a.dat
304   git commit -m "add a.dat"
305
306   echo "bad push"
307   git lfs env
308   git lfs push origin master 2>&1 | tee push.log
309   grep "Uploading LFS objects:   0% (0/1), 0 B" push.log
310
311   echo "good push"
312   gitserverhost=$(echo "$GITSERVER" | cut -d'/' -f3)
313   git config lfs.url http://requirecreds:pass@$gitserverhost/$reponame.git/info/lfs
314   git lfs env
315   git lfs push origin master 2>&1 | tee push.log
316   grep "Uploading LFS objects:   0% (0/1), 0 B" push.log
317
318   echo "bad fetch"
319   rm -rf .git/lfs/objects
320   git config lfs.url http://$gitserverhost/$reponame.git/info/lfs
321   git lfs env
322   git lfs fetch --all 2>&1 | tee fetch.log
323   grep "Downloading LFS objects:   0% (0/1), 0 B" fetch.log
324
325   echo "good fetch"
326   rm -rf .git/lfs/objects
327   git config lfs.url http://requirecreds:pass@$gitserverhost/$reponame.git/info/lfs
328   git lfs env
329   git lfs fetch --all 2>&1 | tee fetch.log
330   grep "Downloading LFS objects: 100% (1/1), 7 B" fetch.log
331 )
332 end_test
333
334 begin_test "credentials from remote.origin.url"
335 (
336   set -e
337
338   reponame="requirecreds"
339   setup_remote_repo "$reponame"
340   clone_repo "$reponame" requirecreds-remoteurl
341
342   git lfs track "*.dat"
343   echo "push b" > b.dat
344   git add .gitattributes b.dat
345   git commit -m "add b.dat"
346
347   echo "bad push"
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 "good push"
353   gitserverhost=$(echo "$GITSERVER" | cut -d'/' -f3)
354   git config remote.origin.url http://requirecreds:pass@$gitserverhost/$reponame.git
355   git lfs env
356   git lfs push origin master 2>&1 | tee push.log
357   grep "Uploading LFS objects: 100% (1/1), 7 B" push.log
358
359   echo "bad fetch"
360   rm -rf .git/lfs/objects
361   git config remote.origin.url http://$gitserverhost/$reponame.git
362   git lfs env
363   git lfs fetch --all 2>&1 | tee fetch.log
364   # Missing authentication causes `git lfs fetch` to fail before the progress
365   # meter is printed to the TTY.
366
367   echo "good fetch"
368   rm -rf .git/lfs/objects
369   git config remote.origin.url http://requirecreds:pass@$gitserverhost/$reponame.git
370   git lfs env
371   git lfs fetch --all 2>&1 | tee fetch.log
372   grep "Downloading LFS objects: 100% (1/1), 7 B" fetch.log
373 )
374 end_test