Git fork
1#!/bin/sh
2
3test_description='credential-store tests'
4
5. ./test-lib.sh
6. "$TEST_DIRECTORY"/lib-credential.sh
7
8helper_test store
9
10test_expect_success 'when xdg file does not exist, xdg file not created' '
11 test_path_is_missing "$HOME/.config/git/credentials" &&
12 test -s "$HOME/.git-credentials"
13'
14
15test_expect_success 'setup xdg file' '
16 rm -f "$HOME/.git-credentials" &&
17 mkdir -p "$HOME/.config/git" &&
18 >"$HOME/.config/git/credentials"
19'
20
21helper_test store
22
23test_expect_success 'when xdg file exists, home file not created' '
24 test -s "$HOME/.config/git/credentials" &&
25 test_path_is_missing "$HOME/.git-credentials"
26'
27
28test_expect_success 'setup custom xdg file' '
29 rm -f "$HOME/.git-credentials" &&
30 rm -f "$HOME/.config/git/credentials" &&
31 mkdir -p "$HOME/xdg/git" &&
32 >"$HOME/xdg/git/credentials"
33'
34
35XDG_CONFIG_HOME="$HOME/xdg"
36export XDG_CONFIG_HOME
37helper_test store
38unset XDG_CONFIG_HOME
39
40test_expect_success 'if custom xdg file exists, home and xdg files not created' '
41 test_when_finished "rm -f \"$HOME/xdg/git/credentials\"" &&
42 test -s "$HOME/xdg/git/credentials" &&
43 test_path_is_missing "$HOME/.git-credentials" &&
44 test_path_is_missing "$HOME/.config/git/credentials"
45'
46
47test_expect_success 'get: use home file if both home and xdg files have matches' '
48 echo "https://home-user:home-pass@example.com" >"$HOME/.git-credentials" &&
49 mkdir -p "$HOME/.config/git" &&
50 echo "https://xdg-user:xdg-pass@example.com" >"$HOME/.config/git/credentials" &&
51 check fill store <<-\EOF
52 protocol=https
53 host=example.com
54 --
55 protocol=https
56 host=example.com
57 username=home-user
58 password=home-pass
59 --
60 EOF
61'
62
63test_expect_success 'get: use xdg file if home file has no matches' '
64 >"$HOME/.git-credentials" &&
65 mkdir -p "$HOME/.config/git" &&
66 echo "https://xdg-user:xdg-pass@example.com" >"$HOME/.config/git/credentials" &&
67 check fill store <<-\EOF
68 protocol=https
69 host=example.com
70 --
71 protocol=https
72 host=example.com
73 username=xdg-user
74 password=xdg-pass
75 --
76 EOF
77'
78
79test_expect_success POSIXPERM,SANITY 'get: use xdg file if home file is unreadable' '
80 echo "https://home-user:home-pass@example.com" >"$HOME/.git-credentials" &&
81 chmod -r "$HOME/.git-credentials" &&
82 mkdir -p "$HOME/.config/git" &&
83 echo "https://xdg-user:xdg-pass@example.com" >"$HOME/.config/git/credentials" &&
84 check fill store <<-\EOF
85 protocol=https
86 host=example.com
87 --
88 protocol=https
89 host=example.com
90 username=xdg-user
91 password=xdg-pass
92 --
93 EOF
94'
95
96test_expect_success 'store: if both xdg and home files exist, only store in home file' '
97 >"$HOME/.git-credentials" &&
98 mkdir -p "$HOME/.config/git" &&
99 >"$HOME/.config/git/credentials" &&
100 check approve store <<-\EOF &&
101 protocol=https
102 host=example.com
103 username=store-user
104 password=store-pass
105 EOF
106 echo "https://store-user:store-pass@example.com" >expected &&
107 test_cmp expected "$HOME/.git-credentials" &&
108 test_must_be_empty "$HOME/.config/git/credentials"
109'
110
111test_expect_success 'erase: erase matching credentials from both xdg and home files' '
112 echo "https://home-user:home-pass@example.com" >"$HOME/.git-credentials" &&
113 mkdir -p "$HOME/.config/git" &&
114 echo "https://xdg-user:xdg-pass@example.com" >"$HOME/.config/git/credentials" &&
115 check reject store <<-\EOF &&
116 protocol=https
117 host=example.com
118 EOF
119 test_must_be_empty "$HOME/.git-credentials" &&
120 test_must_be_empty "$HOME/.config/git/credentials"
121'
122
123invalid_credential_test() {
124 test_expect_success "get: ignore credentials without $1 as invalid" '
125 echo "$2" >"$HOME/.git-credentials" &&
126 check fill store <<-\EOF
127 protocol=https
128 host=example.com
129 --
130 protocol=https
131 host=example.com
132 username=askpass-username
133 password=askpass-password
134 --
135 askpass: Username for '\''https://example.com'\'':
136 askpass: Password for '\''https://askpass-username@example.com'\'':
137 --
138 EOF
139 '
140}
141
142invalid_credential_test "scheme" ://user:pass@example.com
143invalid_credential_test "valid host/path" https://user:pass@
144invalid_credential_test "username/password" https://pass@example.com
145
146test_expect_success 'get: credentials with DOS line endings are invalid' '
147 printf "https://user:pass@example.com\r\n" >"$HOME/.git-credentials" &&
148 check fill store <<-\EOF
149 protocol=https
150 host=example.com
151 --
152 protocol=https
153 host=example.com
154 username=askpass-username
155 password=askpass-password
156 --
157 askpass: Username for '\''https://example.com'\'':
158 askpass: Password for '\''https://askpass-username@example.com'\'':
159 --
160 EOF
161'
162
163test_expect_success 'get: credentials with path and DOS line endings are valid' '
164 printf "https://user:pass@example.com/repo.git\r\n" >"$HOME/.git-credentials" &&
165 check fill store <<-\EOF
166 url=https://example.com/repo.git
167 --
168 protocol=https
169 host=example.com
170 username=user
171 password=pass
172 --
173 EOF
174'
175
176test_expect_success 'get: credentials with DOS line endings are invalid if path is relevant' '
177 printf "https://user:pass@example.com/repo.git\r\n" >"$HOME/.git-credentials" &&
178 test_config credential.useHttpPath true &&
179 check fill store <<-\EOF
180 url=https://example.com/repo.git
181 --
182 protocol=https
183 host=example.com
184 path=repo.git
185 username=askpass-username
186 password=askpass-password
187 --
188 askpass: Username for '\''https://example.com/repo.git'\'':
189 askpass: Password for '\''https://askpass-username@example.com/repo.git'\'':
190 --
191 EOF
192'
193
194test_expect_success 'get: store file can contain empty/bogus lines' '
195 echo "" >"$HOME/.git-credentials" &&
196 q_to_tab <<-\CREDENTIAL >>"$HOME/.git-credentials" &&
197 #comment
198 Q
199 https://user:pass@example.com
200 CREDENTIAL
201 check fill store <<-\EOF
202 protocol=https
203 host=example.com
204 --
205 protocol=https
206 host=example.com
207 username=user
208 password=pass
209 --
210 EOF
211'
212
213test_done