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 echo 'access token ready, smtp server accepts message'
38 run_test
"send -draft -server 127.0.0.1 -port ${smtp_port} -oauth test -user nobody@example.com"
41 echo 'expired access token, refresh works, smtp server accepts message'
51 expect_http_post_refresh
53 fake_json_response
<<EOF
55 "access_token": "test-access",
56 "token_type": "Bearer",
73 echo 'expired access token, refresh works and gets updated, smtp server accepts message'
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"
103 echo 'access token has no expiration, refresh works, smtp server accepts message'
109 refresh: test-refresh
112 expect_http_post_refresh
114 fake_json_response
<<EOF
116 "access_token": "test-access",
117 "token_type": "Bearer"
123 refresh: test-refresh
131 echo 'no access token, refresh works, smtp server accepts message'
136 refresh: test-refresh
139 expect_http_post_refresh
141 fake_json_response
<<EOF
143 "access_token": "test-access",
144 "token_type": "Bearer"
150 refresh: test-refresh
164 echo 'no service definition'
166 run_test
"send -draft -server 127.0.0.1 -port ${smtp_port} -oauth bogus -user nobody@example.com" 'send: incomplete OAuth2 service definition: scope is missing'
169 echo 'no creds file -- should tell user to mhlogin'
171 rm -f "${MHTMPDIR}/oauth-test"
173 test_send_no_servers
'send: no credentials -- run mhlogin -oauth test'
176 echo 'empty creds file -- should tell user to mhlogin'
178 fake_creds
< /dev
/null
180 test_send_no_servers
'send: no valid credentials -- run mhlogin -oauth test'
183 echo '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 echo 'unexpected field in creds file'
198 test_send_no_servers
'send: error loading cred file: unexpected field'
201 echo 'garbage expiration time'
205 expire: 99999999999999999999999999999999
208 test_send_no_servers
'send: error loading cred file: invalid expiration time'
211 echo 'refresh response has no access token'
214 refresh: 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 echo 'expired access token, no refresh token -- tell user to mhlogin'
237 test_send_no_servers
'send: no valid credentials -- run mhlogin -oauth test'
240 echo 'access token has no expiration, no refresh token -- tell user to mhlogin'
246 test_send_no_servers
'send: no valid credentials -- run mhlogin -oauth test'
249 echo 'refresh finds no http server'
253 refresh: 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 -oauth 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 echo '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 echo '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 echo '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 -oath test'
315 echo 'refresh gets response too big'
318 refresh: 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 echo 'smtp server rejects token'
340 XOAUTH
='not-that-one'
347 test_send_only_fakesmtp
'post: problem initializing server; [BHST] Not no way, not no how!
348 send: message not delivered to anyone'
351 echo "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'