]> diplodocus.org Git - nmh/blob - test/oauth/common.sh
Replace getcpy() with mh_xstrdup() where the string isn't NULL.
[nmh] / test / oauth / common.sh
1 # Common routines for OAuth tests
2
3 . "${MH_OBJ_DIR}/test/common.sh"
4
5 setup_test
6
7 if [ "${OAUTH_SUPPORT}" -eq 0 ]; then
8 test_skip 'no oauth support'
9 fi
10
11 testname="${MH_TEST_DIR}/$$"
12
13 arith_eval 64001 + $$ % 1000
14 http_port=${arith_val}
15
16 arith_eval ${http_port} + 1
17 pop_port=${arith_val}
18
19 arith_eval ${pop_port} + 1
20 smtp_port=${arith_val}
21
22 cat >> ${MH} <<EOF
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
29 EOF
30
31 setup_pop() {
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>
37 Subject: Hello
38 Date: Sun, 17 Dec 2006 12:13:14 -0500
39
40 Hey man
41 EOM
42 }
43
44 setup_draft() {
45 cat > "${MH_TEST_DIR}/Mail/draft" <<EOF
46 From: Mr Nobody <nobody@example.com>
47 To: Somebody Else <somebody@example.com>
48 Subject: Test
49 MIME-Version: 1.0
50 Content-Type: text/plain; charset="us-ascii"
51
52 This is a test
53 EOF
54 }
55
56 start_fakehttp() {
57 "${MH_OBJ_DIR}/test/fakehttp" "${testname}.http-req" ${http_port} \
58 "${testname}.http-res" > /dev/null
59 }
60
61 start_pop() {
62 "${MH_OBJ_DIR}/test/fakepop" "${pop_port}" "$1" "$2" "${pop_message}" \
63 > /dev/null
64 }
65
66 start_pop_xoauth() {
67 start_pop XOAUTH \
68 'dXNlcj1ub2JvZHlAZXhhbXBsZS5jb20BYXV0aD1CZWFyZXIgdGVzdC1hY2Nlc3MBAQ=='
69 }
70
71 start_fakesmtp() {
72 "${MH_OBJ_DIR}/test/fakesmtp" "${testname}.smtp-req" ${smtp_port} \
73 > /dev/null
74 }
75
76 clean_fakesmtp() {
77 rm "${testname}.smtp-req"
78 }
79
80 fake_creds() {
81 cat > "${MHTMPDIR}/oauth-test"
82 }
83
84 fake_http_response() {
85 echo "HTTP/1.1 $1" > "${testname}.http-res"
86 cat >> "${testname}.http-res"
87 }
88
89 clean_fakehttp() {
90 rm -f "${testname}.http-res"
91 }
92
93 fake_json_response() {
94 (echo 'Content-Type: application/json';
95 echo;
96 cat) | fake_http_response '200 OK'
97 }
98
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.
104 expect_http_post() {
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}
109 Accept: */*
110 Content-Length: $1
111 Content-Type: application/x-www-form-urlencoded
112
113 $2
114 EOF
115 }
116
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'
119 }
120
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'
123 }
124
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'
127 }
128
129 expect_creds() {
130 cat > "${MHTMPDIR}/$$.expected-creds"
131 }
132
133 test_inc() {
134 run_test "inc -host 127.0.0.1 -port ${pop_port} -sasl -saslmech xoauth2 -authservice test -user nobody@example.com -width 80" "$@"
135 }
136
137 test_inc_success() {
138 test_inc 'Incorporating new mail into inbox...
139
140 11+ 12/17 No Such User Hello<<Hey man >>'
141 check "${pop_message}" "`mhpath +inbox 11`" 'keep first'
142 }
143
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" "$@"
146 }
147
148 test_send_only_fakesmtp() {
149 start_fakesmtp
150 test_send_no_servers "$@"
151 }
152
153 test_send() {
154 start_fakehttp
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"
159 }
160
161 check_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"
165 }
166
167 check_creds_private() {
168 f="${MHTMPDIR}/oauth-test"
169 if ls -dl "$f" | grep '^-rw-------' > /dev/null 2>&1; then
170 :
171 else
172 echo "$f permissions not private"
173 failed=`expr ${failed:-0} + 1`
174 fi
175 }
176
177 check_creds() {
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"
182
183 sed 's/^\(expire.*:\).*/\1/' "$f" > "$f".notime
184 check "$f".notime "${MHTMPDIR}/$$.expected-creds"
185 rm "$f"
186 }