Skip to content

Commit 8b49cae

Browse files
committed
test: improve zlib tests
1 parent 2a96549 commit 8b49cae

8 files changed

+277
-212
lines changed

test/parallel/test-zlib-failed-init.js

Lines changed: 0 additions & 46 deletions
This file was deleted.

test/parallel/test-zlib-from-string.js

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
// Test compressing and uncompressing a string with zlib
2423

25-
const common = require('../common');
26-
const assert = require('assert');
27-
const zlib = require('zlib');
24+
require('../common');
25+
26+
const assert = require('node:assert');
27+
const zlib = require('node:zlib');
28+
const { test } = require('node:test');
2829

2930
const inputString = 'ΩΩLorem ipsum dolor sit amet, consectetur adipiscing eli' +
3031
't. Morbi faucibus, purus at gravida dictum, libero arcu ' +
@@ -54,30 +55,56 @@ const expectedBase64Gzip = 'H4sIAAAAAAAAA11RS05DMQy8yhzg6d2BPSAkJPZu4laWkjiN4' +
5455
'mHo33kJO8xfkckmLjE5XMKBQ4gxIsfvCZ44doUThF2mcZq8q2' +
5556
'sHnHNzRtagj5AQAA';
5657

57-
zlib.deflate(inputString, common.mustCall((err, buffer) => {
58-
zlib.inflate(buffer, common.mustCall((err, inflated) => {
59-
assert.strictEqual(inflated.toString(), inputString);
60-
}));
61-
}));
58+
test('properly deflate and inflate', async (t) => {
59+
const { promise, resolve } = Promise.withResolvers();
60+
zlib.deflate(inputString, (err, buffer) => {
61+
assert.ifError(err);
62+
zlib.inflate(buffer, (err, inflated) => {
63+
assert.ifError(err);
64+
assert.strictEqual(inflated.toString(), inputString);
65+
resolve();
66+
});
67+
});
68+
await promise;
69+
});
6270

63-
zlib.gzip(inputString, common.mustCall((err, buffer) => {
64-
// Can't actually guarantee that we'll get exactly the same
65-
// deflated bytes when we compress a string, since the header
66-
// depends on stuff other than the input string itself.
67-
// However, decrypting it should definitely yield the same
68-
// result that we're expecting, and this should match what we get
69-
// from inflating the known valid deflate data.
70-
zlib.gunzip(buffer, common.mustCall((err, gunzipped) => {
71-
assert.strictEqual(gunzipped.toString(), inputString);
72-
}));
73-
}));
71+
test('properly gzip and gunzip', async (t) => {
72+
const { promise, resolve } = Promise.withResolvers();
73+
zlib.gzip(inputString, (err, buffer) => {
74+
assert.ifError(err);
75+
// Can't actually guarantee that we'll get exactly the same
76+
// deflated bytes when we compress a string, since the header
77+
// depends on stuff other than the input string itself.
78+
// However, decrypting it should definitely yield the same
79+
// result that we're expecting, and this should match what we get
80+
// from inflating the known valid deflate data.
81+
zlib.gunzip(buffer, (err, gunzipped) => {
82+
assert.ifError(err);
83+
assert.strictEqual(gunzipped.toString(), inputString);
84+
resolve();
85+
});
86+
});
87+
await promise;
88+
});
7489

75-
let buffer = Buffer.from(expectedBase64Deflate, 'base64');
76-
zlib.unzip(buffer, common.mustCall((err, buffer) => {
77-
assert.strictEqual(buffer.toString(), inputString);
78-
}));
90+
test('properly unzip base64 deflate', async (t) => {
91+
const { promise, resolve } = Promise.withResolvers();
92+
const buffer = Buffer.from(expectedBase64Deflate, 'base64');
93+
zlib.unzip(buffer, (err, buffer) => {
94+
assert.ifError(err);
95+
assert.strictEqual(buffer.toString(), inputString);
96+
resolve();
97+
});
98+
await promise;
99+
});
79100

