11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Tests zlib streams with truncated compressed input
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_cirequire('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst zlib = require('zlib');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst inputString = 'ΩΩLorem ipsum dolor sit amet, consectetur adipiscing eli' +
91cb0ef41Sopenharmony_ci                    't. Morbi faucibus, purus at gravida dictum, libero arcu ' +
101cb0ef41Sopenharmony_ci                    'convallis lacus, in commodo libero metus eu nisi. Nullam' +
111cb0ef41Sopenharmony_ci                    ' commodo, neque nec porta placerat, nisi est fermentum a' +
121cb0ef41Sopenharmony_ci                    'ugue, vitae gravida tellus sapien sit amet tellus. Aenea' +
131cb0ef41Sopenharmony_ci                    'n non diam orci. Proin quis elit turpis. Suspendisse non' +
141cb0ef41Sopenharmony_ci                    ' diam ipsum. Suspendisse nec ullamcorper odio. Vestibulu' +
151cb0ef41Sopenharmony_ci                    'm arcu mi, sodales non suscipit id, ultrices ut massa. S' +
161cb0ef41Sopenharmony_ci                    'ed ac sem sit amet arcu malesuada fermentum. Nunc sed. ';
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst errMessage = /unexpected end of file/;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci[
211cb0ef41Sopenharmony_ci  { comp: 'gzip', decomp: 'gunzip', decompSync: 'gunzipSync' },
221cb0ef41Sopenharmony_ci  { comp: 'gzip', decomp: 'unzip', decompSync: 'unzipSync' },
231cb0ef41Sopenharmony_ci  { comp: 'deflate', decomp: 'inflate', decompSync: 'inflateSync' },
241cb0ef41Sopenharmony_ci  { comp: 'deflateRaw', decomp: 'inflateRaw', decompSync: 'inflateRawSync' },
251cb0ef41Sopenharmony_ci].forEach(function(methods) {
261cb0ef41Sopenharmony_ci  zlib[methods.comp](inputString, function(err, compressed) {
271cb0ef41Sopenharmony_ci    assert.ifError(err);
281cb0ef41Sopenharmony_ci    const truncated = compressed.slice(0, compressed.length / 2);
291cb0ef41Sopenharmony_ci    const toUTF8 = (buffer) => buffer.toString('utf-8');
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci    // sync sanity
321cb0ef41Sopenharmony_ci    const decompressed = zlib[methods.decompSync](compressed);
331cb0ef41Sopenharmony_ci    assert.strictEqual(toUTF8(decompressed), inputString);
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci    // async sanity
361cb0ef41Sopenharmony_ci    zlib[methods.decomp](compressed, function(err, result) {
371cb0ef41Sopenharmony_ci      assert.ifError(err);
381cb0ef41Sopenharmony_ci      assert.strictEqual(toUTF8(result), inputString);
391cb0ef41Sopenharmony_ci    });
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci    // Sync truncated input test
421cb0ef41Sopenharmony_ci    assert.throws(function() {
431cb0ef41Sopenharmony_ci      zlib[methods.decompSync](truncated);
441cb0ef41Sopenharmony_ci    }, errMessage);
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci    // Async truncated input test
471cb0ef41Sopenharmony_ci    zlib[methods.decomp](truncated, function(err, result) {
481cb0ef41Sopenharmony_ci      assert.match(err.message, errMessage);
491cb0ef41Sopenharmony_ci    });
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci    const syncFlushOpt = { finishFlush: zlib.constants.Z_SYNC_FLUSH };
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci    // Sync truncated input test, finishFlush = Z_SYNC_FLUSH
541cb0ef41Sopenharmony_ci    const result = toUTF8(zlib[methods.decompSync](truncated, syncFlushOpt));
551cb0ef41Sopenharmony_ci    assert.strictEqual(result, inputString.substr(0, result.length));
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci    // Async truncated input test, finishFlush = Z_SYNC_FLUSH
581cb0ef41Sopenharmony_ci    zlib[methods.decomp](truncated, syncFlushOpt, function(err, decompressed) {
591cb0ef41Sopenharmony_ci      assert.ifError(err);
601cb0ef41Sopenharmony_ci      const result = toUTF8(decompressed);
611cb0ef41Sopenharmony_ci      assert.strictEqual(result, inputString.substr(0, result.length));
621cb0ef41Sopenharmony_ci    });
631cb0ef41Sopenharmony_ci  });
641cb0ef41Sopenharmony_ci});
65