11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_ci// Test convenience methods with and without options supplied
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciconst common = require('../common');
261cb0ef41Sopenharmony_ciconst assert = require('assert');
271cb0ef41Sopenharmony_ciconst zlib = require('zlib');
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci// Must be a multiple of 4 characters in total to test all ArrayBufferView
301cb0ef41Sopenharmony_ci// types.
311cb0ef41Sopenharmony_ciconst expectStr = 'blah'.repeat(8);
321cb0ef41Sopenharmony_ciconst expectBuf = Buffer.from(expectStr);
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciconst opts = {
351cb0ef41Sopenharmony_ci  level: 9,
361cb0ef41Sopenharmony_ci  chunkSize: 1024,
371cb0ef41Sopenharmony_ci};
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciconst optsInfo = {
401cb0ef41Sopenharmony_ci  info: true
411cb0ef41Sopenharmony_ci};
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_cifor (const [type, expect] of [
441cb0ef41Sopenharmony_ci  ['string', expectStr],
451cb0ef41Sopenharmony_ci  ['Buffer', expectBuf],
461cb0ef41Sopenharmony_ci  ...common.getBufferSources(expectBuf).map((obj) =>
471cb0ef41Sopenharmony_ci    [obj[Symbol.toStringTag], obj]
481cb0ef41Sopenharmony_ci  ),
491cb0ef41Sopenharmony_ci]) {
501cb0ef41Sopenharmony_ci  for (const method of [
511cb0ef41Sopenharmony_ci    ['gzip', 'gunzip', 'Gzip', 'Gunzip'],
521cb0ef41Sopenharmony_ci    ['gzip', 'unzip', 'Gzip', 'Unzip'],
531cb0ef41Sopenharmony_ci    ['deflate', 'inflate', 'Deflate', 'Inflate'],
541cb0ef41Sopenharmony_ci    ['deflateRaw', 'inflateRaw', 'DeflateRaw', 'InflateRaw'],
551cb0ef41Sopenharmony_ci    ['brotliCompress', 'brotliDecompress',
561cb0ef41Sopenharmony_ci     'BrotliCompress', 'BrotliDecompress'],
571cb0ef41Sopenharmony_ci  ]) {
581cb0ef41Sopenharmony_ci    zlib[method[0]](expect, opts, common.mustCall((err, result) => {
591cb0ef41Sopenharmony_ci      zlib[method[1]](result, opts, common.mustCall((err, result) => {
601cb0ef41Sopenharmony_ci        assert.strictEqual(result.toString(), expectStr,
611cb0ef41Sopenharmony_ci                           `Should get original string after ${method[0]}/` +
621cb0ef41Sopenharmony_ci                           `${method[1]} ${type} with options.`);
631cb0ef41Sopenharmony_ci      }));
641cb0ef41Sopenharmony_ci    }));
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci    zlib[method[0]](expect, common.mustCall((err, result) => {
671cb0ef41Sopenharmony_ci      zlib[method[1]](result, common.mustCall((err, result) => {
681cb0ef41Sopenharmony_ci        assert.strictEqual(result.toString(), expectStr,
691cb0ef41Sopenharmony_ci                           `Should get original string after ${method[0]}/` +
701cb0ef41Sopenharmony_ci                           `${method[1]} ${type} without options.`);
711cb0ef41Sopenharmony_ci      }));
721cb0ef41Sopenharmony_ci    }));
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci    zlib[method[0]](expect, optsInfo, common.mustCall((err, result) => {
751cb0ef41Sopenharmony_ci      assert.ok(result.engine instanceof zlib[method[2]],
761cb0ef41Sopenharmony_ci                `Should get engine ${method[2]} after ${method[0]} ` +
771cb0ef41Sopenharmony_ci                `${type} with info option.`);
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci      const compressed = result.buffer;
801cb0ef41Sopenharmony_ci      zlib[method[1]](compressed, optsInfo, common.mustCall((err, result) => {
811cb0ef41Sopenharmony_ci        assert.strictEqual(result.buffer.toString(), expectStr,
821cb0ef41Sopenharmony_ci                           `Should get original string after ${method[0]}/` +
831cb0ef41Sopenharmony_ci                           `${method[1]} ${type} with info option.`);
841cb0ef41Sopenharmony_ci        assert.ok(result.engine instanceof zlib[method[3]],
851cb0ef41Sopenharmony_ci                  `Should get engine ${method[3]} after ${method[0]} ` +
861cb0ef41Sopenharmony_ci                  `${type} with info option.`);
871cb0ef41Sopenharmony_ci      }));
881cb0ef41Sopenharmony_ci    }));
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci    {
911cb0ef41Sopenharmony_ci      const compressed = zlib[`${method[0]}Sync`](expect, opts);
921cb0ef41Sopenharmony_ci      const decompressed = zlib[`${method[1]}Sync`](compressed, opts);
931cb0ef41Sopenharmony_ci      assert.strictEqual(decompressed.toString(), expectStr,
941cb0ef41Sopenharmony_ci                         `Should get original string after ${method[0]}Sync/` +
951cb0ef41Sopenharmony_ci                         `${method[1]}Sync ${type} with options.`);
961cb0ef41Sopenharmony_ci    }
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci    {
1001cb0ef41Sopenharmony_ci      const compressed = zlib[`${method[0]}Sync`](expect);
1011cb0ef41Sopenharmony_ci      const decompressed = zlib[`${method[1]}Sync`](compressed);
1021cb0ef41Sopenharmony_ci      assert.strictEqual(decompressed.toString(), expectStr,
1031cb0ef41Sopenharmony_ci                         `Should get original string after ${method[0]}Sync/` +
1041cb0ef41Sopenharmony_ci                         `${method[1]}Sync ${type} without options.`);
1051cb0ef41Sopenharmony_ci    }
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci    {
1091cb0ef41Sopenharmony_ci      const compressed = zlib[`${method[0]}Sync`](expect, optsInfo);
1101cb0ef41Sopenharmony_ci      assert.ok(compressed.engine instanceof zlib[method[2]],
1111cb0ef41Sopenharmony_ci                `Should get engine ${method[2]} after ${method[0]} ` +
1121cb0ef41Sopenharmony_ci                `${type} with info option.`);
1131cb0ef41Sopenharmony_ci      const decompressed = zlib[`${method[1]}Sync`](compressed.buffer,
1141cb0ef41Sopenharmony_ci                                                    optsInfo);
1151cb0ef41Sopenharmony_ci      assert.strictEqual(decompressed.buffer.toString(), expectStr,
1161cb0ef41Sopenharmony_ci                         `Should get original string after ${method[0]}Sync/` +
1171cb0ef41Sopenharmony_ci                         `${method[1]}Sync ${type} without options.`);
1181cb0ef41Sopenharmony_ci      assert.ok(decompressed.engine instanceof zlib[method[3]],
1191cb0ef41Sopenharmony_ci                `Should get engine ${method[3]} after ${method[0]} ` +
1201cb0ef41Sopenharmony_ci                `${type} with info option.`);
1211cb0ef41Sopenharmony_ci    }
1221cb0ef41Sopenharmony_ci  }
1231cb0ef41Sopenharmony_ci}
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ciassert.throws(
1261cb0ef41Sopenharmony_ci  () => zlib.gzip('abc'),
1271cb0ef41Sopenharmony_ci  {
1281cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
1291cb0ef41Sopenharmony_ci    name: 'TypeError',
1301cb0ef41Sopenharmony_ci    message: 'The "callback" argument must be of type function. ' +
1311cb0ef41Sopenharmony_ci             'Received undefined'
1321cb0ef41Sopenharmony_ci  }
1331cb0ef41Sopenharmony_ci);
134