From 826b7533e8ac8fb900314b16775d261ef868c439 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sat, 31 Aug 2024 20:35:18 -0400 Subject: [PATCH] test: move more zlib tests to node:test PR-URL: https://github.com/nodejs/node/pull/54609 Reviewed-By: James M Snell Reviewed-By: Chemi Atlow --- .../test-zlib-unzip-one-byte-chunks.js | 56 +++++++++++-------- test/parallel/test-zlib-write-after-close.js | 33 +++++++---- test/parallel/test-zlib-write-after-end.js | 30 ++++++---- test/parallel/test-zlib-write-after-flush.js | 55 ++++++++++-------- test/parallel/test-zlib-zero-byte.js | 45 ++++++++------- test/parallel/test-zlib-zero-windowBits.js | 25 ++++----- 6 files changed, 141 insertions(+), 103 deletions(-) diff --git a/test/parallel/test-zlib-unzip-one-byte-chunks.js b/test/parallel/test-zlib-unzip-one-byte-chunks.js index 51af5153a4dd48..b379584a0438ec 100644 --- a/test/parallel/test-zlib-unzip-one-byte-chunks.js +++ b/test/parallel/test-zlib-unzip-one-byte-chunks.js @@ -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; +}); diff --git a/test/parallel/test-zlib-write-after-close.js b/test/parallel/test-zlib-write-after-close.js index eb8ff4353963d4..2d5d4965b0fec4 100644 --- a/test/parallel/test-zlib-write-after-close.js +++ b/test/parallel/test-zlib-write-after-close.js @@ -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); +}); diff --git a/test/parallel/test-zlib-write-after-end.js b/test/parallel/test-zlib-write-after-end.js index 2b31ff30dc8591..2017e7f7d9cc6b 100644 --- a/test/parallel/test-zlib-write-after-end.js +++ b/test/parallel/test-zlib-write-after-end.js @@ -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); +}); diff --git a/test/parallel/test-zlib-write-after-flush.js b/test/parallel/test-zlib-write-after-flush.js index 6edcae2e2f18bf..c3706678508a93 100644 --- a/test/parallel/test-zlib-write-after-flush.js +++ b/test/parallel/test-zlib-write-after-flush.js @@ -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; + } +}); diff --git a/test/parallel/test-zlib-zero-byte.js b/test/parallel/test-zlib-zero-byte.js index fc57960f1e56cb..7e547b40fadf6c 100644 --- a/test/parallel/test-zlib-zero-byte.js +++ b/test/parallel/test-zlib-zero-byte.js @@ -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; + } +}); diff --git a/test/parallel/test-zlib-zero-windowBits.js b/test/parallel/test-zlib-zero-windowBits.js index a27fd6734a5425..220bbf8a402395 100644 --- a/test/parallel/test-zlib-zero-windowBits.js +++ b/test/parallel/test-zlib-zero-windowBits.js @@ -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' }); -} +});