Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: move more zlib tests to node:test #54609

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 32 additions & 24 deletions test/parallel/test-zlib-unzip-one-byte-chunks.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const zlib = require('zlib');

const data = Buffer.concat([
zlib.gzipSync('abc'),
zlib.gzipSync('def'),
]);
require('../common');

const resultBuffers = [];
const assert = require('node:assert');
const zlib = require('node:zlib');
const { test } = require('node:test');

const unzip = zlib.createUnzip()
.on('error', (err) => {
assert.ifError(err);
})
.on('data', (data) => resultBuffers.push(data))
.on('finish', common.mustCall(() => {
const unzipped = Buffer.concat(resultBuffers).toString();
assert.strictEqual(unzipped, 'abcdef',
`'${unzipped}' should match 'abcdef' after zipping ` +
'and unzipping');
}));
test('zlib should unzip one byte chunks', async () => {
const { promise, resolve } = Promise.withResolvers();
const data = Buffer.concat([
zlib.gzipSync('abc'),
zlib.gzipSync('def'),
]);

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

unzip.end();
const unzip = zlib.createUnzip()
.on('error', (err) => {
assert.ifError(err);
})
.on('data', (data) => resultBuffers.push(data))
.on('finish', () => {
const unzipped = Buffer.concat(resultBuffers).toString();
assert.strictEqual(unzipped, 'abcdef',
`'${unzipped}' should match 'abcdef' after zipping ` +
'and unzipping');
resolve();
});

for (let i = 0; i < data.length; i++) {
// Write each single byte individually.
unzip.write(Buffer.from([data[i]]));
}

unzip.end();
await promise;
});
33 changes: 22 additions & 11 deletions test/parallel/test-zlib-write-after-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,26 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
const common = require('../common');
const zlib = require('zlib');

zlib.gzip('hello', common.mustCall(function(err, out) {
const unzip = zlib.createGunzip();
unzip.close(common.mustCall());
unzip.write('asd', common.expectsError({
code: 'ERR_STREAM_DESTROYED',
name: 'Error',
message: 'Cannot call write after a stream was destroyed'
}));
}));
require('../common');

const zlib = require('node:zlib');
const assert = require('node:assert');
const { test } = require('node:test');

test('zlib should not allow writing after close', async (t) => {
const { promise, resolve } = Promise.withResolvers();
const closeCallback = t.mock.fn();
zlib.gzip('hello', function() {
const unzip = zlib.createGunzip();
unzip.close(closeCallback);
unzip.write('asd', function(err) {
assert.strictEqual(err.code, 'ERR_STREAM_DESTROYED');
assert.strictEqual(err.name, 'Error');
assert.strictEqual(err.message, 'Cannot call write after a stream was destroyed');
resolve();
});
});
await promise;
assert.strictEqual(closeCallback.mock.callCount(), 1);
});
30 changes: 18 additions & 12 deletions test/parallel/test-zlib-write-after-end.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
'use strict';
const common = require('../common');
const zlib = require('zlib');

// Regression test for https://github.com/nodejs/node/issues/30976
// Writes to a stream should finish even after the readable side has been ended.

const data = zlib.deflateRawSync('Welcome');
require('../common');

const inflate = zlib.createInflateRaw();
const assert = require('node:assert');
const zlib = require('node:zlib');
const { test } = require('node:test');

inflate.resume();
inflate.write(data, common.mustCall());
inflate.write(Buffer.from([0x00]), common.mustCall());
inflate.write(Buffer.from([0x00]), common.mustCall());
inflate.flush(common.mustCall());
// Regression test for https://github.com/nodejs/node/issues/30976
test('Writes to a stream should finish even after the readable side has been ended.', async (t) => {
const { promise, resolve } = Promise.withResolvers();
const data = zlib.deflateRawSync('Welcome');
const inflate = zlib.createInflateRaw();
const writeCallback = t.mock.fn();
inflate.resume();
inflate.write(data, writeCallback);
inflate.write(Buffer.from([0x00]), writeCallback);
inflate.write(Buffer.from([0x00]), writeCallback);
inflate.flush(resolve);
await promise;
assert.strictEqual(writeCallback.mock.callCount(), 3);
});
55 changes: 32 additions & 23 deletions test/parallel/test-zlib-write-after-flush.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,39 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
const common = require('../common');
const assert = require('assert');
const zlib = require('zlib');

