Intial Commit

This commit is contained in:
valki
2020-10-17 18:42:50 +02:00
commit 664c6d8ca3
5892 changed files with 759183 additions and 0 deletions

View File

@@ -0,0 +1,92 @@
var assert = require('assert'),
net = require('net'),
Imap = require('../lib/Connection'),
result;
var CRLF = '\r\n';
var RESPONSES = [
['* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA CHILDREN',
'A0 OK Thats all she wrote!',
''
].join(CRLF),
['* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA CHILDREN UIDPLUS MOVE',
'A1 OK authenticated (Success)',
''
].join(CRLF),
['* NAMESPACE (("" "/")) NIL NIL',
'A2 OK Success',
''
].join(CRLF),
['* LIST (\\Noselect) "/" "/"',
'A3 OK Success',
''
].join(CRLF),
['* FLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen)',
'* OK [PERMANENTFLAGS ()] Flags permitted.',
'* OK [UIDVALIDITY 2] UIDs valid.',
'* 685 EXISTS',
'* 0 RECENT',
'* OK [UIDNEXT 4422] Predicted next UID.',
'A4 OK [READ-ONLY] INBOX selected. (Success)',
''
].join(CRLF),
['* 1 FETCH (UID 1)',
'* 1 FETCH (INTERNALDATE "05-Sep-2004 00:38:03 +0000" UID 1000)',
'* 1 FETCH (FLAGS (\\Seen))',
'A5 OK Success',
''
].join(CRLF),
['* BYE LOGOUT Requested',
'A6 OK good day (Success)',
''
].join(CRLF)
];
var srv = net.createServer(function(sock) {
sock.write('* OK asdf\r\n');
var buf = '', lines;
sock.on('data', function(data) {
buf += data.toString('utf8');
if (buf.indexOf(CRLF) > -1) {
lines = buf.split(CRLF);
buf = lines.pop();
lines.forEach(function() {
sock.write(RESPONSES.shift());
});
}
});
});
srv.listen(0, '127.0.0.1', function() {
var port = srv.address().port;
var imap = new Imap({
user: 'foo',
password: 'bar',
host: '127.0.0.1',
port: port,
keepalive: false
});
imap.on('ready', function() {
imap.openBox('INBOX', true, function() {
var f = imap.seq.fetch(1);
f.on('message', function(m) {
m.once('attributes', function(attrs) {
result = attrs;
});
});
f.on('end', function() {
srv.close();
imap.end();
});
});
});
imap.connect();
});
process.once('exit', function() {
assert.deepEqual(result, {
uid: 1,
date: new Date('05-Sep-2004 00:38:03 +0000'),
flags: [ '\\Seen' ]
});
});

View File

@@ -0,0 +1,92 @@
var assert = require('assert'),
net = require('net'),
Imap = require('../lib/Connection'),
result;
var CRLF = '\r\n';
var RESPONSES = [
['* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA CHILDREN',
'A0 OK Thats all she wrote!',
''
].join(CRLF),
['* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA CHILDREN UIDPLUS MOVE',
'A1 OK authenticated (Success)',
''
].join(CRLF),
['* NAMESPACE (("" "/")) NIL NIL',
'A2 OK Success',
''
].join(CRLF),
['* LIST (\\Noselect) "/" "/"',
'A3 OK Success',
''
].join(CRLF),
['* FLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen)',
'* OK [PERMANENTFLAGS ()] Flags permitted.',
'* OK [UIDVALIDITY 2] UIDs valid.',
'* 685 EXISTS',
'* 0 RECENT',
'* OK [UIDNEXT 4422] Predicted next UID.',
'A4 OK [READ-ONLY] INBOX selected. (Success)',
''
].join(CRLF),
['* 1 FETCH (UID 1)',
'* 1 FETCH (INTERNALDATE "05-Sep-2004 00:38:03 +0000")',
'* 1 FETCH (FLAGS (\\Seen))',
'A5 OK Success',
''
].join(CRLF),
['* BYE LOGOUT Requested',
'A6 OK good day (Success)',
''
].join(CRLF)
];
var srv = net.createServer(function(sock) {
sock.write('* OK asdf\r\n');
var buf = '', lines;
sock.on('data', function(data) {
buf += data.toString('utf8');
if (buf.indexOf(CRLF) > -1) {
lines = buf.split(CRLF);
buf = lines.pop();
lines.forEach(function() {
sock.write(RESPONSES.shift());
});
}
});
});
srv.listen(0, '127.0.0.1', function() {
var port = srv.address().port;
var imap = new Imap({
user: 'foo',
password: 'bar',
host: '127.0.0.1',
port: port,
keepalive: false
});
imap.on('ready', function() {
imap.openBox('INBOX', true, function() {
var f = imap.seq.fetch(1);
f.on('message', function(m) {
m.once('attributes', function(attrs) {
result = attrs;
});
});
f.on('end', function() {
srv.close();
imap.end();
});
});
});
imap.connect();
});
process.once('exit', function() {
assert.deepEqual(result, {
uid: 1,
date: new Date('05-Sep-2004 00:38:03 +0000'),
flags: [ '\\Seen' ]
});
});

View File

