]> diplodocus.org Git - nmh/blob - test/oauth/test-send
Replace getcpy() with mh_xstrdup() where the string isn't NULL.
[nmh] / test / oauth / test-send
1 #!/bin/sh
2 #
3 # Test the XOAUTH2 support in send
4 #
5
6 if test -z "${MH_OBJ_DIR}"; then
7 srcdir=`dirname "$0"`/../..
8 MH_OBJ_DIR=`cd "${srcdir}" && pwd`; export MH_OBJ_DIR
9 fi
10
11 . "${srcdir}/test/oauth/common.sh"
12
13 test_send_only_fakehttp() {
14 start_fakehttp
15 test_send_no_servers "$@"
16 check_http_req
17 }
18
19 #
20 # success cases
21 #
22
23 export XOAUTH
24 XOAUTH='dXNlcj1ub2JvZHlAZXhhbXBsZS5jb20BYXV0aD1CZWFyZXIgdGVzdC1hY2Nlc3MBAQ=='
25
26 # TEST
27 start_test 'access token ready, smtp server accepts message'
28
29 setup_draft
30
31 fake_creds <<EOF
32 access-nobody@example.com: test-access
33 refresh-nobody@example.com: test-refresh
34 expire-nobody@example.com: 2000000000
35 EOF
36
37 start_fakesmtp
38 run_test "send -draft -server 127.0.0.1 -port ${smtp_port} -sasl -saslmech xoauth2 -authservice test -user nobody@example.com"
39
40 # TEST
41 start_test 'expired access token, refresh works, smtp server accepts message'
42
43 setup_draft
44
45 fake_creds <<EOF
46 access-nobody@example.com: old-access
47 refresh-nobody@example.com: test-refresh
48 expire-nobody@example.com: 1414303986
49 EOF
50
51 expect_http_post_refresh
52
53 fake_json_response <<EOF
54 {
55 "access_token": "test-access",
56 "token_type": "Bearer",
57 "expires_in": 3600
58 }
59 EOF
60
61 expect_creds <<EOF
62 access-nobody@example.com: test-access
63 refresh-nobody@example.com: test-refresh
64 expire-nobody@example.com:
65 EOF
66
67 test_send
68
69 check_creds_private
70 check_creds
71
72 # TEST
73 start_test 'expired access token, refresh works and gets updated, smtp server accepts message'
74
75 setup_draft
76
77 fake_creds <<EOF
78 access-nobody@example.com: old-access
79 refresh-nobody@example.com: old-refresh
80 expire-nobody@example.com: 1414303986
81 EOF
82
83 expect_http_post_old_refresh
84
85 fake_json_response <<EOF
86 {
87 "access_token": "test-access",
88 "refresh_token": "test-refresh",
89 "token_type": "Bearer"
90 }
91 EOF
92
93 expect_creds <<EOF
94 access-nobody@example.com: test-access
95 refresh-nobody@example.com: test-refresh
96 EOF
97
98 test_send
99
100 check_creds
101
102 # TEST
103 start_test 'access token has no expiration, refresh works, smtp server accepts message'
104
105 setup_draft
106
107 fake_creds <<EOF
108 access-nobody@example.com: old-access
109 refresh-nobody@example.com: test-refresh
110 EOF
111
112 expect_http_post_refresh
113
114 fake_json_response <<EOF
115 {
116 "access_token": "test-access",
117 "token_type": "Bearer"
118 }
119 EOF
120
121 expect_creds <<EOF
122 access-nobody@example.com: test-access
123 refresh-nobody@example.com: test-refresh
124 EOF
125
126 test_send
127
128 check_creds
129
130 # TEST
131 start_test 'no access token, refresh works, smtp server accepts message'
132
133 setup_draft
134
135 fake_creds <<EOF
136 refresh-nobody@example.com: test-refresh
137 EOF
138
139 expect_http_post_refresh
140
141 fake_json_response <<EOF
142 {
143 "access_token": "test-access",
144 "token_type": "Bearer"
145 }
146 EOF
147
148 expect_creds <<EOF
149 access-nobody@example.com: test-access
150 refresh-nobody@example.com: test-refresh
151 EOF
152
153 test_send
154
155 check_creds
156
157 #
158 # fail cases
159 #
160
161 setup_draft
162
163 # TEST
164 start_test 'no service definition'
165
166 run_test "send -draft -server 127.0.0.1 -port ${smtp_port} -sasl -saslmech xoauth2 -authservice bogus -user nobody@example.com" 'send: Unable to retrieve oauth profile entries: scope is missing'
167
168 # TEST
169 start_test 'no creds file -- should tell user to mhlogin'
170
171 rm -f "${MHTMPDIR}/oauth-test"
172
173 test_send_only_fakesmtp 'post: no credentials -- run mhlogin -saslmech xoauth2 -authservice test
174 send: message not delivered to anyone'
175
176 # TEST
177 start_test 'empty creds file -- should tell user to mhlogin'
178
179 fake_creds < /dev/null
180
181 test_send_only_fakesmtp 'post: user not found in cred file: nobody@example.com
182 send: message not delivered to anyone'
183
184 # TEST
185 start_test 'garbage creds file'
186
187 echo bork | fake_creds
188
189 test_send_only_fakesmtp 'post: eof encountered in field "bork"
190 post: error loading cred file
191 send: message not delivered to anyone'
192
193 # TEST
194 start_test 'unexpected field in creds file'
195
196 fake_creds <<EOF
197 bork: bork
198 access-nobody@example.com: test-access
199 EOF
200
201 test_send_only_fakesmtp 'post: error loading cred file: unexpected field
202 send: message not delivered to anyone'
203
204 # TEST
205 start_test 'garbage expiration time'
206
207 fake_creds <<EOF
208 access-nobody@example.com: test-access
209 expire-nobody@example.com: 99999999999999999999999999999999
210 EOF
211
212 test_send_only_fakesmtp 'post: error loading cred file: invalid expiration time
213 send: message not delivered to anyone'
214
215 # TEST
216 start_test 'refresh response has no access token'
217
218 fake_creds <<EOF
219 refresh-nobody@example.com: test-refresh
220 EOF
221
222 expect_http_post_refresh
223
224 fake_json_response <<EOF
225 {
226 "refresh_token": "refresh-token",
227 "token_type": "Bearer"
228 }
229 EOF
230
231 test_send 'post: error refreshing OAuth2 token
232 post: invalid response: no access token
233 send: message not delivered to anyone'
234
235 # TEST
236 start_test 'expired access token, no refresh token -- tell user to mhlogin'
237
238 fake_creds <<EOF
239 access-nobody@example.com: test-access
240 expire-nobody@example.com: 1414303986
241 EOF
242
243 test_send_only_fakesmtp 'post: no valid credentials -- run mhlogin -saslmech xoauth2 -authservice test
244 send: message not delivered to anyone'
245
246 # TEST
247 start_test 'access token has no expiration, no refresh token -- tell user to mhlogin'
248
249 fake_creds <<EOF
250 access-nobody@example.com: test-access
251 EOF
252
253 test_send_only_fakesmtp 'post: no valid credentials -- run mhlogin -saslmech xoauth2 -authservice test
254 send: message not delivered to anyone'
255
256 # TEST
257 start_test 'refresh finds no http server'
258
259 fake_creds <<EOF
260 access-nobody@example.com: test-access
261 refresh-nobody@example.com: test-refresh
262 EOF
263
264 cat > "${testname}.expected-send-output" <<EOF
265 post: error refreshing OAuth2 token
266 post: error making HTTP request to OAuth2 authorization endpoint: [details]
267 send: message not delivered to anyone
268 EOF
269
270 start_fakesmtp
271 run_prog send -draft -server 127.0.0.1 -port ${smtp_port} \
272 -sasl -saslmech xoauth2 -authservice test -user nobody@example.com > "${testname}.send-output" 2>&1 || true
273 # Clear out an error message we get from libcurl on some systems (seen at least
274 # 3 different versions of this error message, on FreeBSD 10.1, Ubuntu 12.04, and
275 # Ubuntu 14.04).
276 f="${testname}.send-output"
277 sed 's/\(post: error making HTTP request to OAuth2 authorization endpoint:\).*/\1 [details]/' "$f" > "$f".clean
278 check "$f".clean "${testname}.expected-send-output"
279 rm "$f"
280
281 # TEST
282 start_test 'refresh gets bogus 200 response from http server'
283
284 expect_http_post_refresh
285
286 fake_http_response '200 OK' <<EOF
287 Content-Type: text/html
288
289 <html>doh!</htmxl>
290 EOF
291
292 test_send 'post: error refreshing OAuth2 token
293 post: invalid response
294 send: message not delivered to anyone'
295
296 # TEST
297 start_test 'refresh gets 500 response from http server'
298
299 expect_http_post_refresh
300
301 fake_http_response '500 Server Error' <<EOF
302 Content-Type: text/html
303
304 <html>doh!</html>
305 EOF
306
307 test_send 'post: error refreshing OAuth2 token
308 post: invalid response
309 send: message not delivered to anyone'
310
311 # TEST
312 start_test 'refresh gets proper error from http'
313
314 expect_http_post_refresh
315
316 fake_http_response '400 Bad Request' <<EOF
317 Content-Type: application/json
318
319 {
320 "error": "invalid_grant"
321 }
322 EOF
323
324 test_send 'post: credentials rejected -- run mhlogin -saslmech xoauth2 -authservice test
325 send: message not delivered to anyone'
326
327 # TEST
328 start_test 'refresh gets response too big'
329
330 fake_creds <<EOF
331 refresh-nobody@example.com: test-refresh
332 EOF
333
334 expect_http_post_refresh
335
336 fake_json_response <<EOF
337 {
338 "access_token": "test-access",
339 "token_type": "Bearer",
340 "expires_in": 3600
341 }
342 EOF
343
344 awk 'BEGIN { for (i = 0; i < 8192; i++) { print "." } }' \
345 >> "${testname}.http-res"
346
347 test_send 'post: error refreshing OAuth2 token
348 post: refusing to process response body larger than 8192 bytes
349 send: message not delivered to anyone'
350
351 # TEST
352 start_test 'smtp server rejects token'
353
354 XOAUTH='not-that-one'
355
356 fake_creds <<EOF
357 access-nobody@example.com: test-access
358 expire-nobody@example.com: 2000000000
359 EOF
360
361 test_send_only_fakesmtp 'post: problem initializing server; [BHST] Authentication failed: Not no way, not no how!
362 send: message not delivered to anyone'
363
364 # TEST
365 start_test 'botched json response'
366
367 fake_creds <<EOF
368 refresh-nobody@example.com: test-refresh
369 EOF
370
371 expect_http_post_refresh
372
373 fake_json_response <<EOF
374 {
375 "refresh_token": "refresh-toke
376 }
377 EOF
378
379 test_send 'post: error refreshing OAuth2 token
380 post: invalid response
381 send: message not delivered to anyone'
382
383 # TEST
384 start_test "smtp server doesn't support oauth"
385
386 unset XOAUTH
387
388 test_send_only_fakesmtp 'post: problem initializing server; [BHST] SMTP server does not support SASL
389 send: message not delivered to anyone'
390
391 clean_fakesmtp
392 clean_fakehttp
393 finish_test
394
395 exit ${failed:-0}