Skip to content

Commit c76ec71

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

8 files changed

+231
-295
lines changed

test/parallel/test-zlib-from-gzip-with-trailing-garbage.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ assert.throws(
3838
);
3939

4040
zlib.gunzip(data, common.mustCall((err, result) => {
41-
assert(err instanceof Error);
42-
assert.strictEqual(err.code, 'Z_DATA_ERROR');
43-
assert.strictEqual(err.message, 'unknown compression method');
41+
common.expectsError({
42+
code: 'Z_DATA_ERROR',
43+
type: Error,
44+
message: 'unknown compression method'
45+
})(err);
4446
assert.strictEqual(result, undefined);
4547
}));
4648

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

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

4444
inp.pipe(gunzip).pipe(out);
45-
out.on('close', function() {
45+
out.on('close', common.mustCall(() => {
4646
const actual = fs.readFileSync(outputFile);
4747
assert.strictEqual(actual.length, expect.length, 'length should match');
4848
for (let i = 0, l = actual.length; i < l; i++) {
4949
assert.strictEqual(actual[i], expect[i], `byte[${i}]`);
5050
}
51-
});
51+
}));

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

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

25-
require('../common');
25+
const common = require('../common');
2626
const assert = require('assert');
2727
const zlib = require('zlib');
2828

@@ -54,32 +54,32 @@ const expectedBase64Gzip = 'H4sIAAAAAAAAA11RS05DMQy8yhzg6d2BPSAkJPZu4laWkjiN4' +
5454
'mHo33kJO8xfkckmLjE5XMKBQ4gxIsfvCZ44doUThF2mcZq8q2' +
5555
'sHnHNzRtagj5AQAA';
5656

57-
zlib.deflate(inputString, function(err, buffer) {
57+
zlib.deflate(inputString, common.mustCall((err, buffer) => {
5858
assert.strictEqual(buffer.toString('base64'), expectedBase64Deflate,
5959
'deflate encoded string should match');
60-
});
60+
}));
6161

62-
zlib.gzip(inputString, function(err, buffer) {
62+
zlib.gzip(inputString, common.mustCall((err, buffer) => {
6363
// Can't actually guarantee that we'll get exactly the same
6464
// deflated bytes when we compress a string, since the header
6565
// depends on stuff other than the input string itself.
6666
// However, decrypting it should definitely yield the same
6767
// result that we're expecting, and this should match what we get
6868
// from inflating the known valid deflate data.
69-
zlib.gunzip(buffer, function(err, gunzipped) {
69+
zlib.gunzip(buffer, common.mustCall((err, gunzipped) => {
7070
assert.strictEqual(gunzipped.toString(), inputString,
7171
'Should get original string after gzip/gunzip');
72-
});
73-
});
72+
}));
73+
}));
7474

7575
let buffer = Buffer.from(expectedBase64Deflate, 'base64');
76-
zlib.unzip(buffer, function(err, buffer) {
76+
zlib.unzip(buffer, common.mustCall((err, buffer) => {
7777
assert.strictEqual(buffer.toString(), inputString,
7878
'decoded inflated string should match');
79-
});
79+
}));
8080

8181
buffer = Buffer.from(expectedBase64Gzip, 'base64');
82-
zlib.unzip(buffer, function(err, buffer) {
82+
zlib.unzip(buffer, common.mustCall((err, buffer) => {
8383
assert.strictEqual(buffer.toString(), inputString,
8484
'decoded gunzipped string should match');
85-
});
85+
}));

test/parallel/test-zlib-invalid-input.js

+21-27
Original file line numberDiff line numberDiff line change
@@ -22,45 +22,39 @@
2222
'use strict';
2323
// test uncompressing invalid input
2424

25-
require('../common');
25+
const common = require('../common');
2626
const assert = require('assert');
2727
const zlib = require('zlib');
2828

29-
const nonStringInputs = [1, true, {a: 1}, ['a']];
29+
const nonStringInputs = [
30+
1,
31+
true,
32+
{ a: 1 },
33+
['a']
34+
];
3035

