summaryrefslogtreecommitdiffstats
path: root/testing/xpcshell/node-http2/test/compressor.js
blob: f86baf5db926b9467aebd6a7c794e73c07c57cca (plain)
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
var expect = require('chai').expect;
var util = require('./util');

var compressor = require('../lib/protocol/compressor');
var HeaderTable = compressor.HeaderTable;
var HuffmanTable = compressor.HuffmanTable;
var HeaderSetCompressor = compressor.HeaderSetCompressor;
var HeaderSetDecompressor = compressor.HeaderSetDecompressor;
var Compressor = compressor.Compressor;
var Decompressor = compressor.Decompressor;

var test_integers = [{
  N: 5,
  I: 10,
  buffer: new Buffer([10])
}, {
  N: 0,
  I: 10,
  buffer: new Buffer([10])
}, {
  N: 5,
  I: 1337,
  buffer: new Buffer([31, 128 + 26, 10])
}, {
  N: 0,
  I: 1337,
  buffer: new Buffer([128 + 57, 10])
}];

var test_strings = [{
  string: 'www.foo.com',
  buffer: new Buffer('89f1e3c2f29ceb90f4ff', 'hex')
}, {
  string: 'éáűőúöüó€',
  buffer: new Buffer('13c3a9c3a1c5b1c591c3bac3b6c3bcc3b3e282ac', 'hex')
}];

test_huffman_request = {
  'GET': 'c5837f',
  'http': '9d29af',
  '/': '63',
  'www.foo.com': 'f1e3c2f29ceb90f4ff',
  'https': '9d29ad1f',
  'www.bar.com': 'f1e3c2f18ec5c87a7f',
  'no-cache': 'a8eb10649cbf',
  '/custom-path.css': '6096a127a56ac699d72211',
  'custom-key': '25a849e95ba97d7f',
  'custom-value': '25a849e95bb8e8b4bf'
};

test_huffman_response = {
  '302': '6402',
  'private': 'aec3771a4b',
  'Mon, 21 OCt 2013 20:13:21 GMT': 'd07abe941054d5792a0801654102e059b820a98b46ff',
  ': https://www.bar.com': 'b8a4e94d68b8c31e3c785e31d8b90f4f',
  '200': '1001',
  'Mon, 21 OCt 2013 20:13:22 GMT': 'd07abe941054d5792a0801654102e059b821298b46ff',
  'https://www.bar.com': '9d29ad171863c78f0bc63b1721e9',
  'gzip': '9bd9ab',
  'foo=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
AAAAAAAAAAAAAAAAAAAAAAAAAALASDJKHQKBZXOQWEOPIUAXQWEOIUAXLJKHQWOEIUAL\
QWEOIUAXLQEUAXLLKJASDQWEOUIAXN1234LASDJKHQKBZXOQWEOPIUAXQWEOIUAXLJKH\
QWOEIUALQWEOIUAXLQEUAXLLKJASDQWEOUIAXN1234LASDJKHQKBZXOQWEOPIUAXQWEO\
IUAXLJKHQWOEIUALQWEOIUAXLQEUAXLLKJASDQWEOUIAXN1234LASDJKHQKBZXOQWEOP\
IUAXQWEOIUAXLJKHQWOEIUALQWEOIUAXLQEUAXLLKJASDQWEOUIAXN1234ZZZZZZZZZZ\
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ1234 m\
ax-age=3600; version=1': '94e7821861861861861861861861861861861861861861861861861861861861861861861861861861861861861861861861861861861861873c3bafe5cd8f666bbfbf9ab672c1ab5e4e10fe6ce583564e10fe67cb9b1ece5ab064e10e7d9cb06ac9c21fccfb307087f33e7cd961dd7f672c1ab86487f34844cb59e1dd7f2e6c7b335dfdfcd5b3960d5af27087f3672c1ab27087f33e5cd8f672d583270873ece583564e10fe67d983843f99f3e6cb0eebfb3960d5c3243f9a42265acf0eebf97363d99aefefe6ad9cb06ad793843f9b3960d593843f99f2e6c7b396ac1938439f672c1ab27087f33ecc1c21fccf9f3658775fd9cb06ae1921fcd21132d678775fcb9b1eccd77f7f356ce58356bc9c21fcd9cb06ac9c21fccf97363d9cb560c9c21cfb3960d593843f99f660e10fe67cf9b2c3bafece583570c90fe6908996bf7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f42265a5291f9587316065c003ed4ee5b1063d5007f',
  'foo=ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ\
ZZZZZZZZZZZZZZZZZZZZZZZZZZLASDJKHQKBZXOQWEOPIUAXQWEOIUAXLJKHQWOEIUAL\
QWEOIUAXLQEUAXLLKJASDQWEOUIAXN1234LASDJKHQKBZXOQWEOPIUAXQWEOIUAXLJKH\
QWOEIUALQWEOIUAXLQEUAXLLKJASDQWEOUIAXN1234LASDJKHQKBZXOQWEOPIUAXQWEO\
IUAXLJKHQWOEIUALQWEOIUAXLQEUAXLLKJASDQWEOUIAXN1234LASDJKHQKBZXOQWEOP\
IUAXQWEOIUAXLJKHQWOEIUALQWEOIUAXLQEUAXLLKJASDQWEOUIAXN1234AAAAAAAAAA\
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA1234 m\
ax-age=3600; version=1': '94e783f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f73c3bafe5cd8f666bbfbf9ab672c1ab5e4e10fe6ce583564e10fe67cb9b1ece5ab064e10e7d9cb06ac9c21fccfb307087f33e7cd961dd7f672c1ab86487f34844cb59e1dd7f2e6c7b335dfdfcd5b3960d5af27087f3672c1ab27087f33e5cd8f672d583270873ece583564e10fe67d983843f99f3e6cb0eebfb3960d5c3243f9a42265acf0eebf97363d99aefefe6ad9cb06ad793843f9b3960d593843f99f2e6c7b396ac1938439f672c1ab27087f33ecc1c21fccf9f3658775fd9cb06ae1921fcd21132d678775fcb9b1eccd77f7f356ce58356bc9c21fcd9cb06ac9c21fccf97363d9cb560c9c21cfb3960d593843f99f660e10fe67cf9b2c3bafece583570c90fe6908996a1861861861861861861861861861861861861861861861861861861861861861861861861861861861861861861861861861861861842265a5291f9587316065c003ed4ee5b1063d5007f'
};

