Skip to content

Commit

Permalink
test: improve zlib tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Nov 4, 2024
1 parent 2a96549 commit 8b49cae
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 212 deletions.
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)
}
);
});
});
28 changes: 18 additions & 10 deletions test/parallel/test-zlib-object-write.js
Original file line number Diff line number Diff line change
@@ -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;
});
Loading

0 comments on commit 8b49cae

Please sign in to comment.