Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 1 | 'use strict'; |
Nicolas LaCasse | 40c9348 | 2012-01-20 14:36:28 | [diff] [blame] | 2 | // test uncompressing invalid input |
| 3 | |
Brendan Ashworth | 0df5430 | 2015-02-22 02:47:04 | [diff] [blame] | 4 | var common = require('../common'), |
Nicolas LaCasse | 40c9348 | 2012-01-20 14:36:28 | [diff] [blame] | 5 | assert = require('assert'), |
| 6 | zlib = require('zlib'); |
| 7 | |
| 8 | var nonStringInputs = [1, true, {a: 1}, ['a']]; |
| 9 | |
isaacs | 01d46f3 | 2012-04-01 04:01:55 | [diff] [blame] | 10 | console.error('Doing the non-strings'); |
Nicolas LaCasse | 40c9348 | 2012-01-20 14:36:28 | [diff] [blame] | 11 | nonStringInputs.forEach(function(input) { |
| 12 | // zlib.gunzip should not throw an error when called with bad input. |
isaacs | 0cdf85e | 2012-02-18 23:01:35 | [diff] [blame] | 13 | assert.doesNotThrow(function() { |
| 14 | zlib.gunzip(input, function(err, buffer) { |
Nicolas LaCasse | 40c9348 | 2012-01-20 14:36:28 | [diff] [blame] | 15 | // zlib.gunzip should pass the error to the callback. |
| 16 | assert.ok(err); |
| 17 | }); |
| 18 | }); |
| 19 | }); |
isaacs | 01d46f3 | 2012-04-01 04:01:55 | [diff] [blame] | 20 | |
| 21 | console.error('Doing the unzips'); |
| 22 | // zlib.Unzip classes need to get valid data, or else they'll throw. |
| 23 | var unzips = [ zlib.Unzip(), |
| 24 | zlib.Gunzip(), |
| 25 | zlib.Inflate(), |
| 26 | zlib.InflateRaw() ]; |
| 27 | var hadError = []; |
Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 28 | unzips.forEach(function(uz, i) { |
| 29 | console.error('Error for ' + uz.constructor.name); |
isaacs | 01d46f3 | 2012-04-01 04:01:55 | [diff] [blame] | 30 | uz.on('error', function(er) { |
| 31 | console.error('Error event', er); |
| 32 | hadError[i] = true; |
isaacs | 01d46f3 | 2012-04-01 04:01:55 | [diff] [blame] | 33 | }); |
| 34 | |
| 35 | uz.on('end', function(er) { |
Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 36 | throw new Error('end event should not be emitted ' + uz.constructor.name); |
isaacs | 01d46f3 | 2012-04-01 04:01:55 | [diff] [blame] | 37 | }); |
| 38 | |
| 39 | // this will trigger error event |
| 40 | uz.write('this is not valid compressed data.'); |
| 41 | }); |
| 42 | |
| 43 | process.on('exit', function() { |
| 44 | assert.deepEqual(hadError, [true, true, true, true], 'expect 4 errors'); |
| 45 | }); |