File: test-send

package info (click to toggle)
nmh 1.8-4
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 7,860 kB
  • sloc: ansic: 50,445; sh: 22,697; makefile: 1,138; lex: 740; perl: 509; yacc: 265
file content (456 lines) | stat: -rwxr-xr-x 10,601 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#!/bin/sh
#
# Test the XOAUTH2 support in send
#

if test -z "${MH_OBJ_DIR}"; then
    srcdir=`dirname "$0"`/../..
    MH_OBJ_DIR=`cd "${srcdir}" && pwd`; export MH_OBJ_DIR
fi

. "${srcdir}/test/oauth/common.sh"

check_exit '-eq 1' send -

test_send_only_fakehttp() {
    start_fakehttp
    test_send_no_servers "$@"
    check_http_req
}

#
# success cases
#

export XOAUTH
XOAUTH='dXNlcj1ub2JvZHlAZXhhbXBsZS5jb20BYXV0aD1CZWFyZXIgdGVzdC1hY2Nlc3MBAQ=='

# TEST
start_test 'access token ready, smtp server accepts message'

setup_draft

fake_creds <<EOF
access-nobody@example.com: test-access
refresh-nobody@example.com: test-refresh
expire-nobody@example.com: 2000000000
EOF

start_fakesmtp
run_test "send -draft -server 127.0.0.1 -port ${smtp_port} -sasl -saslmech xoauth2 -authservice test -user nobody@example.com"

# TEST
start_test 'expired access token, refresh works, smtp server accepts message'

setup_draft

fake_creds <<EOF
access-nobody@example.com: old-access
refresh-nobody@example.com: test-refresh
expire-nobody@example.com: 1414303986
EOF

expect_http_post_refresh

fake_json_response <<EOF
{
  "access_token": "test-access",
  "token_type": "Bearer",
  "expires_in": 3600
}
EOF

expect_creds <<EOF
access-nobody@example.com: test-access
refresh-nobody@example.com: test-refresh
expire-nobody@example.com:
EOF

test_send

check_creds_private
check_creds

# TEST
start_test 'expired access token, refresh works and gets updated, smtp server accepts message'

setup_draft

fake_creds <<EOF
access-nobody@example.com: old-access
refresh-nobody@example.com: old-refresh
expire-nobody@example.com: 1414303986
EOF

expect_http_post_old_refresh

fake_json_response <<EOF
{
  "access_token": "test-access",
  "refresh_token": "test-refresh",
  "token_type": "Bearer"
}
EOF

expect_creds <<EOF
access-nobody@example.com: test-access
refresh-nobody@example.com: test-refresh
EOF

test_send

check_creds

# TEST
start_test 'access token has no expiration, refresh works, smtp server accepts message'

setup_draft

fake_creds <<EOF
access-nobody@example.com: old-access
refresh-nobody@example.com: test-refresh
EOF

expect_http_post_refresh

fake_json_response <<EOF
{
  "access_token": "test-access",
  "token_type": "Bearer"
}
EOF

expect_creds <<EOF
access-nobody@example.com: test-access
refresh-nobody@example.com: test-refresh
EOF

test_send

check_creds

# TEST
start_test 'no access token, refresh works, smtp server accepts message'

setup_draft

fake_creds <<EOF
refresh-nobody@example.com: test-refresh
EOF

expect_http_post_refresh

fake_json_response <<EOF
{
  "access_token": "test-access",
  "token_type": "Bearer"
}
EOF

expect_creds <<EOF
access-nobody@example.com: test-access
refresh-nobody@example.com: test-refresh
EOF

test_send

check_creds

# TEST
start_test 'fill all slots in creds table'

setup_draft

