Skip to content

Commit

Permalink
zlib: tighten up dictionary tests
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
thusoy authored and Fishrock123 committed Oct 11, 2016
1 parent 1547495 commit 7403aaa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
15 changes: 6 additions & 9 deletions test/parallel/test-zlib-dictionary-fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,29 @@ const common = require('../common');
const assert = require('assert');
const zlib = require('zlib');

// Should raise an error, not trigger an assertion in src/node_zlib.cc
// String "test" encoded with dictionary "dict".
const input = Buffer.from([0x78, 0xBB, 0x04, 0x09, 0x01, 0xA5]);

{
const stream = zlib.createInflate();

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

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

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

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

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

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

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

// String "test" encoded with dictionary "dict".
stream.write(Buffer.from([0x78, 0xBB, 0x04, 0x09, 0x01, 0xA5]));
stream.write(input);
}
30 changes: 17 additions & 13 deletions test/parallel/test-zlib-dictionary.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
// test compression/decompression with dictionary

require('../common');
const common = require('../common');
const assert = require('assert');
const zlib = require('zlib');

Expand Down Expand Up @@ -32,6 +32,7 @@ function basicDictionaryTest() {
let output = '';
const deflate = zlib.createDeflate({ dictionary: spdyDict });
const inflate = zlib.createInflate({ dictionary: spdyDict });
inflate.setEncoding('utf-8');

deflate.on('data', function(chunk) {
inflate.write(chunk);
Expand All @@ -45,9 +46,9 @@ function basicDictionaryTest() {
inflate.end();
});

inflate.on('end', function() {
assert.equal(input, output);
});
inflate.on('end', common.mustCall(function() {
assert.strictEqual(input, output);
}));

deflate.write(input);
deflate.end();
Expand All @@ -58,6 +59,7 @@ function deflateResetDictionaryTest() {
let output = '';
const deflate = zlib.createDeflate({ dictionary: spdyDict });
const inflate = zlib.createInflate({ dictionary: spdyDict });
inflate.setEncoding('utf-8');

deflate.on('data', function(chunk) {
if (doneReset)
Expand All @@ -72,9 +74,9 @@ function deflateResetDictionaryTest() {
inflate.end();
});

inflate.on('end', function() {
assert.equal(input, output);
});
inflate.on('end', common.mustCall(function() {
assert.strictEqual(input, output);
}));

deflate.write(input);
deflate.flush(function() {
Expand All @@ -89,6 +91,7 @@ function rawDictionaryTest() {
let output = '';
const deflate = zlib.createDeflateRaw({ dictionary: spdyDict });
const inflate = zlib.createInflateRaw({ dictionary: spdyDict });
inflate.setEncoding('utf-8');

deflate.on('data', function(chunk) {
inflate.write(chunk);
Expand All @@ -102,9 +105,9 @@ function rawDictionaryTest() {
inflate.end();
});

inflate.on('end', function() {
assert.equal(input, output);
});
inflate.on('end', common.mustCall(function() {
assert.strictEqual(input, output);
}));

deflate.write(input);
deflate.end();
Expand All @@ -115,6 +118,7 @@ function deflateRawResetDictionaryTest() {
let output = '';
const deflate = zlib.createDeflateRaw({ dictionary: spdyDict });
const inflate = zlib.createInflateRaw({ dictionary: spdyDict });
inflate.setEncoding('utf-8');

deflate.on('data', function(chunk) {
if (doneReset)
Expand All @@ -129,9 +133,9 @@ function deflateRawResetDictionaryTest() {
inflate.end();
});

inflate.on('end', function() {
assert.equal(input, output);
});
inflate.on('end', common.mustCall(function() {
assert.strictEqual(input, output);
}));

deflate.write(input);
deflate.flush(function() {
Expand Down

0 comments on commit 7403aaa

Please sign in to comment.