1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23// Test compressing and uncompressing a string with zlib 24 25const common = require('../common'); 26const assert = require('assert'); 27const zlib = require('zlib'); 28 29const inputString = 'ΩΩLorem ipsum dolor sit amet, consectetur adipiscing eli' + 30 't. Morbi faucibus, purus at gravida dictum, libero arcu ' + 31 'convallis lacus, in commodo libero metus eu nisi. Nullam' + 32 ' commodo, neque nec porta placerat, nisi est fermentum a' + 33 'ugue, vitae gravida tellus sapien sit amet tellus. Aenea' + 34 'n non diam orci. Proin quis elit turpis. Suspendisse non' + 35 ' diam ipsum. Suspendisse nec ullamcorper odio. Vestibulu' + 36 'm arcu mi, sodales non suscipit id, ultrices ut massa. S' + 37 'ed ac sem sit amet arcu malesuada fermentum. Nunc sed. '; 38const expectedBase64Deflate = 'eJxdUUtOQzEMvMoc4OndgT0gJCT2buJWlpI4jePeqZfpmX' + 39 'AKLRKbLOzx/HK73q6vOrhCunlF1qIDJhNUeW5I2ozT5OkD' + 40 'lKWLJWkncJG5403HQXAkT3Jw29B9uIEmToMukglZ0vS6oc' + 41 'iBh4JG8sV4oVLEUCitK2kxq1WzPnChHDzsaGKy491LofoA' + 42 'bWh8do43oeuYhB5EPCjcLjzYJo48KrfQBvnJecNFJvHT1+' + 43 'RSQsGoC7dn2t/xjhduTA1NWyQIZR0pbHwMDatnD+crPqKS' + 44 'qGPHp1vnlsWM/07ubf7bheF7kqSj84Bm0R1fYTfaK8vqqq' + 45 'fKBtNMhe3OZh6N95CTvMX5HJJi4xOVzCgUOIMSLH7wmeOH' + 46 'aFE4RdpnGavKtrB5xzfO/Ll9'; 47const expectedBase64Gzip = 'H4sIAAAAAAAAA11RS05DMQy8yhzg6d2BPSAkJPZu4laWkjiN4' + 48 '96pl+mZcAotEpss7PH8crverq86uEK6eUXWogMmE1R5bkjajN' + 49 'Pk6QOUpYslaSdwkbnjTcdBcCRPcnDb0H24gSZOgy6SCVnS9Lq' + 50 'hyIGHgkbyxXihUsRQKK0raTGrVbM+cKEcPOxoYrLj3Uuh+gBt' + 51 'aHx2jjeh65iEHkQ8KNwuPNgmjjwqt9AG+cl5w0Um8dPX5FJCw' + 52 'agLt2fa3/GOF25MDU1bJAhlHSlsfAwNq2cP5ys+opKoY8enW+' + 53 'eWxYz/Tu5t/tuF4XuSpKPzgGbRHV9hN9ory+qqp8oG00yF7c5' + 54 'mHo33kJO8xfkckmLjE5XMKBQ4gxIsfvCZ44doUThF2mcZq8q2' + 55 'sHnHNzRtagj5AQAA'; 56 57zlib.deflate(inputString, common.mustCall((err, buffer) => { 58 assert.strictEqual(buffer.toString('base64'), expectedBase64Deflate); 59})); 60 61zlib.gzip(inputString, common.mustCall((err, buffer) => { 62 // Can't actually guarantee that we'll get exactly the same 63 // deflated bytes when we compress a string, since the header 64 // depends on stuff other than the input string itself. 65 // However, decrypting it should definitely yield the same 66 // result that we're expecting, and this should match what we get 67 // from inflating the known valid deflate data. 68 zlib.gunzip(buffer, common.mustCall((err, gunzipped) => { 69 assert.strictEqual(gunzipped.toString(), inputString); 70 })); 71})); 72 73let buffer = Buffer.from(expectedBase64Deflate, 'base64'); 74zlib.unzip(buffer, common.mustCall((err, buffer) => { 75 assert.strictEqual(buffer.toString(), inputString); 76})); 77 78buffer = Buffer.from(expectedBase64Gzip, 'base64'); 79zlib.unzip(buffer, common.mustCall((err, buffer) => { 80 assert.strictEqual(buffer.toString(), inputString); 81})); 82