fake_creds <<EOF
access-nobody@example.com: test-access
refresh-nobody@example.com: test-refresh
expire-nobody@example.com: 2000000000
access-nobody2@example.com: test-access
refresh-nobody2@example.com: test-refresh
expire-nobody2@example.com: 2000000000
access-nobody3@example.com: test-access
refresh-nobod3y@example.com: test-refresh
expire-nobody3@example.com: 2000000000
access-nobody4@example.com: test-access
refresh-nobody4@example.com: test-refresh
expire-nobody4@example.com: 2000000000
access-nobody5@example.com: test-access
refresh-nobody5@example.com: test-refresh
expire-nobody5@example.com: 2000000000
access-nobody6@example.com: test-access
refresh-nobody6@example.com: test-refresh
expire-nobody6@example.com: 2000000000
access-nobody7@example.com: test-access
refresh-nobody7@example.com: test-refresh
expire-nobody7@example.com: 2000000000
access-nobody8@example.com: test-access
refresh-nobody8@example.com: test-refresh
expire-nobody8@example.com: 2000000000
access-nobody9@example.com: test-access
refresh-nobody9@example.com: test-refresh
expire-nobody9@example.com: 2000000000
access-nobody10@example.com: test-access
refresh-nobody10@example.com: test-refresh
expire-nobody10@example.com: 2000000000
access-nobody11@example.com: test-access
refresh-nobody11@example.com: test-refresh
expire-nobody11@example.com: 2000000000
access-nobody12@example.com: test-access
refresh-nobody12@example.com: test-refresh
expire-nobody12@example.com: 2000000000
access-nobody13@example.com: test-access
refresh-nobody13@example.com: test-refresh
expire-nobody13@example.com: 2000000000
access-nobody14@example.com: test-access
refresh-nobody14@example.com: test-refresh
expire-nobody14@example.com: 2000000000
access-nobody15@example.com: test-access
refresh-nobody15@example.com: test-refresh
expire-nobody15@example.com: 2000000000
access-nobody16@example.com: test-access
refresh-nobody16@example.com: test-refresh
expire-nobody16@example.com: 2000000000
EOF

start_fakesmtp
run_test "send -draft -server 127.0.0.1 -port ${smtp_port} -sasl -saslmech xoauth2 -authservice test -user nobody@example.com"

#
# fail cases
#

setup_draft

# TEST
start_test 'no service definition'

run_test "send -draft -server 127.0.0.1 -port ${smtp_port} -sasl -saslmech xoauth2 -authservice bogus -user nobody@example.com" 'send: Unable to retrieve oauth profile entries: scope is missing'

# TEST
start_test 'no creds file -- should tell user to mhlogin'

rm -f "${MHTMPDIR}/oauth-test"

test_send_only_fakesmtp 'post: no credentials -- run mhlogin -saslmech xoauth2 -authservice test
send: message not delivered to anyone'

# TEST
start_test 'empty creds file -- should tell user to mhlogin'

fake_creds < /dev/null

test_send_only_fakesmtp 'post: user not found in cred file: nobody@example.com
send: message not delivered to anyone'

# TEST
start_test 'garbage creds file'

echo bork | fake_creds

test_send_only_fakesmtp 'post: eof encountered in field "bork"
post: error loading cred file
send: message not delivered to anyone'

# TEST
start_test 'unexpected field in creds file'

fake_creds <<EOF
bork: bork
access-nobody@example.com: test-access
EOF

test_send_only_fakesmtp 'post: error loading cred file: unexpected field
send: message not delivered to anyone'

# TEST
start_test 'garbage expiration time'

fake_creds <<EOF
access-nobody@example.com: test-access
expire-nobody@example.com: 99999999999999999999999999999999
EOF

test_send_only_fakesmtp 'post: error loading cred file: invalid expiration time
send: message not delivered to anyone'

# TEST
start_test 'refresh response has no access token'

fake_creds <<EOF
refresh-nobody@example.com: test-refresh
EOF

expect_http_post_refresh

fake_json_response <<EOF
{
  "refresh_token": "refresh-token",
  "token_type": "Bearer"
}
EOF

test_send 'post: error refreshing OAuth2 token
post: invalid response: no access token
send: message not delivered to anyone'

# TEST
start_test 'expired access token, no refresh token -- tell user to mhlogin'

fake_creds <<EOF
access-nobody@example.com: test-access
expire-nobody@example.com: 1414303986
EOF

