Skip to content

Commit 68cf7f0

Browse files
jasnellMylesBorins
authored andcommitted
test: improve multiple zlib tests
PR-URL: #14455 Reviewed-By: Anna Henningsen <[email protected]>
1 parent f35f06d commit 68cf7f0

7 files changed

+226
-292
lines changed

test/parallel/test-zlib-from-gzip.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ const inp = fs.createReadStream(fixture);
2121
const out = fs.createWriteStream(outputFile);
2222

2323
inp.pipe(gunzip).pipe(out);
24-
out.on('close', function() {
24+
out.on('close', common.mustCall(() => {
2525
const actual = fs.readFileSync(outputFile);
2626
assert.strictEqual(actual.length, expect.length, 'length should match');
2727
for (let i = 0, l = actual.length; i < l; i++) {
2828
assert.strictEqual(actual[i], expect[i], `byte[${i}]`);
2929
}
30-
});
30+
}));

test/parallel/test-zlib-from-string.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
// test compressing and uncompressing a string with zlib
33

4-
require('../common');
4+
const common = require('../common');
55
const assert = require('assert');
66
const zlib = require('zlib');
77

@@ -33,32 +33,32 @@ const expectedBase64Gzip = 'H4sIAAAAAAAAA11RS05DMQy8yhzg6d2BPSAkJPZu4laWkjiN4' +
3333
'mHo33kJO8xfkckmLjE5XMKBQ4gxIsfvCZ44doUThF2mcZq8q2' +
3434
'sHnHNzRtagj5AQAA';
3535

36-
zlib.deflate(inputString, function(err, buffer) {
36+
zlib.deflate(inputString, common.mustCall((err, buffer) => {
3737
assert.strictEqual(buffer.toString('base64'), expectedBase64Deflate,
3838
'deflate encoded string should match');
39-
});
39+
}));
4040

41-
zlib.gzip(inputString, function(err, buffer) {
41+
zlib.gzip(inputString, common.mustCall((err, buffer) => {
4242
// Can't actually guarantee that we'll get exactly the same
4343
// deflated bytes when we compress a string, since the header
4444
// depends on stuff other than the input string itself.
4545
// However, decrypting it should definitely yield the same
4646
// result that we're expecting, and this should match what we get
4747
// from inflating the known valid deflate data.
48-
zlib.gunzip(buffer, function(err, gunzipped) {
48+
zlib.gunzip(buffer, common.mustCall((err, gunzipped) => {
4949
assert.strictEqual(gunzipped.toString(), inputString,
5050
'Should get original string after gzip/gunzip');
51-
});
52-
});
51+
}));
52+
}));
5353

5454
let buffer = Buffer.from(expectedBase64Deflate, 'base64');
55-
zlib.unzip(buffer, function(err, buffer) {
55+
zlib.unzip(buffer, common.mustCall((err, buffer) => {
5656
assert.strictEqual(buffer.toString(), inputString,
5757
'decoded inflated string should match');
58-
});
58+
}));
5959

6060
buffer = Buffer.from(expectedBase64Gzip, 'base64');
61-
zlib.unzip(buffer, function(err, buffer) {
61+
zlib.unzip(buffer, common.mustCall((err, buffer) => {
6262
assert.strictEqual(buffer.toString(), inputString,
6363
'decoded gunzipped string should match');
64-
});
64+
}));
+21-27
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,39 @@
11
'use strict';
22
// test uncompressing invalid input
33

4-
require('../common');
4+
const common = require('../common');
55
const assert = require('assert');
66
const zlib = require('zlib');
77

8-
const nonStringInputs = [1, true, {a: 1}, ['a']];
8+
const nonStringInputs = [
9+
1,
10+
true,
11+
{ a: 1 },
12+
['a']
13+
];
914

10-
console.error('Doing the non-strings');
11-
nonStringInputs.forEach(function(input) {
15+
// zlib.Unzip classes need to get valid data, or else they'll throw.
16+
const unzips = [
17+
zlib.Unzip(),
18+
zlib.Gunzip(),
19+
zlib.Inflate(),
20+
zlib.InflateRaw()
21+
];
22+
23+
nonStringInputs.forEach(common.mustCall((input) => {
1224
// zlib.gunzip should not throw an error when called with bad input.
1325
assert.doesNotThrow(function() {
1426
zlib.gunzip(input, function(err, buffer) {
1527
// zlib.gunzip should pass the error to the callback.
1628
assert.ok(err);
1729
});
1830
});
19-
});
31+
}, nonStringInputs.length));
2032

