summaryrefslogtreecommitdiff
path: root/test/parallel/test-zlib-premature-end.js
blob: 9e191c4c88282c2efe2e7b8d16fa92f2204148c8 (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
'use strict';
const common = require('../common');
const zlib = require('zlib');
const assert = require('assert');

const input = '0123456789'.repeat(4);

for (const [ compress, decompressor ] of [
  [ zlib.deflateRawSync, zlib.createInflateRaw ],
  [ zlib.deflateSync, zlib.createInflate ],
  [ zlib.brotliCompressSync, zlib.createBrotliDecompress ]
]) {
  const compressed = compress(input);
  const trailingData = Buffer.from('not valid compressed data');

  for (const variant of [
    (stream) => { stream.end(compressed); },
    (stream) => { stream.write(compressed); stream.write(trailingData); },
    (stream) => { stream.write(compressed); stream.end(trailingData); },
    (stream) => { stream.write(Buffer.concat([compressed, trailingData])); },
    (stream) => { stream.end(Buffer.concat([compressed, trailingData])); }
  ]) {
    let output = '';
    const stream = decompressor();
    stream.setEncoding('utf8');
    stream.on('data', (chunk) => output += chunk);
    stream.on('end', common.mustCall(() => {
      assert.strictEqual(output, input);
      assert.strictEqual(stream.bytesWritten, compressed.length);
    }));
    variant(stream);
  }
}