@@ -0,0 +1,176 @@
// Test for _at least_ GH Issues #345, #379, #392, #411
var assert = require('assert'),
net = require('net'),
crypto = require('crypto'),
Imap = require('../lib/Connection');
var result = [],
body = [],
bodyInfo = [];
var CRLF = '\r\n';
// generate data larger than highWaterMark
var bytes = crypto.pseudoRandomBytes(10240).toString('hex');
var RESPONSES = [
['* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA CHILDREN',
'A0 OK Thats all she wrote!',
''
].join(CRLF),
['* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA CHILDREN UIDPLUS MOVE',
'A1 OK authenticated (Success)',
''
].join(CRLF),
['* NAMESPACE (("" "/")) NIL NIL',
'A2 OK Success',
''
].join(CRLF),
['* LIST (\\Noselect) "/" "/"',
'A3 OK Success',
''
].join(CRLF),
['* FLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen)',
'* OK [PERMANENTFLAGS ()] Flags permitted.',
'* OK [UIDVALIDITY 2] UIDs valid.',
'* 685 EXISTS',
'* 0 RECENT',
'* OK [UIDNEXT 4422] Predicted next UID.',
'A4 OK [READ-ONLY] INBOX selected. (Success)',
''
].join(CRLF),
'* 1 FETCH (UID 1000 FLAGS (\\Seen) INTERNALDATE "05-Sep-2004 00:38:03 +0000" BODY[TEXT] {'
+ bytes.length
+ '}'
+ CRLF
+ bytes.substring(0, 20000),
['* BYE LOGOUT Requested',
'A6 OK good day (Success)',
''
].join(CRLF)
];
var EXPECTED = [
'A0 CAPABILITY',
'A1 LOGIN "foo" "bar"',
'A2 NAMESPACE',
'A3 LIST "" ""',
'A4 EXAMINE "INBOX"',
'A5 FETCH 1,2 (UID FLAGS INTERNALDATE BODY.PEEK[TEXT])',
'A6 LOGOUT'
];
var exp = -1,
res = -1;
var srv = net.createServer(function(sock) {
sock.write('* OK asdf\r\n');
var buf = '', lines;
sock.on('data', function(data) {
buf += data.toString('utf8');
if (buf.indexOf(CRLF) > -1) {
lines = buf.split(CRLF);
buf = lines.pop();
lines.forEach(function(l) {
assert(l === EXPECTED[++exp], 'Unexpected client request: ' + l);
assert(RESPONSES[++res], 'No response for client request: ' + l);
sock.write(RESPONSES[res]);
});
}
});
});
srv.listen(0, '127.0.0.1', function() {
var port = srv.address().port;
var imap = new Imap({
user: 'foo',
password: 'bar',
host: '127.0.0.1',
port: port,
keepalive: false
});
imap.on('ready', function() {
srv.close();
imap.openBox('INBOX', true, function() {
var f = imap.seq.fetch([1,2], { bodies: ['TEXT'] });
var nbody = -1;
f.on('message', function(m) {
m.on('body', function(stream, info) {
++nbody;
bodyInfo.push(info);
body[nbody] = '';
if (nbody === 0) {
// first allow body.push() to return false in parser.js
setTimeout(function() {
stream.on('data', function(chunk) {
body[nbody] += chunk.toString('binary');
});
setTimeout(function() {
var oldRead = stream._read,
readCalled = false;
stream._read = function(n) {
readCalled = true;
stream._read = oldRead;
imap._sock.push(bytes.substring(100, 200)
+ ')'
+ CRLF
+ 'A5 OK Success'
+ CRLF);
imap._parser._tryread();
};
imap._sock.push(bytes.substring(20000)
+ ')'
+ CRLF
+ '* 2 FETCH (UID 1001 FLAGS (\\Seen) INTERNALDATE "05-Sep-2004 00:38:13 +0000" BODY[TEXT] {200}'
+ CRLF
+ bytes.substring(0, 100));
// if we got this far, then we didn't get an exception and we
// are running with the bug fix in place
if (!readCalled) {
imap._sock.push(bytes.substring(100, 200)
+ ')'
+ CRLF
+ 'A5 OK Success'
+ CRLF);
}
}, 100);
}, 100);
} else {
stream.on('data', function(chunk) {
body[nbody] += chunk.toString('binary');
});
}
});
m.on('attributes', function(attrs) {
result.push(attrs);
});
});
f.on('end', function() {
imap.end();
});
});
});
imap.connect();
});
process.once('exit', function() {
assert.deepEqual(result, [{
uid: 1000,
date: new Date('05-Sep-2004 00:38:03 +0000'),
flags: [ '\\Seen' ]
}, {
uid: 1001,
date: new Date('05-Sep-2004 00:38:13 +0000'),
flags: [ '\\Seen' ]
}]);
assert.deepEqual(body, [bytes, bytes.substring(0, 200)]);
assert.deepEqual(bodyInfo, [{
seqno: 1,
which: 'TEXT',
size: bytes.length
}, {
seqno: 2,
which: 'TEXT',
size: 200
}]);
});

View File

@@ -0,0 +1,104 @@
var assert = require('assert'),
net = require('net'),
Imap = require('../lib/Connection');
var result, body = '', bodyInfo;
var CRLF = '\r\n';
var RESPONSES = [
['* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA CHILDREN',
'A0 OK Thats all she wrote!',
''
].join(CRLF),
['* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA CHILDREN UIDPLUS MOVE',
'A1 OK authenticated (Success)',
''
].join(CRLF),
['* NAMESPACE (("" "/")) NIL NIL',
'A2 OK Success',
''
].join(CRLF),
['* LIST (\\Noselect) "/" "/"',
'A3 OK Success',
''
].join(CRLF),
['* FLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen)',
'* OK [PERMANENTFLAGS ()] Flags permitted.',
'* OK [UIDVALIDITY 2] UIDs valid.',
'* 685 EXISTS',
'* 0 RECENT',
'* OK [UIDNEXT 4422] Predicted next UID.',
'A4 OK [READ-ONLY] INBOX selected. (Success)',
''
].join(CRLF),
['* 1 FETCH (UID 1)',
'* 1 FETCH (INTERNALDATE "05-Sep-2004 00:38:03 +0000" UID 1000)',
'* 1 FETCH (BODY[TEXT] "IMAP is terrible")',
'* 1 FETCH (FLAGS (\\Seen))',
'A5 OK Success',
''
].join(CRLF),
['* BYE LOGOUT Requested',
'A6 OK good day (Success)',
''
].join(CRLF)
];
var srv = net.createServer(function(sock) {
sock.write('* OK asdf\r\n');
var buf = '', lines;
sock.on('data', function(data) {
buf += data.toString('utf8');
if (buf.indexOf(CRLF) > -1) {
lines = buf.split(CRLF);
buf = lines.pop();
lines.forEach(function() {
sock.write(RESPONSES.shift());
});
}
});
});
srv.listen(0, '127.0.0.1', function() {
var port = srv.address().port;
var imap = new Imap({
user: 'foo',
password: 'bar',
host: '127.0.0.1',
port: port,
keepalive: false
});
imap.on('ready', function() {
imap.openBox('INBOX', true, function() {
var f = imap.seq.fetch(1, { bodies: ['TEXT'] });
f.on('message', function(m) {
m.on('body', function(stream, info) {
bodyInfo = info;
stream.on('data', function(chunk) { body += chunk.toString('utf8'); });
});
m.on('attributes', function(attrs) {
result = attrs;
});
});
f.on('end', function() {
srv.close();
imap.end();
});
});
});
imap.connect();
});
process.once('exit', function() {
assert.deepEqual(result, {
uid: 1,
date: new Date('05-Sep-2004 00:38:03 +0000'),
flags: [ '\\Seen' ]
});
assert.equal(body, 'IMAP is terrible');
assert.deepEqual(bodyInfo, {
seqno: 1,
which: 'TEXT',
size: 16
});
});

View File

