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