1'use strict'; 2// Test unzipping a gzip file that has trailing garbage 3 4const common = require('../common'); 5const assert = require('assert'); 6const zlib = require('zlib'); 7 8// Should ignore trailing null-bytes 9let data = Buffer.concat([ 10 zlib.gzipSync('abc'), 11 zlib.gzipSync('def'), 12 Buffer.alloc(10), 13]); 14 15assert.strictEqual(zlib.gunzipSync(data).toString(), 'abcdef'); 16 17zlib.gunzip(data, common.mustSucceed((result) => { 18 assert.strictEqual( 19 result.toString(), 20 'abcdef', 21 `result '${result.toString()}' should match original string` 22 ); 23})); 24 25// If the trailing garbage happens to look like a gzip header, it should 26// throw an error. 27data = Buffer.concat([ 28 zlib.gzipSync('abc'), 29 zlib.gzipSync('def'), 30 Buffer.from([0x1f, 0x8b, 0xff, 0xff]), 31 Buffer.alloc(10), 32]); 33 34assert.throws( 35 () => zlib.gunzipSync(data), 36 /^Error: unknown compression method$/ 37); 38 39zlib.gunzip(data, common.mustCall((err, result) => { 40 common.expectsError({ 41 code: 'Z_DATA_ERROR', 42 name: 'Error', 43 message: 'unknown compression method' 44 })(err); 45 assert.strictEqual(result, undefined); 46})); 47 48// In this case the trailing junk is too short to be a gzip segment 49// So we ignore it and decompression succeeds. 50data = Buffer.concat([ 51 zlib.gzipSync('abc'), 52 zlib.gzipSync('def'), 53 Buffer.from([0x1f, 0x8b, 0xff, 0xff]), 54]); 55 56assert.throws( 57 () => zlib.gunzipSync(data), 58 /^Error: unknown compression method$/ 59); 60 61zlib.gunzip(data, common.mustCall((err, result) => { 62 assert(err instanceof Error); 63 assert.strictEqual(err.code, 'Z_DATA_ERROR'); 64 assert.strictEqual(err.message, 'unknown compression method'); 65 assert.strictEqual(result, undefined); 66})); 67