21-
console.error('Doing the unzips');
22-
// zlib.Unzip classes need to get valid data, or else they'll throw.
23-
const unzips = [ zlib.Unzip(),
24-
zlib.Gunzip(),
25-
zlib.Inflate(),
26-
zlib.InflateRaw() ];
27-
const hadError = [];
28-
unzips.forEach(function(uz, i) {
29-
console.error(`Error for ${uz.constructor.name}`);
30-
uz.on('error', function(er) {
31-
console.error('Error event', er);
32-
hadError[i] = true;
33-
});
34-
35-
uz.on('end', function(er) {
36-
throw new Error(`end event should not be emitted ${uz.constructor.name}`);
37-
});
33+
unzips.forEach(common.mustCall((uz, i) => {
34+
uz.on('error', common.mustCall());
35+
uz.on('end', common.mustNotCall);
3836

3937
// this will trigger error event
4038
uz.write('this is not valid compressed data.');
41-
});
42-
43-
process.on('exit', function() {
44-
assert.deepStrictEqual(hadError, [true, true, true, true], 'expect 4 errors');
45-
});
39+
}, unzips.length));

test/parallel/test-zlib-random-byte-pipes.js

+85-107
Original file line numberDiff line numberDiff line change
@@ -6,124 +6,119 @@ if (!common.hasCrypto)
66
const assert = require('assert');
77
const crypto = require('crypto');
88
const stream = require('stream');
9-
const util = require('util');
109
const zlib = require('zlib');
1110

1211
const Stream = stream.Stream;
1312

1413
// emit random bytes, and keep a shasum
15-
function RandomReadStream(opt) {
16-
Stream.call(this);
14+
class RandomReadStream extends Stream {
15+
constructor(opt) {
16+
super();
1717

18-
this.readable = true;
19-
this._paused = false;
20-
this._processing = false;
21-
22-
this._hasher = crypto.createHash('sha1');
23-
opt = opt || {};
24-
25-
// base block size.
26-
opt.block = opt.block || 256 * 1024;
18+
this.readable = true;
19+
this._paused = false;
20+
this._processing = false;
2721

28-
// total number of bytes to emit
29-
opt.total = opt.total || 256 * 1024 * 1024;
30-
this._remaining = opt.total;
22+
this._hasher = crypto.createHash('sha1');
23+
opt = opt || {};
3124

32-
// how variable to make the block sizes
33-
opt.jitter = opt.jitter || 1024;
25+
// base block size.
26+
opt.block = opt.block || 256 * 1024;
3427

35-
this._opt = opt;
28+
// total number of bytes to emit
29+
opt.total = opt.total || 256 * 1024 * 1024;
30+
this._remaining = opt.total;
3631

37-
this._process = this._process.bind(this);
32+
// how variable to make the block sizes
33+
opt.jitter = opt.jitter || 1024;
3834

39-
process.nextTick(this._process);
40-
}
35+
this._opt = opt;
4136

42-
util.inherits(RandomReadStream, Stream);
37+
this._process = this._process.bind(this);
4338

44-
RandomReadStream.prototype.pause = function() {
45-
this._paused = true;
46-
this.emit('pause');
47-
};
39+
process.nextTick(this._process);
40+
}
4841

49-
RandomReadStream.prototype.resume = function() {
50-
// console.error("rrs resume");
51-
this._paused = false;
52-
this.emit('resume');
53-
this._process();
54-
};
42+
pause() {
43+
this._paused = true;
44+
this.emit('pause');
45+
}
5546

56-
RandomReadStream.prototype._process = function() {
57-
if (this._processing) return;
58-
if (this._paused) return;
47+
resume() {
48+
// console.error("rrs resume");
49+
this._paused = false;
50+
this.emit('resume');
51+
this._process();
52+
}
5953

60-
this._processing = true;
54+
_process() {
55+
if (this._processing) return;
56+
if (this._paused) return;
6157

62-
if (!this._remaining) {
63-
this._hash = this._hasher.digest('hex').toLowerCase().trim();
64-
this._processing = false;
58+
this._processing = true;
6559

66-
this.emit('end');
67-
return;
68-
}
60+
if (!this._remaining) {
61+
this._hash = this._hasher.digest('hex').toLowerCase().trim();
62+
this._processing = false;
6963

70-
// figure out how many bytes to output
71-
// if finished, then just emit end.
72-
let block = this._opt.block;
73-
const jitter = this._opt.jitter;
74-
if (jitter) {
75-
block += Math.ceil(Math.random() * jitter - (jitter / 2));
76-
}
77-
block = Math.min(block, this._remaining);
78-
const buf = Buffer.allocUnsafe(block);
79-
for (let i = 0; i < block; i++) {
80-
buf[i] = Math.random() * 256;
81-
}
64+
this.emit('end');
65+
return;
66+
}
8267

83-
this._hasher.update(buf);
68+
// figure out how many bytes to output
69+
// if finished, then just emit end.
70+
let block = this._opt.block;
71+
const jitter = this._opt.jitter;
72+
if (jitter) {
73+
block += Math.ceil(Math.random() * jitter - (jitter / 2));
74+
}
75+
block = Math.min(block, this._remaining);
76+
const buf = Buffer.allocUnsafe(block);
77+
for (let i = 0; i < block; i++) {
78+
buf[i] = Math.random() * 256;
79+
}
8480

85-
this._remaining -= block;
81+
this._hasher.update(buf);
8682

87-
console.error('block=%d\nremain=%d\n', block, this._remaining);
88-
this._processing = false;
83+
this._remaining -= block;
8984

90-
this.emit('data', buf);
91-
process.nextTick(this._process);
92-
};
85+
this._processing = false;
9386

87+
this.emit('data', buf);
88+
process.nextTick(this._process);
89+
}
90+
}
9491

