11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Test unzipping a gzip file that has trailing garbage
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst zlib = require('zlib');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// Should ignore trailing null-bytes
91cb0ef41Sopenharmony_cilet data = Buffer.concat([
101cb0ef41Sopenharmony_ci  zlib.gzipSync('abc'),
111cb0ef41Sopenharmony_ci  zlib.gzipSync('def'),
121cb0ef41Sopenharmony_ci  Buffer.alloc(10),
131cb0ef41Sopenharmony_ci]);
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciassert.strictEqual(zlib.gunzipSync(data).toString(), 'abcdef');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cizlib.gunzip(data, common.mustSucceed((result) => {
181cb0ef41Sopenharmony_ci  assert.strictEqual(
191cb0ef41Sopenharmony_ci    result.toString(),
201cb0ef41Sopenharmony_ci    'abcdef',
211cb0ef41Sopenharmony_ci    `result '${result.toString()}' should match original string`
221cb0ef41Sopenharmony_ci  );
231cb0ef41Sopenharmony_ci}));
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci// If the trailing garbage happens to look like a gzip header, it should
261cb0ef41Sopenharmony_ci// throw an error.
271cb0ef41Sopenharmony_cidata = Buffer.concat([
281cb0ef41Sopenharmony_ci  zlib.gzipSync('abc'),
291cb0ef41Sopenharmony_ci  zlib.gzipSync('def'),
301cb0ef41Sopenharmony_ci  Buffer.from([0x1f, 0x8b, 0xff, 0xff]),
311cb0ef41Sopenharmony_ci  Buffer.alloc(10),
321cb0ef41Sopenharmony_ci]);
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciassert.throws(
351cb0ef41Sopenharmony_ci  () => zlib.gunzipSync(data),
361cb0ef41Sopenharmony_ci  /^Error: unknown compression method$/
371cb0ef41Sopenharmony_ci);
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_cizlib.gunzip(data, common.mustCall((err, result) => {
401cb0ef41Sopenharmony_ci  common.expectsError({
411cb0ef41Sopenharmony_ci    code: 'Z_DATA_ERROR',
421cb0ef41Sopenharmony_ci    name: 'Error',
431cb0ef41Sopenharmony_ci    message: 'unknown compression method'
441cb0ef41Sopenharmony_ci  })(err);
451cb0ef41Sopenharmony_ci  assert.strictEqual(result, undefined);
461cb0ef41Sopenharmony_ci}));
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci// In this case the trailing junk is too short to be a gzip segment
491cb0ef41Sopenharmony_ci// So we ignore it and decompression succeeds.
501cb0ef41Sopenharmony_cidata = Buffer.concat([
511cb0ef41Sopenharmony_ci  zlib.gzipSync('abc'),
521cb0ef41Sopenharmony_ci  zlib.gzipSync('def'),
531cb0ef41Sopenharmony_ci  Buffer.from([0x1f, 0x8b, 0xff, 0xff]),
541cb0ef41Sopenharmony_ci]);
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ciassert.throws(
571cb0ef41Sopenharmony_ci  () => zlib.gunzipSync(data),
581cb0ef41Sopenharmony_ci  /^Error: unknown compression method$/
591cb0ef41Sopenharmony_ci);
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_cizlib.gunzip(data, common.mustCall((err, result) => {
621cb0ef41Sopenharmony_ci  assert(err instanceof Error);
631cb0ef41Sopenharmony_ci  assert.strictEqual(err.code, 'Z_DATA_ERROR');
641cb0ef41Sopenharmony_ci  assert.strictEqual(err.message, 'unknown compression method');
651cb0ef41Sopenharmony_ci  assert.strictEqual(result, undefined);
661cb0ef41Sopenharmony_ci}));
67