0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 07:27:22 +01:00
nodejs/test/parallel/test-zlib-from-gzip-with-trailing-garbage.js
Anna Henningsen 54a5287e3e zlib: fix gzip member head/buffer boundary issue
Make sure that, even if an `inflate()` call only sees the first
few bytes of a following gzip member, all members are decompressed
and part of the full output.

This change also modifies behaviour for trailing garbage:
If there is trailing garbage which happens to start with the
gzip magic bytes, it is no longer discarded but rather throws
an error, since we cannot reliably tell random garbage from
a valid gzip member anyway and have to try and decompress it.
(Null byte padding is not affected, since it has been pointed
out at various occasions that such padding is normal and
discarded by `gzip(1)`, too.)

Adds tests for the special case that the first `inflate()` call
receives only the first few bytes of a second gzip member but
not the whole header (or even just the magic bytes).

PR-URL: https://github.com/nodejs/node/pull/5883
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
2016-03-31 13:23:31 +02:00

50 lines
1.2 KiB
JavaScript

'use strict';
// test unzipping a gzip file that has trailing garbage
const common = require('../common');
const assert = require('assert');
const zlib = require('zlib');
// should ignore trailing null-bytes
let data = Buffer.concat([
zlib.gzipSync('abc'),
zlib.gzipSync('def'),
Buffer(10).fill(0)
]);
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');
}));
// if the trailing garbage happens to look like a gzip header, it should
// throw an error.
data = Buffer.concat([
zlib.gzipSync('abc'),
zlib.gzipSync('def'),
Buffer([0x1f, 0x8b, 0xff, 0xff]),
Buffer(10).fill(0)
]);
assert.throws(() => zlib.gunzipSync(data));
zlib.gunzip(data, common.mustCall((err, result) => {
assert(err);
}));
// In this case the trailing junk is too short to be a gzip segment
// So we ignore it and decompression succeeds.
data = Buffer.concat([
zlib.gzipSync('abc'),
zlib.gzipSync('def'),
Buffer([0x1f, 0x8b, 0xff, 0xff])
]);
assert.throws(() => zlib.gunzipSync(data));
zlib.gunzip(data, common.mustCall((err, result) => {
assert(err);
}));