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