This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
test_incoming_prober.js
654 lines (591 loc) · 17.7 KB
/
test_incoming_prober.js
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/**
* Test the IMAP and POP3 probers in isolation.
*
* None of these test actually establish a network connection. They all use
* FawltySocketFactory to generate canned failures. We test timeouts by mocking
* out the setTimeout/clearTimeout used by imap.js so we can log when timers
* are set/cleared and we can control exactly when the timers fire.
*/
define(['rdcommon/testcontext', './resources/th_main',
'./resources/fault_injecting_socket', 'mailapi/imap/probe', 'imap',
'mailapi/pop3/probe', 'pop3/pop3', 'exports'],
function($tc, $th_main, $fawlty, $imapProbe,
$imap, $pop3Probe, $pop3, exports) {
var FawltySocketFactory = $fawlty.FawltySocketFactory;
var TD = exports.TD = $tc.defineTestsFor(
{ id: 'test_incoming_prober' }, null, [$th_main.TESTHELPER], ['app']);
function thunkTimeouts(lazyLogger) {
var timeouts = [];
function thunkedSetTimeout(func, delay) {
lazyLogger.namedValue('incoming:setTimeout', delay);
return timeouts.push(func);
}
function thunkedClearTimeout() {
lazyLogger.event('incoming:clearTimeout');
}
$imap.TEST_useTimeoutFuncs(thunkedSetTimeout, thunkedClearTimeout);
$pop3.setTimeoutFuncs(thunkedSetTimeout, thunkedClearTimeout);
return function fireThunkedTimeout(index) {
timeouts[index]();
timeouts[index] = null;
};
}
function proberTimeout(RT) {
if (RT.envOptions.type === "imap") {
return $imapProbe.CONNECT_TIMEOUT_MS;
} else if (RT.envOptions.type === "pop3") {
return $pop3Probe.CONNECT_TIMEOUT_MS;
}
}
function proberClass(RT) {
if (RT.envOptions.type === "imap") {
return $imapProbe.ImapProber;
} else if (RT.envOptions.type === "pop3") {
return $pop3Probe.Pop3Prober;
}
}
function openResponse(RT) {
if (RT.envOptions.type === "imap") {
return '* OK IMAP rules, POP3 drools\r\n';
} else if (RT.envOptions.type === "pop3") {
return '+OK POP3 READY\r\n';
}
}
function badStarttlsResponse(RT) {
if (RT.envOptions.type === "imap") {
return 'A1 BAD STARTTLS Unsupported\r\n';
} else if (RT.envOptions.type === "pop3") {
return '-ERR no starttls\r\n';
}
}
function capabilityResponse(RT) {
if (RT.envOptions.type === "imap") {
return [
'* CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE STARTTLS AUTH=PLAIN',
'A1 OK Pre-login capabilities listed, post-login capabilities have more.',
].join('\r\n') + '\r\n';
} else if (RT.envOptions.type === "pop3") {
return '+OK\r\n.\r\n';
}
}
// Currently all the tests in here are completely fake; we never connect.
const HOST = 'localhost', PORT = 65535;
function makeCredsAndConnInfo() {
return {
credentials: {
username: 'USERNAME',
password: 'PASSWORD',
},
connInfo: {
hostname: HOST,
port: PORT,
crypto: true,
},
};
}
TD.commonCase('timeout failure', function(T, RT) {
$th_main.thunkConsoleForNonTestUniverse();
var eCheck = T.lazyLogger('check'),
prober = null;
var fireTimeout = thunkTimeouts(eCheck);
var cci = makeCredsAndConnInfo();
T.action(eCheck, 'create prober', function() {
FawltySocketFactory.precommand(HOST, PORT, 'unresponsive-server');
eCheck.expect_namedValue('incoming:setTimeout', proberTimeout(RT));
prober = new (proberClass(RT))(
cci.credentials, cci.connInfo, eCheck._logger);
prober.onresult = function(err) {
eCheck.namedValue('probe result', err);
};
});
T.action(eCheck, 'trigger timeout', function() {
eCheck.expect_event('incoming:clearTimeout');
eCheck.expect_namedValue('probe result', 'unresponsive-server');
fireTimeout(0);
});
});
TD.commonCase('SSL failure', function(T, RT) {
$th_main.thunkConsoleForNonTestUniverse();
var eCheck = T.lazyLogger('check'),
prober = null;
var fireTimeout = thunkTimeouts(eCheck);
var cci = makeCredsAndConnInfo();
T.action(eCheck, 'create prober, see SSL error', function() {
FawltySocketFactory.precommand(HOST, PORT, 'bad-security');
eCheck.expect_namedValue('incoming:setTimeout', proberTimeout(RT));
prober = new (proberClass(RT))(
cci.credentials, cci.connInfo, eCheck._logger);
prober.onresult = function(err) {
eCheck.namedValue('probe result', err);
};
eCheck.expect_event('incoming:clearTimeout');
eCheck.expect_namedValue('probe result', 'bad-security');
});
});
TD.commonCase('STARTTLS unsupported', function(T, RT) {
$th_main.thunkConsoleForNonTestUniverse();
var eCheck = T.lazyLogger('check'),
prober = null;
var fireTimeout = thunkTimeouts(eCheck);
var cci = makeCredsAndConnInfo();
cci.connInfo.port = PORT;
cci.connInfo.crypto = 'starttls';
T.action(eCheck, 'create prober, no STARTTLS response', function() {
var precommands = [];
precommands.push({
match: /TLS/,
actions: [
{
cmd: 'fake-receive',
data: badStarttlsResponse(RT),
}
],
});
FawltySocketFactory.precommand(
HOST, cci.connInfo.port,
{
cmd: 'fake',
data: openResponse(RT)
},
precommands);
eCheck.expect_namedValue('incoming:setTimeout', proberTimeout(RT));
prober = new (proberClass(RT))(
cci.credentials, cci.connInfo, eCheck._logger);
prober.onresult = function(err) {
eCheck.namedValue('probe result', err);
};
eCheck.expect_event('incoming:clearTimeout');
eCheck.expect_namedValue('probe result', 'bad-security');
});
});
TD.commonCase('POP3 UIDL unsupported', function(T, RT) {
if (RT.envOptions.type !== 'pop3') { return; }
$th_main.thunkConsoleForNonTestUniverse();
var eCheck = T.lazyLogger('check'),
prober = null;
var fireTimeout = thunkTimeouts(eCheck);
var cci = makeCredsAndConnInfo();
cci.connInfo.port = PORT;
T.action(eCheck, 'respond to initial probes with nope', function() {
var precommands = [];
precommands.push({
match: /AUTH/,
actions: [
{
cmd: 'fake-receive',
data: '+OK nope\r\n',
},
],
});
precommands.push({
match: /UIDL/,
actions: [
{
cmd: 'fake-receive',
data: '-ERR nope\r\n',
},
],
});
precommands.push({
match: /UIDL/,
actions: [
{
cmd: 'fake-receive',
data: '-ERR nope\r\n',
},
],
});
FawltySocketFactory.precommand(
HOST, PORT,
{
cmd: 'fake',
data: '+OK hey\r\n',
},
precommands);
eCheck.expect_namedValue('incoming:setTimeout', proberTimeout(RT));
prober = new (proberClass(RT))(
cci.credentials, cci.connInfo, eCheck._logger);
prober.onresult = function(err) {
eCheck.namedValue('probe result', err);
};
eCheck.expect_event('incoming:clearTimeout');
eCheck.expect_namedValue('probe result', 'pop-server-not-great');
});
});
TD.commonCase('POP3 TOP unsupported', function(T, RT) {
if (RT.envOptions.type !== 'pop3') { return; }
$th_main.thunkConsoleForNonTestUniverse();
var eCheck = T.lazyLogger('check'),
prober = null;
var fireTimeout = thunkTimeouts(eCheck);
var cci = makeCredsAndConnInfo();
cci.connInfo.port = PORT;
T.action(eCheck, 'respond to initial probes with nope', function() {
var precommands = [];
precommands.push({
match: /AUTH/,
actions: [
{
cmd: 'fake-receive',
data: '+OK nope\r\n',
},
],
});
precommands.push({
match: /UIDL 1/,
actions: [
{
cmd: 'fake-receive',
data: '+OK\r\n',
},
],
});
precommands.push({
match: /TOP/,
actions: [
{
cmd: 'fake-receive',
data: '-ERR nope\r\n',
},
],
});
FawltySocketFactory.precommand(
HOST, PORT,
{
cmd: 'fake',
data: '+OK hey\r\n',
},
precommands);
eCheck.expect_namedValue('incoming:setTimeout', proberTimeout(RT));
prober = new (proberClass(RT))(
cci.credentials, cci.connInfo, eCheck._logger);
prober.onresult = function(err) {
eCheck.namedValue('probe result', err);
};
eCheck.expect_event('incoming:clearTimeout');
eCheck.expect_namedValue('probe result', 'pop-server-not-great');
});
});
/**
* Attempt to connect with SASL AUTH first, skipping APOP because
* there was no greeting timestamp. Successfully auth with USER/PASS
* and make sure we report that's the one that worked.
*/
TD.commonCase('POP3 selects preferredAuthMethod', function(T, RT) {
if (RT.envOptions.type !== 'pop3') { return; }
$th_main.thunkConsoleForNonTestUniverse();
var eCheck = T.lazyLogger('check'),
prober = null;
var fireTimeout = thunkTimeouts(eCheck);
var cci = makeCredsAndConnInfo();
cci.connInfo.port = PORT;
T.action(eCheck, 'works with USER-PASS auth', function() {
var precommands = [];
precommands.push({
match: /AUTH/,
actions: [
{
cmd: 'fake-receive',
data: '-ERR\r\n',
},
],
});
precommands.push({
match: /USER/,
actions: [
{
cmd: 'fake-receive',
data: '+OK\r\n',
},
],
});
precommands.push({
match: /PASS/,
actions: [
{
cmd: 'fake-receive',
data: '+OK\r\n',
},
],
});
precommands.push({
match: /UIDL 1/,
actions: [
{
cmd: 'fake-receive',
data: '+OK\r\n',
},
],
});
precommands.push({
match: /TOP 1/,
actions: [
{
cmd: 'fake-receive',
data: '+OK\r\n.\r\n',
},
],
});
FawltySocketFactory.precommand(
HOST, PORT,
{
cmd: 'fake',
data: '+OK hey\r\n',
},
precommands);
eCheck.expect_namedValue('incoming:setTimeout', proberTimeout(RT));
prober = new (proberClass(RT))(cci.credentials, cci.connInfo, eCheck._logger);
prober.onresult = function(err, conn) {
eCheck.namedValue('authMethod', conn.authMethod);
};
eCheck.expect_event('incoming:clearTimeout');
eCheck.expect_namedValue('authMethod', 'user-pass');
});
});
/**
* Ensure that APOP authentication works properly.
*/
TD.commonCase('POP3 APOP', function(T, RT) {
if (RT.envOptions.type !== 'pop3') { return; }
$th_main.thunkConsoleForNonTestUniverse();
var eCheck = T.lazyLogger('check'),
prober = null;
var fireTimeout = thunkTimeouts(eCheck);
var cci = makeCredsAndConnInfo();
cci.connInfo.port = PORT;
T.action(eCheck, 'works with APOP', function() {
var precommands = [];
precommands.push({
match: /APOP/,
actions: [
{
cmd: 'fake-receive',
data: '+OK\r\n',
},
],
});
precommands.push({
match: /UIDL 1/,
actions: [
{
cmd: 'fake-receive',
data: '+OK\r\n',
},
],
});
precommands.push({
match: /TOP 1/,
actions: [
{
cmd: 'fake-receive',
data: '+OK\r\n.\r\n',
},
],
});
FawltySocketFactory.precommand(
HOST, PORT,
{
cmd: 'fake',
data: '+OK POP3 Ready <apop@apop>\r\n',
},
precommands);
eCheck.expect_namedValue('incoming:setTimeout', proberTimeout(RT));
prober = new (proberClass(RT))(cci.credentials, cci.connInfo, eCheck._logger);
prober.onresult = function(err, conn) {
eCheck.namedValue('authMethod', conn.authMethod);
};
eCheck.expect_event('incoming:clearTimeout');
eCheck.expect_namedValue('authMethod', 'apop');
});
});
/**
* Some servers (ex: aol.com) will hang-up on us on an auth error with a bad
* password.
*/
TD.commonCase('POP3 bad creds on server that hangs up', function(T, RT) {
if (RT.envOptions.type !== 'pop3') { return; }
$th_main.thunkConsoleForNonTestUniverse();
var eCheck = T.lazyLogger('check'),
prober = null;
var fireTimeout = thunkTimeouts(eCheck);
var cci = makeCredsAndConnInfo();
cci.connInfo.port = PORT;
T.action(eCheck, 'hangup gives bad-user-or-pass', function() {
var precommands = [];
precommands.push({
match: /AUTH/,
actions: [
{
cmd: 'fake-receive',
data: '-ERR\r\n',
},
{
cmd: 'instant-close',
}
],
});
FawltySocketFactory.precommand(
HOST, PORT,
{
cmd: 'fake',
data: '+OK hey\r\n',
},
precommands);
eCheck.expect_namedValue('incoming:setTimeout', proberTimeout(RT));
prober = new (proberClass(RT))(cci.credentials, cci.connInfo, eCheck._logger);
prober.onresult = function(err, conn) {
eCheck.namedValue('err', err);
};
eCheck.expect_event('incoming:clearTimeout');
eCheck.expect_namedValue('err', 'bad-user-or-pass');
});
});
var KEEP_ALIVE_TIMEOUT_MS = 10000;
function cannedLoginTest(T, RT, opts) {
$th_main.thunkConsoleForNonTestUniverse();
var eCheck = T.lazyLogger('check');
var fireTimeout = thunkTimeouts(eCheck),
cci = makeCredsAndConnInfo(),
prober;
T.action('connect, get error, return', eCheck, function() {
eCheck.expect_namedValue('incoming:setTimeout', proberTimeout(RT));
eCheck.expect_event('incoming:clearTimeout');
eCheck.expect_namedValue('probe result', opts.expectResult);
// Even though we will fail to login, from the IMAP connection's
// perspective we won't want the connection to die.
// ...And now I've restored the original event functionality.
//eCheck.expect_namedValue('incoming:setTimeout', KEEP_ALIVE_TIMEOUT_MS);
var precommands = [];
if (RT.envOptions.type === 'pop3') {
// If SASL AUTH fails, POP3 just tries USER blindly.
// So push a failing SASL auth here, and then we'll respond to
// the login error after USER.
precommands.push({
match: true,
actions: [
{
cmd: 'fake-receive',
data: '-ERR\r\n',
},
],
});
} else { // IMAP
precommands.push({
match: /CAPABILITY/,
actions: [
{
cmd: 'fake-receive',
data: opts.capabilityResponse || capabilityResponse(RT),
},
],
});
}
precommands.push({
match: true,
actions: [
{
cmd: 'fake-receive',
data: (RT.envOptions.type === 'imap' ? 'A2 ' : '-ERR ') +
opts.loginErrorString + '\r\n',
}
],
});
FawltySocketFactory.precommand(
HOST, PORT,
{
cmd: 'fake',
data: opts.openResponse || openResponse(RT),
},
precommands);
prober = new (proberClass(RT))(
cci.credentials, cci.connInfo, eCheck._logger);
prober.onresult = function(err) {
eCheck.namedValue('probe result', err);
};
});
};
TD.commonCase('gmail 2-factor auth error', function(T, RT) {
cannedLoginTest(T, RT, {
loginErrorString: (RT.envOptions.type === 'imap' ?
'NO [ALERT] Application-specific password required' :
'-ERR [AUTH] Application-specific password required'),
expectResult: 'needs-app-pass',
});
});
TD.commonCase('gmail IMAP user disabled error', function(T, RT) {
cannedLoginTest(T, RT, {
loginErrorString: (RT.envOptions.type === 'imap' ?
'NO [ALERT] Your account is not enabled for IMAP use.' :
'-ERR [SYS/PERM] Your account is not enabled for POP'),
expectResult:
(RT.envOptions.type === 'imap' ? 'imap-disabled' : 'pop3-disabled'),
});
});
TD.commonCase('gmail IMAP domain disabled error', function(T, RT) {
cannedLoginTest(T, RT, {
loginErrorString:
(RT.envOptions.type === 'imap' ?
'NO [ALERT] IMAP access is disabled for your domain.' :
'-ERR [SYS/PERM] POP access is disabled for your domain.'),
expectResult:
(RT.envOptions.type === 'imap' ? 'imap-disabled' : 'pop3-disabled'),
});
});
TD.commonCase('server maintenance', function(T, RT) {
if (RT.envOptions.type === "imap") {
cannedLoginTest(T, RT, {
openResponse: openResponse(RT).replace('AUTH=PLAIN', 'LOGINDISABLED'),
capabilityResponse: capabilityResponse(RT).replace('AUTH=PLAIN',
'LOGINDISABLED'),
// we won't get to the login string
loginErrorString: null,
expectResult: 'server-maintenance',
});
} else {
cannedLoginTest(T, RT, {
openResponse: "-ERR [SYS] System in use...\r\n",
capabilityResponse: "-ERR [SYS] System in use...\r\n",
// we won't get to the login string
loginErrorString: null,
expectResult: 'server-maintenance',
});
}
});
TD.commonCase('account in use (pop3)', function(T, RT) {
if (RT.envOptions.type === "pop3") {
cannedLoginTest(T, RT, {
loginErrorString: "-ERR [IN-USE] Your account is in use, try later.\r\n",
expectResult: 'server-maintenance',
});
}
});
/**
* Test the timezone parsing logic from received headers that is used by the
* timezone offset calculation logic.
*/
TD.commonCase('timezone extraction unit', function(T, RT) {
if (RT.envOptions.type === "imap") {
$th_main.thunkConsoleForNonTestUniverse();
var eCheck = T.lazyLogger('check');
var caseData = [
{
name: '2nd',
headers: [
{ key: 'received',
value: 'from 127.0.0.1 (EHLO lists.mozilla.org) (63.245.216.66)\n' +
' by mta1310.mail.gq1.yahoo.com with SMTP; ' +
'Wed, 09 Jan 2013 05:46:19 -0800' },
],
tzHours: -8
}
];
caseData.forEach(function(data) {
T.check(data.name, eCheck, function() {
eCheck.expect_namedValue('tzHours', data.tzHours);
var tz = $imapProbe._extractTZFromHeaders(data.headers);
eCheck.namedValue('tzHours', tz && tz / (60 * 60 * 1000));
});
});
}
});
}); // end define