-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: move more zlib tests to node:test
- Loading branch information
Showing
6 changed files
with
141 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
'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 assert = require('node:assert'); | ||
const zlib = require('node:zlib'); | ||
const { test } = require('node:test'); | ||
|
||
const inflate = zlib.createInflateRaw(); | ||
|
||
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,29 @@ | ||
'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' | ||
}); | ||
} | ||
}); |