var test_headers = [{
  // index
  header: {
    name: 1,
    value: 1,
    index: false,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('82', 'hex')
}, {
  // index
  header: {
    name: 5,
    value: 5,
    index: false,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('86', 'hex')
}, {
  // index
  header: {
    name: 3,
    value: 3,
    index: false,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('84', 'hex')
}, {
  // literal w/index, name index
  header: {
    name: 0,
    value: 'www.foo.com',
    index: true,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('41' + '89f1e3c2f29ceb90f4ff', 'hex')
}, {
  // indexed
  header: {
    name: 1,
    value: 1,
    index: false,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('82', 'hex')
}, {
  // indexed
  header: {
    name: 6,
    value: 6,
    index: false,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('87', 'hex')
}, {
  // indexed
  header: {
    name: 3,
    value: 3,
    index: false,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('84', 'hex')
}, {
  // literal w/index, name index
  header: {
    name: 0,
    value: 'www.bar.com',
    index: true,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('41' + '89f1e3c2f18ec5c87a7f', 'hex')
}, {
  // literal w/index, name index
  header: {
    name: 23,
    value: 'no-cache',
    index: true,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('58' + '86a8eb10649cbf', 'hex')
}, {
  // index
  header: {
    name: 1,
    value: 1,
    index: false,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('82', 'hex')
}, {
  // index
  header: {
    name: 6,
    value: 6,
    index: false,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('87', 'hex')
}, {
  // literal w/index, name index
  header: {
    name: 3,
    value: '/custom-path.css',
    index: true,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('44' + '8b6096a127a56ac699d72211', 'hex')
}, {
  // index
  header: {
    name: 63,
    value: 63,
    index: false,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('C0', 'hex')
}, {
  // literal w/index, new name & value
  header: {
    name: 'custom-key',
    value: 'custom-value',
    index: true,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('40' + '8825a849e95ba97d7f' + '8925a849e95bb8e8b4bf', 'hex')
}, {
  // index
  header: {
    name: 1,
    value: 1,
    index: false,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('82', 'hex')
}, {
  // index
  header: {
    name: 6,
    value: 6,
    index: false,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('87', 'hex')
}, {
  // index
  header: {
    name: 62,
    value: 62,
    index: false,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('BF', 'hex')
}, {
  // index
  header: {
    name: 65,
    value: 65,
    index: false,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('C2', 'hex')
}, {
  // index
  header: {
    name: 64,
    value: 64,
    index: false,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('C1', 'hex')
}, {
  // index
  header: {
    name: 61,
    value: 61,
    index: false,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('BE', 'hex')
}, {
  // Literal w/o index, name index
  header: {
    name: 6,
    value: "whatever",
    index: false,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('07' + '86f138d25ee5b3', 'hex')
}, {
  // Literal w/o index, new name & value
  header: {
    name: "foo",
    value: "bar",
    index: false,
    mustNeverIndex: false,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('00' + '8294e7' + '03626172', 'hex')
}, {
  // Literal never indexed, name index
  header: {
    name: 6,
    value: "whatever",
    index: false,
    mustNeverIndex: true,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('17' + '86f138d25ee5b3', 'hex')
}, {
  // Literal never indexed, new name & value
  header: {
    name: "foo",
    value: "bar",
    index: false,
    mustNeverIndex: true,
    contextUpdate: false,
    newMaxSize: 0
  },
  buffer: new Buffer('10' + '8294e7' + '03626172', 'hex')
}, {
  header: {
    name: -1,
    value: -1,
    index: false,
    mustNeverIndex: false,
    contextUpdate: true,
    newMaxSize: 100
  },
  buffer: new Buffer('3F45', 'hex')
}];

var test_header_sets = [{
  headers: {
    ':method': 'GET',
    ':scheme': 'http',
    ':path': '/',
    ':authority': 'www.foo.com'
  },
  buffer: util.concat(test_headers.slice(0, 4).map(function(test) { return test.buffer; }))
}, {
  headers: {
    ':method': 'GET',
    ':scheme': 'https',
    ':path': '/',
    ':authority': 'www.bar.com',
    'cache-control': 'no-cache'
  },
  buffer: util.concat(test_headers.slice(4, 9).map(function(test) { return test.buffer; }))
}, {
  headers: {
    ':method': 'GET',
    ':scheme': 'https',
    ':path': '/custom-path.css',
    ':authority': 'www.bar.com',
    'custom-key': 'custom-value'
  },
  buffer: util.concat(test_headers.slice(9, 14).map(function(test) { return test.buffer; }))
}, {
  headers: {
    ':method': 'GET',
    ':scheme': 'https',
    ':path': '/custom-path.css',
    ':authority': ['www.foo.com', 'www.bar.com'],
    'custom-key': 'custom-value'
  },
  buffer: util.concat(test_headers.slice(14, 19).map(function(test) { return test.buffer; }))
}];

describe('compressor.js', function() {
  describe('HeaderTable', function() {
  });

  describe('HuffmanTable', function() {
    describe('method encode(buffer)', function() {
      it('should return the Huffman encoded version of the input buffer', function() {
        var table = HuffmanTable.huffmanTable;
        for (var decoded in test_huffman_request) {
          var encoded = test_huffman_request[decoded];
          expect(table.encode(new Buffer(decoded)).toString('hex')).to.equal(encoded);
        }
        table = HuffmanTable.huffmanTable;
        for (decoded in test_huffman_response) {
          encoded = test_huffman_response[decoded];
          expect(table.encode(new Buffer(decoded)).toString('hex')).to.equal(encoded);
        }
      });
    });
    describe('method decode(buffer)', function() {
      it('should return the Huffman decoded version of the input buffer', function() {
        var table = HuffmanTable.huffmanTable;
        for (var decoded in test_huffman_request) {
          var encoded = test_huffman_request[decoded];
          expect(table.decode(new Buffer(encoded, 'hex')).toString()).to.equal(decoded);
        }
        table = HuffmanTable.huffmanTable;
        for (decoded in test_huffman_response) {
          encoded = test_huffman_response[decoded];
          expect(table.decode(new Buffer(encoded, 'hex')).toString()).to.equal(decoded);
        }
      });
    });
  });

  describe('HeaderSetCompressor', function() {
    describe('static method .integer(I, N)', function() {
      it('should return an array of buffers that represent the N-prefix coded form of the integer I', function() {
        for (var i = 0; i < test_integers.length; i++) {
          var test = test_integers[i];
          test.buffer.cursor = 0;
          expect(util.concat(HeaderSetCompressor.integer(test.I, test.N))).to.deep.equal(test.buffer);
        }
      });
    });
    describe('static method .string(string)', function() {
      it('should return an array of buffers that represent the encoded form of the string', function() {
        var table = HuffmanTable.huffmanTable;
        for (var i = 0; i < test_strings.length; i++) {
          var test = test_strings[i];
          expect(util.concat(HeaderSetCompressor.string(test.string, table))).to.deep.equal(test.buffer);
        }
      });
    });
    describe('static method .header({ name, value, index })', function() {
      it('should return an array of buffers that represent the encoded form of the header', function() {
        var table = HuffmanTable.huffmanTable;
        for (var i = 0; i < test_headers.length; i++) {
          var test = test_headers[i];
          expect(util.concat(HeaderSetCompressor.header(test.header, table))).to.deep.equal(test.buffer);
        }
      });
    });
  });

  describe('HeaderSetDecompressor', function() {
    describe('static method .integer(buffer, N)', function() {
      it('should return the parsed N-prefix coded number and increase the cursor property of buffer', function() {
        for (var i = 0; i < test_integers.length; i++) {
          var test = test_integers[i];
          test.buffer.cursor = 0;
          expect(HeaderSetDecompressor.integer(test.buffer, test.N)).to.equal(test.I);
          expect(test.buffer.cursor).to.equal(test.buffer.length);
        }
      });
    });
    describe('static method .string(buffer)', function() {
      it('should return the parsed string and increase the cursor property of buffer', function() {
        var table = HuffmanTable.huffmanTable;
        for (var i = 0; i < test_strings.length; i++) {
          var test = test_strings[i];
          test.buffer.cursor = 0;
          expect(HeaderSetDecompressor.string(test.buffer, table)).to.equal(test.string);
          expect(test.buffer.cursor).to.equal(test.buffer.length);
        }
      });
    });
    describe('static method .header(buffer)', function() {
      it('should return the parsed header and increase the cursor property of buffer', function() {
        var table = HuffmanTable.huffmanTable;
        for (var i = 0; i < test_headers.length; i++) {
          var test = test_headers[i];
          test.buffer.cursor = 0;
          expect(HeaderSetDecompressor.header(test.buffer, table)).to.deep.equal(test.header);
          expect(test.buffer.cursor).to.equal(test.buffer.length);
        }
      });
    });
  });
  describe('Decompressor', function() {
    describe('method decompress(buffer)', function() {
      it('should return the parsed header set in { name1: value1, name2: [value2, value3], ... } format', function() {
        var decompressor = new Decompressor(util.log, 'REQUEST');
        for (var i = 0; i < test_header_sets.length - 1; i++) {
          var header_set = test_header_sets[i];
          expect(decompressor.decompress(header_set.buffer)).to.deep.equal(header_set.headers);
        }
      });
    });
    describe('transform stream', function() {
      it('should emit an error event if a series of header frames is interleaved with other frames', function() {
        var decompressor = new Decompressor(util.log, 'REQUEST');
        var error_occured = false;
        decompressor.on('error', function() {
          error_occured = true;
        });
        decompressor.write({
          type: 'HEADERS',
          flags: {
            END_HEADERS: false
          },
          data: new Buffer(5)
        });
        decompressor.write({
          type: 'DATA',
          flags: {},
          data: new Buffer(5)
        });
        expect(error_occured).to.be.equal(true);
      });
    });
  });

  describe('invariant', function() {
    describe('decompressor.decompress(compressor.compress(headerset)) === headerset', function() {
      it('should be true for any header set if the states are synchronized', function() {
        var compressor = new Compressor(util.log, 'REQUEST');
        var decompressor = new Decompressor(util.log, 'REQUEST');
        var n = test_header_sets.length;
        for (var i = 0; i < 10; i++) {
          var headers = test_header_sets[i%n].headers;
          var compressed = compressor.compress(headers);
          var decompressed = decompressor.decompress(compressed);
          expect(decompressed).to.deep.equal(headers);
          expect(compressor._table).to.deep.equal(decompressor._table);
        }
      });
    });
    describe('source.pipe(compressor).pipe(decompressor).pipe(destination)', function() {
      it('should behave like source.pipe(destination) for a stream of frames', function(done) {
        var compressor = new Compressor(util.log, 'RESPONSE');
        var decompressor = new Decompressor(util.log, 'RESPONSE');
        var n = test_header_sets.length;
        compressor.pipe(decompressor);
        for (var i = 0; i < 10; i++) {
          compressor.write({
            type: i%2 ? 'HEADERS' : 'PUSH_PROMISE',
            flags: {},
            headers: test_header_sets[i%n].headers
          });
        }
        setTimeout(function() {
          for (var j = 0; j < 10; j++) {
            expect(decompressor.read().headers).to.deep.equal(test_header_sets[j%n].headers);
          }
          done();
        }, 10);
      });
    });
    describe('huffmanTable.decompress(huffmanTable.compress(buffer)) === buffer', function() {
      it('should be true for any buffer', function() {
        for (var i = 0; i < 10; i++) {
          var buffer = [];
          while (Math.random() > 0.1) {
            buffer.push(Math.floor(Math.random() * 256))
          }
          buffer = new Buffer(buffer);
          var table = HuffmanTable.huffmanTable;
          var result = table.decode(table.encode(buffer));
          expect(result).to.deep.equal(buffer);
        }
      });
    });
  });
});