Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve zlib tests #55716

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
18 changes: 11 additions & 7 deletions test/parallel/test-zlib-empty-buffer.js
Original file line number Diff line number Diff line change
@@ -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' ],
Expand All @@ -23,4 +27,4 @@ const emptyBuffer = Buffer.alloc(0);
`Expected ${inspect(compressed)} to match ${inspect(decompressed)} ` +
`to match for ${method}`);
}
})().then(common.mustCall());
});
46 changes: 0 additions & 46 deletions test/parallel/test-zlib-failed-init.js

This file was deleted.

83 changes: 55 additions & 28 deletions test/parallel/test-zlib-from-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ' +
Expand Down Expand Up @@ -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;
});
20 changes: 0 additions & 20 deletions test/parallel/test-zlib-invalid-arg-value-brotli-compress.js

This file was deleted.

125 changes: 93 additions & 32 deletions test/parallel/test-zlib-invalid-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
53 changes: 28 additions & 25 deletions test/parallel/test-zlib-not-string-or-buffer.js
Original file line number Diff line number Diff line change
@@ -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)
}
);
});
});
Loading
Loading