diff --git a/test/parallel/test-zlib-empty-buffer.js b/test/parallel/test-zlib-empty-buffer.js index 27fd1340fd1eb4..9a449c1395567c 100644 --- a/test/parallel/test-zlib-empty-buffer.js +++ b/test/parallel/test-zlib-empty-buffer.js @@ -1,11 +1,15 @@ 'use strict'; -const common = require('../common'); -const zlib = require('zlib'); -const { inspect, promisify } = require('util'); -const assert = require('assert'); -const emptyBuffer = Buffer.alloc(0); -(async function() { +require('../common'); + +const zlib = require('node:zlib'); +const { inspect, promisify } = require('node:util'); +const assert = require('node:assert'); +const { test } = require('node:test'); + +test('empty buffer', async (t) => { + const emptyBuffer = Buffer.alloc(0); + for (const [ compress, decompress, method ] of [ [ zlib.deflateRawSync, zlib.inflateRawSync, 'raw sync' ], [ zlib.deflateSync, zlib.inflateSync, 'deflate sync' ], @@ -23,4 +27,4 @@ const emptyBuffer = Buffer.alloc(0); `Expected ${inspect(compressed)} to match ${inspect(decompressed)} ` + `to match for ${method}`); } -})().then(common.mustCall()); +}); diff --git a/test/parallel/test-zlib-failed-init.js b/test/parallel/test-zlib-failed-init.js deleted file mode 100644 index 95f401a3718f30..00000000000000 --- a/test/parallel/test-zlib-failed-init.js +++ /dev/null @@ -1,46 +0,0 @@ -'use strict'; - -require('../common'); - -const assert = require('assert'); -const zlib = require('zlib'); - -assert.throws( - () => zlib.createGzip({ chunkSize: 0 }), - { - code: 'ERR_OUT_OF_RANGE', - name: 'RangeError', - message: 'The value of "options.chunkSize" is out of range. It must ' + - 'be >= 64. Received 0' - } -); - -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' - } -); - -assert.throws( - () => zlib.createGzip({ memLevel: 0 }), - { - code: 'ERR_OUT_OF_RANGE', - name: 'RangeError', - message: 'The value of "options.memLevel" is out of range. It must ' + - 'be >= 1 and <= 9. Received 0' - } -); - -{ - const stream = zlib.createGzip({ level: NaN }); - assert.strictEqual(stream._level, zlib.constants.Z_DEFAULT_COMPRESSION); -} - -{ - const stream = zlib.createGzip({ strategy: NaN }); - assert.strictEqual(stream._strategy, zlib.constants.Z_DEFAULT_STRATEGY); -} diff --git a/test/parallel/test-zlib-from-string.js b/test/parallel/test-zlib-from-string.js index 92b6f8664666a0..4e9e776c740c71 100644 --- a/test/parallel/test-zlib-from-string.js +++ b/test/parallel/test-zlib-from-string.js @@ -20,11 +20,12 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -// Test compressing and uncompressing a string with zlib -const common = require('../common'); -const assert = require('assert'); -const zlib = require('zlib'); +require('../common'); + +const assert = require('node:assert'); +const zlib = require('node:zlib'); +const { test } = require('node:test'); const inputString = 'ΩΩLorem ipsum dolor sit amet, consectetur adipiscing eli' + 't. Morbi faucibus, purus at gravida dictum, libero arcu ' + @@ -54,30 +55,56 @@ const expectedBase64Gzip = 'H4sIAAAAAAAAA11RS05DMQy8yhzg6d2BPSAkJPZu4laWkjiN4' + 'mHo33kJO8xfkckmLjE5XMKBQ4gxIsfvCZ44doUThF2mcZq8q2' + 'sHnHNzRtagj5AQAA'; -zlib.deflate(inputString, common.mustCall((err, buffer) => { - zlib.inflate(buffer, common.mustCall((err, inflated) => { - assert.strictEqual(inflated.toString(), inputString); - })); -})); +test('properly deflate and inflate', async (t) => { + const { promise, resolve } = Promise.withResolvers(); + zlib.deflate(inputString, (err, buffer) => { + assert.ifError(err); + zlib.inflate(buffer, (err, inflated) => { + assert.ifError(err); + assert.strictEqual(inflated.toString(), inputString); + resolve(); + }); + }); + await promise; +}); -zlib.gzip(inputString, common.mustCall((err, buffer) => { - // Can't actually guarantee that we'll get exactly the same - // deflated bytes when we compress a string, since the header - // depends on stuff other than the input string itself. - // However, decrypting it should definitely yield the same - // result that we're expecting, and this should match what we get - // from inflating the known valid deflate data. - zlib.gunzip(buffer, common.mustCall((err, gunzipped) => { - assert.strictEqual(gunzipped.toString(), inputString); - })); -})); +test('properly gzip and gunzip', async (t) => { + const { promise, resolve } = Promise.withResolvers(); + zlib.gzip(inputString, (err, buffer) => { + assert.ifError(err); + // Can't actually guarantee that we'll get exactly the same + // deflated bytes when we compress a string, since the header + // depends on stuff other than the input string itself. + // However, decrypting it should definitely yield the same + // result that we're expecting, and this should match what we get + // from inflating the known valid deflate data. + zlib.gunzip(buffer, (err, gunzipped) => { + assert.ifError(err); + assert.strictEqual(gunzipped.toString(), inputString); + resolve(); + }); + }); + await promise; +}); -let buffer = Buffer.from(expectedBase64Deflate, 'base64'); -zlib.unzip(buffer, common.mustCall((err, buffer) => { - assert.strictEqual(buffer.toString(), inputString); -})); +test('properly unzip base64 deflate', async (t) => { + const { promise, resolve } = Promise.withResolvers(); + const buffer = Buffer.from(expectedBase64Deflate, 'base64'); + zlib.unzip(buffer, (err, buffer) => { + assert.ifError(err); + assert.strictEqual(buffer.toString(), inputString); + resolve(); + }); + await promise; +}); -buffer = Buffer.from(expectedBase64Gzip, 'base64'); -zlib.unzip(buffer, common.mustCall((err, buffer) => { - assert.strictEqual(buffer.toString(), inputString); -})); +test('properly unzip base64 gzip', async (t) => { + const { promise, resolve } = Promise.withResolvers(); + const buffer = Buffer.from(expectedBase64Gzip, 'base64'); + zlib.unzip(buffer, (err, buffer) => { + assert.ifError(err); + assert.strictEqual(buffer.toString(), inputString); + resolve(); + }); + await promise; +}); diff --git a/test/parallel/test-zlib-invalid-arg-value-brotli-compress.js b/test/parallel/test-zlib-invalid-arg-value-brotli-compress.js deleted file mode 100644 index 688acddd16a136..00000000000000 --- a/test/parallel/test-zlib-invalid-arg-value-brotli-compress.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict'; - -require('../common'); - -// This test ensures that the BrotliCompress function throws -// ERR_INVALID_ARG_TYPE when the values of the `params` key-value object are -// neither numbers nor booleans. - -const assert = require('assert'); -const { BrotliCompress, constants } = require('zlib'); - -const opts = { - params: { - [constants.BROTLI_PARAM_MODE]: 'lol' - } -}; - -assert.throws(() => BrotliCompress(opts), { - code: 'ERR_INVALID_ARG_TYPE' -}); diff --git a/test/parallel/test-zlib-invalid-input.js b/test/parallel/test-zlib-invalid-input.js index 7aa44dfe7090a1..9f41964570e867 100644 --- a/test/parallel/test-zlib-invalid-input.js +++ b/test/parallel/test-zlib-invalid-input.js @@ -22,39 +22,100 @@ 'use strict'; // Test uncompressing invalid input -const common = require('../common'); -const assert = require('assert'); -const zlib = require('zlib'); - -const nonStringInputs = [ - 1, - true, - { a: 1 }, - ['a'], -]; - -// zlib.Unzip classes need to get valid data, or else they'll throw. -const unzips = [ - zlib.Unzip(), - zlib.Gunzip(), - zlib.Inflate(), - zlib.InflateRaw(), - zlib.BrotliDecompress(), -]; - -nonStringInputs.forEach(common.mustCall((input) => { - assert.throws(() => { - zlib.gunzip(input); - }, { - name: 'TypeError', +require('../common'); + +const assert = require('node:assert'); +const zlib = require('node:zlib'); +const { test } = require('node:test'); + +test('uncompressing invalid input', async (t) => { + const nonStringInputs = [ + 1, + true, + { a: 1 }, + ['a'], + ]; + + // zlib.Unzip classes need to get valid data, or else they'll throw. + const unzips = [ + new zlib.Unzip(), + new zlib.Gunzip(), + new zlib.Inflate(), + new zlib.InflateRaw(), + new zlib.BrotliDecompress(), + ]; + + for (const input of nonStringInputs) { + assert.throws(() => { + zlib.gunzip(input); + }, { + name: 'TypeError', + code: 'ERR_INVALID_ARG_TYPE' + }); + } + + for (const uz of unzips) { + const { promise, resolve, reject } = Promise.withResolvers(); + uz.on('error', resolve); + uz.on('end', reject); + + // This will trigger error event + uz.write('this is not valid compressed data.'); + await promise; + } +}); + +// This test ensures that the BrotliCompress function throws +// ERR_INVALID_ARG_TYPE when the values of the `params` key-value object are +// neither numbers nor booleans. +test('ensure BrotliCompress throws error on invalid params', async (t) => { + assert.throws(() => new zlib.BrotliCompress({ + params: { + [zlib.constants.BROTLI_PARAM_MODE]: 'lol' + } + }), { code: 'ERR_INVALID_ARG_TYPE' }); -}, nonStringInputs.length)); +}); + +test('validate failed init', async (t) => { + assert.throws( + () => zlib.createGzip({ chunkSize: 0 }), + { + code: 'ERR_OUT_OF_RANGE', + name: 'RangeError', + message: 'The value of "options.chunkSize" is out of range. It must ' + + 'be >= 64. Received 0' + } + ); + + 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' + } + ); + + assert.throws( + () => zlib.createGzip({ memLevel: 0 }), + { + code: 'ERR_OUT_OF_RANGE', + name: 'RangeError', + message: 'The value of "options.memLevel" is out of range. It must ' + + 'be >= 1 and <= 9. Received 0' + } + ); -unzips.forEach(common.mustCall((uz, i) => { - uz.on('error', common.mustCall()); - uz.on('end', common.mustNotCall()); + { + const stream = zlib.createGzip({ level: NaN }); + assert.strictEqual(stream._level, zlib.constants.Z_DEFAULT_COMPRESSION); + } - // This will trigger error event - uz.write('this is not valid compressed data.'); -}, unzips.length)); + { + const stream = zlib.createGzip({ strategy: NaN }); + assert.strictEqual(stream._strategy, zlib.constants.Z_DEFAULT_STRATEGY); + } +}); diff --git a/test/parallel/test-zlib-not-string-or-buffer.js b/test/parallel/test-zlib-not-string-or-buffer.js index a0954a0e1eaef7..f08f8c1d3e0fcd 100644 --- a/test/parallel/test-zlib-not-string-or-buffer.js +++ b/test/parallel/test-zlib-not-string-or-buffer.js @@ -1,30 +1,33 @@ 'use strict'; -// Check the error condition testing for passing something other than a string -// or buffer. +const { invalidArgTypeHelper } = require('../common'); -const common = 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'); -[ - undefined, - null, - true, - false, - 0, - 1, - [1, 2, 3], - { foo: 'bar' }, -].forEach((input) => { - assert.throws( - () => zlib.deflateSync(input), - { - code: 'ERR_INVALID_ARG_TYPE', - name: 'TypeError', - message: 'The "buffer" argument must be of type string or an instance ' + - 'of Buffer, TypedArray, DataView, or ArrayBuffer.' + - common.invalidArgTypeHelper(input) - } - ); +// Check the error condition testing for passing something other than a string +// or buffer. +test('check zlib for not string or buffer inputs', async (t) => { + [ + undefined, + null, + true, + false, + 0, + 1, + [1, 2, 3], + { foo: 'bar' }, + ].forEach((input) => { + assert.throws( + () => zlib.deflateSync(input), + { + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + message: 'The "buffer" argument must be of type string or an instance ' + + 'of Buffer, TypedArray, DataView, or ArrayBuffer.' + + invalidArgTypeHelper(input) + } + ); + }); }); diff --git a/test/parallel/test-zlib-object-write.js b/test/parallel/test-zlib-object-write.js index 2be5edab897510..46411ec851808a 100644 --- a/test/parallel/test-zlib-object-write.js +++ b/test/parallel/test-zlib-object-write.js @@ -1,14 +1,22 @@ 'use strict'; -const common = require('../common'); -const assert = require('assert'); -const { Gunzip } = require('zlib'); +require('../common'); -const gunzip = new Gunzip({ objectMode: true }); -gunzip.on('error', common.mustNotCall()); -assert.throws(() => { - gunzip.write({}); -}, { - name: 'TypeError', - code: 'ERR_INVALID_ARG_TYPE' +const assert = require('node:assert'); +const { Gunzip } = require('node:zlib'); +const { test } = require('node:test'); + +test('zlib object write', async (t) => { + const { promise, resolve, reject } = Promise.withResolvers(); + const gunzip = new Gunzip({ objectMode: true }); + gunzip.on('error', reject); + gunzip.on('close', resolve); + assert.throws(() => { + gunzip.write({}); + }, { + name: 'TypeError', + code: 'ERR_INVALID_ARG_TYPE' + }); + gunzip.close(); + await promise; }); diff --git a/test/parallel/test-zlib-premature-end.js b/test/parallel/test-zlib-premature-end.js index 17446c907ddc13..3de1b4a477f6d6 100644 --- a/test/parallel/test-zlib-premature-end.js +++ b/test/parallel/test-zlib-premature-end.js @@ -1,33 +1,56 @@ 'use strict'; -const common = require('../common'); -const zlib = require('zlib'); -const assert = require('assert'); -const input = '0123456789'.repeat(4); +require('../common'); -for (const [ compress, decompressor ] of [ - [ zlib.deflateRawSync, zlib.createInflateRaw ], - [ zlib.deflateSync, zlib.createInflate ], - [ zlib.brotliCompressSync, zlib.createBrotliDecompress ], -]) { - const compressed = compress(input); - const trailingData = Buffer.from('not valid compressed data'); +const zlib = require('node:zlib'); +const assert = require('node:assert'); +const { test } = require('node:test'); - for (const variant of [ - (stream) => { stream.end(compressed); }, - (stream) => { stream.write(compressed); stream.write(trailingData); }, - (stream) => { stream.write(compressed); stream.end(trailingData); }, - (stream) => { stream.write(Buffer.concat([compressed, trailingData])); }, - (stream) => { stream.end(Buffer.concat([compressed, trailingData])); }, +test('zlib should handle premature end', async (t) => { + const input = '0123456789'.repeat(4); + + for (const [compress, decompressor] of [ + [zlib.deflateRawSync, zlib.createInflateRaw], + [zlib.deflateSync, zlib.createInflate], + [zlib.brotliCompressSync, zlib.createBrotliDecompress], ]) { - let output = ''; - const stream = decompressor(); - stream.setEncoding('utf8'); - stream.on('data', (chunk) => output += chunk); - stream.on('end', common.mustCall(() => { - assert.strictEqual(output, input); - assert.strictEqual(stream.bytesWritten, compressed.length); - })); - variant(stream); + const compressed = compress(input); + const trailingData = Buffer.from('not valid compressed data'); + + for (const variant of [ + (stream) => { + stream.end(compressed); + }, + (stream) => { + stream.write(compressed); + stream.write(trailingData); + }, + (stream) => { + stream.write(compressed); + stream.end(trailingData); + }, + (stream) => { + stream.write(Buffer.concat([compressed, trailingData])); + }, + (stream) => { + stream.end(Buffer.concat([compressed, trailingData])); + }, + ]) { + const { promise, resolve } = Promise.withResolvers(); + const endCallback = t.mock.fn(); + endCallback.mock.mockImplementation(() => { + assert.strictEqual(output, input); + assert.strictEqual(stream.bytesWritten, compressed.length); + resolve(); + }); + let output = ''; + const stream = decompressor(); + stream.setEncoding('utf8'); + stream.on('data', (chunk) => output += chunk); + stream.on('end', endCallback); + variant(stream); + await promise; + assert.ok(endCallback.mock.calls.length > 0, 'Should have called end callback'); + } } -} +}); diff --git a/test/parallel/test-zlib-random-byte-pipes.js b/test/parallel/test-zlib-random-byte-pipes.js index 918f0df629280d..25b45aa8c02fa3 100644 --- a/test/parallel/test-zlib-random-byte-pipes.js +++ b/test/parallel/test-zlib-random-byte-pipes.js @@ -20,16 +20,18 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -const common = require('../common'); -if (!common.hasCrypto) - common.skip('missing crypto'); -const assert = require('assert'); -const crypto = require('crypto'); -const stream = require('stream'); -const zlib = require('zlib'); +// Explicitly require to ensure no other common functionality is needed/loaded. +const { hasCrypto, skip } = require('../common'); +if (!hasCrypto) + skip('missing crypto'); -const Stream = stream.Stream; +const assert = require('node:assert'); +const crypto = require('node:crypto'); +const { Stream } = require('node:stream'); +const zlib = require('node:zlib'); +const { test } = require('node:test'); +const { once } = require('node:events'); // Emit random bytes, and keep a shasum class RandomReadStream extends Stream { @@ -66,7 +68,6 @@ class RandomReadStream extends Stream { } resume() { - // console.error("rrs resume"); this._paused = false; this.emit('resume'); this._process(); @@ -141,18 +142,24 @@ class HashStream extends Stream { } } -for (const [ createCompress, createDecompress ] of [ - [ zlib.createGzip, zlib.createGunzip ], - [ zlib.createBrotliCompress, zlib.createBrotliDecompress ], -]) { - const inp = new RandomReadStream({ total: 1024, block: 256, jitter: 16 }); - const out = new HashStream(); - const gzip = createCompress(); - const gunz = createDecompress(); - - inp.pipe(gzip).pipe(gunz).pipe(out); - - out.on('data', common.mustCall((c) => { - assert.strictEqual(c, inp._hash, `Hash '${c}' equals '${inp._hash}'.`); - })); -} +test('random byte pipes', async (t) => { + for (const [ createCompress, createDecompress ] of [ + [ zlib.createGzip, zlib.createGunzip ], + [ zlib.createBrotliCompress, zlib.createBrotliDecompress ], + ]) { + const inp = new RandomReadStream({ total: 1024, block: 256, jitter: 16 }); + const out = new HashStream(); + const gzip = createCompress(); + const gunz = createDecompress(); + + inp.pipe(gzip).pipe(gunz).pipe(out); + + const onDataCallback = t.mock.fn(); + onDataCallback.mock.mockImplementation((c) => { + assert.strictEqual(c, inp._hash, `Hash '${c}' equals '${inp._hash}'.`); + }); + out.on('data', onDataCallback); + await once(out, 'end'); + assert.ok(onDataCallback.mock.calls.length > 0, 'Should have called on data callback'); + } +});