11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst zlib = require('zlib');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// Test some brotli-specific properties of the brotli streams that can not
81cb0ef41Sopenharmony_ci// be easily covered through expanding zlib-only tests.
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst sampleBuffer = fixtures.readSync('/pss-vectors.json');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci{
131cb0ef41Sopenharmony_ci  // Test setting the quality parameter at stream creation:
141cb0ef41Sopenharmony_ci  const sizes = [];
151cb0ef41Sopenharmony_ci  for (let quality = zlib.constants.BROTLI_MIN_QUALITY;
161cb0ef41Sopenharmony_ci    quality <= zlib.constants.BROTLI_MAX_QUALITY;
171cb0ef41Sopenharmony_ci    quality++) {
181cb0ef41Sopenharmony_ci    const encoded = zlib.brotliCompressSync(sampleBuffer, {
191cb0ef41Sopenharmony_ci      params: {
201cb0ef41Sopenharmony_ci        [zlib.constants.BROTLI_PARAM_QUALITY]: quality
211cb0ef41Sopenharmony_ci      }
221cb0ef41Sopenharmony_ci    });
231cb0ef41Sopenharmony_ci    sizes.push(encoded.length);
241cb0ef41Sopenharmony_ci  }
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  // Increasing quality should roughly correspond to decreasing compressed size:
271cb0ef41Sopenharmony_ci  for (let i = 0; i < sizes.length - 1; i++) {
281cb0ef41Sopenharmony_ci    assert(sizes[i + 1] <= sizes[i] * 1.05, sizes);  // 5 % margin of error.
291cb0ef41Sopenharmony_ci  }
301cb0ef41Sopenharmony_ci  assert(sizes[0] > sizes[sizes.length - 1], sizes);
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci{
341cb0ef41Sopenharmony_ci  // Test that setting out-of-bounds option values or keys fails.
351cb0ef41Sopenharmony_ci  assert.throws(() => {
361cb0ef41Sopenharmony_ci    zlib.createBrotliCompress({
371cb0ef41Sopenharmony_ci      params: {
381cb0ef41Sopenharmony_ci        10000: 0
391cb0ef41Sopenharmony_ci      }
401cb0ef41Sopenharmony_ci    });
411cb0ef41Sopenharmony_ci  }, {
421cb0ef41Sopenharmony_ci    code: 'ERR_BROTLI_INVALID_PARAM',
431cb0ef41Sopenharmony_ci    name: 'RangeError',
441cb0ef41Sopenharmony_ci    message: '10000 is not a valid Brotli parameter'
451cb0ef41Sopenharmony_ci  });
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  // Test that accidentally using duplicate keys fails.
481cb0ef41Sopenharmony_ci  assert.throws(() => {
491cb0ef41Sopenharmony_ci    zlib.createBrotliCompress({
501cb0ef41Sopenharmony_ci      params: {
511cb0ef41Sopenharmony_ci        '0': 0,
521cb0ef41Sopenharmony_ci        '00': 0
531cb0ef41Sopenharmony_ci      }
541cb0ef41Sopenharmony_ci    });
551cb0ef41Sopenharmony_ci  }, {
561cb0ef41Sopenharmony_ci    code: 'ERR_BROTLI_INVALID_PARAM',
571cb0ef41Sopenharmony_ci    name: 'RangeError',
581cb0ef41Sopenharmony_ci    message: '00 is not a valid Brotli parameter'
591cb0ef41Sopenharmony_ci  });
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  assert.throws(() => {
621cb0ef41Sopenharmony_ci    zlib.createBrotliCompress({
631cb0ef41Sopenharmony_ci      params: {
641cb0ef41Sopenharmony_ci        // This is a boolean flag
651cb0ef41Sopenharmony_ci        [zlib.constants.BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING]: 42
661cb0ef41Sopenharmony_ci      }
671cb0ef41Sopenharmony_ci    });
681cb0ef41Sopenharmony_ci  }, {
691cb0ef41Sopenharmony_ci    code: 'ERR_ZLIB_INITIALIZATION_FAILED',
701cb0ef41Sopenharmony_ci    name: 'Error',
711cb0ef41Sopenharmony_ci    message: 'Initialization failed'
721cb0ef41Sopenharmony_ci  });
731cb0ef41Sopenharmony_ci}
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci{
761cb0ef41Sopenharmony_ci  // Test options.flush range
771cb0ef41Sopenharmony_ci  assert.throws(() => {
781cb0ef41Sopenharmony_ci    zlib.brotliCompressSync('', { flush: zlib.constants.Z_FINISH });
791cb0ef41Sopenharmony_ci  }, {
801cb0ef41Sopenharmony_ci    code: 'ERR_OUT_OF_RANGE',
811cb0ef41Sopenharmony_ci    name: 'RangeError',
821cb0ef41Sopenharmony_ci    message: 'The value of "options.flush" is out of range. It must be >= 0 ' +
831cb0ef41Sopenharmony_ci      'and <= 3. Received 4',
841cb0ef41Sopenharmony_ci  });
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  assert.throws(() => {
871cb0ef41Sopenharmony_ci    zlib.brotliCompressSync('', { finishFlush: zlib.constants.Z_FINISH });
881cb0ef41Sopenharmony_ci  }, {
891cb0ef41Sopenharmony_ci    code: 'ERR_OUT_OF_RANGE',
901cb0ef41Sopenharmony_ci    name: 'RangeError',
911cb0ef41Sopenharmony_ci    message: 'The value of "options.finishFlush" is out of range. It must be ' +
921cb0ef41Sopenharmony_ci      '>= 0 and <= 3. Received 4',
931cb0ef41Sopenharmony_ci  });
941cb0ef41Sopenharmony_ci}
95