Skip to content
Closed
Prev Previous commit
Next Next commit
test: improve test-zlib-invalid-input
  • Loading branch information
jasnell committed Aug 3, 2017
commit c46527a11ce2fb590aabe29636695ada17465c5d
48 changes: 21 additions & 27 deletions test/parallel/test-zlib-invalid-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,39 @@
'use strict';
// test uncompressing invalid input

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

const nonStringInputs = [1, true, { a: 1 }, ['a']];
const nonStringInputs = [
1,
true,
{ a: 1 },
['a']
];

console.error('Doing the non-strings');
nonStringInputs.forEach(function(input) {
// zlib.Unzip classes need to get valid data, or else they'll throw.
const unzips = [
zlib.Unzip(),
zlib.Gunzip(),
zlib.Inflate(),
zlib.InflateRaw()
];

nonStringInputs.forEach(common.mustCall((input) => {
// zlib.gunzip should not throw an error when called with bad input.
assert.doesNotThrow(function() {
zlib.gunzip(input, function(err, buffer) {
// zlib.gunzip should pass the error to the callback.
assert.ok(err);
});
});
});

console.error('Doing the unzips');
// zlib.Unzip classes need to get valid data, or else they'll throw.
const unzips = [ zlib.Unzip(),
zlib.Gunzip(),
zlib.Inflate(),
zlib.InflateRaw() ];
const hadError = [];
unzips.forEach(function(uz, i) {
console.error(`Error for ${uz.constructor.name}`);
uz.on('error', function(er) {
console.error('Error event', er);
hadError[i] = true;
});
}, nonStringInputs.length));

uz.on('end', function(er) {
throw new Error(`end event should not be emitted ${uz.constructor.name}`);
});
unzips.forEach(common.mustCall((uz, i) => {
uz.on('error', common.mustCall());
uz.on('end', common.mustNotCall);

// this will trigger error event
uz.write('this is not valid compressed data.');
});

process.on('exit', function() {
assert.deepStrictEqual(hadError, [true, true, true, true], 'expect 4 errors');
});
}, unzips.length));