blob: e1e81bacc1a499d2428d6bd9a7296c8f3052be7c [file] [log] [blame]
Roman Reissf29762f2015-05-19 11:00:061'use strict';
Nicolas LaCasse40c93482012-01-20 14:36:282// test uncompressing invalid input
3
Brendan Ashworth0df54302015-02-22 02:47:044var common = require('../common'),
Nicolas LaCasse40c93482012-01-20 14:36:285 assert = require('assert'),
6 zlib = require('zlib');
7
8var nonStringInputs = [1, true, {a: 1}, ['a']];
9
isaacs01d46f32012-04-01 04:01:5510console.error('Doing the non-strings');
Nicolas LaCasse40c93482012-01-20 14:36:2811nonStringInputs.forEach(function(input) {
12 // zlib.gunzip should not throw an error when called with bad input.
isaacs0cdf85e2012-02-18 23:01:3513 assert.doesNotThrow(function() {
14 zlib.gunzip(input, function(err, buffer) {
Nicolas LaCasse40c93482012-01-20 14:36:2815 // zlib.gunzip should pass the error to the callback.
16 assert.ok(err);
17 });
18 });
19});
isaacs01d46f32012-04-01 04:01:5520
21console.error('Doing the unzips');
22// zlib.Unzip classes need to get valid data, or else they'll throw.
23var unzips = [ zlib.Unzip(),
24 zlib.Gunzip(),
25 zlib.Inflate(),
26 zlib.InflateRaw() ];
27var hadError = [];
Roman Reissf29762f2015-05-19 11:00:0628unzips.forEach(function(uz, i) {
29 console.error('Error for ' + uz.constructor.name);
isaacs01d46f32012-04-01 04:01:5530 uz.on('error', function(er) {
31 console.error('Error event', er);
32 hadError[i] = true;
isaacs01d46f32012-04-01 04:01:5533 });
34
35 uz.on('end', function(er) {
Roman Reissf29762f2015-05-19 11:00:0636 throw new Error('end event should not be emitted ' + uz.constructor.name);
isaacs01d46f32012-04-01 04:01:5537 });
38
39 // this will trigger error event
40 uz.write('this is not valid compressed data.');
41});
42
43process.on('exit', function() {
44 assert.deepEqual(hadError, [true, true, true, true], 'expect 4 errors');
45});