|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const fixtures = require('../common/fixtures'); |
| 4 | +const assert = require('assert'); |
| 5 | +const zlib = require('zlib'); |
| 6 | + |
| 7 | +// Test some brotli-specific properties of the brotli streams that can not |
| 8 | +// be easily covered through expanding zlib-only tests. |
| 9 | + |
| 10 | +const sampleBuffer = fixtures.readSync('/pss-vectors.json'); |
| 11 | + |
| 12 | +{ |
| 13 | + // Test setting the quality parameter at stream creation: |
| 14 | + const sizes = []; |
| 15 | + for (let quality = zlib.constants.BROTLI_MIN_QUALITY; |
| 16 | + quality <= zlib.constants.BROTLI_MAX_QUALITY; |
| 17 | + quality++) { |
| 18 | + const encoded = zlib.brotliCompressSync(sampleBuffer, { |
| 19 | + params: { |
| 20 | + [zlib.constants.BROTLI_PARAM_QUALITY]: quality |
| 21 | + } |
| 22 | + }); |
| 23 | + sizes.push(encoded.length); |
| 24 | + } |
| 25 | + |
| 26 | + // Increasing quality should roughly correspond to decreasing compressed size: |
| 27 | + for (let i = 0; i < sizes.length - 1; i++) { |
| 28 | + assert(sizes[i + 1] <= sizes[i] * 1.05, sizes); // 5 % margin of error. |
| 29 | + } |
| 30 | + assert(sizes[0] > sizes[sizes.length - 1], sizes); |
| 31 | +} |
| 32 | + |
| 33 | +{ |
| 34 | + // Test that setting out-of-bounds option values or keys fails. |
| 35 | + common.expectsError(() => { |
| 36 | + zlib.createBrotliCompress({ |
| 37 | + params: { |
| 38 | + 10000: 0 |
| 39 | + } |
| 40 | + }); |
| 41 | + }, { |
| 42 | + code: 'ERR_BROTLI_INVALID_PARAM', |
| 43 | + type: RangeError, |
| 44 | + message: '10000 is not a valid Brotli parameter' |
| 45 | + }); |
| 46 | + |
| 47 | + // Test that accidentally using duplicate keys fails. |
| 48 | + common.expectsError(() => { |
| 49 | + zlib.createBrotliCompress({ |
| 50 | + params: { |
| 51 | + '0': 0, |
| 52 | + '00': 0 |
| 53 | + } |
| 54 | + }); |
| 55 | + }, { |
| 56 | + code: 'ERR_BROTLI_INVALID_PARAM', |
| 57 | + type: RangeError, |
| 58 | + message: '00 is not a valid Brotli parameter' |
| 59 | + }); |
| 60 | + |
| 61 | + common.expectsError(() => { |
| 62 | + zlib.createBrotliCompress({ |
| 63 | + params: { |
| 64 | + // This is a boolean flag |
| 65 | + [zlib.constants.BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING]: 42 |
| 66 | + } |
| 67 | + }); |
| 68 | + }, { |
| 69 | + code: 'ERR_ZLIB_INITIALIZATION_FAILED', |
| 70 | + type: Error, |
| 71 | + message: 'Initialization failed' |
| 72 | + }); |
| 73 | +} |
0 commit comments