node/test/parallel/test-zlib-from-gzip-with-trailing-garbage.js
Rich Trott 14f5a9becc test: change deprecated method to recommended
In non-buffer tests, change usage of the Buffer constructor to one of
the recommended alternatives.

PR-URL: https://github.com/nodejs/node/pull/13649
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Brian White <mscdex@mscdex.net>
2017-06-15 13:59:43 -07:00

66 lines
1.7 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.alloc(10)
]);
assert.strictEqual(zlib.gunzipSync(data).toString(), 'abcdef');
zlib.gunzip(data, common.mustCall((err, result) => {
assert.ifError(err);
assert.strictEqual(
result.toString(),
'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.from([0x1f, 0x8b, 0xff, 0xff]),
Buffer.alloc(10)
]);
assert.throws(
() => zlib.gunzipSync(data),
/^Error: unknown compression method$/
);
zlib.gunzip(data, common.mustCall((err, result) => {
assert(err instanceof Error);
assert.strictEqual(err.code, 'Z_DATA_ERROR');
assert.strictEqual(err.message, 'unknown compression method');
assert.strictEqual(result, undefined);
}));
// 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.from([0x1f, 0x8b, 0xff, 0xff])
]);
assert.throws(
() => zlib.gunzipSync(data),
/^Error: unknown compression method$/
);
zlib.gunzip(data, common.mustCall((err, result) => {
assert(err instanceof Error);
assert.strictEqual(err.code, 'Z_DATA_ERROR');
assert.strictEqual(err.message, 'unknown compression method');
assert.strictEqual(result, undefined);
}));