test-zlib-from-string.js 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // test compressing and uncompressing a string with zlib
  22. var tape = require('tape');
  23. var zlib = require('../');
  24. var inputString = '\u03A9\u03A9Lorem ipsum dolor sit amet, consectetur adipiscing el' +
  25. 'it. Morbi faucibus, purus at gravida dictum, libero arcu convallis la' +
  26. 'cus, in commodo libero metus eu nisi. Nullam commodo, neque nec porta' +
  27. ' placerat, nisi est fermentum augue, vitae gravida tellus sapien sit ' +
  28. 'amet tellus. Aenean non diam orci. Proin quis elit turpis. Suspendiss' +
  29. 'e non diam ipsum. Suspendisse nec ullamcorper odio. Vestibulum arcu m' +
  30. 'i, sodales non suscipit id, ultrices ut massa. Sed ac sem sit amet ar' +
  31. 'cu malesuada fermentum. Nunc sed. ';
  32. var expectedBase64Deflate = 'eJxdUUtOQzEMvMoc4OndgT0gJCT2buJWlpI4jePeqZfpm' +
  33. 'XAKLRKbLOzx/HK73q6vOrhCunlF1qIDJhNUeW5I2ozT5OkDlKWLJWkncJG5403HQXAkT3' +
  34. 'Jw29B9uIEmToMukglZ0vS6ociBh4JG8sV4oVLEUCitK2kxq1WzPnChHDzsaGKy491Lofo' +
  35. 'AbWh8do43oeuYhB5EPCjcLjzYJo48KrfQBvnJecNFJvHT1+RSQsGoC7dn2t/xjhduTA1N' +
  36. 'WyQIZR0pbHwMDatnD+crPqKSqGPHp1vnlsWM/07ubf7bheF7kqSj84Bm0R1fYTfaK8vqq' +
  37. 'qfKBtNMhe3OZh6N95CTvMX5HJJi4xOVzCgUOIMSLH7wmeOHaFE4RdpnGavKtrB5xzfO/Ll9';
  38. var expectedBase64Gzip = 'H4sIAAAAAAAAA11RS05DMQy8yhzg6d2BPSAkJPZu4laWkjiN' +
  39. '496pl+mZcAotEpss7PH8crverq86uEK6eUXWogMmE1R5bkjajNPk6QOUpYslaSdwkbnjT' +
  40. 'cdBcCRPcnDb0H24gSZOgy6SCVnS9LqhyIGHgkbyxXihUsRQKK0raTGrVbM+cKEcPOxoYr' +
  41. 'Lj3Uuh+gBtaHx2jjeh65iEHkQ8KNwuPNgmjjwqt9AG+cl5w0Um8dPX5FJCwagLt2fa3/G' +
  42. 'OF25MDU1bJAhlHSlsfAwNq2cP5ys+opKoY8enW+eWxYz/Tu5t/tuF4XuSpKPzgGbRHV9h' +
  43. 'N9ory+qqp8oG00yF7c5mHo33kJO8xfkckmLjE5XMKBQ4gxIsfvCZ44doUThF2mcZq8q2s' +
  44. 'HnHNzRtagj5AQAA';
  45. tape('deflate', function(t) {
  46. t.plan(1);
  47. zlib.deflate(inputString, function(err, buffer) {
  48. t.equal(buffer.toString('base64'), expectedBase64Deflate,
  49. 'deflate encoded string should match');
  50. });
  51. });
  52. tape('gzip', function(t) {
  53. t.plan(1);
  54. zlib.gzip(inputString, function(err, buffer) {
  55. // Can't actually guarantee that we'll get exactly the same
  56. // deflated bytes when we compress a string, since the header
  57. // depends on stuff other than the input string itself.
  58. // However, decrypting it should definitely yield the same
  59. // result that we're expecting, and this should match what we get
  60. // from inflating the known valid deflate data.
  61. zlib.gunzip(buffer, function(err, gunzipped) {
  62. t.equal(gunzipped.toString(), inputString,
  63. 'Should get original string after gzip/gunzip');
  64. });
  65. });
  66. });
  67. tape('unzip deflate', function(t) {
  68. t.plan(1);
  69. var buffer = new Buffer(expectedBase64Deflate, 'base64');
  70. zlib.unzip(buffer, function(err, buffer) {
  71. t.equal(buffer.toString(), inputString,
  72. 'decoded inflated string should match');
  73. });
  74. });
  75. tape('unzip gzip', function(t) {
  76. t.plan(1);
  77. buffer = new Buffer(expectedBase64Gzip, 'base64');
  78. zlib.unzip(buffer, function(err, buffer) {
  79. t.equal(buffer.toString(), inputString,
  80. 'decoded gunzipped string should match');
  81. });
  82. });