test: increase test coverage for lib/zlib.js
Add tests for constructor behavior and parameter validation. Remove condition check that cannot be triggered (nothing is greater than `Infinity`). PR-URL: https://github.com/nodejs/node/pull/9366 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Minwoo Jung <jmwsoft@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
54d34314e3
commit
3eea668c7a
@ -300,8 +300,7 @@ function Zlib(opts, mode) {
|
||||
opts.finishFlush : constants.Z_FINISH;
|
||||
|
||||
if (opts.chunkSize) {
|
||||
if (opts.chunkSize < constants.Z_MIN_CHUNK ||
|
||||
opts.chunkSize > constants.Z_MAX_CHUNK) {
|
||||
if (opts.chunkSize < constants.Z_MIN_CHUNK) {
|
||||
throw new Error('Invalid chunk size: ' + opts.chunkSize);
|
||||
}
|
||||
}
|
||||
|
105
test/parallel/test-zlib-deflate-constructors.js
Normal file
105
test/parallel/test-zlib-deflate-constructors.js
Normal file
@ -0,0 +1,105 @@
|
||||
'use strict';
|
||||
|
||||
require('../common');
|
||||
|
||||
const zlib = require('zlib');
|
||||
const assert = require('assert');
|
||||
|
||||
// Work with and without `new` keyword
|
||||
assert.ok(zlib.Deflate() instanceof zlib.Deflate);
|
||||
assert.ok(new zlib.Deflate() instanceof zlib.Deflate);
|
||||
|
||||
assert.ok(zlib.DeflateRaw() instanceof zlib.DeflateRaw);
|
||||
assert.ok(new zlib.DeflateRaw() instanceof zlib.DeflateRaw);
|
||||
|
||||
// Throws if `opts.chunkSize` is invalid
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({chunkSize: -Infinity}); },
|
||||
/^Error: Invalid chunk size: -Infinity$/
|
||||
);
|
||||
|
||||
// Confirm that maximum chunk size cannot be exceeded because it is `Infinity`.
|
||||
assert.strictEqual(zlib.constants.Z_MAX_CHUNK, Infinity);
|
||||
|
||||
// Throws if `opts.windowBits` is invalid
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({windowBits: -Infinity}); },
|
||||
/^Error: Invalid windowBits: -Infinity$/
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({windowBits: Infinity}); },
|
||||
/^Error: Invalid windowBits: Infinity$/
|
||||
);
|
||||
|
||||
// Throws if `opts.level` is invalid
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({level: -Infinity}); },
|
||||
/^Error: Invalid compression level: -Infinity$/
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({level: Infinity}); },
|
||||
/^Error: Invalid compression level: Infinity$/
|
||||
);
|
||||
|
||||
// Throws a RangeError if `level` invalid in `Deflate.prototype.params()`
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate().params(-Infinity); },
|
||||
/^RangeError: Invalid compression level: -Infinity$/
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate().params(Infinity); },
|
||||
/^RangeError: Invalid compression level: Infinity$/
|
||||
);
|
||||
|
||||
// Throws if `opts.memLevel` is invalid
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({memLevel: -Infinity}); },
|
||||
/^Error: Invalid memLevel: -Infinity$/
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({memLevel: Infinity}); },
|
||||
/^Error: Invalid memLevel: Infinity$/
|
||||
);
|
||||
|
||||
// Does not throw if opts.strategy is valid
|
||||
assert.doesNotThrow(
|
||||
() => { new zlib.Deflate({strategy: zlib.constants.Z_FILTERED}); }
|
||||
);
|
||||
|
||||
assert.doesNotThrow(
|
||||
() => { new zlib.Deflate({strategy: zlib.constants.Z_HUFFMAN_ONLY}); }
|
||||
);
|
||||
|
||||
assert.doesNotThrow(
|
||||
() => { new zlib.Deflate({strategy: zlib.constants.Z_RLE}); }
|
||||
);
|
||||
|
||||
assert.doesNotThrow(
|
||||
() => { new zlib.Deflate({strategy: zlib.constants.Z_FIXED}); }
|
||||
);
|
||||
|
||||
assert.doesNotThrow(
|
||||
() => { new zlib.Deflate({ strategy: zlib.constants.Z_DEFAULT_STRATEGY}); }
|
||||
);
|
||||
|
||||
// Throws if opts.strategy is invalid
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({strategy: 'this is a bogus strategy'}); },
|
||||
/^Error: Invalid strategy: this is a bogus strategy$/
|
||||
);
|
||||
|
||||
// Throws TypeError if `strategy` is invalid in `Deflate.prototype.params()`
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate().params(0, 'I am an invalid strategy'); },
|
||||
/^TypeError: Invalid strategy: I am an invalid strategy$/
|
||||
);
|
||||
|
||||
// Throws if opts.dictionary is not a Buffer
|
||||
assert.throws(
|
||||
() => { new zlib.Deflate({dictionary: 'not a buffer'}); },
|
||||
/^Error: Invalid dictionary: it should be a Buffer instance$/
|
||||
);
|
Loading…
x
Reference in New Issue
Block a user