Skip to content
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
4 changes: 4 additions & 0 deletions test/eslint.config_partial.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ export default [
'wasm-allocation',
'wpt',
].join(',')}}/**/*.{js,mjs,cjs}`,
`test/parallel/test-{${
// 0x61 is code for 'a', this generates a string enumerating latin letters: 'z*,y*,…'
Array.from({ length: 2 }, (_, i) => String.fromCharCode(0x61 + 25 - i, 42)).join(',')
}}.{js,mjs,cjs}`,
],
rules: {
'node-core/must-call-assert': 'error',
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-zlib-brotli-16GB.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions test/parallel/test-zlib-brotli-kmaxlength-rangeerror.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');

// This test ensures that zlib throws a RangeError if the final buffer needs to
// be larger than kMaxLength and concatenation fails.
Expand All @@ -18,9 +18,9 @@ buffer.kMaxLength = oldkMaxLength;
const encoded = Buffer.from('G38A+CXCIrFAIAM=', 'base64');

// Async
zlib.brotliDecompress(encoded, function(err) {
zlib.brotliDecompress(encoded, common.mustCall((err) => {
assert.ok(err instanceof RangeError);
});
}));

// Sync
assert.throws(function() {
Expand Down
8 changes: 2 additions & 6 deletions test/parallel/test-zlib-from-concatenated-gzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ const pmmResultBuffers = [];

fs.createReadStream(pmmFileGz)
.pipe(zlib.createGunzip())
.on('error', (err) => {
assert.ifError(err);
})
.on('error', common.mustNotCall())
.on('data', (data) => pmmResultBuffers.push(data))
.on('finish', common.mustCall(() => {
// Result should match original random garbage
Expand All @@ -61,9 +59,7 @@ fs.createReadStream(pmmFileGz)
const resultBuffers = [];

const unzip = zlib.createGunzip()
.on('error', (err) => {
assert.ifError(err);
})
.on('error', common.mustNotCall())
.on('data', (data) => resultBuffers.push(data))
.on('finish', common.mustCall(() => {
assert.strictEqual(
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-zlib-kmaxlength-rangeerror.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');

// This test ensures that zlib throws a RangeError if the final buffer needs to
// be larger than kMaxLength and concatenation fails.
Expand All @@ -18,9 +18,9 @@ buffer.kMaxLength = oldkMaxLength;
const encoded = Buffer.from('H4sIAAAAAAAAA0tMHFgAAIw2K/GAAAAA', 'base64');

// Async
zlib.gunzip(encoded, function(err) {
zlib.gunzip(encoded, common.mustCall((err) => {
assert.ok(err instanceof RangeError);
});
}));

// Sync
assert.throws(function() {
Expand Down
4 changes: 1 addition & 3 deletions test/parallel/test-zlib-maxOutputLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ assert.throws(function() {
}, RangeError);

// Async
zlib.brotliDecompress(encoded, { maxOutputLength: 256 }, function(err) {
assert.strictEqual(err, null);
});
zlib.brotliDecompress(encoded, { maxOutputLength: 256 }, common.mustSucceed());

// Sync
zlib.brotliDecompressSync(encoded, { maxOutputLength: 256 });
4 changes: 1 addition & 3 deletions test/parallel/test-zlib-reset-before-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ for (const fn of [

const output = [];
inflate
.on('error', (err) => {
assert.ifError(err);
})
.on('error', common.mustNotCall())
.on('data', (chunk) => output.push(chunk))
.on('end', common.mustCall(
() => assert.strictEqual(Buffer.concat(output).toString(), 'abc')));
Expand Down
21 changes: 9 additions & 12 deletions test/parallel/test-zlib-truncated.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
// Tests zlib streams with truncated compressed input

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

Expand All @@ -23,8 +23,7 @@ const errMessage = /unexpected end of file/;
{ comp: 'deflate', decomp: 'inflate', decompSync: 'inflateSync' },
{ comp: 'deflateRaw', decomp: 'inflateRaw', decompSync: 'inflateRawSync' },
].forEach(function(methods) {
zlib[methods.comp](inputString, function(err, compressed) {
assert.ifError(err);
zlib[methods.comp](inputString, common.mustSucceed((compressed) => {
const truncated = compressed.slice(0, compressed.length / 2);
const toUTF8 = (buffer) => buffer.toString('utf-8');

Expand All @@ -33,20 +32,19 @@ const errMessage = /unexpected end of file/;
assert.strictEqual(toUTF8(decompressed), inputString);

// async sanity
zlib[methods.decomp](compressed, function(err, result) {
assert.ifError(err);
zlib[methods.decomp](compressed, common.mustSucceed((result) => {
assert.strictEqual(toUTF8(result), inputString);
});
}));

// Sync truncated input test
assert.throws(function() {
zlib[methods.decompSync](truncated);
}, errMessage);

// Async truncated input test
zlib[methods.decomp](truncated, function(err, result) {
zlib[methods.decomp](truncated, common.mustCall((err) => {
assert.match(err.message, errMessage);
});
}));

const syncFlushOpt = { finishFlush: zlib.constants.Z_SYNC_FLUSH };

Expand All @@ -55,10 +53,9 @@ const errMessage = /unexpected end of file/;
assert.strictEqual(result, inputString.slice(0, result.length));

// Async truncated input test, finishFlush = Z_SYNC_FLUSH
zlib[methods.decomp](truncated, syncFlushOpt, function(err, decompressed) {
assert.ifError(err);
zlib[methods.decomp](truncated, syncFlushOpt, common.mustSucceed((decompressed) => {
const result = toUTF8(decompressed);
assert.strictEqual(result, inputString.slice(0, result.length));
});
});
}));
}));
});
23 changes: 7 additions & 16 deletions test/parallel/test-zlib-type-error.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
'use strict';
require('../common');
const assert = require('assert').strict;
const assert = require('assert');
const test = require('node:test');
const { DecompressionStream } = require('stream/web');

async function expectTypeError(promise) {
let threw = false;
try {
await promise;
} catch (err) {
threw = true;
assert(err instanceof TypeError, `Expected TypeError, got ${err}`);
}
assert(threw, 'Expected promise to reject');
}

test('DecompressStream deflat emits error on trailing data', async () => {
const valid = new Uint8Array([120, 156, 75, 4, 0, 0, 98, 0, 98]); // deflate('a')
const empty = new Uint8Array(1);
const invalid = new Uint8Array([...valid, ...empty]);
const double = new Uint8Array([...valid, ...valid]);

for (const chunk of [[invalid], [valid, empty], [valid, valid], [valid, double]]) {
await expectTypeError(
await assert.rejects(
Array.fromAsync(
new Blob([chunk]).stream().pipeThrough(new DecompressionStream('deflate'))
)
),
{ name: 'TypeError' },
);
}
});
Expand All @@ -37,10 +27,11 @@ test('DecompressStream gzip emits error on trailing data', async () => {
const invalid = new Uint8Array([...valid, ...empty]);
const double = new Uint8Array([...valid, ...valid]);
for (const chunk of [[invalid], [valid, empty], [valid, valid], [double]]) {
await expectTypeError(
await assert.rejects(
Array.fromAsync(
new Blob([chunk]).stream().pipeThrough(new DecompressionStream('gzip'))
)
),
{ name: 'TypeError' },
);
}
});
48 changes: 20 additions & 28 deletions test/parallel/test-zlib-unzip-one-byte-chunks.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
'use strict';

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

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

test('zlib should unzip one byte chunks', async () => {
const { promise, resolve } = Promise.withResolvers();
const data = Buffer.concat([
zlib.gzipSync('abc'),
zlib.gzipSync('def'),
]);
const data = Buffer.concat([
zlib.gzipSync('abc'),
zlib.gzipSync('def'),
]);

const resultBuffers = [];
const resultBuffers = [];

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();
});
const unzip = zlib.createUnzip()
.on('error', common.mustNotCall())
.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');
}));

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

unzip.end();
await promise;
});
unzip.end();
28 changes: 10 additions & 18 deletions test/parallel/test-zlib-write-after-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,17 @@

'use strict';

require('../common');
const common = 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);
});
zlib.gzip('hello', common.mustCall(() => {
const unzip = zlib.createGunzip();
unzip.close(common.mustCall());
unzip.write('asd', common.mustCall((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');
}));
}));
52 changes: 23 additions & 29 deletions test/parallel/test-zlib-write-after-flush.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,33 @@

'use strict';

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

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

test('zlib should accept writing after flush', async () => {
for (const [ createCompress, createDecompress ] of [
[ zlib.createGzip, zlib.createGunzip ],
[ zlib.createBrotliCompress, zlib.createBrotliDecompress ],
[ zlib.createZstdCompress, zlib.createZstdDecompress ],
]) {
const { promise, resolve, reject } = Promise.withResolvers();
const gzip = createCompress();
const gunz = createDecompress();
for (const [ createCompress, createDecompress ] of [
[ zlib.createGzip, zlib.createGunzip ],
[ zlib.createBrotliCompress, zlib.createBrotliDecompress ],
[ zlib.createZstdCompress, zlib.createZstdDecompress ],
]) {
const gzip = createCompress();
const gunz = createDecompress();

gzip.pipe(gunz);
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();
});
let output = '';
const input = 'A line of data\n';
gunz.setEncoding('utf8');
gunz.on('error', common.mustNotCall());
gunz.on('data', (c) => output += c);
gunz.on('end', common.mustCall(() => {
assert.strictEqual(output, input);
}));

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