31-
console.error('Doing the non-strings');
32-
nonStringInputs.forEach(function(input) {
36+
// zlib.Unzip classes need to get valid data, or else they'll throw.
37+
const unzips = [
38+
zlib.Unzip(),
39+
zlib.Gunzip(),
40+
zlib.Inflate(),
41+
zlib.InflateRaw()
42+
];
43+
44+
nonStringInputs.forEach(common.mustCall((input) => {
3345
// zlib.gunzip should not throw an error when called with bad input.
3446
assert.doesNotThrow(function() {
3547
zlib.gunzip(input, function(err, buffer) {
3648
// zlib.gunzip should pass the error to the callback.
3749
assert.ok(err);
3850
});
3951
});
40-
});
41-
42-
console.error('Doing the unzips');
43-
// zlib.Unzip classes need to get valid data, or else they'll throw.
44-
const unzips = [ zlib.Unzip(),
45-
zlib.Gunzip(),
46-
zlib.Inflate(),
47-
zlib.InflateRaw() ];
48-
const hadError = [];
49-
unzips.forEach(function(uz, i) {
50-
console.error(`Error for ${uz.constructor.name}`);
51-
uz.on('error', function(er) {
52-
console.error('Error event', er);
53-
hadError[i] = true;
54-
});
52+
}, nonStringInputs.length));
5553

56-
uz.on('end', function(er) {
57-
throw new Error(`end event should not be emitted ${uz.constructor.name}`);
58-
});
54+
unzips.forEach(common.mustCall((uz, i) => {
55+
uz.on('error', common.mustCall());
56+
uz.on('end', common.mustNotCall);
5957

6058
// this will trigger error event
6159
uz.write('this is not valid compressed data.');
62-
});
63-
64-
process.on('exit', function() {
65-
assert.deepStrictEqual(hadError, [true, true, true, true], 'expect 4 errors');
66-
});
60+
}, unzips.length));

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

+85-107
Original file line numberDiff line numberDiff line change
@@ -27,124 +27,119 @@ if (!common.hasCrypto)
2727
const assert = require('assert');
2828
const crypto = require('crypto');
2929
const stream = require('stream');
30-
const util = require('util');
3130
const zlib = require('zlib');
3231

3332
const Stream = stream.Stream;
3433

3534
// emit random bytes, and keep a shasum
36-
function RandomReadStream(opt) {
37-
Stream.call(this);
35+
class RandomReadStream extends Stream {
36+
constructor(opt) {
37+
super();
3838

39-
this.readable = true;
40-
this._paused = false;
41-
this._processing = false;
42-
43-
this._hasher = crypto.createHash('sha1');
44-
opt = opt || {};
45-
46-
// base block size.
47-
opt.block = opt.block || 256 * 1024;
39+
this.readable = true;
40+
this._paused = false;
41+
this._processing = false;
4842

49-
// total number of bytes to emit
50-
opt.total = opt.total || 256 * 1024 * 1024;
51-
this._remaining = opt.total;
43+
this._hasher = crypto.createHash('sha1');
44+
opt = opt || {};
5245

53-
// how variable to make the block sizes
54-
opt.jitter = opt.jitter || 1024;
46+
// base block size.
47+
opt.block = opt.block || 256 * 1024;
5548

56-
this._opt = opt;
49+
// total number of bytes to emit
50+
opt.total = opt.total || 256 * 1024 * 1024;
51+
this._remaining = opt.total;
5752

58-
this._process = this._process.bind(this);
53+
// how variable to make the block sizes
54+
opt.jitter = opt.jitter || 1024;
5955

60-
process.nextTick(this._process);
61-
}
56+
this._opt = opt;
6257

63-
util.inherits(RandomReadStream, Stream);
58+
this._process = this._process.bind(this);
6459

65-
RandomReadStream.prototype.pause = function() {
66-
this._paused = true;
67-
this.emit('pause');
68-
};
60+
process.nextTick(this._process);
61+
}
6962

70-
RandomReadStream.prototype.resume = function() {
71-
// console.error("rrs resume");
72-
this._paused = false;
73-
this.emit('resume');
74-
this._process();
75-
};
63+
pause() {
64+
this._paused = true;
65+
this.emit('pause');
66+
}
7667

77-
RandomReadStream.prototype._process = function() {
78-
if (this._processing) return;
79-
if (this._paused) return;
68+
resume() {
69+
// console.error("rrs resume");
70+
this._paused = false;
71+
this.emit('resume');
72+
this._process();
73+
}
8074

81-
this._processing = true;
75+
_process() {
76+
if (this._processing) return;
77+
if (this._paused) return;
8278

83-
if (!this._remaining) {
84-
this._hash = this._hasher.digest('hex').toLowerCase().trim();
85-
this._processing = false;
79+
this._processing = true;
8680

87-
this.emit('end');
88-
return;
89-
}
81+
if (!this._remaining) {
82+
this._hash = this._hasher.digest('hex').toLowerCase().trim();
83+
this._processing = false;
9084

91-
// figure out how many bytes to output
92-
// if finished, then just emit end.
93-
let block = this._opt.block;
94-
const jitter = this._opt.jitter;
95-
if (jitter) {
96-
block += Math.ceil(Math.random() * jitter - (jitter / 2));
97-
}
98-
block = Math.min(block, this._remaining);
99-
const buf = Buffer.allocUnsafe(block);
100-
for (let i = 0; i < block; i++) {
101-
buf[i] = Math.random() * 256;
102-
}
85+
this.emit('end');
86+
return;
87+
}
10388

104-
this._hasher.update(buf);
89+
// figure out how many bytes to output
90+
// if finished, then just emit end.
91+
let block = this._opt.block;
92+
const jitter = this._opt.jitter;
93+
if (jitter) {
94+
block += Math.ceil(Math.random() * jitter - (jitter / 2));
95+
}
96+
block = Math.min(block, this._remaining);
97+
const buf = Buffer.allocUnsafe(block);
98+
for (let i = 0; i < block; i++) {
99+
buf[i] = Math.random() * 256;
100+
}
105101

106-
this._remaining -= block;
102+
this._hasher.update(buf);
107103

108-
console.error('block=%d\nremain=%d\n', block, this._remaining);
109-
this._processing = false;
104+
this._remaining -= block;
110105

111-
this.emit('data', buf);
112-
process.nextTick(this._process);
113-
};
106+
this._processing = false;
114107

108+
this.emit('data', buf);
109+
process.nextTick(this._process);
110+
}
111+
}
115112