for (const [ createCompress, createDecompress ] of [
[ zlib.createGzip, zlib.createGunzip ],
[ zlib.createBrotliCompress, zlib.createBrotliDecompress ],
]) {
const gzip = createCompress();
const gunz = createDecompress();
require('../common');

gzip.pipe(gunz);
const assert = require('node:assert');
const zlib = require('node:zlib');
const { test } = require('node:test');

let output = '';
const input = 'A line of data\n';
gunz.setEncoding('utf8');
gunz.on('data', (c) => output += c);
gunz.on('end', common.mustCall(() => {
assert.strictEqual(output, input);
}));
test('zlib should accept writing after flush', async () => {
for (const [createCompress, createDecompress] of [
[zlib.createGzip, zlib.createGunzip],
[zlib.createBrotliCompress, zlib.createBrotliDecompress],
]) {
const { promise, resolve, reject } = Promise.withResolvers();
const gzip = createCompress();
const gunz = createDecompress();

// Make sure that flush/write doesn't trigger an assert failure
gzip.flush();
gzip.write(input);
gzip.end();
gunz.read(0);
}
gzip.pipe(gunz);

let output = '';
const input = 'A line of data\n';
gunz.setEncoding('utf8');
gunz.on('error', reject);
gunz.on('data', (c) => output += c);
gunz.on('end', () => {
assert.strictEqual(output, input);
resolve();
});

// Make sure that flush/write doesn't trigger an assert failure
gzip.flush();
gzip.write(input);
gzip.end();
gunz.read(0);
await promise;
}
});
45 changes: 26 additions & 19 deletions test/parallel/test-zlib-zero-byte.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,31 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
const common = require('../common');
const assert = require('assert');
const zlib = require('zlib');

for (const Compressor of [ zlib.Gzip, zlib.BrotliCompress ]) {
const gz = Compressor();
const emptyBuffer = Buffer.alloc(0);
let received = 0;
gz.on('data', function(c) {
received += c.length;
});
require('../common');

gz.on('end', common.mustCall(function() {
const expected = Compressor === zlib.Gzip ? 20 : 1;
assert.strictEqual(received, expected,
`${received}, ${expected}, ${Compressor.name}`);
}));
gz.on('finish', common.mustCall());
gz.write(emptyBuffer);
gz.end();
}
const assert = require('node:assert');
const zlib = require('node:zlib');
const { test } = require('node:test');

test('zlib should properly handle zero byte input', async () => {
for (const Compressor of [zlib.Gzip, zlib.BrotliCompress]) {
const { promise, resolve, reject } = Promise.withResolvers();
const gz = Compressor();
const emptyBuffer = Buffer.alloc(0);
let received = 0;
gz.on('data', function(c) {
received += c.length;
});
gz.on('error', reject);
gz.on('end', function() {
const expected = Compressor === zlib.Gzip ? 20 : 1;
assert.strictEqual(received, expected,
`${received}, ${expected}, ${Compressor.name}`);
resolve();
});
gz.write(emptyBuffer);
gz.end();
await promise;
}
});
25 changes: 11 additions & 14 deletions test/parallel/test-zlib-zero-windowBits.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
'use strict';

require('../common');
const assert = require('assert');
const zlib = require('zlib');

const assert = require('node:assert');
const zlib = require('node:zlib');
const { test } = require('node:test');

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

{
const gunzip = zlib.createGunzip({ windowBits: 0 });
assert(gunzip instanceof zlib.Gunzip);
}
assert.ok(gunzip instanceof zlib.Gunzip);

{
const unzip = zlib.createUnzip({ windowBits: 0 });
assert(unzip instanceof zlib.Unzip);
}
assert.ok(unzip instanceof zlib.Unzip);
});

{
test('windowBits should be valid', () => {
assert.throws(() => zlib.createGzip({ windowBits: 0 }), {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "options.windowBits" is out of range. ' +
'It must be >= 9 and <= 15. Received 0'
'It must be >= 9 and <= 15. Received 0'
});
}
});
Loading