80-
buffer = Buffer.from(expectedBase64Gzip, 'base64');
81-
zlib.unzip(buffer, common.mustCall((err, buffer) => {
82-
assert.strictEqual(buffer.toString(), inputString);
83-
}));
101+
test('properly unzip base64 gzip', async (t) => {
102+
const { promise, resolve } = Promise.withResolvers();
103+
const buffer = Buffer.from(expectedBase64Gzip, 'base64');
104+
zlib.unzip(buffer, (err, buffer) => {
105+
assert.ifError(err);
106+
assert.strictEqual(buffer.toString(), inputString);
107+
resolve();
108+
});
109+
await promise;
110+
});

test/parallel/test-zlib-invalid-arg-value-brotli-compress.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

test/parallel/test-zlib-invalid-input.js

Lines changed: 93 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,100 @@
2222
'use strict';
2323
// Test uncompressing invalid input
2424

25-
const common = require('../common');
26-
const assert = require('assert');
27-
const zlib = require('zlib');
28-
29-
const nonStringInputs = [
30-
1,
31-
true,
32-
{ a: 1 },
33-
['a'],
34-
];
35-
36-
// zlib.Unzip classes need to get valid data, or else they'll throw.
37-
const unzips = [
38-
zlib.Unzip(),
39-
zlib.Gunzip(),
40-
zlib.Inflate(),
41-
zlib.InflateRaw(),
42-
zlib.BrotliDecompress(),
43-
];
44-
45-
nonStringInputs.forEach(common.mustCall((input) => {
46-
assert.throws(() => {
47-
zlib.gunzip(input);
48-
}, {
49-
name: 'TypeError',
25+
require('../common');
26+
27+
const assert = require('node:assert');
28+
const zlib = require('node:zlib');
29+
const { test } = require('node:test');
30+
31+
test('uncompressing invalid input', async (t) => {
32+
const nonStringInputs = [
33+
1,
34+
true,
35+
{ a: 1 },
36+
['a'],
37+
];
38+
39+
// zlib.Unzip classes need to get valid data, or else they'll throw.
40+
const unzips = [
41+
new zlib.Unzip(),
42+
new zlib.Gunzip(),
43+
new zlib.Inflate(),
44+
new zlib.InflateRaw(),
45+
new zlib.BrotliDecompress(),
46+
];
47+
48+
for (const input of nonStringInputs) {
49+
assert.throws(() => {
50+
zlib.gunzip(input);
51+
}, {
52+
name: 'TypeError',
53+
code: 'ERR_INVALID_ARG_TYPE'
54+
});
55+
}
56+
57+
for (const uz of unzips) {
58+
const { promise, resolve, reject } = Promise.withResolvers();
59+
uz.on('error', resolve);
60+
uz.on('end', reject);
61+
62+
// This will trigger error event
63+
uz.write('this is not valid compressed data.');
64+
await promise;
65+
}
66+
});
67+
68+
// This test ensures that the BrotliCompress function throws
69+
// ERR_INVALID_ARG_TYPE when the values of the `params` key-value object are
70+
// neither numbers nor booleans.
71+
test('ensure BrotliCompress throws error on invalid params', async (t) => {
72+
assert.throws(() => new zlib.BrotliCompress({
73+
params: {
74+
[zlib.constants.BROTLI_PARAM_MODE]: 'lol'
75+
}
76+
}), {
5077
code: 'ERR_INVALID_ARG_TYPE'
5178
});
52-
}, nonStringInputs.length));
79+
});
80+
81+
test('validate failed init', async (t) => {
82+
assert.throws(
83+
() => zlib.createGzip({ chunkSize: 0 }),
84+
{
85+
code: 'ERR_OUT_OF_RANGE',
86+
name: 'RangeError',
87+
message: 'The value of "options.chunkSize" is out of range. It must ' +
88+
'be >= 64. Received 0'
89+
}
90+
);
91+
92+
assert.throws(
93+
() => zlib.createGzip({ windowBits: 0 }),
94+
{
95+
code: 'ERR_OUT_OF_RANGE',
96+
name: 'RangeError',
97+
message: 'The value of "options.windowBits" is out of range. It must ' +
98+
'be >= 9 and <= 15. Received 0'
99+
}
100+
);
101+
102+
assert.throws(
103+
() => zlib.createGzip({ memLevel: 0 }),
104+
{
105+
code: 'ERR_OUT_OF_RANGE',
106+
name: 'RangeError',
107+
message: 'The value of "options.memLevel" is out of range. It must ' +
108+
'be >= 1 and <= 9. Received 0'
109+
}
110+
);
53111

54-
unzips.forEach(common.mustCall((uz, i) => {
55-
uz.on('error', common.mustCall());
56-
uz.on('end', common.mustNotCall());
112+
{
113+
const stream = zlib.createGzip({ level: NaN });
114+
assert.strictEqual(stream._level, zlib.constants.Z_DEFAULT_COMPRESSION);
115+
}
57116

58-
// This will trigger error event
59-
uz.write('this is not valid compressed data.');
60-
}, unzips.length));
117+
{
118+
const stream = zlib.createGzip({ strategy: NaN });
119+
assert.strictEqual(stream._strategy, zlib.constants.Z_DEFAULT_STRATEGY);
120+
}
121+
});
Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
'use strict';
22

3-
// Check the error condition testing for passing something other than a string
4-
// or buffer.
3+
const { invalidArgTypeHelper } = require('../common');
54

6-
const common = require('../common');
7-
const assert = require('assert');
8-
const zlib = require('zlib');
5+
const assert = require('node:assert');
6+
const zlib = require('node:zlib');
7+
const { test } = require('node:test');
98

10-
[
11-
undefined,
12-
null,
13-
true,
14-
false,
15-
0,
16-
1,
17-
[1, 2, 3],
18-
{ foo: 'bar' },
19-
].forEach((input) => {
20-
assert.throws(
21-
() => zlib.deflateSync(input),
22-
{
23-
code: 'ERR_INVALID_ARG_TYPE',
24-
name: 'TypeError',
25-
message: 'The "buffer" argument must be of type string or an instance ' +
26-
'of Buffer, TypedArray, DataView, or ArrayBuffer.' +
27-
common.invalidArgTypeHelper(input)
28-
}
29-
);
9+
// Check the error condition testing for passing something other than a string
10+
// or buffer.
11+
test('check zlib for not string or buffer inputs', async (t) => {
12+
[
13+
undefined,
14+
null,
15+
true,
16+
false,
17+
0,
18+
1,
19+
[1, 2, 3],
20+
{ foo: 'bar' },
21+
].forEach((input) => {
22+
assert.throws(
23+
() => zlib.deflateSync(input),
24+
{
25+
code: 'ERR_INVALID_ARG_TYPE',
26+
name: 'TypeError',
27+
message: 'The "buffer" argument must be of type string or an instance ' +
28+
'of Buffer, TypedArray, DataView, or ArrayBuffer.' +
29+
invalidArgTypeHelper(input)
30+
}
31+
);
32+
});
3033
});
Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
'use strict';
22

3-
const common = require('../common');
4-
const assert = require('assert');
5-
const { Gunzip } = require('zlib');
3+
require('../common');
64

7-
const gunzip = new Gunzip({ objectMode: true });
8-
gunzip.on('error', common.mustNotCall());
9-
assert.throws(() => {
10-
gunzip.write({});
11-
}, {
12-
name: 'TypeError',
13-
code: 'ERR_INVALID_ARG_TYPE'
5+
const assert = require('node:assert');
6+
const { Gunzip } = require('node:zlib');
7+
const { test } = require('node:test');
8+
9+
test('zlib object write', async (t) => {
10+
const { promise, resolve, reject } = Promise.withResolvers();
11+
const gunzip = new Gunzip({ objectMode: true });
12+
gunzip.on('error', reject);
13+
gunzip.on('close', resolve);
14+
assert.throws(() => {
15+
gunzip.write({});
16+
}, {
17+
name: 'TypeError',
18+
code: 'ERR_INVALID_ARG_TYPE'
19+
});
20+
gunzip.close();
21+
await promise;
1422
});

0 commit comments

Comments
 (0)