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