diff --git a/test/parallel/test-crypto-hash.js b/test/parallel/test-crypto-hash.js index ec35cad7e8960c..5cc622b48c76c4 100644 --- a/test/parallel/test-crypto-hash.js +++ b/test/parallel/test-crypto-hash.js @@ -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( diff --git a/test/parallel/test-crypto-hmac.js b/test/parallel/test-crypto-hmac.js index 223b5c3c077251..f21db29d36107f 100644 --- a/test/parallel/test-crypto-hmac.js +++ b/test/parallel/test-crypto-hmac.js @@ -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), { diff --git a/test/parallel/test-crypto-tostring-segfault.js b/test/parallel/test-crypto-tostring-segfault.js deleted file mode 100644 index b2c95117d3b1f8..00000000000000 --- a/test/parallel/test-crypto-tostring-segfault.js +++ /dev/null @@ -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'); - })); -});