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.mustCall((err, result) => { 18 assert.ifError(err); 19 assert.strictEqual( 20 result.toString(), 21 'abcdef', 22 `result '${result.toString()}' should match original string` 23 ); 24})); 25 26// If the trailing garbage happens to look like a gzip header, it should 27// throw an error. 28data = Buffer.concat([ 29 zlib.gzipSync('abc'), 30 zlib.gzipSync('def'), 31 Buffer.from([0x1f, 0x8b, 0xff, 0xff]), 32 Buffer.alloc(10) 33]); 34 35assert.throws( 36 () => zlib.gunzipSync(data), 37 /^Error: unknown compression method$/ 38); 39 40zlib.gunzip(data, common.mustCall((err, result) => { 41 common.expectsError({ 42 code: 'Z_DATA_ERROR', 43 name: 'Error', 44 message: 'unknown compression method' 45 })(err); 46 assert.strictEqual(result, undefined); 47})); 48 49// In this case the trailing junk is too short to be a gzip segment 50// So we ignore it and decompression succeeds. 51data = Buffer.concat([ 52 zlib.gzipSync('abc'), 53 zlib.gzipSync('def'), 54 Buffer.from([0x1f, 0x8b, 0xff, 0xff]) 55]); 56 57assert.throws( 58 () => zlib.gunzipSync(data), 59 /^Error: unknown compression method$/ 60); 61 62zlib.gunzip(data, common.mustCall((err, result) => { 63 assert(err instanceof Error); 64 assert.strictEqual(err.code, 'Z_DATA_ERROR'); 65 assert.strictEqual(err.message, 'unknown compression method'); 66 assert.strictEqual(result, undefined); 67})); 68