@@ -0,0 +1,144 @@
var assert = require('assert'),
net = require('net'),
Imap = require('../lib/Connection');
var result, body = '', bodyInfo;
var CRLF = '\r\n';
var RESPONSES = [
['* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA CHILDREN',
'A0 OK Thats all she wrote!',
''
].join(CRLF),
['* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA CHILDREN UIDPLUS MOVE',
'A1 OK authenticated (Success)',
''
].join(CRLF),
['* NAMESPACE (("" "/")) NIL NIL',
'A2 OK Success',
''
].join(CRLF),
['* LIST (\\Noselect) "/" "/"',
'A3 OK Success',
''
].join(CRLF),
['* FLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen)',
'* OK [PERMANENTFLAGS ()] Flags permitted.',
'* OK [UIDVALIDITY 2] UIDs valid.',
'* 685 EXISTS',
'* 0 RECENT',
'* OK [UIDNEXT 4422] Predicted next UID.',
'A4 OK [READ-ONLY] INBOX selected. (Success)',
''
].join(CRLF),
['* 1 FETCH (UID 1)',
'* 1 FETCH (INTERNALDATE "05-Sep-2004 00:38:03 +0000" UID 1000)',
'* 1 FETCH (BODY[TEXT] "IMAP is terrible")',
'* 1 FETCH (FLAGS (\\Seen))',
'A5 OK Success',
''
].join(CRLF),
['* STATUS test (MESSAGES 231 RECENT 0 UNSEEN 0 UIDVALIDITY 123 UIDNEXT 442)',
'A6 OK STATUS completed',
''
].join(CRLF),
['* BYE LOGOUT Requested',
'A7 OK good day (Success)',
''
].join(CRLF)
];
var EXPECTED = [
'A0 CAPABILITY',
'A1 LOGIN "foo" "bar"',
'A2 NAMESPACE',
'A3 LIST "" ""',
'A4 EXAMINE "INBOX"',
'A5 FETCH 1 (UID FLAGS INTERNALDATE BODY.PEEK[TEXT])',
'IDLE IDLE',
'DONE',
'A6 STATUS "test" (MESSAGES RECENT UNSEEN UIDVALIDITY UIDNEXT)',
'A7 LOGOUT'
];
var exp = -1,
res = -1,
sentCont = false;
var srv = net.createServer(function(sock) {
sock.write('* OK asdf\r\n');
var buf = '', lines;
sock.on('data', function(data) {
buf += data.toString('utf8');
if (buf.indexOf(CRLF) > -1) {
lines = buf.split(CRLF);
buf = lines.pop();
lines.forEach(function(l) {
assert(l === EXPECTED[++exp], 'Unexpected client request: ' + l);
if (l === 'DONE') {
assert(sentCont, 'DONE seen before continuation sent');
sock.write('IDLE ok\r\n');
} else if (l === 'IDLE IDLE') {
setTimeout(function() {
sentCont = true;
sock.write('+ idling\r\n');
}, 100);
} else {
assert(RESPONSES[++res], 'No response for client request: ' + l);
sock.write(RESPONSES[res]);
}
});
}
});
});
srv.listen(0, '127.0.0.1', function() {
var port = srv.address().port;
var imap = new Imap({
user: 'foo',
password: 'bar',
host: '127.0.0.1',
port: port,
keepalive: true
});
imap.on('ready', function() {
srv.close();
imap.openBox('INBOX', true, function() {
var f = imap.seq.fetch(1, { bodies: ['TEXT'] });
f.on('message', function(m) {
m.on('body', function(stream, info) {
bodyInfo = info;
stream.on('data', function(chunk) { body += chunk.toString('utf8'); });
});
m.on('attributes', function(attrs) {
result = attrs;
});
});
f.on('end', function() {
setTimeout(function() {
var timeout = setTimeout(function() {
assert(false, 'Timed out waiting for STATUS');
}, 500);
imap.status('test', function(err, status) {
clearTimeout(timeout);
imap.end();
});
}, 500);
});
});
});
imap.connect();
});
process.once('exit', function() {
assert.deepEqual(result, {
uid: 1,
date: new Date('05-Sep-2004 00:38:03 +0000'),
flags: [ '\\Seen' ]
});
assert.equal(body, 'IMAP is terrible');
assert.deepEqual(bodyInfo, {
seqno: 1,
which: 'TEXT',
size: 16
});
});

View File

@@ -0,0 +1,138 @@
var assert = require('assert'),
net = require('net'),
Imap = require('../lib/Connection');
var result, body = '', bodyInfo;
var CRLF = '\r\n';
var RESPONSES = [
['* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA CHILDREN',
'A0 OK Thats all she wrote!',
''
].join(CRLF),
['* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA CHILDREN UIDPLUS MOVE',
'A1 OK authenticated (Success)',
''
].join(CRLF),
['* NAMESPACE (("" "/")) NIL NIL',
'A2 OK Success',
''
].join(CRLF),
['* LIST (\\Noselect) "/" "/"',
'A3 OK Success',
''
].join(CRLF),
['* FLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen)',
'* OK [PERMANENTFLAGS ()] Flags permitted.',
'* OK [UIDVALIDITY 2] UIDs valid.',
'* 685 EXISTS',
'* 0 RECENT',
'* OK [UIDNEXT 4422] Predicted next UID.',
'A4 OK [READ-ONLY] INBOX selected. (Success)',
''
].join(CRLF),
['* 1 FETCH (UID 1)',
'* 1 FETCH (INTERNALDATE "05-Sep-2004 00:38:03 +0000" UID 1000)',
'* 1 FETCH (BODY[TEXT] "IMAP is terrible")',
'* 1 FETCH (FLAGS (\\Seen))',
'A5 OK Success',
''
].join(CRLF),
['* STATUS test (MESSAGES 231 RECENT 0 UNSEEN 0 UIDVALIDITY 123 UIDNEXT 442)',
'A6 OK STATUS completed',
''
].join(CRLF),
['* BYE LOGOUT Requested',
'A7 OK good day (Success)',
''
].join(CRLF)
];
var EXPECTED = [
'A0 CAPABILITY',
'A1 LOGIN "foo" "bar"',
'A2 NAMESPACE',
'A3 LIST "" ""',
'A4 EXAMINE "INBOX"',
'A5 FETCH 1 (UID FLAGS INTERNALDATE BODY.PEEK[TEXT])',
'IDLE IDLE',
'DONE',
'A6 STATUS "test" (MESSAGES RECENT UNSEEN UIDVALIDITY UIDNEXT)',
'A7 LOGOUT'
];
var exp = -1,
res = -1,
sentCont = false;
var srv = net.createServer(function(sock) {
sock.write('* OK asdf\r\n');
var buf = '', lines;
sock.on('data', function(data) {
buf += data.toString('utf8');
if (buf.indexOf(CRLF) > -1) {
lines = buf.split(CRLF);
buf = lines.pop();
lines.forEach(function(l) {
assert(l === EXPECTED[++exp], 'Unexpected client request: ' + l);
if (l === 'DONE') {
assert(sentCont, 'DONE seen before continuation sent');
sock.write('IDLE ok\r\n');
} else if (l === 'IDLE IDLE') {
setTimeout(function() {
sentCont = true;
sock.write('+ idling\r\n');
}, 100);
} else {
assert(RESPONSES[++res], 'No response for client request: ' + l);
sock.write(RESPONSES[res]);
}
});
}
});
});
srv.listen(0, '127.0.0.1', function() {
var port = srv.address().port;
var imap = new Imap({
user: 'foo',
password: 'bar',
host: '127.0.0.1',
port: port,
keepalive: true
});
imap.on('ready', function() {
srv.close();
imap.openBox('INBOX', true, function() {
var f = imap.seq.fetch(1, { bodies: ['TEXT'] });
f.on('message', function(m) {
m.on('body', function(stream, info) {
bodyInfo = info;
stream.on('data', function(chunk) { body += chunk.toString('utf8'); });
});
m.on('attributes', function(attrs) {
result = attrs;
});
});
f.on('end', function() {
imap.status('test', function(err, status) {
imap.end();
});
});
});
});
imap.connect();
});
process.once('exit', function() {
assert.deepEqual(result, {
uid: 1,
date: new Date('05-Sep-2004 00:38:03 +0000'),
flags: [ '\\Seen' ]
});
assert.equal(body, 'IMAP is terrible');
assert.deepEqual(bodyInfo, {
seqno: 1,
which: 'TEXT',
size: 16
});
});

