Skip to content

Commit e696bc3

Browse files
Trottevanlucas
authored andcommitted
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: #9366 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent b51db71 commit e696bc3

File tree

2 files changed

+106
-2
lines changed

2 files changed

+106
-2
lines changed

lib/zlib.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,7 @@ function Zlib(opts, mode) {
300300
opts.finishFlush : constants.Z_FINISH;
301301

302302
if (opts.chunkSize) {
303-
if (opts.chunkSize < constants.Z_MIN_CHUNK ||
304-
opts.chunkSize > constants.Z_MAX_CHUNK) {
303+
if (opts.chunkSize < constants.Z_MIN_CHUNK) {
305304
throw new Error('Invalid chunk size: ' + opts.chunkSize);
306305
}
307306
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const zlib = require('zlib');
6+
const assert = require('assert');
7+
8+
// Work with and without `new` keyword
9+
assert.ok(zlib.Deflate() instanceof zlib.Deflate);
10+
assert.ok(new zlib.Deflate() instanceof zlib.Deflate);
11+
12+
assert.ok(zlib.DeflateRaw() instanceof zlib.DeflateRaw);
13+
assert.ok(new zlib.DeflateRaw() instanceof zlib.DeflateRaw);
14+
15+
// Throws if `opts.chunkSize` is invalid
16+
assert.throws(
17+
() => { new zlib.Deflate({chunkSize: -Infinity}); },
18+
/^Error: Invalid chunk size: -Infinity$/
19+
);
20+
21+
// Confirm that maximum chunk size cannot be exceeded because it is `Infinity`.
22+
assert.strictEqual(zlib.constants.Z_MAX_CHUNK, Infinity);
23+
24+
// Throws if `opts.windowBits` is invalid
25+
assert.throws(
26+
() => { new zlib.Deflate({windowBits: -Infinity}); },
27+
/^Error: Invalid windowBits: -Infinity$/
28+
);
29+
30+
assert.throws(
31+
() => { new zlib.Deflate({windowBits: Infinity}); },
32+
/^Error: Invalid windowBits: Infinity$/
33+
);
34+
35+
// Throws if `opts.level` is invalid
36+
assert.throws(
37+
() => { new zlib.Deflate({level: -Infinity}); },
38+
/^Error: Invalid compression level: -Infinity$/
39+
);
40+
41+
assert.throws(
42+
() => { new zlib.Deflate({level: Infinity}); },
43+
/^Error: Invalid compression level: Infinity$/
44+
);
45+
46+
// Throws a RangeError if `level` invalid in `Deflate.prototype.params()`
47+
assert.throws(
48+
() => { new zlib.Deflate().params(-Infinity); },
49+
/^RangeError: Invalid compression level: -Infinity$/
50+
);
51+
52+
assert.throws(
53+
() => { new zlib.Deflate().params(Infinity); },
54+
/^RangeError: Invalid compression level: Infinity$/
55+
);
56+
57+
// Throws if `opts.memLevel` is invalid
58+
assert.throws(
59+
() => { new zlib.Deflate({memLevel: -Infinity}); },
60+
/^Error: Invalid memLevel: -Infinity$/
61+
);
62+
63+
assert.throws(
64+
() => { new zlib.Deflate({memLevel: Infinity}); },
65+
/^Error: Invalid memLevel: Infinity$/
66+
);
67+
68+
// Does not throw if opts.strategy is valid
69+
assert.doesNotThrow(
70+
() => { new zlib.Deflate({strategy: zlib.constants.Z_FILTERED}); }
71+
);
72+
73+
assert.doesNotThrow(
74+
() => { new zlib.Deflate({strategy: zlib.constants.Z_HUFFMAN_ONLY}); }
75+
);
76+
77+
assert.doesNotThrow(
78+
() => { new zlib.Deflate({strategy: zlib.constants.Z_RLE}); }
79+
);
80+
81+
assert.doesNotThrow(
82+
() => { new zlib.Deflate({strategy: zlib.constants.Z_FIXED}); }
83+
);
84+
85+
assert.doesNotThrow(
86+
() => { new zlib.Deflate({ strategy: zlib.constants.Z_DEFAULT_STRATEGY}); }
87+
);
88+
89+
// Throws if opts.strategy is invalid
90+
assert.throws(
91+
() => { new zlib.Deflate({strategy: 'this is a bogus strategy'}); },
92+
/^Error: Invalid strategy: this is a bogus strategy$/
93+
);
94+
95+
// Throws TypeError if `strategy` is invalid in `Deflate.prototype.params()`
96+
assert.throws(
97+
() => { new zlib.Deflate().params(0, 'I am an invalid strategy'); },
98+
/^TypeError: Invalid strategy: I am an invalid strategy$/
99+
);
100+
101+
// Throws if opts.dictionary is not a Buffer
102+
assert.throws(
103+
() => { new zlib.Deflate({dictionary: 'not a buffer'}); },
104+
/^Error: Invalid dictionary: it should be a Buffer instance$/
105+
);

0 commit comments

Comments
 (0)