imapflow 1.3.6 → 1.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ // Self-signed certificate + key for in-process TLS/STARTTLS mock servers in tests.
4
+ // Generated once with: openssl req -x509 -newkey rsa:2048 -nodes -subj "/CN=localhost"
5
+ module.exports = {
6
+ cert: "-----BEGIN CERTIFICATE-----\nMIIDCTCCAfGgAwIBAgIUQSc0WP7RlcOqGCdsMaxzCA97mfkwDQYJKoZIhvcNAQEL\nBQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTI2MDYwODA0Mzg1NFoXDTM2MDYw\nNTA0Mzg1NFowFDESMBAGA1UEAwwJbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEF\nAAOCAQ8AMIIBCgKCAQEA4TYnyK2nEn6wXEgVeEI3I9CDlONvWDO1Y51mslOaUwZR\nfuZp3YMUZDINNdZuFrREkD1WJfClRZtDKEurw9FfjEfA0gsM9RPvkQYXZ2zzhXgW\nx86li0Ra/Zx2RmajsWc+THTEpdOK0nqOqMMKFxiYJ7E+KiJY0X6HqKjdaB9CpZv7\nA1ROJoLjTnsdDG/NT9MCQxFbw05N2Bbbq+McKyXIEzxiZguYLIXsMKCi9MQOozvY\nT8X0AFWpFJCa8mRCW7LQngeSRQQckDONzHyELXnbvgIskr/WcmBEXMraTts3Z3QW\nouee6HFnYzi/pgCtVKX1Wu3oZm/nMblsWtopqg/YDwIDAQABo1MwUTAdBgNVHQ4E\nFgQUF5oycZ3ZaPs1fTaxZb666FQt+KcwHwYDVR0jBBgwFoAUF5oycZ3ZaPs1fTax\nZb666FQt+KcwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAWFk0\nMGw2pdkeU/s5j0T9GL8GjbOVC0G7H8tlx92XhQICmt/9GNHeb5nm84DgqRbkiTwS\nOXPoMsSvVEubnX0j/sqcbi5yQ6/YBG6Vx5E9CnfUbo4m9x1GofDkLdl+e6l636Mj\nWB5kJpNCH9sm2xPdLpl47pv/lwvyYjdy1DaATHn5nMGoM73IeEt0d4xoNN8Bso7B\nTTkjKRJ1QWgI2EjgU93x8tG4AqTj0CeH8Z6DDmfLhvJ6E0hSws4OQSnxWudh2aX8\niN+FkHgRobPS1GVCtibr+FxH1DtBv/grXhAbs4xP3Nf3m3Pn6AE0/lMtto3e67As\np4lf866gU9nbRkODqw==\n-----END CERTIFICATE-----\n",
7
+ key: "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDhNifIracSfrBc\nSBV4Qjcj0IOU429YM7VjnWayU5pTBlF+5mndgxRkMg011m4WtESQPVYl8KVFm0Mo\nS6vD0V+MR8DSCwz1E++RBhdnbPOFeBbHzqWLRFr9nHZGZqOxZz5MdMSl04rSeo6o\nwwoXGJgnsT4qIljRfoeoqN1oH0Klm/sDVE4mguNOex0Mb81P0wJDEVvDTk3YFtur\n4xwrJcgTPGJmC5gshewwoKL0xA6jO9hPxfQAVakUkJryZEJbstCeB5JFBByQM43M\nfIQtedu+AiySv9ZyYERcytpO2zdndBai557ocWdjOL+mAK1UpfVa7ehmb+cxuWxa\n2imqD9gPAgMBAAECggEACgBGqgG+0/JuUIV0S/UyJP6t098kYwKrgR33S99XO21M\nvIRCRUT8kyp/La81D6sNCb6/jvXDJnaA9/l29OjdA7TCr6aFT8jahafYmDjdY5rz\n9Vw25nAFqRIuqm0FGJa2Id0O8oeUuEjFrGGPltD6c3Yf2u0li+rA3tDeQYZ0hjge\nsVXzgI58SbO1Z1XCAXcdbcJOqjpEJ3w99GB5J/B1dDui3KR5Q6+L34HfoW8XmY/X\nCLe+Jcuk06Xa31iZ+0NEMIPOhVoCW//PfgEWD/dfG3jXhk84/Pb8DXaGahOvRPTY\nH1AVrqRBqW6PezR4qU4PdUfU3sp5NEBZl3oaW7/weQKBgQD8CKI7ek00KUhc9j5m\nHGuSR+lUzLziXRBt7xw0JXLa1rfok2RGDeTdIEGSjBr8xsNOe5aga843geoLptNe\n7LRrtJajgHl39NXnblroNidufmXjt9CRgjUsQuIREpjq9NyUGIh8o26/uZLB/Knq\nJ7Uum9YUHI/eEztmI/CZDbnUbQKBgQDkwXaf5UxkY9tsdbw8jHhV5Ypy/hwLXAcI\n90sCI9/mSQAOA9qNa+944MbRfReTseM+OFXHJRHXfuMXPPOrtObtVN94ECwFVePO\n2MI5CHc/SYZ/4Icf+jpPY+TAJtEW/39muwak43cmLhmrSmb9ExMhwfDoQotFBDOQ\nLyPPfMo46wKBgFwpWBHB1kt1sSV1UjoKuHdkXnmI6KUp/OEGZ9zVuNs3W0br5uYL\nHJ6Ttga9vvyiPjcL0HfXFDCODCS4m2CB1DPydIdTapen6zRW0V1KuD6ab+3GgLlE\n0ObfikVN9KcuZR6unfJd3Ri05mqTVxrt7D4f9DekfiNqCvV9vPRq5YI9AoGBAMeS\nYUOucK9EzzIYpVjN1E5cLCWWvDDM4PZZiP8c/UGTfpC7m/i1qf7zW4F4eRFJqM+n\npm5PUPmIoeasLoonETENUskUktM008KqKQhI8oAsTUNTR5AGZsjMhc1+D/zYevXN\nRUaljFWq8ooWJzOzaIWTBfIb3BUiTToFZmCmpmZxAoGAH2W3eDhuKrsE50ntofhZ\nx9Uy0OD30nztyecen26kuljcX7qdWh564mBob75wGSAoYzGSePZjJ/tQ4FznXUes\n6F4gM8Y58d3YZOjNq3xnT7AfaJHNnnjxA4pfnGRVr3wA2vwyrD3McgAPXf+mnB23\nl8nz6wxk97WnaIyo7Ofi/+A=\n-----END PRIVATE KEY-----\n"
8
+ };
@@ -0,0 +1,334 @@
1
+ /* eslint no-unused-expressions: 0, prefer-arrow-callback: 0, new-cap: 0 */
2
+
3
+ 'use strict';
4
+
5
+ const proxyquire = require('proxyquire').noCallThru();
6
+
7
+ const { parser, compiler } = require('../lib/handler/imap-handler');
8
+ const { ImapStream } = require('../lib/handler/imap-stream');
9
+ const { ParserInstance } = require('../lib/handler/parser-instance');
10
+ const { TokenParser } = require('../lib/handler/token-parser');
11
+ const { searchCompiler } = require('../lib/search-compiler');
12
+
13
+ let asyncWrapper = (test, handler) => {
14
+ handler(test)
15
+ .then(() => test.done())
16
+ .catch(err => {
17
+ test.ifError(err);
18
+ test.done();
19
+ });
20
+ };
21
+
22
+ // ============================================================================
23
+ // imap-compiler.js
24
+ // ============================================================================
25
+
26
+ // formatRespEntry: `return entry` when entry is a Buffer (line 23-25). A literal
27
+ // whose value is a Buffer, compiled in non-logging mode, passes the Buffer straight
28
+ // through formatRespEntry.
29
+ module.exports['imap-compiler: Buffer literal value passes through formatRespEntry'] = test =>
30
+ asyncWrapper(test, async test => {
31
+ let out = (
32
+ await compiler({
33
+ tag: '*',
34
+ command: 'CMD',
35
+ attributes: [{ type: 'LITERAL', value: Buffer.from('hello world') }]
36
+ })
37
+ ).toString();
38
+ test.equal(out, '* CMD {11}\r\nhello world');
39
+ });
40
+
41
+ // number node === 0 -> `Math.round(node) || 0` falls back to 0 (line 117). A bare
42
+ // numeric attribute of 0 exercises the `|| 0` right side.
43
+ module.exports['imap-compiler: bare number 0 attribute uses fallback'] = test =>
44
+ asyncWrapper(test, async test => {
45
+ let out = (
46
+ await compiler({
47
+ tag: '*',
48
+ command: 'CMD',
49
+ attributes: [0]
50
+ })
51
+ ).toString();
52
+ test.equal(out, '* CMD 0');
53
+ });
54
+
55
+ // LITERAL node with empty value -> `!node.value ? 0 : ...` true side (line 133).
56
+ module.exports['imap-compiler: LITERAL with empty string value yields {0}'] = test =>
57
+ asyncWrapper(test, async test => {
58
+ let out = (
59
+ await compiler({
60
+ tag: '*',
61
+ command: 'CMD',
62
+ attributes: [{ type: 'LITERAL', value: '' }]
63
+ })
64
+ ).toString();
65
+ test.equal(out, '* CMD {0}\r\n');
66
+ });
67
+
68
+ // LITERAL- (literalMinus) with literal <= 4096 bytes in asArray mode -> exercises the
69
+ // `(literalMinus && literalLength <= 4096)` branch of canAppend (line 139) and usePlus.
70
+ module.exports['imap-compiler: LITERAL- inlines small literals in asArray mode'] = test =>
71
+ asyncWrapper(test, async test => {
72
+ let out = (
73
+ await compiler(
74
+ {
75
+ tag: '*',
76
+ command: 'CMD',
77
+ attributes: [{ type: 'LITERAL', value: 'small' }]
78
+ },
79
+ { asArray: true, literalMinus: true }
80
+ )
81
+ ).map(entry => entry.toString());
82
+ // LITERAL- still appends a non-synchronizing '+' marker for inlined literals
83
+ test.deepEqual(out, ['* CMD {5+}\r\nsmall']);
84
+ });
85
+
86
+ // LITERAL- with a literal larger than 4096 bytes in asArray mode -> canAppend false,
87
+ // so the synchronizing-literal split path runs. The new resp segment is seeded with
88
+ // `formatRespEntry(node.value, true) || []`. Using a Buffer value keeps formatRespEntry
89
+ // returning the Buffer (the `|| []` right side, line 156, is the focus together with the
90
+ // split branch).
91
+ module.exports['imap-compiler: LITERAL- large literal splits (synchronizing) in asArray mode'] = test =>
92
+ asyncWrapper(test, async test => {
93
+ let big = Buffer.alloc(5000, 0x61); // 5000 bytes > 4096
94
+ let out = (
95
+ await compiler(
96
+ {
97
+ tag: '*',
98
+ command: 'CMD',
99
+ attributes: [{ type: 'LITERAL', value: big }]
100
+ },
101
+ { asArray: true, literalMinus: true }
102
+ )
103
+ ).map(entry => entry.toString());
104
+ test.equal(out.length, 2);
105
+ test.equal(out[0], '* CMD {5000}\r\n');
106
+ test.equal(out[1], big.toString());
107
+ });
108
+
109
+ // Synchronizing literal whose value is not a recognized type -> formatRespEntry(value, true)
110
+ // returns null, so `|| []` kicks in (line 156 right side) and the seeded resp is empty.
111
+ module.exports['imap-compiler: synchronizing literal with non-buffer value seeds empty segment'] = test =>
112
+ asyncWrapper(test, async test => {
113
+ let out = (
114
+ await compiler(
115
+ {
116
+ tag: '*',
117
+ command: 'CMD',
118
+ // value is an object that is neither string/number/Buffer but has a numeric
119
+ // length, so literalLength resolves to 10 while formatRespEntry(value, true)
120
+ // returns null -> the seeded resp falls back to [] (line 156 right side).
121
+ attributes: [{ type: 'LITERAL', value: { length: 10 } }]
122
+ },
123
+ { asArray: true }
124
+ )
125
+ ).map(entry => entry.toString());
126
+ // Only the header segment is emitted; the empty seeded data segment is dropped
127
+ // because `if (resp.length)` is false at the end.
128
+ test.deepEqual(out, ['* CMD {10}\r\n']);
129
+ });
130
+
131
+ // STRING node with falsy value -> `(node.value || '')` right side (line 165).
132
+ module.exports['imap-compiler: STRING with empty/falsy value compiles to empty quoted string'] = test =>
133
+ asyncWrapper(test, async test => {
134
+ let out = (
135
+ await compiler({
136
+ tag: '*',
137
+ command: 'CMD',
138
+ attributes: [{ type: 'STRING', value: '' }]
139
+ })
140
+ ).toString();
141
+ test.equal(out, '* CMD ""');
142
+ });
143
+
144
+ // response.attributes that is a single (non-array) object -> `[].concat(response.attributes)`
145
+ // right side of the ternary on line 215.
146
+ module.exports['imap-compiler: single non-array attributes object is wrapped'] = test =>
147
+ asyncWrapper(test, async test => {
148
+ let out = (
149
+ await compiler({
150
+ tag: '*',
151
+ command: 'CMD',
152
+ attributes: { type: 'ATOM', value: 'EXPUNGE' }
153
+ })
154
+ ).toString();
155
+ test.equal(out, '* CMD EXPUNGE');
156
+ });
157
+
158
+ // ============================================================================
159
+ // imap-stream.js
160
+ // ============================================================================
161
+
162
+ // Provide a logger object so the constructor takes the `this.options.logger` branch
163
+ // (lines 61-62) instead of creating a child logger.
164
+ module.exports['imap-stream: explicit logger option is used as-is'] = test => {
165
+ let calls = [];
166
+ let fakeLogger = {
167
+ trace: m => calls.push(m),
168
+ child: () => {
169
+ throw new Error('child should not be called when a logger is supplied');
170
+ }
171
+ };
172
+ let stream = new ImapStream({ cid: 'x', logger: fakeLogger });
173
+ test.equal(stream.log, fakeLogger, 'supplied logger object is used directly');
174
+ test.done();
175
+ };
176
+
177
+ // checkLiteralMarker: a line that is all digits + "}\r\n" but has no opening "{" makes
178
+ // the backward scan run off the start of the line, hitting the final `return false`
179
+ // (line 154).
180
+ module.exports['imap-stream: checkLiteralMarker returns false when no opening brace is found'] = test => {
181
+ let stream = new ImapStream({ cid: 'x' });
182
+ // "12}\n": scan finds '}', then digits '2','1', runs past index 0 -> final return false
183
+ test.equal(stream.checkLiteralMarker(Buffer.from('12}\n')), false);
184
+ // also exercise the CRLF variant
185
+ test.equal(stream.checkLiteralMarker(Buffer.from('99}\r\n')), false);
186
+ test.done();
187
+ };
188
+
189
+ // _transform with a string chunk -> `chunk = Buffer.from(chunk, encoding)` (lines 299-301).
190
+ // Call _transform directly with a string so the typeof === 'string' branch runs.
191
+ // The string->Buffer conversion and byte counting happen synchronously, before the
192
+ // chunk is queued for async processing, so readBytesCounter reflects the decoded length.
193
+ module.exports['imap-stream: _transform converts string chunks to Buffers'] = test => {
194
+ let stream = new ImapStream({ cid: 'x' });
195
+ stream._transform('A1 NOOP\r\n', 'utf8', () => {});
196
+ test.equal(stream.readBytesCounter, Buffer.byteLength('A1 NOOP\r\n'), 'decoded byte length was counted');
197
+ stream.destroy();
198
+ test.done();
199
+ };
200
+
201
+ // ============================================================================
202
+ // parser-instance.js
203
+ // ============================================================================
204
+
205
+ // Drive ParserInstance directly so getCommand is reached with a falsy (empty-string)
206
+ // command and a non-"+" tag, exercising the `(this.command || '')` right side (line 71).
207
+ // This state is not produced by normal parsing, so we construct it explicitly.
208
+ module.exports['parser-instance: getCommand falls back to empty string for falsy command'] = test =>
209
+ asyncWrapper(test, async test => {
210
+ let inst = new ParserInstance('* ', {});
211
+ inst.tag = '*'; // not '+', so the early continuation branch is skipped
212
+ // Pre-set command to a value getElement would have produced, then blank the
213
+ // remainder so the `if (!this.command)` re-fetch path throws? No - instead we
214
+ // set command to empty string and clear remainder so getElement is NOT re-run
215
+ // is impossible (empty string is falsy). Use a getElement stub on the instance
216
+ // to deterministically yield an empty command without touching lib/ source.
217
+ inst.getElement = async () => '';
218
+ let command = await inst.getCommand();
219
+ test.equal(command, '', 'command is the empty string');
220
+ });
221
+
222
+ // ============================================================================
223
+ // token-parser.js
224
+ // ============================================================================
225
+
226
+ // Construct TokenParser directly with a falsy str, undefined options and a falsy
227
+ // startPos to cover the `str || ''` (40), `options || {}` (41) and `startPos || 0`
228
+ // (45) fallbacks. These cannot occur through ParserInstance.getAttributes (which
229
+ // guards against an empty remainder), so direct construction is required.
230
+ module.exports['token-parser: constructor fallbacks for empty str / no options / zero startPos'] = test =>
231
+ asyncWrapper(test, async test => {
232
+ let parent = { command: 'FETCH' };
233
+ let tp = new TokenParser(parent, 0, undefined, undefined);
234
+ test.equal(tp.str, '');
235
+ test.deepEqual(tp.options, {});
236
+ test.equal(tp.pos, 0);
237
+ let attrs = await tp.getAttributes();
238
+ test.deepEqual(attrs, []);
239
+ });
240
+
241
+ // Cover the `(node.type || '')` fallback (line 85) by walking a tree that contains a
242
+ // closed child node with a falsy type. Normal parsing always assigns a type, so the
243
+ // tree is constructed explicitly.
244
+ module.exports['token-parser: walk handles a closed node with a falsy type'] = test =>
245
+ asyncWrapper(test, async test => {
246
+ let parent = { command: 'FETCH' };
247
+ let tp = new TokenParser(parent, 0, '', {});
248
+ tp.tree = {
249
+ type: 'TREE',
250
+ isClosed: true,
251
+ childNodes: [{ type: false, isClosed: true, value: '', childNodes: [] }]
252
+ };
253
+ let attrs = await tp.getAttributes();
254
+ test.deepEqual(attrs, []);
255
+ });
256
+
257
+ // ============================================================================
258
+ // search-compiler.js
259
+ // ============================================================================
260
+
261
+ let createMockConnection = (options = {}) => ({
262
+ capabilities: new Map(options.capabilities || [['IMAP4rev1', true]]),
263
+ enabled: new Set(options.enabled || []),
264
+ mailbox: options.mailbox || {
265
+ flags: new Set(['\\Seen']),
266
+ permanentFlags: new Set(['\\*'])
267
+ }
268
+ });
269
+
270
+ // setOpt NOT branch (lines 46-48): EMAILID/THREADID call setOpt with an unguarded
271
+ // params[term]. Passing the boolean `false` as the value reaches `value === false`
272
+ // and pushes a leading NOT atom. (A `null` value also enters this branch, but setOpt
273
+ // then crashes on `value.toString()` at line 56, so `false` is the only value that
274
+ // drives this branch to completion through the public API.)
275
+ module.exports['search-compiler: threadid false triggers setOpt NOT branch'] = test => {
276
+ let connection = createMockConnection({ capabilities: [['OBJECTID', true]] });
277
+ let attrs = searchCompiler(connection, { threadid: false });
278
+ test.deepEqual(attrs, [
279
+ { type: 'ATOM', value: 'NOT' },
280
+ { type: 'ATOM', value: 'THREADID' },
281
+ { type: 'ATOM', value: 'false' }
282
+ ]);
283
+ test.done();
284
+ };
285
+
286
+ // KEYWORD search where the flag is NOT usable via canUseFlag but IS already present in
287
+ // the mailbox flags set -> exercises the `mailbox.flags.has(flag)` right side (line 297).
288
+ module.exports['search-compiler: keyword already present in mailbox flags is accepted'] = test => {
289
+ // permanentFlags has no '\\*', so canUseFlag(mailbox, customFlag) is false, but the
290
+ // flag is in mailbox.flags, so the OR short-circuits to the right operand.
291
+ let connection = createMockConnection({
292
+ mailbox: {
293
+ flags: new Set(['$Label1']),
294
+ permanentFlags: new Set(['\\Seen']) // no '\\*' wildcard -> custom flags not auto-usable
295
+ }
296
+ });
297
+ let attrs = searchCompiler(connection, { keyword: '$Label1' });
298
+ test.deepEqual(attrs, [
299
+ { type: 'ATOM', value: 'KEYWORD' },
300
+ { type: 'ATOM', value: '$Label1' }
301
+ ]);
302
+ test.done();
303
+ };
304
+
305
+ // ============================================================================
306
+ // proxy-connection.js
307
+ // ============================================================================
308
+
309
+ // attachEarlyErrorHandler guard (lines 23-25): the proxy client returns a truthy socket
310
+ // that lacks an `.on` method, so the guard returns early.
311
+ module.exports['proxy-connection: socket without .on skips early error handler attach'] = async test => {
312
+ let logger = { info: () => {}, error: () => {} };
313
+ let bareSocket = { write() {}, end() {} }; // truthy, but no .on
314
+ const { proxyConnection } = proxyquire('../lib/proxy-connection', {
315
+ 'nodemailer/lib/smtp-connection/http-proxy-client': (url, port, host, cb) => {
316
+ cb(null, bareSocket);
317
+ },
318
+ socks: { SocksClient: {} },
319
+ dns: { promises: { resolve: async () => ['127.0.0.1'] } },
320
+ net: { isIP: () => true }
321
+ });
322
+
323
+ let socket = await proxyConnection(logger, 'http://proxy.example.com:8080', '192.168.1.1', 993);
324
+ test.equal(socket, bareSocket, 'socket returned unchanged');
325
+ test.ok(!('_earlyErrorHandler' in socket), 'no early error handler was attached to a socket without .on');
326
+ test.done();
327
+ };
328
+
329
+ // Sanity: untagged parse still round-trips, just to keep the parser import exercised.
330
+ module.exports['sanity: parser/compiler round-trip'] = test =>
331
+ asyncWrapper(test, async test => {
332
+ let parsed = await parser('* OK Ready', { allowUntagged: true });
333
+ test.equal(parsed.command, 'OK');
334
+ });
@@ -56,6 +56,53 @@ module.exports['IMAP Compiler: TEXT'] = test =>
56
56
  )
