test: merge test with unnecessary child process

Test didn't require child process creation. While this test has not been
unstable, child process creation is slower and can be flaky in ci, so
test directly for the segfault regression.

PR-URL: https://github.com/nodejs/node/pull/25025
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
This commit is contained in:
Sam Roberts 2018-12-13 13:39:26 -08:00 committed by Rich Trott
parent 3439c955ab
commit b54d4a68e3
3 changed files with 21 additions and 27 deletions

View File

@ -111,6 +111,17 @@ assert.throws(function() {
crypto.createHash('xyzzy');
}, /Digest method not supported/);
// Issue https://github.com/nodejs/node/issues/9819: throwing encoding used to
// segfault.
common.expectsError(
() => crypto.createHash('sha256').digest({
toString: () => { throw new Error('boom'); },
}),
{
type: Error,
message: 'boom'
});
// Default UTF-8 encoding
const hutf8 = crypto.createHash('sha512').update('УТФ-8 text').digest('hex');
assert.strictEqual(

View File

@ -21,6 +21,16 @@ common.expectsError(
message: 'The "hmac" argument must be of type string. Received type object'
});
// This used to segfault. See: https://github.com/nodejs/node/issues/9819
common.expectsError(
() => crypto.createHmac('sha256', 'key').digest({
toString: () => { throw new Error('boom'); },
}),
{
type: Error,
message: 'boom'
});
common.expectsError(
() => crypto.createHmac('sha1', null),
{

View File

@ -1,27 +0,0 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
// This test ensures that node doesn't SEGFAULT when either of
// `crypto.createHash` or `crypto.createHmac` are given an object that defines
// a throwing `toString`.
// https://github.com/nodejs/node/issues/9819
const assert = require('assert');
const execFile = require('child_process').execFile;
const setup = 'const enc = { toString: () => { throw new Error("xyz"); } };';
const scripts = [
'crypto.createHash("sha256").digest(enc)',
'crypto.createHmac("sha256", "msg").digest(enc)'
];
scripts.forEach((script) => {
const node = process.execPath;
const code = `${setup};${script}`;
execFile(node, [ '-e', code ], common.mustCall((err, stdout, stderr) => {
assert(stderr.includes('Error: xyz'), 'digest crashes');
}));
});