9592
// a filter that just verifies a shasum
96-
function HashStream() {
97-
Stream.call(this);
93+
class HashStream extends Stream {
94+
constructor() {
95+
super();
96+
this.readable = this.writable = true;
97+
this._hasher = crypto.createHash('sha1');
98+
}
9899

99-
this.readable = this.writable = true;
100-
this._hasher = crypto.createHash('sha1');
101-
}
100+
write(c) {
101+
// Simulate the way that an fs.ReadStream returns false
102+
// on *every* write, only to resume a moment later.
103+
this._hasher.update(c);
104+
process.nextTick(() => this.resume());
105+
return false;
106+
}
107+
108+
resume() {
109+
this.emit('resume');
110+
process.nextTick(() => this.emit('drain'));
111+
}
102112

103-
util.inherits(HashStream, Stream);
104-
105-
HashStream.prototype.write = function(c) {
106-
// Simulate the way that an fs.ReadStream returns false
107-
// on *every* write like a jerk, only to resume a
108-
// moment later.
109-
this._hasher.update(c);
110-
process.nextTick(this.resume.bind(this));
111-
return false;
112-
};
113-
114-
HashStream.prototype.resume = function() {
115-
this.emit('resume');
116-
process.nextTick(this.emit.bind(this, 'drain'));
117-
};
118-
119-
HashStream.prototype.end = function(c) {
120-
if (c) {
121-
this.write(c);
113+
end(c) {
114+
if (c) {
115+
this.write(c);
116+
}
117+
this._hash = this._hasher.digest('hex').toLowerCase().trim();
118+
this.emit('data', this._hash);
119+
this.emit('end');
122120
}
123-
this._hash = this._hasher.digest('hex').toLowerCase().trim();
124-
this.emit('data', this._hash);
125-
this.emit('end');
126-
};
121+
}
127122

128123

129124
const inp = new RandomReadStream({ total: 1024, block: 256, jitter: 16 });
@@ -133,23 +128,6 @@ const gunz = zlib.createGunzip();
133128

134129
inp.pipe(gzip).pipe(gunz).pipe(out);
135130

136-
inp.on('data', function(c) {
137-
console.error('inp data', c.length);
138-
});
139-
140-
gzip.on('data', function(c) {
141-
console.error('gzip data', c.length);
142-
});
143-
144-
gunz.on('data', function(c) {
145-
console.error('gunz data', c.length);
146-
});
147-
148-
out.on('data', function(c) {
149-
console.error('out data', c.length);
150-
});
151-
152-
out.on('data', common.mustCall(function(c) {
153-
console.error('hash=%s', c);
131+
out.on('data', common.mustCall((c) => {
154132
assert.strictEqual(c, inp._hash, 'hashes should match');
155133
}));

test/parallel/test-zlib-sync-no-event.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const zlib = require('zlib');
44
const assert = require('assert');
55

6-
const shouldNotBeCalled = () => { throw new Error('unexpected event'); };
7-
86
const message = 'Come on, Fhqwhgads.';
7+
const buffer = Buffer.from(message);
98

109
const zipper = new zlib.Gzip();
11-
zipper.on('close', shouldNotBeCalled);
10+
zipper.on('close', common.mustNotCall);
1211

13-
const buffer = Buffer.from(message);
1412
const zipped = zipper._processChunk(buffer, zlib.Z_FINISH);
1513

1614
const unzipper = new zlib.Gunzip();
17-
unzipper.on('close', shouldNotBeCalled);
15+
unzipper.on('close', common.mustNotCall);
1816

1917
const unzipped = unzipper._processChunk(zipped, zlib.Z_FINISH);
2018
assert.notStrictEqual(zipped.toString(), message);

0 commit comments

Comments
 (0)