Skip to content

Commit abf4dcc

Browse files
committed
test: move more zlib tests to node:test
1 parent 4f14eb1 commit abf4dcc

6 files changed

+141
-105
lines changed
Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
11
'use strict';
2-
const common = require('../common');
3-
const assert = require('assert');
4-
const zlib = require('zlib');
52

6-
const data = Buffer.concat([
7-
zlib.gzipSync('abc'),
8-
zlib.gzipSync('def'),
9-
]);
3+
require('../common');
104

11-
const resultBuffers = [];
5+
const assert = require('node:assert');
6+
const zlib = require('node:zlib');
7+
const { test } = require('node:test');
128

13-
const unzip = zlib.createUnzip()
14-
.on('error', (err) => {
15-
assert.ifError(err);
16-
})
17-
.on('data', (data) => resultBuffers.push(data))
18-
.on('finish', common.mustCall(() => {
19-
const unzipped = Buffer.concat(resultBuffers).toString();
20-
assert.strictEqual(unzipped, 'abcdef',
21-
`'${unzipped}' should match 'abcdef' after zipping ` +
22-
'and unzipping');
23-
}));
9+
test('zlib should unzip one byte chunks', async () => {
10+
const { promise, resolve } = Promise.withResolvers();
11+
const data = Buffer.concat([
12+
zlib.gzipSync('abc'),
13+
zlib.gzipSync('def'),
14+
]);
2415

25-
for (let i = 0; i < data.length; i++) {
26-
// Write each single byte individually.
27-
unzip.write(Buffer.from([data[i]]));
28-
}
16+
const resultBuffers = [];
2917

30-
unzip.end();
18+
const unzip = zlib.createUnzip()
19+
.on('error', (err) => {
20+
assert.ifError(err);
21+
})
22+
.on('data', (data) => resultBuffers.push(data))
23+
.on('finish', () => {
24+
const unzipped = Buffer.concat(resultBuffers).toString();
25+
assert.strictEqual(unzipped, 'abcdef',
26+
`'${unzipped}' should match 'abcdef' after zipping ` +
27+
'and unzipping');
28+
resolve();
29+
});
30+
31+
for (let i = 0; i < data.length; i++) {
32+
// Write each single byte individually.
33+
unzip.write(Buffer.from([data[i]]));
34+
}
35+
36+
unzip.end();
37+
await promise;
38+
});

test/parallel/test-zlib-write-after-close.js

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

2222
'use strict';
23-
const common = require('../common');
24-
const zlib = require('zlib');
2523

26-
zlib.gzip('hello', common.mustCall(function(err, out) {
27-
const unzip = zlib.createGunzip();
28-
unzip.close(common.mustCall());
29-
unzip.write('asd', common.expectsError({
30-
code: 'ERR_STREAM_DESTROYED',
31-
name: 'Error',
32-
message: 'Cannot call write after a stream was destroyed'
33-
}));
34-
}));
24+
require('../common');
25+
26+
const zlib = require('node:zlib');
27+
const assert = require('node:assert');
28+
const { test } = require('node:test');
29+
30+
test('zlib should not allow writing after close', async (t) => {
31+
const { promise, resolve } = Promise.withResolvers();
32+
const closeCallback = t.mock.fn();
33+
zlib.gzip('hello', function() {
34+
const unzip = zlib.createGunzip();
35+
unzip.close(closeCallback);
36+
unzip.write('asd', function(err) {
37+
assert.strictEqual(err.code, 'ERR_STREAM_DESTROYED');
38+
assert.strictEqual(err.name, 'Error');
39+
assert.strictEqual(err.message, 'Cannot call write after a stream was destroyed');
40+
resolve();
41+
});
42+
});
43+
await promise;
44+
assert.strictEqual(closeCallback.mock.callCount(), 1);
45+
});
Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
'use strict';
2-
const common = require('../common');
3-
const zlib = require('zlib');
42

5-
// Regression test for https://github.com/nodejs/node/issues/30976
6-
// Writes to a stream should finish even after the readable side has been ended.
7-
8-
const data = zlib.deflateRawSync('Welcome');
3+
require('../common');
4+
const assert = require('node:assert');
5+
const zlib = require('node:zlib');
6+
const { test } = require('node:test');
97

10-
const inflate = zlib.createInflateRaw();
11-
12-
inflate.resume();
13-
inflate.write(data, common.mustCall());
14-
inflate.write(Buffer.from([0x00]), common.mustCall());
15-
inflate.write(Buffer.from([0x00]), common.mustCall());
16-
inflate.flush(common.mustCall());
8+
// Regression test for https://github.com/nodejs/node/issues/30976
9+
test('Writes to a stream should finish even after the readable side has been ended.', async (t) => {
10+
const { promise, resolve } = Promise.withResolvers();
11+
const data = zlib.deflateRawSync('Welcome');
12+
const inflate = zlib.createInflateRaw();
13+
const writeCallback = t.mock.fn();
14+
inflate.resume();
15+
inflate.write(data, writeCallback);
16+
inflate.write(Buffer.from([0x00]), writeCallback);
17+
inflate.write(Buffer.from([0x00]), writeCallback);
18+
inflate.flush(resolve);
19+
await promise;
20+
assert.strictEqual(writeCallback.mock.callCount(), 3);
21+
});

test/parallel/test-zlib-write-after-flush.js

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,39 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
const common = require('../common');
24-
const assert = require('assert');
25-
const zlib = require('zlib');
2623