View File

@@ -0,0 +1,83 @@
var parseBodyStructure = require('../lib/Parser').parseBodyStructure;
var assert = require('assert'),
inspect = require('util').inspect;
[
{ source: '("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 1152 23)'
+ '("TEXT" "PLAIN" ("CHARSET" "US-ASCII" "NAME" "cc.diff")'
+ ' "<960723163407.20117h@cac.washington.edu>" "Compiler diff"'
+ ' "BASE64" 4554 73)'
+ '"MIXED"',
expected: [ { type: 'mixed' },
[ { partID: '1',
type: 'text',
subtype: 'plain',
params: { charset: 'US-ASCII' },
id: null,
description: null,
encoding: '7BIT',
size: 1152,
lines: 23
}
],
[ { partID: '2',
type: 'text',
subtype: 'plain',
params: { charset: 'US-ASCII', name: 'cc.diff' },
id: '<960723163407.20117h@cac.washington.edu>',
description: 'Compiler diff',
encoding: 'BASE64',
size: 4554,
lines: 73
}
]
],
what: 'RFC3501 example #1'
},
{ source: 'NIL NIL ("CHARSET" "GB2312") NIL NIL NIL 176 NIL NIL NIL',
expected: [ { type: null,
params: null,
disposition: null,
language: [ 'CHARSET', 'GB2312' ],
location: null,
extensions: null
}
],
what: 'Issue 477'
},
{ source: '"TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 3028 92',
expected: [ { partID: '1',
type: 'text',
subtype: 'plain',
params: { charset: 'US-ASCII' },
id: null,
description: null,
encoding: '7BIT',
size: 3028,
lines: 92
}
],
what: 'RFC3501 example #2'
},
].forEach(function(v) {
var result;
try {
result = parseBodyStructure(v.source);
} catch (e) {
console.log(makeMsg(v.what, 'JS Exception: ' + e.stack));
return;
}
assert.deepEqual(result,
v.expected,
makeMsg(v.what,
'Result mismatch:'
+ '\nParsed: ' + inspect(result, false, 10)
+ '\nExpected: ' + inspect(v.expected, false, 10)
)
);
});
function makeMsg(what, msg) {
return '[' + what + ']: ' + msg;
}

View File

@@ -0,0 +1,117 @@
var parseExpr = require('../lib/Parser').parseExpr,
parseEnvelopeAddresses = require('../lib/Parser').parseEnvelopeAddresses;
var assert = require('assert'),
inspect = require('util').inspect;
[
{ source: '("Terry Gray" NIL "gray" "cac.washington.edu")',
expected: [ { name: 'Terry Gray',
mailbox: 'gray',
host: 'cac.washington.edu'
}
],
what: 'RFC3501 example #1'
},
{ source: '(NIL NIL "imap" "cac.washington.edu")',
expected: [ { name: null,
mailbox: 'imap',
host: 'cac.washington.edu'
}
],
what: 'RFC3501 example #2'
},
{ source: '("=?utf-8?Q?=C2=A9=C2=AEAZ=C2=A5?=" NIL "crazy" "example.org")',
expected: [ { name: '©®AZ¥',
mailbox: 'crazy',
host: 'example.org'
}
],
what: 'Name with encoded word(s)'
},
{ source: '(NIL NIL "imap" NIL)'
+ '(NIL NIL NIL NIL)',
expected: [ { group: 'imap',
addresses: []
}
],
what: 'Zero-length group'
},
{ source: '(NIL NIL "imap" NIL)'
+ '("Terry Gray" NIL "gray" "cac.washington.edu")'
+ '(NIL NIL NIL NIL)',
expected: [ { group: 'imap',
addresses: [
{ name: 'Terry Gray',
mailbox: 'gray',
host: 'cac.washington.edu'
}
]
}
],
what: 'One-length group'
},
{ source: '(NIL NIL "imap" NIL)'
+ '("Terry Gray" NIL "gray" "cac.washington.edu")'
+ '(NIL NIL NIL NIL)'
+ '(NIL NIL "imap" "cac.washington.edu")',
expected: [ { group: 'imap',
addresses: [
{ name: 'Terry Gray',
mailbox: 'gray',
host: 'cac.washington.edu'
}
]
},
{ name: null,
mailbox: 'imap',
host: 'cac.washington.edu'
}
],
what: 'One-length group and address'
},
{ source: '(NIL NIL "imap" NIL)'
+ '("Terry Gray" NIL "gray" "cac.washington.edu")',
expected: [ { group: 'imap',
addresses: [
{ name: 'Terry Gray',
mailbox: 'gray',
host: 'cac.washington.edu'
}
]
}
],
what: 'Implicit group end'
},
{ source: '("Terry Gray" NIL "gray" "cac.washington.edu")'
+ '(NIL NIL NIL NIL)',
expected: [ { name: 'Terry Gray',
mailbox: 'gray',
host: 'cac.washington.edu'
}
],
what: 'Group end without start'
},
].forEach(function(v) {
var result;
try {
result = parseEnvelopeAddresses(parseExpr(v.source));
} catch (e) {
console.log(makeMsg(v.what, 'JS Exception: ' + e.stack));
return;
}
assert.deepEqual(result,
v.expected,
makeMsg(v.what,
'Result mismatch:'
+ '\nParsed: ' + inspect(result, false, 10)
+ '\nExpected: ' + inspect(v.expected, false, 10)
)
);
});
function makeMsg(what, msg) {
return '[' + what + ']: ' + msg;
}

