diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2018-02-09 00:26:59 +0800 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2018-02-25 02:31:14 +0800 |
commit | da886d9a4cd923bd5fc33eff7df22ff7d855a00b (patch) | |
tree | 86341e4d5709cce7394ff1f25b3380cfa63cfc8c /test/parallel/test-zlib-deflate-constructors.js | |
parent | 15e41a9951c425913c43073ba5f6af04c0867715 (diff) | |
download | android-node-v8-da886d9a4cd923bd5fc33eff7df22ff7d855a00b.tar.gz android-node-v8-da886d9a4cd923bd5fc33eff7df22ff7d855a00b.tar.bz2 android-node-v8-da886d9a4cd923bd5fc33eff7df22ff7d855a00b.zip |
zlib: improve zlib errors
- Use assert to check mode in the Zlib constructor since it should
only be passed by us.
- Introduce checkRangesOrGetDefault() and checkFiniteNumber()
to simplify type and range checking for numeric arguments
- Instead of `ERR_INVALID_OPT_VALUE`, throw `ERR_OUT_OF_RANGE` and
`ERR_INVALID_ARG_TYPE` with descriptions of the expected ranges
or types to make the errors more user-friendly.
- Add message tests for the changed errors
PR-URL: https://github.com/nodejs/node/pull/18675
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Diffstat (limited to 'test/parallel/test-zlib-deflate-constructors.js')
-rw-r--r-- | test/parallel/test-zlib-deflate-constructors.js | 247 |
1 files changed, 211 insertions, 36 deletions
diff --git a/test/parallel/test-zlib-deflate-constructors.js b/test/parallel/test-zlib-deflate-constructors.js index 97ece1e8af..5d5e9fb4a2 100644 --- a/test/parallel/test-zlib-deflate-constructors.js +++ b/test/parallel/test-zlib-deflate-constructors.js @@ -12,83 +12,201 @@ 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 +// Throws if `options.chunkSize` is invalid +common.expectsError( + () => new zlib.Deflate({ chunkSize: 'test' }), + { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "options.chunkSize" property must be of type number. ' + + 'Received type string' + } +); + common.expectsError( () => new zlib.Deflate({ chunkSize: -Infinity }), { - code: 'ERR_INVALID_OPT_VALUE', - type: RangeError + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "options.chunkSize" is out of range. It must ' + + 'be a finite number. Received -Infinity' + } +); + +common.expectsError( + () => new zlib.Deflate({ chunkSize: 0 }), + { + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "options.chunkSize" is out of range. It must ' + + 'be >= 64. Received 0' } ); // 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 +// Throws if `options.windowBits` is invalid +common.expectsError( + () => new zlib.Deflate({ windowBits: 'test' }), + { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "options.windowBits" property must be of type number. ' + + 'Received type string' + } +); + common.expectsError( () => new zlib.Deflate({ windowBits: -Infinity }), { - code: 'ERR_INVALID_OPT_VALUE', - type: RangeError + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "options.windowBits" is out of range. It must ' + + 'be a finite number. Received -Infinity' } ); common.expectsError( () => new zlib.Deflate({ windowBits: Infinity }), { - code: 'ERR_INVALID_OPT_VALUE', - type: RangeError + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "options.windowBits" is out of range. It must ' + + 'be a finite number. Received Infinity' + } +); + +common.expectsError( + () => new zlib.Deflate({ windowBits: 0 }), + { + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "options.windowBits" is out of range. It must ' + + 'be >= 8 and <= 15. Received 0' + } +); + +// Throws if `options.level` is invalid +common.expectsError( + () => new zlib.Deflate({ level: 'test' }), + { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "options.level" property must be of type number. ' + + 'Received type string' } ); -// Throws if `opts.level` is invalid common.expectsError( () => new zlib.Deflate({ level: -Infinity }), { - code: 'ERR_INVALID_OPT_VALUE', - type: RangeError + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "options.level" is out of range. It must ' + + 'be a finite number. Received -Infinity' } ); common.expectsError( () => new zlib.Deflate({ level: Infinity }), { - code: 'ERR_INVALID_OPT_VALUE', - type: RangeError + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "options.level" is out of range. It must ' + + 'be a finite number. Received Infinity' + } +); + +common.expectsError( + () => new zlib.Deflate({ level: -2 }), + { + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "options.level" is out of range. It must ' + + 'be >= -1 and <= 9. Received -2' + } +); + +// Throws if `level` invalid in `Deflate.prototype.params()` +common.expectsError( + () => new zlib.Deflate().params('test'), + { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "level" argument must be of type number. ' + + 'Received type string' } ); -// Throws a RangeError if `level` invalid in `Deflate.prototype.params()` common.expectsError( () => new zlib.Deflate().params(-Infinity), { - code: 'ERR_INVALID_ARG_VALUE', - type: RangeError + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "level" is out of range. It must ' + + 'be a finite number. Received -Infinity' } ); common.expectsError( () => new zlib.Deflate().params(Infinity), { - code: 'ERR_INVALID_ARG_VALUE', - type: RangeError + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "level" is out of range. It must ' + + 'be a finite number. Received Infinity' + } +); + +common.expectsError( + () => new zlib.Deflate().params(-2), + { + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "level" is out of range. It must ' + + 'be >= -1 and <= 9. Received -2' + } +); + +// Throws if options.memLevel is invalid +common.expectsError( + () => new zlib.Deflate({ memLevel: 'test' }), + { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "options.memLevel" property must be of type number. ' + + 'Received type string' } ); -// Throws if `opts.memLevel` is invalid common.expectsError( () => new zlib.Deflate({ memLevel: -Infinity }), { - code: 'ERR_INVALID_OPT_VALUE', - type: RangeError + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "options.memLevel" is out of range. It must ' + + 'be a finite number. Received -Infinity' } ); common.expectsError( () => new zlib.Deflate({ memLevel: Infinity }), { - code: 'ERR_INVALID_OPT_VALUE', - type: RangeError + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "options.memLevel" is out of range. It must ' + + 'be a finite number. Received Infinity' + } +); + +common.expectsError( + () => new zlib.Deflate({ memLevel: -2 }), + { + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "options.memLevel" is out of range. It must ' + + 'be >= 1 and <= 9. Received -2' } ); @@ -99,30 +217,85 @@ new zlib.Deflate({ strategy: zlib.constants.Z_RLE }); new zlib.Deflate({ strategy: zlib.constants.Z_FIXED }); new zlib.Deflate({ strategy: zlib.constants.Z_DEFAULT_STRATEGY }); -// Throws if opt.strategy is the wrong type. +// Throws if options.strategy is invalid common.expectsError( - () => new zlib.Deflate({ strategy: String(zlib.constants.Z_RLE) }), + () => new zlib.Deflate({ strategy: 'test' }), { - code: 'ERR_INVALID_OPT_VALUE', - type: TypeError + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "options.strategy" property must be of type number. ' + + 'Received type string' } ); -// Throws if opts.strategy is invalid common.expectsError( - () => new zlib.Deflate({ strategy: 'this is a bogus strategy' }), + () => new zlib.Deflate({ strategy: -Infinity }), { - code: 'ERR_INVALID_OPT_VALUE', - type: TypeError + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "options.strategy" is out of range. It must ' + + 'be a finite number. Received -Infinity' + } +); + +common.expectsError( + () => new zlib.Deflate({ strategy: Infinity }), + { + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "options.strategy" is out of range. It must ' + + 'be a finite number. Received Infinity' + } +); + +common.expectsError( + () => new zlib.Deflate({ strategy: -2 }), + { + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "options.strategy" is out of range. It must ' + + 'be >= 0 and <= 4. Received -2' } ); // Throws TypeError if `strategy` is invalid in `Deflate.prototype.params()` common.expectsError( - () => new zlib.Deflate().params(0, 'I am an invalid strategy'), + () => new zlib.Deflate().params(0, 'test'), + { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "strategy" argument must be of type number. ' + + 'Received type string' + } +); + +common.expectsError( + () => new zlib.Deflate().params(0, -Infinity), + { + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "strategy" is out of range. It must ' + + 'be a finite number. Received -Infinity' + } +); + +common.expectsError( + () => new zlib.Deflate().params(0, Infinity), + { + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "strategy" is out of range. It must ' + + 'be a finite number. Received Infinity' + } +); + +common.expectsError( + () => new zlib.Deflate().params(0, -2), { - code: 'ERR_INVALID_ARG_VALUE', - type: TypeError + code: 'ERR_OUT_OF_RANGE', + type: RangeError, + message: 'The value of "strategy" is out of range. It must ' + + 'be >= 0 and <= 4. Received -2' } ); @@ -130,7 +303,9 @@ common.expectsError( common.expectsError( () => new zlib.Deflate({ dictionary: 'not a buffer' }), { - code: 'ERR_INVALID_OPT_VALUE', - type: TypeError + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "options.dictionary" property must be one of type Buffer, ' + + 'TypedArray, DataView, or ArrayBuffer. Received type string' } ); |