27-
for (const [ createCompress, createDecompress ] of [
28-
[ zlib.createGzip, zlib.createGunzip ],
29-
[ zlib.createBrotliCompress, zlib.createBrotliDecompress ],
30-
]) {
31-
const gzip = createCompress();
32-
const gunz = createDecompress();
24+
require('../common');
3325

34-
gzip.pipe(gunz);
26+
const assert = require('node:assert');
27+
const zlib = require('node:zlib');
28+
const { test } = require('node:test');
3529

36-
let output = '';
37-
const input = 'A line of data\n';
38-
gunz.setEncoding('utf8');
39-
gunz.on('data', (c) => output += c);
40-
gunz.on('end', common.mustCall(() => {
41-
assert.strictEqual(output, input);
42-
}));
30+
test('zlib should accept writing after flush', async () => {
31+
for (const [createCompress, createDecompress] of [
32+
[zlib.createGzip, zlib.createGunzip],
33+
[zlib.createBrotliCompress, zlib.createBrotliDecompress],
34+
]) {
35+
const { promise, resolve, reject } = Promise.withResolvers();
36+
const gzip = createCompress();
37+
const gunz = createDecompress();
4338

44-
// Make sure that flush/write doesn't trigger an assert failure
45-
gzip.flush();
46-
gzip.write(input);
47-
gzip.end();
48-
gunz.read(0);
49-
}
39+
gzip.pipe(gunz);
40+
41+
let output = '';
42+
const input = 'A line of data\n';
43+
gunz.setEncoding('utf8');
44+
gunz.on('error', reject);
45+
gunz.on('data', (c) => output += c);
46+
gunz.on('end', () => {
47+
assert.strictEqual(output, input);
48+
resolve();
49+
});
50+
51+
// Make sure that flush/write doesn't trigger an assert failure
52+
gzip.flush();
53+
gzip.write(input);
54+
gzip.end();
55+
gunz.read(0);
56+
await promise;
57+
}
58+
});

test/parallel/test-zlib-zero-byte.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,31 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
const common = require('../common');
24-
const assert = require('assert');
25-
const zlib = require('zlib');
2623

27-
for (const Compressor of [ zlib.Gzip, zlib.BrotliCompress ]) {
28-
const gz = Compressor();
29-
const emptyBuffer = Buffer.alloc(0);
30-
let received = 0;
31-
gz.on('data', function(c) {
32-
received += c.length;
33-
});
24+
require('../common');
3425

35-
gz.on('end', common.mustCall(function() {
36-
const expected = Compressor === zlib.Gzip ? 20 : 1;
37-
assert.strictEqual(received, expected,
38-
`${received}, ${expected}, ${Compressor.name}`);
39-
}));
40-
gz.on('finish', common.mustCall());
41-
gz.write(emptyBuffer);
42-
gz.end();
43-
}
26+
const assert = require('node:assert');
27+
const zlib = require('node:zlib');
28+
const { test } = require('node:test');
29+
30+
test('zlib should properly handle zero byte input', async () => {
31+
for (const Compressor of [zlib.Gzip, zlib.BrotliCompress]) {
32+
const { promise, resolve, reject } = Promise.withResolvers();
33+
const gz = Compressor();
34+
const emptyBuffer = Buffer.alloc(0);
35+
let received = 0;
36+
gz.on('data', function(c) {
37+
received += c.length;
38+
});
39+
gz.on('error', reject);
40+
gz.on('end', function() {
41+
const expected = Compressor === zlib.Gzip ? 20 : 1;
42+
assert.strictEqual(received, expected,
43+
`${received}, ${expected}, ${Compressor.name}`);
44+
resolve();
45+
});
46+
gz.write(emptyBuffer);
47+
gz.end();
48+
await promise;
49+
}
50+
});
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
'use strict';
22

33
require('../common');
4-
const assert = require('assert');
5-
const zlib = require('zlib');
6-
4+
const assert = require('node:assert');
5+
const zlib = require('node:zlib');
6+
const { test } = require('node:test');
77

88
// windowBits is a special case in zlib. On the compression side, 0 is invalid.
99
// On the decompression side, it indicates that zlib should use the value from
1010
// the header of the compressed stream.
11-
{
11+
test('zlib should support zero windowBits', (t) => {
1212
const inflate = zlib.createInflate({ windowBits: 0 });
13-
assert(inflate instanceof zlib.Inflate);
14-
}
13+
assert.ok(inflate instanceof zlib.Inflate);
1514

16-
{
1715
const gunzip = zlib.createGunzip({ windowBits: 0 });
18-
assert(gunzip instanceof zlib.Gunzip);
19-
}
16+
assert.ok(gunzip instanceof zlib.Gunzip);
2017

21-
{
2218
const unzip = zlib.createUnzip({ windowBits: 0 });
23-
assert(unzip instanceof zlib.Unzip);
24-
}
19+
assert.ok(unzip instanceof zlib.Unzip);
20+
});
2521

26-
{
22+
test('windowBits should be valid', () => {
2723
assert.throws(() => zlib.createGzip({ windowBits: 0 }), {
2824
code: 'ERR_OUT_OF_RANGE',
2925
name: 'RangeError',
3026
message: 'The value of "options.windowBits" is out of range. ' +
31-
'It must be >= 9 and <= 15. Received 0'
27+
'It must be >= 9 and <= 15. Received 0'
3228
});
33-
}
29+
});

0 commit comments

Comments
 (0)