3 # Test the XOAUTH2 support in sen
6 if test -z "${MH_OBJ_DIR}"; then
7 srcdir
=`dirname "$0"`/..
/..
8 MH_OBJ_DIR
=`cd "${srcdir}" && pwd`; export MH_OBJ_DIR
11 .
"${srcdir}/test/oauth/common.sh"
13 test_send_only_fakehttp
() {
15 test_send_no_servers
"$@"
24 XOAUTH
='dXNlcj1ub2JvZHlAZXhhbXBsZS5jb20BYXV0aD1CZWFyZXIgdGVzdC1hY2Nlc3MBAQ=='
27 start_test
'access token ready, smtp server accepts message'
32 access-nobody@example.com: test-access
33 refresh-nobody@example.com: test-refresh
34 expire-nobody@example.com: 2000000000
38 run_test
"send -draft -server 127.0.0.1 -port ${smtp_port} -saslmech xoauth2 -authservice test -user nobody@example.com"
41 start_test
'expired access token, refresh works, smtp server accepts message'
46 access-nobody@example.com: old-access
47 refresh-nobody@example.com: test-refresh
48 expire-nobody@example.com: 1414303986
51 expect_http_post_refresh
53 fake_json_response
<<EOF
55 "access_token": "test-access",
56 "token_type": "Bearer",
62 access-nobody@example.com: test-access
63 refresh-nobody@example.com: test-refresh
64 expire-nobody@example.com:
73 start_test
'expired access token, refresh works and gets updated, smtp server accepts message'
78 access-nobody@example.com: old-access
79 refresh-nobody@example.com: old-refresh
80 expire-nobody@example.com: 1414303986
83 expect_http_post_old_refresh
85 fake_json_response
<<EOF
87 "access_token": "test-access",
88 "refresh_token": "test-refresh",
89 "token_type": "Bearer"
94 access-nobody@example.com: test-access
95 refresh-nobody@example.com: test-refresh
103 start_test
'access token has no expiration, refresh works, smtp server accepts message'
108 access-nobody@example.com: old-access
109 refresh-nobody@example.com: test-refresh
112 expect_http_post_refresh
114 fake_json_response
<<EOF
116 "access_token": "test-access",
117 "token_type": "Bearer"
122 access-nobody@example.com: test-access
123 refresh-nobody@example.com: test-refresh
131 start_test
'no access token, refresh works, smtp server accepts message'
136 refresh-nobody@example.com: test-refresh
139 expect_http_post_refresh
141 fake_json_response
<<EOF
143 "access_token": "test-access",
144 "token_type": "Bearer"
149 access-nobody@example.com: test-access
150 refresh-nobody@example.com: test-refresh
164 start_test
'no service definition'
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'
169 start_test
'no creds file -- should tell user to mhlogin'
171 rm -f "${MHTMPDIR}/oauth-test"
173 test_send_no_servers
'send: no credentials -- run mhlogin -saslmech xoauth2 -authservice test'
176 start_test
'empty creds file -- should tell user to mhlogin'
178 fake_creds
< /dev
/null
180 test_send_no_servers
'send: user not found in cred file: nobody@example.com'
183 start_test
'garbage creds file'
185 echo bork
| fake_creds
187 test_send_no_servers
'send: eof encountered in field "bork"
188 send: error loading cred file'
191 start_test
'unexpected field in creds file'
195 access-nobody@example.com: test-access
198 test_send_no_servers
'send: error loading cred file: unexpected field'
201 start_test
'garbage expiration time'
204 access-nobody@example.com: test-access
205 expire-nobody@example.com: 99999999999999999999999999999999
208 test_send_no_servers
'send: error loading cred file: invalid expiration time'
211 start_test
'refresh response has no access token'
214 refresh-nobody@example.com: test-refresh
217 expect_http_post_refresh
219 fake_json_response
<<EOF
221 "refresh_token": "refresh-token",
222 "token_type": "Bearer"
226 test_send_only_fakehttp
'send: error refreshing OAuth2 token
227 send: invalid response: no access token'
230 start_test
'expired access token, no refresh token -- tell user to mhlogin'
233 access-nobody@example.com: test-access
234 expire-nobody@example.com: 1414303986
237 test_send_no_servers
'send: no valid credentials -- run mhlogin -saslmech xoauth2 -authservice test'
240 start_test
'access token has no expiration, no refresh token -- tell user to mhlogin'
243 access-nobody@example.com: test-access
246 test_send_no_servers
'send: no valid credentials -- run mhlogin -saslmech xoauth2 -authservice test'
249 start_test
'refresh finds no http server'
252 access-nobody@example.com: test-access
253 refresh-nobody@example.com: test-refresh
256 cat > "${testname}.expected-send-output" <<EOF
257 send: error refreshing OAuth2 token
258 send: error making HTTP request to OAuth2 authorization endpoint: [details]
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
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"
272 start_test
'refresh gets bogus 200 response from http server'
274 expect_http_post_refresh
276 fake_http_response
'200 OK' <<EOF
277 Content-Type: text/html
282 test_send_only_fakehttp
'send: error refreshing OAuth2 token
283 send: invalid response'
286 start_test
'refresh gets 500 response from http server'
288 expect_http_post_refresh
290 fake_http_response
'500 Server Error' <<EOF
291 Content-Type: text/html
296 test_send_only_fakehttp
'send: error refreshing OAuth2 token
297 send: invalid response'
300 start_test
'refresh gets proper error from http'
302 expect_http_post_refresh
304 fake_http_response
'400 Bad Request' <<EOF
305 Content-Type: application/json
308 "error": "invalid_grant"
312 test_send_only_fakehttp
'send: credentials rejected -- run mhlogin -saslmech xoauth2 -authservice test'
315 start_test
'refresh gets response too big'
318 refresh-nobody@example.com: test-refresh
321 expect_http_post_refresh
323 fake_json_response
<<EOF
325 "access_token": "test-access",
326 "token_type": "Bearer",
331 awk 'BEGIN { for (i = 0; i < 8192; i++) { print "." } }' \
332 >> "${testname}.http-res"
334 test_send_only_fakehttp
'send: error refreshing OAuth2 token
335 send: refusing to process response body larger than 8192 bytes'
338 start_test
'smtp server rejects token'
340 XOAUTH
='not-that-one'
343 access-nobody@example.com: test-access
344 expire-nobody@example.com: 2000000000
347 test_send_only_fakesmtp
'post: problem initializing server; [BHST] Not no way, not no how!
348 send: message not delivered to anyone'
351 start_test
"smtp server doesn't support oauth"
355 test_send_only_fakesmtp
'post: problem initializing server; [BHST] SMTP server does not support SASL XOAUTH2
356 send: message not delivered to anyone'
359 start_test
'botched json response'
362 refresh-nobody@example.com: test-refresh
365 expect_http_post_refresh
367 fake_json_response
<<EOF
369 "refresh_token": "refresh-toke
373 test_send_only_fakehttp
'send: error refreshing OAuth2 token
374 send: invalid response'