File: test-zlib-from-concatenated-gzip.js

package info (click to toggle)
node-browserify-zlib 0.2.0%2B20170820git8b3f0a862f6b%2Bdfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 336 kB
  • sloc: javascript: 1,905; makefile: 5
file content (80 lines) | stat: -rw-r--r-- 2,542 bytes parent folder | download | duplicates (4)
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
'use strict';
// Test unzipping a gzip file that contains multiple concatenated "members"

const common = require('./common');
const assert = require('assert');
const zlib = require('../');
const path = require('path');
const fs = require('fs');

const abcEncoded = zlib.gzipSync('abc');
const defEncoded = zlib.gzipSync('def');

const data = Buffer.concat([
  abcEncoded,
  defEncoded
]);

assert.equal(zlib.gunzipSync(data).toString(), 'abcdef');

zlib.gunzip(data, common.mustCall((err, result) => {
  assert.ifError(err);
  assert.equal(result, 'abcdef', 'result should match original string');
}));

zlib.unzip(data, common.mustCall((err, result) => {
  assert.ifError(err);
  assert.equal(result, 'abcdef', 'result should match original string');
}));

// Multi-member support does not apply to zlib inflate/deflate.
zlib.unzip(Buffer.concat([
  zlib.deflateSync('abc'),
  zlib.deflateSync('def')
]), common.mustCall((err, result) => {
  assert.ifError(err);
  assert.equal(result, 'abc', 'result should match contents of first "member"');
}));

// files that have the "right" magic bytes for starting a new gzip member
// in the middle of themselves, even if they are part of a single
// regularly compressed member
const pmmFileZlib = path.join(common.fixturesDir, 'pseudo-multimember-gzip.z');
const pmmFileGz = path.join(common.fixturesDir, 'pseudo-multimember-gzip.gz');

const pmmExpected = zlib.inflateSync(fs.readFileSync(pmmFileZlib));
const pmmResultBuffers = [];

fs.createReadStream(pmmFileGz)
  .pipe(zlib.createGunzip())
  .on('error', (err) => {
    assert.ifError(err);
  })
  .on('data', (data) => pmmResultBuffers.push(data))
  .on('finish', common.mustCall(() => {
    assert.deepStrictEqual(Buffer.concat(pmmResultBuffers), pmmExpected,
      'result should match original random garbage');
  }));

// test that the next gzip member can wrap around the input buffer boundary
[0, 1, 2, 3, 4, defEncoded.length].forEach((offset) => {
  const resultBuffers = [];

  const unzip = zlib.createGunzip()
   .on('error', (err) => {
     assert.ifError(err);
   })
   .on('data', (data) => resultBuffers.push(data))
   .on('finish', common.mustCall(() => {
     assert.strictEqual(Buffer.concat(resultBuffers).toString(), 'abcdef',
      `result should match original input (offset = ${offset})`);
   }));

  // first write: write "abc" + the first bytes of "def"
  unzip.write(Buffer.concat([
    abcEncoded, defEncoded.slice(0, offset)
  ]));

  // write remaining bytes of "def"
  unzip.end(defEncoded.slice(offset));
});