57
57
  );
58
58
 
59
+ module.exports['IMAP Compiler: NUMBER attribute'] = test =>
60
+ asyncWrapper(test, async test =>
61
+ test.equal(
62
+ (
63
+ await compiler({
64
+ tag: '*',
65
+ command: 'CMD',
66
+ attributes: [
67
+ {
68
+ type: 'NUMBER',
69
+ value: 42
70
+ }
71
+ ]
72
+ })
73
+ ).toString(),
74
+ '* CMD 42'
75
+ )
76
+ );
77
+
78
+ module.exports['IMAP Compiler: NUMBER attribute defaults missing value to 0'] = test =>
79
+ asyncWrapper(test, async test =>
80
+ test.equal(
81
+ (
82
+ await compiler({
83
+ tag: '*',
84
+ command: 'CMD',
85
+ attributes: [{ type: 'NUMBER' }]
86
+ })
87
+ ).toString(),
88
+ '* CMD 0'
89
+ )
90
+ );
91
+
92
+ module.exports['IMAP Compiler: unrecognized value type compiles to empty buffer'] = test =>
93
+ asyncWrapper(test, async test => {
94
+ // A SEQUENCE node whose value is neither string/number/Buffer falls through
95
+ // formatRespEntry to an empty Buffer rather than throwing.
96
+ let out = (
97
+ await compiler({
98
+ tag: '*',
99
+ command: 'CMD',
100
+ attributes: [{ type: 'SEQUENCE', value: { unexpected: true } }]
101
+ })
102
+ ).toString();
103
+ test.equal(out, '* CMD ');
104
+ });
105
+
59
106
  module.exports['IMAP Compiler: SECTION'] = test =>