View File

@@ -0,0 +1,92 @@
var parseExpr = require('../lib/Parser').parseExpr;
var assert = require('assert'),
inspect = require('util').inspect;
[
{ source: '',
expected: [],
what: 'Empty value'
},
{ source: '""',
expected: [''],
what: 'Empty quoted string'
},
{ source: 'FLAGS NIL RFC822.SIZE 44827',
expected: ['FLAGS', null, 'RFC822.SIZE', 44827],
what: 'Simple, two key-value pairs with nil'
},
{ source: 'FLAGS (\\Seen) RFC822.SIZE 44827',
expected: ['FLAGS', ['\\Seen'], 'RFC822.SIZE', 44827],
what: 'Simple, two key-value pairs with list'
},
{ source: 'RFC822.SIZE 9007199254740993',
expected: ['RFC822.SIZE', '9007199254740993'],
what: 'Integer exceeding JavaScript max int size'
},
{ source: 'FLAGS (\\Seen) INTERNALDATE "17-Jul-1996 02:44:25 -0700"',
expected: ['FLAGS', ['\\Seen'], 'INTERNALDATE', '17-Jul-1996 02:44:25 -0700'],
what: 'Quoted string'
},
{ source: '("Foo")("Bar") ("Baz")',
expected: [['Foo'], ['Bar'], ['Baz']],
what: 'Lists with varying spacing'
},
{ source: '"\\"IMAP\\" is terrible :\\\\"',
expected: ['"IMAP" is terrible :\\'],
what: 'Quoted string with escaped chars'
},
{ source: '"\\\\"IMAP\\" is terrible :\\\\"',
expected: ['\\"IMAP" is terrible :\\'],
what: 'Quoted string with escaped chars #2'
},
{ source: '"Who does not think \\"IMAP\\" is terrible\\\\bad?"',
expected: ['Who does not think "IMAP" is terrible\\bad?'],
what: 'Quoted string with escaped chars #3'
},
{ source: '"Who does not think \\\\"IMAP\\" is terrible\\\\bad?"',
expected: ['Who does not think \\"IMAP" is terrible\\bad?'],
what: 'Quoted string with escaped chars #4'
},
{ source: 'ENVELOPE ("Wed, 30 Mar 2014 02:38:23 +0100" "=?ISO-8859-1?Q?##ALLCAPS##123456## - ?= =?ISO-8859-1?Q?[ALERT][P3][ONE.TWO.FR] ?= =?ISO-8859-1?Q?Some Subject Line \\"D:\\\\\\"?=" (("Test Account (Rltvty L)" NIL "account" "test.com")) (("Test Account (Rltvty L)" NIL "account" "test.com")) ((NIL NIL "account" "test.com")) ((NIL NIL "one.two" "test.fr") (NIL NIL "two.three" "test.fr")) NIL NIL NIL "<message@test.eu>")',
expected: [
'ENVELOPE',
[ 'Wed, 30 Mar 2014 02:38:23 +0100',
'=?ISO-8859-1?Q?##ALLCAPS##123456## - ?= =?ISO-8859-1?Q?[ALERT][P3][ONE.TWO.FR] ?= =?ISO-8859-1?Q?Some Subject Line "D:\\"?=',
[ [ 'Test Account (Rltvty L)', null, 'account', 'test.com' ] ],
[ [ 'Test Account (Rltvty L)', null, 'account', 'test.com' ] ],
[ [ null, null, 'account', 'test.com' ] ],
[ [ null, null, 'one.two', 'test.fr' ],
[ null, null, 'two.three', 'test.fr' ]
],
null,
null,
null,
'<message@test.eu>'
]
],
what: 'Triple backslash in quoted string (GH Issue #345)'
},
].forEach(function(v) {
var result;
try {
result = parseExpr(v.source);
} catch (e) {
console.log(makeMsg(v.what, 'JS Exception: ' + e.stack));
return;
}
assert.deepEqual(result,
v.expected,
makeMsg(v.what,
'Result mismatch:'
+ '\nParsed: ' + inspect(result, false, 10)
+ '\nExpected: ' + inspect(v.expected, false, 10)
)
);
});
function makeMsg(what, msg) {
return '[' + what + ']: ' + msg;
}

View File

