GeezerVison is showing github.com — test: move more zlib tests to node:test · nodejs/node@67e30de
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +32-24Lines changed: 32 additions & 24 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | | -const common = require('../common'); | 3 | | -const assert = require('assert'); | 4 | | -const zlib = require('zlib'); | 5 | 2 |
| 6 | | -const data = Buffer.concat([ | 7 | | - zlib.gzipSync('abc'), | 8 | | - zlib.gzipSync('def'), | 9 | | -]); | | 3 | +require('../common'); | 10 | 4 |
| 11 | | -const resultBuffers = []; | | 5 | +const assert = require('node:assert'); | | 6 | +const zlib = require('node:zlib'); | | 7 | +const { test } = require('node:test'); | 12 | 8 |
| 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 | + ]); | 24 | 15 |
| 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 = []; | 29 | 17 |
| 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 | +}); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-zlib-write-after-close.js +22-11Lines changed: 22 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | 21 | 21 |
| 22 | 22 | 'use strict'; | 23 | | -const common = require('../common'); | 24 | | -const zlib = require('zlib'); | 25 | 23 |
| 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 | +}); |
Collapse file Copy file name to clipboard +18-12Lines changed: 18 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | | -const common = require('../common'); | 3 | | -const zlib = require('zlib'); | 4 | 2 |
| 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'); | 9 | 4 |
| 10 | | -const inflate = zlib.createInflateRaw(); | | 5 | +const assert = require('node:assert'); | | 6 | +const zlib = require('node:zlib'); | | 7 | +const { test } = require('node:test'); | 11 | 8 |
| 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()); | | 9 | +// Regression test for https://github.com/nodejs/node/issues/30976 | | 10 | +test('Writes to a stream should finish even after the readable side has been ended.', async (t) => { | | 11 | + const { promise, resolve } = Promise.withResolvers(); | | 12 | + const data = zlib.deflateRawSync('Welcome'); | | 13 | + const inflate = zlib.createInflateRaw(); | | 14 | + const writeCallback = t.mock.fn(); | | 15 | + inflate.resume(); | | 16 | + inflate.write(data, writeCallback); | | 17 | + inflate.write(Buffer.from([0x00]), writeCallback); | | 18 | + inflate.write(Buffer.from([0x00]), writeCallback); | | 19 | + inflate.flush(resolve); | | 20 | + await promise; | | 21 | + assert.strictEqual(writeCallback.mock.callCount(), 3); | | 22 | +}); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-zlib-write-after-flush.js +32-23Lines changed: 32 additions & 23 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | 21 | 21 |
| 22 | 22 | 'use strict'; | 23 | | -const common = require('../common'); | 24 | | -const assert = require('assert'); | 25 | | -const zlib = require('zlib'); | 26 | 23 |
| 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'); | 33 | 25 |
| 34 | | - gzip.pipe(gunz); | | 26 | +const assert = require('node:assert'); | | 27 | +const zlib = require('node:zlib'); | | 28 | +const { test } = require('node:test'); | 35 | 29 |
| 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(); | 43 | 38 |
| 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 | +}); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-zlib-zero-byte.js +26-19Lines changed: 26 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | 21 | 21 |
| 22 | 22 | 'use strict'; | 23 | | -const common = require('../common'); | 24 | | -const assert = require('assert'); | 25 | | -const zlib = require('zlib'); | 26 | 23 |
| 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'); | 34 | 25 |
| 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 | +}); |
Collapse file Copy file name to clipboard +11-14Lines changed: 11 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | 3 | require('../common'); | 4 | | -const assert = require('assert'); | 5 | | -const zlib = require('zlib'); | 6 | 4 |
| | 5 | +const assert = require('node:assert'); | | 6 | +const zlib = require('node:zlib'); | | 7 | +const { test } = require('node:test'); | 7 | 8 |
| 8 | 9 | // windowBits is a special case in zlib. On the compression side, 0 is invalid. | 9 | 10 | // On the decompression side, it indicates that zlib should use the value from | 10 | 11 | // the header of the compressed stream. | 11 | | -{ | | 12 | +test('zlib should support zero windowBits', (t) => { | 12 | 13 | const inflate = zlib.createInflate({ windowBits: 0 }); | 13 | | - assert(inflate instanceof zlib.Inflate); | 14 | | -} | | 14 | + assert.ok(inflate instanceof zlib.Inflate); | 15 | 15 |
| 16 | | -{ | 17 | 16 | const gunzip = zlib.createGunzip({ windowBits: 0 }); | 18 | | - assert(gunzip instanceof zlib.Gunzip); | 19 | | -} | | 17 | + assert.ok(gunzip instanceof zlib.Gunzip); | 20 | 18 |
| 21 | | -{ | 22 | 19 | const unzip = zlib.createUnzip({ windowBits: 0 }); | 23 | | - assert(unzip instanceof zlib.Unzip); | 24 | | -} | | 20 | + assert.ok(unzip instanceof zlib.Unzip); | | 21 | +}); | 25 | 22 |
| 26 | | -{ | | 23 | +test('windowBits should be valid', () => { | 27 | 24 | assert.throws(() => zlib.createGzip({ windowBits: 0 }), { | 28 | 25 | code: 'ERR_OUT_OF_RANGE', | 29 | 26 | name: 'RangeError', | 30 | 27 | message: 'The value of "options.windowBits" is out of range. ' + | 31 | | - 'It must be >= 9 and <= 15. Received 0' | | 28 | + 'It must be >= 9 and <= 15. Received 0' | 32 | 29 | }); | 33 | | -} | | 30 | +}); |
|