60
107
  asyncWrapper(test, async test =>
61
108
  test.equal(
@@ -0,0 +1,154 @@
1
+ 'use strict';
2
+
3
+ // End-to-end test of the DEFLATE compression path (RFC 4978). The mock server
4
+ // negotiates COMPRESS and then speaks raw-deflate in both directions, mirroring
5
+ // the client's inflate/deflate pipeline. This covers ImapFlow.compress() and the
6
+ // compression-stream cleanup inside close().
7
+
8
+ const net = require('net');
9
+ const zlib = require('zlib');
10
+ const { ImapFlow } = require('../lib/imap-flow');
11
+
12
+ const CAPS = 'IMAP4rev1 ID ENABLE NAMESPACE COMPRESS=DEFLATE';
13
+
14
+ const listen = server => new Promise(resolve => server.listen(0, '127.0.0.1', () => resolve(server.address().port)));
15
+
16
+ // IMAP session line responder shared before and after compression.
17
+ const respond = (write, line) => {
18
+ let parts = line.split(' ');
19
+ let tag = parts[0];
20
+ let cmd = (parts[1] || '').toUpperCase();
21
+ switch (cmd) {
22
+ case 'CAPABILITY':
23
+ write(`* CAPABILITY ${CAPS}\r\n${tag} OK done\r\n`);
24
+ break;
25
+ case 'ID':
26
+ write(`* ID ("name" "mock" "version" "1")\r\n${tag} OK done\r\n`);
27
+ break;
28
+ case 'LOGIN':
29
+ write(`${tag} OK LOGIN done\r\n`);
30
+ break;
31
+ case 'NAMESPACE':
32
+ write(`* NAMESPACE (("" "/")) NIL NIL\r\n${tag} OK done\r\n`);
33
+ break;
34
+ case 'ENABLE':
35
+ write(`${tag} OK done\r\n`);
36
+ break;
37
+ case 'NOOP':
38
+ write(`${tag} OK NOOP done\r\n`);
39
+ break;
40
+ case 'LOGOUT':
41
+ write(`* BYE bye\r\n${tag} OK done\r\n`);
42
+ break;
43
+ default:
44
+ write(`${tag} BAD unknown ${cmd}\r\n`);
45
+ }
46
+ };
47
+
48
+ module.exports['Compress: session negotiates DEFLATE and runs commands'] = async test => {
49
+ let server = net.createServer(socket => {
50
+ socket.on('error', () => {});
51
+
52
+ let compressed = false;
53
+ let deflate = null;
54
+ let inflate = null;
55
+ let buf = '';
56
+
57
+ // Plain writer (pre-compression)
58
+ let writePlain = str => socket.write(str);
59
+ // Compressed writer: deflate + flush so the client receives complete frames
60
+ let writeDeflate = str => {
61
+ deflate.write(Buffer.from(str, 'binary'));
62
+ deflate.flush();
63
+ };
64
+
65
+ // function declarations (hoisted) because feed() and onLine() are mutually recursive
66
+ let lineBuf = '';
67
+ function onLine(line) {
68
+ let parts = line.split(' ');
69
+ let tag = parts[0];
70
+ let cmd = (parts[1] || '').toUpperCase();
71
+ if (cmd === 'COMPRESS') {
72
+ // Acknowledge, then switch both directions to raw deflate
73
+ writePlain(`${tag} OK Begin compression\r\n`);
74
+ compressed = true;
75
+ deflate = zlib.createDeflateRaw();
76
+ inflate = zlib.createInflateRaw();
77
+ deflate.pipe(socket);
78
+ inflate.on('data', d => feed(d.toString('binary')));
79
+ inflate.on('error', () => {});
80
+ deflate.on('error', () => {});
81
+ return;
82
+ }
83
+ respond(compressed ? writeDeflate : writePlain, line);
84
+ }
85
+
86
+ function feed(chunk) {
87
+ lineBuf += chunk;
88
+ let idx;
89
+ while ((idx = lineBuf.indexOf('\r\n')) >= 0) {
90
+ let line = lineBuf.slice(0, idx);
91
+ lineBuf = lineBuf.slice(idx + 2);
92
+ onLine(line);
93
+ }
94
+ }
95
+
96
+ socket.on('data', data => {
97
+ if (compressed) {
98
+ // After COMPRESS negotiation, all inbound bytes are deflated
99
+ inflate.write(data);
100
+ } else {
101
+ buf += data.toString('binary');
102
+ let idx;
103
+ while ((idx = buf.indexOf('\r\n')) >= 0) {
104
+ let line = buf.slice(0, idx);
105
+ buf = buf.slice(idx + 2);
106
+ // hand off any already-buffered bytes after COMPRESS to inflate
107
+ onLine(line);
108
+ if (compressed && buf.length) {
109
+ inflate.write(Buffer.from(buf, 'binary'));
110
+ buf = '';
111
+ }
112
+ }
113
+ }
114
+ });
115
+
116
+ socket.write(`* OK [CAPABILITY ${CAPS}] ready\r\n`);
117
+ });
118
+
119
+ let port = await listen(server);
120
+ let client = new ImapFlow({
121
+ host: '127.0.0.1',
122
+ port,
123
+ secure: false,
124
+ disableAutoIdle: true,
125
+ disableCompression: false, // enable COMPRESS negotiation
126
+ logger: false,
127
+ auth: { user: 'test', pass: 'secret' }
128
+ });
129
+ client.on('error', () => {});
130
+
131
+ await client.connect();
132
+ test.ok(client.usable);
133
+ test.ok(client._deflate, 'deflate pipeline established');
134
+ test.ok(client._inflate, 'inflate pipeline established');
135
+
136
+ // Run a command through the compressed channel
137
+ await client.noop();
138
+
139
+ // Push a buffer larger than the deflate writable highWaterMark (64KB) straight
140
+ // through the compression pump so its buffer overflows and readNext waits for 'drain'.
141
+ client.state = client.states.AUTHENTICATED;
142
+ client.commandParts = [];
143
+ client.write(Buffer.alloc(128 * 1024, 0x61));
144
+ await new Promise(r => setTimeout(r, 50));
145
+
146
+ await client.logout();
147
+ client.close();
148
+ // close() must tear down the compression streams
149
+ test.ok(!client._deflate, 'deflate cleaned up after close');
150
+ test.ok(!client._inflate, 'inflate cleaned up after close');
151
+
152
+ server.close();
153
+ test.done();
154
+ };