@@ -0,0 +1,109 @@
var parseHeader = require('../lib/Parser').parseHeader;
var assert = require('assert'),
inspect = require('util').inspect;
var CRLF = '\r\n';
[
{ source: ['To: Foo', CRLF,
' Bar Baz', CRLF],
expected: { to: [ 'Foo Bar Baz' ] },
what: 'Folded header value (plain -- space)'
},
{ source: ['To: Foo', CRLF,
'\tBar\tBaz', CRLF],
expected: { to: [ 'Foo\tBar\tBaz' ] },
what: 'Folded header value (plain -- tab)'
},
{ source: ['Subject: =?iso-8859-1?Q?=A1Hola,_se=F1or!?=', CRLF],
expected: { subject: [ '¡Hola, señor!' ] },
what: 'MIME encoded-word in value'
},
{ source: ['Subject: =?iso-8859-1*es?Q?=A1Hola,_se=F1or!?=', CRLF],
expected: { subject: [ '¡Hola, señor!' ] },
what: 'MIME encoded-word in value with language set (RFC2231)'
},
{ source: ['Subject: =?iso-8859-1*?Q?=A1Hola,_se=F1or!?=', CRLF],
expected: { subject: [ '¡Hola, señor!' ] },
what: 'MIME encoded-word in value with empty language set'
},
{ source: ['Subject: =?GB2312?Q?=B2=E2=CA=D4=CC=E2=C4=BF=D3=EB=D6=D0=B9=FA=D0=C5_long_subjects_are_not_OK_12?=', CRLF,
' =?GB2312?Q?345678901234567890123456789012345678901234567890123456789012?=', CRLF,
' =?GB2312?Q?345678901234567890?=', CRLF],
expected: { subject: [ '测试题目与中国信 long subjects are not OK 12345678901234567890123456789012345678901234567890123456789012345678901234567890' ] },
what: 'Folded header value (adjacent MIME encoded-words)'
},
{ source: ['Subject: =?GB2312?Q?=B2=E2=CA=D4=CC=E2=C4=BF=D3=EB=D6=D0=B9=FA=D0=C5_long_subjects_are_not_OK_12?=', CRLF,
' 3=?GB2312?Q?45678901234567890123456789012345678901234567890123456789012?=', CRLF,
' 3=?GB2312?Q?45678901234567890?=', CRLF],
expected: { subject: [ '测试题目与中国信 long subjects are not OK 12 345678901234567890123456789012345678901234567890123456789012 345678901234567890' ] },
what: 'Folded header value (non-adjacent MIME encoded-words)'
},
{ source: ['Subject: =?GB2312?Q?=B2=E2=CA=D4=CC=E2=C4=BF=D3=EB=D6=D0=B9=FA=D0=C5_long_subjects_are_not_OK_12?=', CRLF,
' 3=?GB2312?Q?45678901234567890123456789012345678901234567890123456789012?=', CRLF,
' =?GB2312?Q?345678901234567890?=', CRLF],
expected: { subject: [ '测试题目与中国信 long subjects are not OK 12 345678901234567890123456789012345678901234567890123456789012345678901234567890' ] },
what: 'Folded header value (one adjacent, one non-adjacent MIME encoded-words)'
},
{ source: ['Subject: =?UTF-8?Q?=E0=B9=84=E0=B8=97=E0=B8=A2_=E0=B9=84?=', CRLF,
' ', CRLF,
' =?UTF-8?Q?=E0=B8=97=E0=B8=A2_=E0=B9=84=E0=B8=97?= =?UTF-8?Q?=E0=B8=A2?=', CRLF],
expected: { subject: [ 'ไทย ไทย ไทย' ] },
what: 'Folded header value (adjacent MIME encoded-words seperated by linear whitespace)'
},
{ source: ['Subject: =?utf-8?Q?abcdefghij_=E0=B9=83=E0=B8=99_klmnopqr_=E0=B9=84=E0=B8=A1=E0=B9?=', CRLF,
' =?utf-8?Q?=88=E0=B8=82=E0=B8=B6=E0=B9=89=E0=B8=99?=', CRLF],
expected: { subject: [ 'abcdefghij ใน klmnopqr ไม่ขึ้น' ] },
what: 'Folded header value (incomplete multi-byte character split)'
},
{ source: ['Subject: =?utf-8?B?Rlc6IOC4quC4tOC5iOC4h+C4oeC4tQ==?=', CRLF,
' =?utf-8?B?4LiK4Li14Lin4Li04LiV4Lir4LiZ4LmJ4Liy4LiV?=', CRLF,
' =?utf-8?B?4Liy4LmB4Lib4Lil4LiBIOC5hiDguKPguK3=?=', CRLF,
' =?utf-8?Q?=E0=B8=9A=E0=B9=82=E0=B8=A5=E0=B8=81?=', CRLF],
expected: { subject: [ 'FW: สิ่งมีชีวิตหน้าตาแปลก ๆ รอบโลก' ] },
what: 'Folded header value (consecutive complete base64-encoded words)'
},
{ source: ['Subject: =?utf-8?B?4Lij4Li54Lib4Lig4Liy4Lie4LiX4Li14LmIIGVtYmVkIOC5g+C4meC5gOC4?=', CRLF,
' =?utf-8?B?meC4t+C5ieC4reC5gOC4oeC4peC4peC5jOC5hOC4oeC5iOC5geC4quC4lOC4?=', CRLF,
' =?utf-8?B?hw==?=', CRLF],
expected: { subject: [ 'รูปภาพที่ embed ในเนื้อเมลล์ไม่แสดง' ] },
what: 'Folded header value (consecutive partial base64-encoded words)'
},
{ source: [' ', CRLF,
'To: Foo', CRLF],
expected: { to: [ 'Foo' ] },
what: 'Invalid first line'
},
// header with body
{ source: ['Subject: test subject', CRLF,
'X-Another-Header: test', CRLF,
CRLF,
'This is body: Not a header', CRLF],
expected: { subject: [ 'test subject' ], 'x-another-header': [ 'test' ] },
what: 'Header with the body'
},
].forEach(function(v) {
var result;
try {
result = parseHeader(v.source.join(''));
} catch (e) {
console.log(makeMsg(v.what, 'JS Exception: ' + e.stack));
return;
}
assert.deepEqual(result,
v.expected,
makeMsg(v.what,
'Result mismatch:'
+ '\nParsed: ' + inspect(result, false, 10)
+ '\nExpected: ' + inspect(v.expected, false, 10)
)
);
});
function makeMsg(what, msg) {
return '[' + what + ']: ' + msg;
}

View File

