1'use strict'; 2// Tests zlib streams with truncated compressed input 3 4require('../common'); 5const assert = require('assert'); 6const zlib = require('zlib'); 7 8const inputString = 'ΩΩLorem ipsum dolor sit amet, consectetur adipiscing eli' + 9 't. Morbi faucibus, purus at gravida dictum, libero arcu ' + 10 'convallis lacus, in commodo libero metus eu nisi. Nullam' + 11 ' commodo, neque nec porta placerat, nisi est fermentum a' + 12 'ugue, vitae gravida tellus sapien sit amet tellus. Aenea' + 13 'n non diam orci. Proin quis elit turpis. Suspendisse non' + 14 ' diam ipsum. Suspendisse nec ullamcorper odio. Vestibulu' + 15 'm arcu mi, sodales non suscipit id, ultrices ut massa. S' + 16 'ed ac sem sit amet arcu malesuada fermentum. Nunc sed. '; 17 18const errMessage = /unexpected end of file/; 19 20[ 21 { comp: 'gzip', decomp: 'gunzip', decompSync: 'gunzipSync' }, 22 { comp: 'gzip', decomp: 'unzip', decompSync: 'unzipSync' }, 23 { comp: 'deflate', decomp: 'inflate', decompSync: 'inflateSync' }, 24 { comp: 'deflateRaw', decomp: 'inflateRaw', decompSync: 'inflateRawSync' }, 25].forEach(function(methods) { 26 zlib[methods.comp](inputString, function(err, compressed) { 27 assert.ifError(err); 28 const truncated = compressed.slice(0, compressed.length / 2); 29 const toUTF8 = (buffer) => buffer.toString('utf-8'); 30 31 // sync sanity 32 const decompressed = zlib[methods.decompSync](compressed); 33 assert.strictEqual(toUTF8(decompressed), inputString); 34 35 // async sanity 36 zlib[methods.decomp](compressed, function(err, result) { 37 assert.ifError(err); 38 assert.strictEqual(toUTF8(result), inputString); 39 }); 40 41 // Sync truncated input test 42 assert.throws(function() { 43 zlib[methods.decompSync](truncated); 44 }, errMessage); 45 46 // Async truncated input test 47 zlib[methods.decomp](truncated, function(err, result) { 48 assert(errMessage.test(err.message)); 49 }); 50 51 const syncFlushOpt = { finishFlush: zlib.constants.Z_SYNC_FLUSH }; 52 53 // Sync truncated input test, finishFlush = Z_SYNC_FLUSH 54 const result = toUTF8(zlib[methods.decompSync](truncated, syncFlushOpt)); 55 assert.strictEqual(result, inputString.substr(0, result.length)); 56 57 // Async truncated input test, finishFlush = Z_SYNC_FLUSH 58 zlib[methods.decomp](truncated, syncFlushOpt, function(err, decompressed) { 59 assert.ifError(err); 60 const result = toUTF8(decompressed); 61 assert.strictEqual(result, inputString.substr(0, result.length)); 62 }); 63 }); 64}); 65