Skip to content

Commit 7403aaa

Browse files
thusoyFishrock123
authored andcommitted
zlib: tighten up dictionary tests
Uses const where possible, removes inaccurate comments, prefers strictEqual where possible, ensures functions with assertions are called and enures the inflater has correct encoding set. PR-URL: #8512 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 1547495 commit 7403aaa

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

test/parallel/test-zlib-dictionary-fail.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,29 @@ const common = require('../common');
33
const assert = require('assert');
44
const zlib = require('zlib');
55

6-
// Should raise an error, not trigger an assertion in src/node_zlib.cc
6+
// String "test" encoded with dictionary "dict".
7+
const input = Buffer.from([0x78, 0xBB, 0x04, 0x09, 0x01, 0xA5]);
8+
79
{
810
const stream = zlib.createInflate();
911

1012
stream.on('error', common.mustCall(function(err) {
1113
assert(/Missing dictionary/.test(err.message));
1214
}));
1315

14-
// String "test" encoded with dictionary "dict".
15-
stream.write(Buffer.from([0x78, 0xBB, 0x04, 0x09, 0x01, 0xA5]));
16+
stream.write(input);
1617
}
1718

18-
// Should raise an error, not trigger an assertion in src/node_zlib.cc
1919
{
2020
const stream = zlib.createInflate({ dictionary: Buffer.from('fail') });
2121

2222
stream.on('error', common.mustCall(function(err) {
2323
assert(/Bad dictionary/.test(err.message));
2424
}));
2525

26-
// String "test" encoded with dictionary "dict".
27-
stream.write(Buffer.from([0x78, 0xBB, 0x04, 0x09, 0x01, 0xA5]));
26+
stream.write(input);
2827
}
2928

30-
// Should raise an error, not trigger an assertion in src/node_zlib.cc
3129
{
3230
const stream = zlib.createInflateRaw({ dictionary: Buffer.from('fail') });
3331

@@ -37,6 +35,5 @@ const zlib = require('zlib');
3735
assert(/invalid/.test(err.message));
3836
}));
3937

40-
// String "test" encoded with dictionary "dict".
41-
stream.write(Buffer.from([0x78, 0xBB, 0x04, 0x09, 0x01, 0xA5]));
38+
stream.write(input);
4239
}

test/parallel/test-zlib-dictionary.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
// test compression/decompression with dictionary
33

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

@@ -32,6 +32,7 @@ function basicDictionaryTest() {
3232
let output = '';
3333
const deflate = zlib.createDeflate({ dictionary: spdyDict });
3434
const inflate = zlib.createInflate({ dictionary: spdyDict });
35+
inflate.setEncoding('utf-8');
3536

3637
deflate.on('data', function(chunk) {
3738
inflate.write(chunk);
@@ -45,9 +46,9 @@ function basicDictionaryTest() {
4546
inflate.end();
4647
});
4748

48-
inflate.on('end', function() {
49-
assert.equal(input, output);
50-
});
49+
inflate.on('end', common.mustCall(function() {
50+
assert.strictEqual(input, output);
51+
}));
5152

5253
deflate.write(input);
5354
deflate.end();
@@ -58,6 +59,7 @@ function deflateResetDictionaryTest() {
5859
let output = '';
5960
const deflate = zlib.createDeflate({ dictionary: spdyDict });
6061
const inflate = zlib.createInflate({ dictionary: spdyDict });
62+
inflate.setEncoding('utf-8');
6163

6264
deflate.on('data', function(chunk) {
6365
if (doneReset)
@@ -72,9 +74,9 @@ function deflateResetDictionaryTest() {
7274
inflate.end();
7375
});
7476

75-
inflate.on('end', function() {
76-
assert.equal(input, output);
77-
});
77+
inflate.on('end', common.mustCall(function() {
78+
assert.strictEqual(input, output);
79+
}));
7880

7981
deflate.write(input);
8082
deflate.flush(function() {
@@ -89,6 +91,7 @@ function rawDictionaryTest() {
8991
let output = '';
9092
const deflate = zlib.createDeflateRaw({ dictionary: spdyDict });
9193
const inflate = zlib.createInflateRaw({ dictionary: spdyDict });
94+
inflate.setEncoding('utf-8');
9295

9396
deflate.on('data', function(chunk) {
9497
inflate.write(chunk);
@@ -102,9 +105,9 @@ function rawDictionaryTest() {
102105
inflate.end();
103106
});
104107

105-
inflate.on('end', function() {
106-
assert.equal(input, output);
107-
});
108+
inflate.on('end', common.mustCall(function() {
109+
assert.strictEqual(input, output);
110+
}));
108111

109112
deflate.write(input);
110113
deflate.end();
@@ -115,6 +118,7 @@ function deflateRawResetDictionaryTest() {
115118
let output = '';
116119
const deflate = zlib.createDeflateRaw({ dictionary: spdyDict });
117120
const inflate = zlib.createInflateRaw({ dictionary: spdyDict });
121+
inflate.setEncoding('utf-8');
118122

119123
deflate.on('data', function(chunk) {
120124
if (doneReset)
@@ -129,9 +133,9 @@ function deflateRawResetDictionaryTest() {
129133
inflate.end();
130134
});
131135

132-
inflate.on('end', function() {
133-
assert.equal(input, output);
134-
});
136+
inflate.on('end', common.mustCall(function() {
137+
assert.strictEqual(input, output);
138+
}));
135139

136140
deflate.write(input);
137141
deflate.flush(function() {

0 commit comments

Comments
 (0)