@@ -0,0 +1,543 @@
var Parser = require('../lib/Parser').Parser;
var assert = require('assert'),
crypto = require('crypto'),
inspect = require('util').inspect;
var CR = '\r', LF = '\n', CRLF = CR + LF;
[
{ source: ['A1 OK LOGIN completed', CRLF],
expected: [ { type: 'ok',
tagnum: 1,
textCode: undefined,
text: 'LOGIN completed'
}
],
what: 'Tagged OK'
},
{ source: ['IDLE OK IDLE terminated', CRLF],
expected: [ 'IDLE OK IDLE terminated' ],
what: 'Unknown line'
},
{ source: ['IDLE OK Idle completed (0.002 + 1.783 + 1.783 secs).', CRLF],
expected: [ 'IDLE OK Idle completed (0.002 + 1.783 + 1.783 secs).' ],
what: 'Unknown line with + char'
},
{ source: ['+ idling', CRLF],
expected: [ { textCode: undefined,
text: 'idling'
}
],
what: 'Continuation'
},
{ source: ['+ [ALERT] idling', CRLF],
expected: [ { textCode: 'ALERT',
text: 'idling'
}
],
what: 'Continuation with text code'
},
{ source: ['+', CRLF],
expected: [ { textCode: undefined,
text: undefined
}
],
what: 'Continuation (broken -- RFC violation) sent by AOL IMAP'
},
{ source: ['* NAMESPACE ',
'(("" "/")) ',
'(("~" "/")) ',
'(("#shared/" "/")("#public/" "/")("#ftp/" "/")("#news." "."))',
CRLF],
expected: [ { type: 'namespace',
num: undefined,
textCode: undefined,
text: {
personal: [
{ prefix: '',
delimiter: '/',
extensions: undefined
}
],
other: [
{ prefix: '~',
delimiter: '/',
extensions: undefined
}
],
shared: [
{ prefix: '#shared/',
delimiter: '/',
extensions: undefined
},
{ prefix: '#public/',
delimiter: '/',
extensions: undefined
},
{ prefix: '#ftp/',
delimiter: '/',
extensions: undefined
},
{ prefix: '#news.',
delimiter: '.',
extensions: undefined
}
]
}
}
],
what: 'Multiple namespaces'
},
{ source: ['* NAMESPACE ',
'(("" "/" "X-PARAM" ("FLAG1" "FLAG2"))) ',
'NIL ',
'NIL',
CRLF],
expected: [ { type: 'namespace',
num: undefined,
textCode: undefined,
text: {
personal: [
{ prefix: '',
delimiter: '/',
extensions: {
'X-PARAM': [ 'FLAG1', 'FLAG2' ]
}
}
],
other: null,
shared: null
}
}
],
what: 'Multiple namespaces'
},
{ source: ['* FLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft)', CRLF],
expected: [ { type: 'flags',
num: undefined,
textCode: undefined,
text: [
'\\Answered',
'\\Flagged',
'\\Deleted',
'\\Seen',
'\\Draft'
]
}
],
what: 'Flags'
},
{ source: ['* SEARCH 2 3 6', CRLF],
expected: [ { type: 'search',
num: undefined,
textCode: undefined,
text: [ 2, 3, 6 ]
}
],
what: 'Search'
},
{ source: ['* XLIST (\\Noselect) "/" ~/Mail/foo', CRLF],
expected: [ { type: 'xlist',
num: undefined,
textCode: undefined,
text: {
flags: [ '\\Noselect' ],
delimiter: '/',
name: '~/Mail/foo'
}
}
],
what: 'XList'
},
{ source: ['* LIST (\\Noselect) "/" ~/Mail/foo', CRLF],
expected: [ { type: 'list',
num: undefined,
textCode: undefined,
text: {
flags: [ '\\Noselect' ],
delimiter: '/',
name: '~/Mail/foo'
}
}
],
what: 'List'
},
{ source: ['* STATUS blurdybloop (MESSAGES 231 UIDNEXT 44292)', CRLF],
expected: [ { type: 'status',
num: undefined,
textCode: undefined,
text: {
name: 'blurdybloop',
attrs: { messages: 231, uidnext: 44292 }
}
}
],
what: 'Status'
},
{ source: ['* OK [UNSEEN 17] Message 17 is the first unseen message', CRLF],
expected: [ { type: 'ok',
num: undefined,
textCode: {
key: 'UNSEEN',
val: 17
},
text: 'Message 17 is the first unseen message'
}
],
what: 'Untagged OK (with text code, with text)'
},
{ source: ['* OK [PERMANENTFLAGS (\\Deleted \\Seen \\*)] Limited', CRLF],
expected: [ { type: 'ok',
num: undefined,
textCode: {
key: 'PERMANENTFLAGS',
val: [ '\\Deleted', '\\Seen', '\\*' ]
},
text: 'Limited'
}
],
what: 'Untagged OK (with text code, with text)'
},
{ source: ['* OK [UNSEEN 17]', CRLF],
expected: [ { type: 'ok',
num: undefined,
textCode: {
key: 'UNSEEN',
val: 17
},
text: undefined
}
],
what: 'Untagged OK (no text code, with text) (RFC violation)'
},
{ source: ['* OK IMAP4rev1 Service Ready', CRLF],
expected: [ { type: 'ok',
num: undefined,
textCode: undefined,
text: 'IMAP4rev1 Service Ready'
}
],
what: 'Untagged OK (no text code, with text)'
},
{ source: ['* OK', CRLF], // I have seen servers that send stuff like this ..
expected: [ { type: 'ok',
num: undefined,
textCode: undefined,
text: undefined
}
],
what: 'Untagged OK (no text code, no text) (RFC violation)'
},
{ source: ['* 18 EXISTS', CRLF],
expected: [ { type: 'exists',
num: 18,
textCode: undefined,
text: undefined
}
],
what: 'Untagged EXISTS'
},
{ source: ['* 2 RECENT', CRLF],
expected: [ { type: 'recent',
num: 2,
textCode: undefined,
text: undefined
}
],
what: 'Untagged RECENT'
},
{ source: ['* 12 FETCH (BODY[HEADER] {342}', CRLF,
'Date: Wed, 17 Jul 1996 02:23:25 -0700 (PDT)', CRLF,
'From: Terry Gray <gray@cac.washington.edu>', CRLF,
'Subject: IMAP4rev1 WG mtg summary and minutes', CRLF,
'To: imap@cac.washington.edu', CRLF,
'cc: minutes@CNRI.Reston.VA.US, John Klensin <KLENSIN@MIT.EDU>', CRLF,
'Message-Id: <B27397-0100000@cac.washington.edu>', CRLF,
'MIME-Version: 1.0', CRLF,
'Content-Type: TEXT/PLAIN; CHARSET=US-ASCII', CRLF, CRLF,
')', CRLF],
expected: [ { seqno: 12,
which: 'HEADER',
size: 342
},
{ type: 'fetch',
num: 12,
textCode: undefined,
text: {}
}
],
bodySHA1s: ['1f96faf50f6410f99237791f9e3b89454bf93fa7'],
what: 'Untagged FETCH (body)'
},
{ source: ['* 12 FETCH (BODY[TEXT] "IMAP is terrible")', CRLF],
expected: [ { seqno: 12,
which: 'TEXT',
size: 16
},
{ type: 'fetch',
num: 12,
textCode: undefined,
text: {}
}
],
bodySHA1s: ['bac8a1528c133787a6969a10a1ff453ebb9adfc8'],
what: 'Untagged FETCH (quoted body)'
},
{ source: ['* 12 FETCH (BODY[TEXT] "\\"IMAP\\" is terrible :\\\\")', CRLF],
expected: [ { seqno: 12,
which: 'TEXT',
size: 21
},
{ type: 'fetch',
num: 12,
textCode: undefined,
text: {}
}
],
bodySHA1s: ['7570c08150050a404603f63f60b65b42378d7d42'],
what: 'Untagged FETCH (quoted body with escaped chars)'
},
{ source: ['* 12 FETCH (INTERNALDATE {26}', CRLF,
'17-Jul-1996 02:44:25 -0700)' + CRLF],
expected: [ { type: 'fetch',
num: 12,
textCode: undefined,
text: {
internaldate: new Date('17-Jul-1996 02:44:25 -0700')
}
}
],
what: 'Untagged FETCH with non-body literal'
},
{ source: ['* 12 FETCH (INTERNALDATE {2',
'6}' + CRLF + '17-Jul-1996 02:44:25 -0700)' + CRLF],
expected: [ { type: 'fetch',
num: 12,
textCode: undefined,
text: {
internaldate: new Date('17-Jul-1996 02:44:25 -0700')
}
}
],
what: 'Untagged FETCH with non-body literal (length split)'
},
{ source: ['* 12 FETCH (INTERNALDATE {26}', CRLF,
'17-Jul-1996 02:44:25 -0700)' + CR,
LF],
expected: [ { type: 'fetch',
num: 12,
textCode: undefined,
text: {
internaldate: new Date('17-Jul-1996 02:44:25 -0700')
}
}
],
what: 'Untagged FETCH with non-body literal (split CRLF)'
},
{ source: ['* 12 FETCH (FLAGS (\\Seen)',
' INTERNALDATE "17-Jul-1996 02:44:25 -0700"',
' RFC822.SIZE 4286',
' ENVELOPE ("Wed, 17 Jul 1996 02:23:25 -0700 (PDT)"',
' "IMAP4rev1 WG mtg summary and minutes"',
' (("Terry Gray" NIL "gray" "cac.washington.edu"))',
' (("Terry Gray" NIL "gray" "cac.washington.edu"))',
' (("Terry Gray" NIL "gray" "cac.washington.edu"))',
' ((NIL NIL "imap" "cac.washington.edu"))',
' ((NIL NIL "minutes" "CNRI.Reston.VA.US")',
'("John Klensin" NIL "KLENSIN" "MIT.EDU")) NIL NIL',
' "<B27397-0100000@cac.washington.edu>")',
' BODY ("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 3028',
' 92))',
CRLF],
expected: [ { type: 'fetch',
num: 12,
textCode: undefined,
text: {
flags: [ '\\Seen' ],
internaldate: new Date('17-Jul-1996 02:44:25 -0700'),
'rfc822.size': 4286,
envelope: {
date: new Date('Wed, 17 Jul 1996 02:23:25 -0700 (PDT)'),
subject: 'IMAP4rev1 WG mtg summary and minutes',
from: [
{ name: 'Terry Gray',
mailbox: 'gray',
host: 'cac.washington.edu'
}
],
sender: [
{ name: 'Terry Gray',
mailbox: 'gray',
host: 'cac.washington.edu'
}
],
replyTo: [
{ name: 'Terry Gray',
mailbox: 'gray',
host: 'cac.washington.edu'
}
],
to: [
{ name: null,
mailbox: 'imap',
host: 'cac.washington.edu'
}
],
cc: [
{ name: null,
mailbox: 'minutes',
host: 'CNRI.Reston.VA.US'
},
{ name: 'John Klensin',
mailbox: 'KLENSIN',
host: 'MIT.EDU'
}
],
bcc: null,
inReplyTo: null,
messageId: '<B27397-0100000@cac.washington.edu>'
},
body: [
{ partID: '1',
type: 'text',
subtype: 'plain',
params: { charset: 'US-ASCII' },
id: null,
description: null,
encoding: '7BIT',
size: 3028,
lines: 92
}
]
}
}
],
what: 'Untagged FETCH (flags, date, size, envelope, body[structure])'
},
// EXTENSIONS ================================================================
{ source: ['* ESEARCH (TAG "A285") UID MIN 7 MAX 3800', CRLF],
expected: [ { type: 'esearch',
num: undefined,
textCode: undefined,
text: { min: 7, max: 3800 }
}
],
what: 'ESearch UID, 2 items'
},
{ source: ['* ESEARCH (TAG "A284") MIN 4', CRLF],
expected: [ { type: 'esearch',
num: undefined,
textCode: undefined,
text: { min: 4 }
}
],
what: 'ESearch 1 item'
},
{ source: ['* ESEARCH (TAG "A283") ALL 2,10:11', CRLF],
expected: [ { type: 'esearch',
num: undefined,
textCode: undefined,
text: { all: [ '2', '10:11' ] }
}
],
what: 'ESearch ALL list'
},
{ source: ['* QUOTA "" (STORAGE 10 512)', CRLF],
expected: [ { type: 'quota',
num: undefined,
textCode: undefined,
text: {
root: '',
resources: {
storage: { usage: 10, limit: 512 }
}
}
}
],
what: 'Quota'
},
{ source: ['* QUOTAROOT INBOX ""', CRLF],
expected: [ { type: 'quotaroot',
num: undefined,
textCode: undefined,
text: {
roots: [ '' ],
mailbox: 'INBOX'
}
}
],
what: 'QuotaRoot'
},
{ source: ['A1 OK', CRLF], // some servers like ppops.net sends such response
expected: [ { type: 'ok',
tagnum: 1,
textCode: undefined,
text: ''
}
],
what: 'Tagged OK (no text code, no text)'
},
].forEach(function(v) {
var ss = new require('stream').Readable(), p, result = [];
ss._read = function(){};
p = new Parser(ss);
p.on('tagged', function(info) {
result.push(info);
});
p.on('untagged', function(info) {
result.push(info);
});
p.on('continue', function(info) {
result.push(info);
});
p.on('other', function(line) {
result.push(line);
});
p.on('body', function(stream, info) {
result.push(info);
if (Array.isArray(v.bodySHA1s)) {
var hash = crypto.createHash('sha1');
stream.on('data', function(d) {
hash.update(d);
});
stream.on('end', function() {
var calculated = hash.digest('hex'),
expected = v.bodySHA1s.shift();
assert.equal(calculated,
expected,
makeMsg(v.what,
'Body SHA1 mismatch:'
+ '\nCalculated: ' + calculated
+ '\nExpected: ' + expected
)
);
});
} else
stream.resume();
});
try {
v.source.forEach(function(chunk) {
ss.push(chunk);
});
} catch (e) {
console.log(makeMsg(v.what, 'JS Exception: ' + e.stack));
return;
}
setImmediate(function() {
assert.deepEqual(result,
v.expected,
makeMsg(v.what,
'Result mismatch:'
+ '\nParsed: ' + inspect(result, false, 10)
+ '\nExpected: ' + inspect(v.expected, false, 10)
)
);
});
});
function makeMsg(what, msg) {
return '[' + what + ']: ' + msg;
}

4
nodered/rootfs/data/node_modules/imap/test/test.js generated vendored Normal file
View File

@@ -0,0 +1,4 @@
require('fs').readdirSync(__dirname).forEach(function(f) {
if (f.substr(0, 5).toLowerCase() === 'test-')
require('./' + f);
});