]> diplodocus.org Git - nmh/blob - test/oauth/test-send
Wrapped code block of conditional statement with {}, so that the
[nmh] / test / oauth / test-send
1 #!/bin/sh
2 #
3 # Test the XOAUTH2 support in sen
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} -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} -saslmech xoauth2 -authservice bogus -user nobody@example.com" 'send: incomplete OAuth2 service definition: 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_no_servers 'send: no credentials -- run mhlogin -saslmech xoauth2 -authservice test'
174
175 # TEST
176 start_test 'empty creds file -- should tell user to mhlogin'
177
178 fake_creds < /dev/null
179
180 test_send_no_servers 'send: user not found in cred file: nobody@example.com'
181
182 # TEST
183 start_test 'garbage creds file'
184
185 echo bork | fake_creds
186
187 test_send_no_servers 'send: eof encountered in field "bork"
188 send: error loading cred file'
189
190 # TEST
191 start_test 'unexpected field in creds file'
192
193 fake_creds <<EOF
194 bork: bork
195 access-nobody@example.com: test-access
196 EOF
197
198 test_send_no_servers 'send: error loading cred file: unexpected field'
199
200 # TEST
201 start_test 'garbage expiration time'
202
203 fake_creds <<EOF
204 access-nobody@example.com: test-access
205 expire-nobody@example.com: 99999999999999999999999999999999
206 EOF
207
208 test_send_no_servers 'send: error loading cred file: invalid expiration time'
209
210 # TEST
211 start_test 'refresh response has no access token'
212
213 fake_creds <<EOF
214 refresh-nobody@example.com: test-refresh
215 EOF
216
217 expect_http_post_refresh
218
219 fake_json_response <<EOF
220 {
221 "refresh_token": "refresh-token",
222 "token_type": "Bearer"
223 }
224 EOF
225
226 test_send_only_fakehttp 'send: error refreshing OAuth2 token
227 send: invalid response: no access token'
228
229 # TEST
230 start_test 'expired access token, no refresh token -- tell user to mhlogin'
231
232 fake_creds <<EOF
233 access-nobody@example.com: test-access
234 expire-nobody@example.com: 1414303986
235 EOF
236
237 test_send_no_servers 'send: no valid credentials -- run mhlogin -saslmech xoauth2 -authservice test'
238
239 # TEST
240 start_test 'access token has no expiration, no refresh token -- tell user to mhlogin'
241
242 fake_creds <<EOF
243 access-nobody@example.com: test-access
244 EOF
245
246 test_send_no_servers 'send: no valid credentials -- run mhlogin -saslmech xoauth2 -authservice test'
247
248 # TEST
249 start_test 'refresh finds no http server'
250
251 fake_creds <<EOF
252 access-nobody@example.com: test-access
253 refresh-nobody@example.com: test-refresh
254 EOF
255
256 cat > "${testname}.expected-send-output" <<EOF
257 send: error refreshing OAuth2 token
258 send: error making HTTP request to OAuth2 authorization endpoint: [details]
259 EOF
260
261 run_prog send -draft -server 127.0.0.1 -port ${smtp_port} \
262 -saslmech xoauth2 -authservice test -user nobody@example.com > "${testname}.send-output" 2>&1 || true
263 # Clear out an error message we get from libcurl on some systems (seen at least
264 # 3 different versions of this error message, on FreeBSD 10.1, Ubuntu 12.04, and
265 # Ubuntu 14.04).
266 f="${testname}.send-output"
267 sed 's/\(send: error making HTTP request to OAuth2 authorization endpoint:\).*/\1 [details]/' "$f" > "$f".clean
268 check "$f".clean "${testname}.expected-send-output"
269 rm "$f"
270
271 # TEST
272 start_test 'refresh gets bogus 200 response from http server'
273
274 expect_http_post_refresh
275
276 fake_http_response '200 OK' <<EOF
277 Content-Type: text/html
278
279 <html>doh!</htmxl>
280 EOF
281
282 test_send_only_fakehttp 'send: error refreshing OAuth2 token
283 send: invalid response'
284
285 # TEST
286 start_test 'refresh gets 500 response from http server'
287
288 expect_http_post_refresh
289
290 fake_http_response '500 Server Error' <<EOF
291 Content-Type: text/html
292
293 <html>doh!</html>
294 EOF
295
296 test_send_only_fakehttp 'send: error refreshing OAuth2 token
297 send: invalid response'
298
299 # TEST
300 start_test 'refresh gets proper error from http'
301
302 expect_http_post_refresh
303
304 fake_http_response '400 Bad Request' <<EOF
305 Content-Type: application/json
306
307 {
308 "error": "invalid_grant"
309 }
310 EOF
311
312 test_send_only_fakehttp 'send: credentials rejected -- run mhlogin -saslmech xoauth2 -authservice test'
313
314 # TEST
315 start_test 'refresh gets response too big'
316
317 fake_creds <<EOF
318 refresh-nobody@example.com: test-refresh
319 EOF
320
321 expect_http_post_refresh
322
323 fake_json_response <<EOF
324 {
325 "access_token": "test-access",
326 "token_type": "Bearer",
327 "expires_in": 3600
328 }
329 EOF
330
331 awk 'BEGIN { for (i = 0; i < 8192; i++) { print "." } }' \
332 >> "${testname}.http-res"
333
334 test_send_only_fakehttp 'send: error refreshing OAuth2 token
335 send: refusing to process response body larger than 8192 bytes'
336
337 # TEST
338 start_test 'smtp server rejects token'
339
340 XOAUTH='not-that-one'
341
342 fake_creds <<EOF
343 access-nobody@example.com: test-access
344 expire-nobody@example.com: 2000000000
345 EOF
346
347 test_send_only_fakesmtp 'post: problem initializing server; [BHST] Not no way, not no how!
348 send: message not delivered to anyone'
349
350 # TEST
351 start_test "smtp server doesn't support oauth"
352
353 unset XOAUTH
354
355 test_send_only_fakesmtp 'post: problem initializing server; [BHST] SMTP server does not support SASL XOAUTH2
356 send: message not delivered to anyone'
357
358 # TEST
359 start_test 'botched json response'
360
361 fake_creds <<EOF
362 refresh-nobody@example.com: test-refresh
363 EOF
364
365 expect_http_post_refresh
366
367 fake_json_response <<EOF
368 {
369 "refresh_token": "refresh-toke
370 }
371 EOF
372
373 test_send_only_fakehttp 'send: error refreshing OAuth2 token
374 send: invalid response'
375
376 clean_fakesmtp
377 clean_fakehttp
378 finish_test
379
380 exit ${failed:-0}