test_send_only_fakesmtp 'post: no valid credentials -- run mhlogin -saslmech xoauth2 -authservice test
send: message not delivered to anyone'

# TEST
start_test 'access token has no expiration, no refresh token -- tell user to mhlogin'

fake_creds <<EOF
access-nobody@example.com: test-access
EOF

test_send_only_fakesmtp 'post: no valid credentials -- run mhlogin -saslmech xoauth2 -authservice test
send: message not delivered to anyone'

# TEST
start_test 'refresh finds no http server'

fake_creds <<EOF
access-nobody@example.com: test-access
refresh-nobody@example.com: test-refresh
EOF

cat > "${testname}.expected-send-output" <<EOF
post: error refreshing OAuth2 token
post: error making HTTP request to OAuth2 authorization endpoint: [details]
send: message not delivered to anyone
EOF

start_fakesmtp
run_prog send -draft -server 127.0.0.1 -port ${smtp_port} \
  -sasl -saslmech xoauth2 -authservice test -user nobody@example.com > "${testname}.send-output" 2>&1 || true
# Clear out an error message we get from libcurl on some systems (seen at least
# 3 different versions of this error message, on FreeBSD 10.1, Ubuntu 12.04, and
# Ubuntu 14.04).
f="${testname}.send-output"
sed 's/\(post: error making HTTP request to OAuth2 authorization endpoint:\).*/\1 [details]/' "$f" > "$f".clean
check "$f".clean "${testname}.expected-send-output"
rm "$f"

# TEST
start_test 'refresh gets bogus 200 response from http server'

expect_http_post_refresh

fake_http_response '200 OK' <<EOF
Content-Type: text/html

<html>doh!</htmxl>
EOF

test_send 'post: error refreshing OAuth2 token
post: invalid response
send: message not delivered to anyone'

# TEST
start_test 'refresh gets 500 response from http server'

expect_http_post_refresh

fake_http_response '500 Server Error' <<EOF
Content-Type: text/html

<html>doh!</html>
EOF

test_send 'post: error refreshing OAuth2 token
post: invalid response
send: message not delivered to anyone'

# TEST
start_test 'refresh gets proper error from http'

expect_http_post_refresh

fake_http_response '400 Bad Request' <<EOF
Content-Type: application/json

{
  "error": "invalid_grant"
}
EOF

test_send 'post: credentials rejected -- run mhlogin -saslmech xoauth2 -authservice test
send: message not delivered to anyone'

# TEST
start_test 'refresh gets response too big'

fake_creds <<EOF
refresh-nobody@example.com: test-refresh
EOF

expect_http_post_refresh

fake_json_response <<EOF
{
  "access_token": "test-access",
  "token_type": "Bearer",
  "expires_in": 3600
}
EOF

awk 'BEGIN { for (i = 0; i < 8192; i++) { print "." } }' \
    >> "${testname}.http-res"

test_send 'post: error refreshing OAuth2 token
post: refusing to process response body larger than 8192 bytes
send: message not delivered to anyone'

# TEST
start_test 'smtp server rejects token'

XOAUTH='not-that-one'

fake_creds <<EOF
access-nobody@example.com: test-access
expire-nobody@example.com: 2000000000
EOF

test_send_only_fakesmtp 'post: problem initializing server; [BHST] Authentication failed: Not no way, not no how!
send: message not delivered to anyone'

# TEST
start_test 'botched json response'

fake_creds <<EOF
refresh-nobody@example.com: test-refresh
EOF

expect_http_post_refresh

fake_json_response <<EOF
{
  "refresh_token": "refresh-toke
}
EOF

test_send 'post: error refreshing OAuth2 token
post: invalid response
send: message not delivered to anyone'

# TEST
start_test "smtp server doesn't support oauth"

unset XOAUTH

test_send_only_fakesmtp 'post: problem initializing server; [BHST] SMTP server does not support SASL
send: message not delivered to anyone'

clean_fakesmtp
clean_fakehttp
finish_test

exit ${failed:-0}