116113
// a filter that just verifies a shasum
117-
function HashStream() {
118-
Stream.call(this);
114+
class HashStream extends Stream {
115+
constructor() {
116+
super();
117+
this.readable = this.writable = true;
118+
this._hasher = crypto.createHash('sha1');
119+
}
119120

120-
this.readable = this.writable = true;
121-
this._hasher = crypto.createHash('sha1');
122-
}
121+
write(c) {
122+
// Simulate the way that an fs.ReadStream returns false
123+
// on *every* write, only to resume a moment later.
124+
this._hasher.update(c);
125+
process.nextTick(() => this.resume());
126+
return false;
127+
}
128+
129+
resume() {
130+
this.emit('resume');
131+
process.nextTick(() => this.emit('drain'));
132+
}
123133

124-
util.inherits(HashStream, Stream);
125-
126-
HashStream.prototype.write = function(c) {
127-
// Simulate the way that an fs.ReadStream returns false
128-
// on *every* write like a jerk, only to resume a
129-
// moment later.
130-
this._hasher.update(c);
131-
process.nextTick(this.resume.bind(this));
132-
return false;
133-
};
134-
135-
HashStream.prototype.resume = function() {
136-
this.emit('resume');
137-
process.nextTick(this.emit.bind(this, 'drain'));
138-
};
139-
140-
HashStream.prototype.end = function(c) {
141-
if (c) {
142-
this.write(c);
134+
end(c) {
135+
if (c) {
136+
this.write(c);
137+
}
138+
this._hash = this._hasher.digest('hex').toLowerCase().trim();
139+
this.emit('data', this._hash);
140+
this.emit('end');
143141
}
144-
this._hash = this._hasher.digest('hex').toLowerCase().trim();
145-
this.emit('data', this._hash);
146-
this.emit('end');
147-
};
142+
}
148143

149144

150145
const inp = new RandomReadStream({ total: 1024, block: 256, jitter: 16 });
@@ -154,23 +149,6 @@ const gunz = zlib.createGunzip();
154149

155150
inp.pipe(gzip).pipe(gunz).pipe(out);
156151

157-
inp.on('data', function(c) {
158-
console.error('inp data', c.length);
159-
});
160-
161-
gzip.on('data', function(c) {
162-
console.error('gzip data', c.length);
163-
});
164-
165-
gunz.on('data', function(c) {
166-
console.error('gunz data', c.length);
167-
});
168-
169-
out.on('data', function(c) {
170-
console.error('out data', c.length);
171-
});
172-
173-
out.on('data', common.mustCall(function(c) {
174-
console.error('hash=%s', c);
152+
out.on('data', common.mustCall((c) => {
175153
assert.strictEqual(c, inp._hash, 'hashes should match');
176154
}));

0 commit comments

Comments
 (0)