2016-02-03 18:29:26 +00:00
|
|
|
'use strict';
|
2018-12-10 13:27:32 +01:00
|
|
|
// Test unzipping a gzip file that has trailing garbage
|
2016-02-03 18:29:26 +00:00
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const zlib = require('zlib');
|
|
|
|
|
2019-03-07 01:03:53 +01:00
|
|
|
// Should ignore trailing null-bytes
|
2016-02-03 18:29:26 +00:00
|
|
|
let data = Buffer.concat([
|
|
|
|
zlib.gzipSync('abc'),
|
|
|
|
zlib.gzipSync('def'),
|
2021-03-26 08:51:08 -07:00
|
|
|
Buffer.alloc(10),
|
2016-02-03 18:29:26 +00:00
|
|
|
]);
|
|
|
|
|
2017-01-06 22:11:42 -06:00
|
|
|
assert.strictEqual(zlib.gunzipSync(data).toString(), 'abcdef');
|
2016-02-03 18:29:26 +00:00
|
|
|
|
2020-09-06 22:27:07 +02:00
|
|
|
zlib.gunzip(data, common.mustSucceed((result) => {
|
2017-01-06 22:11:42 -06:00
|
|
|
assert.strictEqual(
|
|
|
|
result.toString(),
|
|
|
|
'abcdef',
|
2017-10-06 10:14:58 -07:00
|
|
|
`result '${result.toString()}' should match original string`
|
2017-01-06 22:11:42 -06:00
|
|
|
);
|
2016-02-03 18:29:26 +00:00
|
|
|
}));
|
|
|
|
|
2018-12-03 17:15:45 +01:00
|
|
|
// If the trailing garbage happens to look like a gzip header, it should
|
2016-02-03 18:29:26 +00:00
|
|
|
// throw an error.
|
|
|
|
data = Buffer.concat([
|
|
|
|
zlib.gzipSync('abc'),
|
|
|
|
zlib.gzipSync('def'),
|
2017-06-12 22:18:11 -07:00
|
|
|
Buffer.from([0x1f, 0x8b, 0xff, 0xff]),
|
2021-03-26 08:51:08 -07:00
|
|
|
Buffer.alloc(10),
|
2016-02-03 18:29:26 +00:00
|
|
|
]);
|
|
|
|
|
2017-01-06 22:11:42 -06:00
|
|
|
assert.throws(
|
|
|
|
() => zlib.gunzipSync(data),
|
|
|
|
/^Error: unknown compression method$/
|
|
|
|
);
|
2016-02-03 18:29:26 +00:00
|
|
|
|
|
|
|
zlib.gunzip(data, common.mustCall((err, result) => {
|
2017-07-24 11:21:48 -07:00
|
|
|
common.expectsError({
|
|
|
|
code: 'Z_DATA_ERROR',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'Error',
|
2017-07-24 11:21:48 -07:00
|
|
|
message: 'unknown compression method'
|
|
|
|
})(err);
|
2017-01-06 22:11:42 -06:00
|
|
|
assert.strictEqual(result, undefined);
|
2016-02-03 18:29:26 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
// 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'),
|
2021-03-26 08:51:08 -07:00
|
|
|
Buffer.from([0x1f, 0x8b, 0xff, 0xff]),
|
2016-02-03 18:29:26 +00:00
|
|
|
]);
|
|
|
|
|
2017-01-06 22:11:42 -06:00
|
|
|
assert.throws(
|
|
|
|
() => zlib.gunzipSync(data),
|
|
|
|
/^Error: unknown compression method$/
|
|
|
|
);
|
2016-02-03 18:29:26 +00:00
|
|
|
|
|
|
|
zlib.gunzip(data, common.mustCall((err, result) => {
|
2017-01-06 22:11:42 -06:00
|
|
|
assert(err instanceof Error);
|
|
|
|
assert.strictEqual(err.code, 'Z_DATA_ERROR');
|
|
|
|
assert.strictEqual(err.message, 'unknown compression method');
|
|
|
|
assert.strictEqual(result, undefined);
|
2016-02-03 18:29:26 +00:00
|
|
|
}));
|