11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Test unzipping a gzip file that contains multiple concatenated "members"
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst zlib = require('zlib');
71cb0ef41Sopenharmony_ciconst fs = require('fs');
81cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst abc = 'abc';
111cb0ef41Sopenharmony_ciconst def = 'def';
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst abcEncoded = zlib.gzipSync(abc);
141cb0ef41Sopenharmony_ciconst defEncoded = zlib.gzipSync(def);
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst data = Buffer.concat([
171cb0ef41Sopenharmony_ci  abcEncoded,
181cb0ef41Sopenharmony_ci  defEncoded,
191cb0ef41Sopenharmony_ci]);
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciassert.strictEqual(zlib.gunzipSync(data).toString(), (abc + def));
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_cizlib.gunzip(data, common.mustSucceed((result) => {
241cb0ef41Sopenharmony_ci  assert.strictEqual(result.toString(), (abc + def));
251cb0ef41Sopenharmony_ci}));
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_cizlib.unzip(data, common.mustSucceed((result) => {
281cb0ef41Sopenharmony_ci  assert.strictEqual(result.toString(), (abc + def));
291cb0ef41Sopenharmony_ci}));
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci// Multi-member support does not apply to zlib inflate/deflate.
321cb0ef41Sopenharmony_cizlib.unzip(Buffer.concat([
331cb0ef41Sopenharmony_ci  zlib.deflateSync('abc'),
341cb0ef41Sopenharmony_ci  zlib.deflateSync('def'),
351cb0ef41Sopenharmony_ci]), common.mustSucceed((result) => {
361cb0ef41Sopenharmony_ci  assert.strictEqual(result.toString(), abc);
371cb0ef41Sopenharmony_ci}));
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci// Files that have the "right" magic bytes for starting a new gzip member
401cb0ef41Sopenharmony_ci// in the middle of themselves, even if they are part of a single
411cb0ef41Sopenharmony_ci// regularly compressed member
421cb0ef41Sopenharmony_ciconst pmmFileZlib = fixtures.path('pseudo-multimember-gzip.z');
431cb0ef41Sopenharmony_ciconst pmmFileGz = fixtures.path('pseudo-multimember-gzip.gz');
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciconst pmmExpected = zlib.inflateSync(fs.readFileSync(pmmFileZlib));
461cb0ef41Sopenharmony_ciconst pmmResultBuffers = [];
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_cifs.createReadStream(pmmFileGz)
491cb0ef41Sopenharmony_ci  .pipe(zlib.createGunzip())
501cb0ef41Sopenharmony_ci  .on('error', (err) => {
511cb0ef41Sopenharmony_ci    assert.ifError(err);
521cb0ef41Sopenharmony_ci  })
531cb0ef41Sopenharmony_ci  .on('data', (data) => pmmResultBuffers.push(data))
541cb0ef41Sopenharmony_ci  .on('finish', common.mustCall(() => {
551cb0ef41Sopenharmony_ci    // Result should match original random garbage
561cb0ef41Sopenharmony_ci    assert.deepStrictEqual(Buffer.concat(pmmResultBuffers), pmmExpected);
571cb0ef41Sopenharmony_ci  }));
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci// Test that the next gzip member can wrap around the input buffer boundary
601cb0ef41Sopenharmony_ci[0, 1, 2, 3, 4, defEncoded.length].forEach((offset) => {
611cb0ef41Sopenharmony_ci  const resultBuffers = [];
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  const unzip = zlib.createGunzip()
641cb0ef41Sopenharmony_ci    .on('error', (err) => {
651cb0ef41Sopenharmony_ci      assert.ifError(err);
661cb0ef41Sopenharmony_ci    })
671cb0ef41Sopenharmony_ci    .on('data', (data) => resultBuffers.push(data))
681cb0ef41Sopenharmony_ci    .on('finish', common.mustCall(() => {
691cb0ef41Sopenharmony_ci      assert.strictEqual(
701cb0ef41Sopenharmony_ci        Buffer.concat(resultBuffers).toString(),
711cb0ef41Sopenharmony_ci        'abcdef',
721cb0ef41Sopenharmony_ci        `result should match original input (offset = ${offset})`
731cb0ef41Sopenharmony_ci      );
741cb0ef41Sopenharmony_ci    }));
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  // First write: write "abc" + the first bytes of "def"
771cb0ef41Sopenharmony_ci  unzip.write(Buffer.concat([
781cb0ef41Sopenharmony_ci    abcEncoded, defEncoded.slice(0, offset),
791cb0ef41Sopenharmony_ci  ]));
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  // Write remaining bytes of "def"
821cb0ef41Sopenharmony_ci  unzip.end(defEncoded.slice(offset));
831cb0ef41Sopenharmony_ci});
84