1 # Common routines for OAuth tests
3 .
"${MH_OBJ_DIR}/test/common.sh"
7 if [ "${OAUTH_SUPPORT}" -eq 0 ]; then
8 test_skip
'no oauth support'
11 testname
="${MH_TEST_DIR}/$$"
13 arith_eval
64001 + $$
% 1000
14 http_port
=${arith_val}
16 arith_eval
${http_port} + 1
19 arith_eval
${pop_port} + 1
20 smtp_port
=${arith_val}
23 oauth-test-scope: test-scope
24 oauth-test-client_id: test-id
25 oauth-test-client_secret: test-secret
26 oauth-test-auth_endpoint: http://127.0.0.1:${http_port}/oauth/auth
27 oauth-test-token_endpoint: http://127.0.0.1:${http_port}/oauth/token
28 oauth-test-redirect_uri: urn:ietf:wg:oauth:2.0:oob
32 pop_message
=${MHTMPDIR}/testmessage
33 cat > "${pop_message}" <<EOM
34 Received: From somewhere
35 From: No Such User <nosuch@example.com>
36 To: Some Other User <someother@example.com>
38 Date: Sun, 17 Dec 2006 12:13:14 -0500
45 cat > "${MH_TEST_DIR}/Mail/draft" <<EOF
46 From: Mr Nobody <nobody@example.com>
47 To: Somebody Else <somebody@example.com>
50 Content-Type: text/plain; charset="us-ascii"
57 "${MH_OBJ_DIR}/test/fakehttp" "${testname}.http-req" ${http_port} \
58 "${testname}.http-res" > /dev
/null
62 "${MH_OBJ_DIR}/test/fakepop" "${pop_port}" "$1" "$2" "${pop_message}" \
68 'dXNlcj1ub2JvZHlAZXhhbXBsZS5jb20BYXV0aD1CZWFyZXIgdGVzdC1hY2Nlc3MBAQ=='
72 "${MH_OBJ_DIR}/test/fakesmtp" "${testname}.smtp-req" ${smtp_port} \
77 rm "${testname}.smtp-req"
81 cat > "${MHTMPDIR}/oauth-test"
84 fake_http_response
() {
85 echo "HTTP/1.1 $1" > "${testname}.http-res"
86 cat >> "${testname}.http-res"
90 rm -f "${testname}.http-res"
93 fake_json_response
() {
94 (echo 'Content-Type: application/json';
96 cat) | fake_http_response
'200 OK'
99 # The format of the POST request is mostly dependent on curl, and could possibly
100 # change with newer or older curl versions, or by configuration. curl 7.39.0
101 # makes POST requests like this on FreeBSD 10 and Ubuntu 14.04. If you find
102 # this failing, you'll need to make this a smarter comparison. Note that it is
103 # sorted, so that it doesn't depend on the JSON being in a specific order.
105 sort > "${testname}.expected-http-req" <<EOF
106 POST /oauth/token HTTP/1.1
107 User-Agent: nmh/${MH_VERSION} ${CURL_USER_AGENT}
108 Host: 127.0.0.1:${http_port}
111 Content-Type: application/x-www-form-urlencoded
117 expect_http_post_code
() {
118 expect_http_post
132 'code=code&grant_type=authorization_code&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&client_id=test-id&client_secret=test-secret'
121 expect_http_post_refresh
() {
122 expect_http_post
95 'grant_type=refresh_token&refresh_token=test-refresh&client_id=test-id&client_secret=test-secret'
125 expect_http_post_old_refresh
() {
126 expect_http_post
94 'grant_type=refresh_token&refresh_token=old-refresh&client_id=test-id&client_secret=test-secret'
130 cat > "${MHTMPDIR}/$$.expected-creds"
134 run_test
"inc -host 127.0.0.1 -port ${pop_port} -sasl -saslmech xoauth2 -authservice test -user nobody@example.com -width 80" "$@"
138 test_inc
'Incorporating new mail into inbox...
140 11+ 12/17 No Such User Hello<<Hey man >>'
141 check
"${pop_message}" "`mhpath +inbox 11`" 'keep first'
144 test_send_no_servers
() {
145 run_test
"send -draft -server 127.0.0.1 -port ${smtp_port} -sasl -saslmech xoauth2 -authservice test -user nobody@example.com" "$@"
148 test_send_only_fakesmtp
() {
150 test_send_no_servers
"$@"
155 test_send_only_fakesmtp
"$@"
156 sort -o "${testname}.http-req.sorted" "${testname}.http-req"
157 rm "${testname}.http-req"
158 check
"${testname}.http-req.sorted" "${testname}.expected-http-req"
162 sort -o "${testname}.http-req.sorted" "${testname}.http-req"
163 rm "${testname}.http-req"
164 check
"${testname}.http-req.sorted" "${testname}.expected-http-req"
167 check_creds_private
() {
168 f
="${MHTMPDIR}/oauth-test"
169 if ls -dl "$f" | grep '^-rw-------' > /dev
/null
2>&1; then
172 echo "$f permissions not private"
173 failed
=`expr ${failed:-0} + 1`
178 # It's hard to calculate the exact expiration time mhlogin is going to use,
179 # so we'll just use sed to remove the actual time so we can easily compare
180 # it against our "correct" output.
181 f
="${MHTMPDIR}/oauth-test"
183 sed 's/^\(expire.*:\).*/\1/' "$f" > "$f".notime
184 check
"$f".notime
"${MHTMPDIR}/$$.expected-creds"