diff --git a/.eslintrc.yaml b/.eslintrc.yaml index 21ed2b558cbd37..339a025d121ce7 100644 --- a/.eslintrc.yaml +++ b/.eslintrc.yaml @@ -146,7 +146,7 @@ rules: # Custom rules in tools/eslint-rules align-multiline-assignment: 2 - assert-throws-arguments: [2, { requireTwo: false }] + assert-throws-arguments: [2, { requireTwo: true }] no-useless-regex-char-class-escape: [2, { override: ['[', ']'] }] # Global scoped method and vars diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index ca25c9965d0640..74ef705f13305f 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -240,7 +240,8 @@ assert.throws( { const re1 = /a/; re1.lastIndex = 3; - assert.throws(makeBlock(a.deepStrictEqual, re1, /a/)); + assert.throws(makeBlock(a.deepStrictEqual, re1, /a/), + /^AssertionError: \/a\/ deepStrictEqual \/a\/$/); } // 7.4 - strict @@ -262,10 +263,12 @@ assert.doesNotThrow(makeBlock(a.deepStrictEqual, {a: 4}, {a: 4})); assert.doesNotThrow(makeBlock(a.deepStrictEqual, {a: 4, b: '2'}, {a: 4, b: '2'})); -assert.throws(makeBlock(a.deepStrictEqual, [4], ['4'])); +assert.throws(makeBlock(a.deepStrictEqual, [4], ['4']), + /^AssertionError: \[ 4 ] deepStrictEqual \[ '4' ]$/); assert.throws(makeBlock(a.deepStrictEqual, {a: 4}, {a: 4, b: true}), - a.AssertionError); -assert.throws(makeBlock(a.deepStrictEqual, ['a'], {0: 'a'})); + /^AssertionError: { a: 4 } deepStrictEqual { a: 4, b: true }$/); +assert.throws(makeBlock(a.deepStrictEqual, ['a'], {0: 'a'}), + /^AssertionError: \[ 'a' ] deepStrictEqual { '0': 'a' }$/); //(although not necessarily the same order), assert.doesNotThrow(makeBlock(a.deepStrictEqual, {a: 4, b: '1'}, @@ -342,9 +345,11 @@ function thrower(errorConstructor) { assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError, 'message'); assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError); +// eslint-disable-next-line assert-throws-arguments assert.throws(makeBlock(thrower, a.AssertionError)); // if not passing an error, catch all. +// eslint-disable-next-line assert-throws-arguments assert.throws(makeBlock(thrower, TypeError)); // when passing a type, only catch errors of the appropriate type @@ -517,6 +522,7 @@ testAssertionMessage({a: NaN, b: Infinity, c: -Infinity}, // #2893 try { + // eslint-disable-next-line assert-throws-arguments assert.throws(function() { assert.ifError(null); }); diff --git a/test/parallel/test-buffer-compare.js b/test/parallel/test-buffer-compare.js index c0db39a6e3c8bc..6182d0967d4900 100644 --- a/test/parallel/test-buffer-compare.js +++ b/test/parallel/test-buffer-compare.js @@ -23,8 +23,11 @@ assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(0)), 0); assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(1)), -1); assert.strictEqual(Buffer.compare(Buffer.alloc(1), Buffer.alloc(0)), 1); -assert.throws(() => Buffer.compare(Buffer.alloc(1), 'abc')); +assert.throws(() => Buffer.compare(Buffer.alloc(1), 'abc'), + /^TypeError: Arguments must be Buffers$/); -assert.throws(() => Buffer.compare('abc', Buffer.alloc(1))); +assert.throws(() => Buffer.compare('abc', Buffer.alloc(1)), + /^TypeError: Arguments must be Buffers$/); -assert.throws(() => Buffer.alloc(1).compare('abc')); +assert.throws(() => Buffer.alloc(1).compare('abc'), + /^TypeError: Argument must be a Buffer$/); diff --git a/test/parallel/test-buffer-equals.js b/test/parallel/test-buffer-equals.js index 2b460c5c6aea1b..3851f05c7b9a6d 100644 --- a/test/parallel/test-buffer-equals.js +++ b/test/parallel/test-buffer-equals.js @@ -13,4 +13,5 @@ assert.ok(!c.equals(d)); assert.ok(!d.equals(e)); assert.ok(d.equals(d)); -assert.throws(() => Buffer.alloc(1).equals('abc')); +assert.throws(() => Buffer.alloc(1).equals('abc'), + /^TypeError: Argument must be a Buffer$/); diff --git a/test/parallel/test-buffer-includes.js b/test/parallel/test-buffer-includes.js index 2b65ce41889cdc..5a0a89ba924f0c 100644 --- a/test/parallel/test-buffer-includes.js +++ b/test/parallel/test-buffer-includes.js @@ -273,15 +273,17 @@ for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) { } } -assert.throws(function() { - b.includes(function() { }); -}); -assert.throws(function() { +const expectedError = + /^TypeError: "val" argument must be string, number or Buffer$/; +assert.throws(() => { + b.includes(() => {}); +}, expectedError); +assert.throws(() => { b.includes({}); -}); -assert.throws(function() { +}, expectedError); +assert.throws(() => { b.includes([]); -}); +}, expectedError); // test truncation of Number arguments to uint8 { diff --git a/test/parallel/test-buffer-indexof.js b/test/parallel/test-buffer-indexof.js index f4eba91b8abded..977d44ba3d8f05 100644 --- a/test/parallel/test-buffer-indexof.js +++ b/test/parallel/test-buffer-indexof.js @@ -346,15 +346,20 @@ assert.strictEqual(Buffer.from('aaaaa').indexOf('b', 'ucs2'), -1); } } -assert.throws(function() { - b.indexOf(function() { }); -}); -assert.throws(function() { +const argumentExpected = + /^TypeError: "val" argument must be string, number or Buffer$/; + +assert.throws(() => { + b.indexOf(() => { }); +}, argumentExpected); + +assert.throws(() => { b.indexOf({}); -}); -assert.throws(function() { +}, argumentExpected); + +assert.throws(() => { b.indexOf([]); -}); +}, argumentExpected); // Test weird offset arguments. // The following offsets coerce to NaN or 0, searching the whole Buffer diff --git a/test/parallel/test-buffer.js b/test/parallel/test-buffer.js index 805c8b9e7ac347..a2fdab55b772a8 100644 --- a/test/parallel/test-buffer.js +++ b/test/parallel/test-buffer.js @@ -41,11 +41,11 @@ for (const [key, value] of e.entries()) { assert.throws(function() { Buffer(8).fill('a', -1); -}); +}, /^RangeError: Out of range index$/); assert.throws(function() { Buffer(8).fill('a', 0, 9); -}); +}, /^RangeError: Out of range index$/); // Make sure this doesn't hang indefinitely. Buffer(8).fill(''); @@ -1433,17 +1433,17 @@ if (common.hasCrypto) { assert.throws(function() { const b = Buffer(1); Buffer.compare(b, 'abc'); -}); +}, /^TypeError: Arguments must be Buffers$/); assert.throws(function() { const b = Buffer(1); Buffer.compare('abc', b); -}); +}, /^TypeError: Arguments must be Buffers$/); assert.throws(function() { const b = Buffer(1); b.compare('abc'); -}); +}, /^TypeError: Argument must be a Buffer$/); // Test Equals { @@ -1461,10 +1461,12 @@ assert.throws(function() { assert.throws(function() { const b = Buffer(1); b.equals('abc'); -}); +}, /^TypeError: Argument must be a Buffer$/); // Regression test for https://github.com/nodejs/node/issues/649. -assert.throws(function() { Buffer(1422561062959).toString('utf8'); }); +assert.throws(function() { + Buffer(1422561062959).toString('utf8'); +}, /^RangeError: Invalid typed array length$/); const ps = Buffer.poolSize; Buffer.poolSize = 0; @@ -1474,7 +1476,7 @@ Buffer.poolSize = ps; // Test Buffer.copy() segfault assert.throws(function() { Buffer(10).copy(); -}); +}, /^TypeError: argument should be a Buffer$/); const regErrorMsg = new RegExp('First argument must be a string, Buffer, ' + 'ArrayBuffer, Array, or array-like object.'); diff --git a/test/parallel/test-crypto-dh.js b/test/parallel/test-crypto-dh.js index 50b0b2d07b9aea..26aca10ff766a0 100644 --- a/test/parallel/test-crypto-dh.js +++ b/test/parallel/test-crypto-dh.js @@ -19,25 +19,28 @@ let key2 = dh2.generateKeys('hex'); let secret1 = dh1.computeSecret(key2, 'hex', 'base64'); let secret2 = dh2.computeSecret(key1, 'latin1', 'buffer'); -assert.strictEqual(secret1, secret2.toString('base64')); +assert.strictEqual(secret2.toString('base64'), secret1); assert.strictEqual(dh1.verifyError, 0); assert.strictEqual(dh2.verifyError, 0); -assert.throws(function() { +const argumentsError = + /^TypeError: First argument should be number, string or Buffer$/; + +assert.throws(() => { crypto.createDiffieHellman([0x1, 0x2]); -}); +}, argumentsError); -assert.throws(function() { - crypto.createDiffieHellman(function() { }); -}); +assert.throws(() => { + crypto.createDiffieHellman(() => { }); +}, argumentsError); -assert.throws(function() { +assert.throws(() => { crypto.createDiffieHellman(/abc/); -}); +}, argumentsError); -assert.throws(function() { +assert.throws(() => { crypto.createDiffieHellman({}); -}); +}, argumentsError); // Create "another dh1" using generated keys from dh1, // and compute secret again @@ -56,21 +59,29 @@ const secret3 = dh3.computeSecret(key2, 'hex', 'base64'); assert.strictEqual(secret1, secret3); +const wrongBlockLength = + new RegExp('^Error: error:0606506D:digital envelope' + + ' routines:EVP_DecryptFinal_ex:wrong final block length$'); + // Run this one twice to make sure that the dh3 clears its error properly { const c = crypto.createDecipheriv('aes-128-ecb', crypto.randomBytes(16), ''); - assert.throws(function() { c.final('utf8'); }, /wrong final block length/); + assert.throws(() => { + c.final('utf8'); + }, wrongBlockLength); } -assert.throws(function() { - dh3.computeSecret(''); -}, /key is too small/i); - { const c = crypto.createDecipheriv('aes-128-ecb', crypto.randomBytes(16), ''); - assert.throws(function() { c.final('utf8'); }, /wrong final block length/); + assert.throws(() => { + c.final('utf8'); + }, wrongBlockLength); } +assert.throws(() => { + dh3.computeSecret(''); +}, /^Error: Supplied key is too small$/); + // Create a shared using a DH group. const alice = crypto.createDiffieHellmanGroup('modp5'); const bob = crypto.createDiffieHellmanGroup('modp5'); @@ -180,9 +191,9 @@ assert.throws(() => { const ecdh3 = crypto.createECDH('secp256k1'); const key3 = ecdh3.generateKeys(); -assert.throws(function() { +assert.throws(() => { ecdh2.computeSecret(key3, 'latin1', 'buffer'); -}); +}, /^Error: Failed to translate Buffer to a EC_POINT$/); // ECDH should allow .setPrivateKey()/.setPublicKey() const ecdh4 = crypto.createECDH('prime256v1'); @@ -190,20 +201,21 @@ const ecdh4 = crypto.createECDH('prime256v1'); ecdh4.setPrivateKey(ecdh1.getPrivateKey()); ecdh4.setPublicKey(ecdh1.getPublicKey()); -assert.throws(function() { +assert.throws(() => { ecdh4.setPublicKey(ecdh3.getPublicKey()); -}, /Failed to convert Buffer to EC_POINT/); +}, /^Error: Failed to convert Buffer to EC_POINT$/); // Verify that we can use ECDH without having to use newly generated keys. const ecdh5 = crypto.createECDH('secp256k1'); // Verify errors are thrown when retrieving keys from an uninitialized object. -assert.throws(function() { +assert.throws(() => { ecdh5.getPublicKey(); -}, /Failed to get ECDH public key/); -assert.throws(function() { +}, /^Error: Failed to get ECDH public key$/); + +assert.throws(() => { ecdh5.getPrivateKey(); -}, /Failed to get ECDH private key/); +}, /^Error: Failed to get ECDH private key$/); // A valid private key for the secp256k1 curve. const cafebabeKey = 'cafebabe'.repeat(8); @@ -249,10 +261,10 @@ assert.strictEqual(ecdh5.getPublicKey('hex', 'compressed'), cafebabePubPtComp); // Show why allowing the public key to be set on this type does not make sense. ecdh5.setPublicKey(peerPubPtComp, 'hex'); assert.strictEqual(ecdh5.getPublicKey('hex'), peerPubPtUnComp); -assert.throws(function() { +assert.throws(() => { // Error because the public key does not match the private key anymore. ecdh5.computeSecret(peerPubPtComp, 'hex', 'hex'); -}, /Invalid key pair/); +}, /^Error: Invalid key pair$/); // Set to a valid key to show that later attempts to set an invalid key are // rejected. @@ -262,10 +274,10 @@ ecdh5.setPrivateKey(cafebabeKey, 'hex'); '0000000000000000000000000000000000000000000000000000000000000000', 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141', 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', -].forEach(function(element, index, object) { - assert.throws(function() { +].forEach((element) => { + assert.throws(() => { ecdh5.setPrivateKey(element, 'hex'); - }, /Private key is not valid for specified curve/); + }, /^Error: Private key is not valid for specified curve.$/); // Verify object state did not change. assert.strictEqual(ecdh5.getPrivateKey('hex'), cafebabeKey); }); diff --git a/test/parallel/test-crypto-rsa-dsa.js b/test/parallel/test-crypto-rsa-dsa.js index e15efe1a73cd3c..47dc68e7131ca2 100644 --- a/test/parallel/test-crypto-rsa-dsa.js +++ b/test/parallel/test-crypto-rsa-dsa.js @@ -26,6 +26,9 @@ const dsaKeyPem = fs.readFileSync(common.fixturesDir + '/test_dsa_privkey.pem', const dsaKeyPemEncrypted = fs.readFileSync( common.fixturesDir + '/test_dsa_privkey_encrypted.pem', 'ascii'); +const decryptError = new RegExp('^Error: error:06065064:digital envelope ' + + 'routines:EVP_DecryptFinal_ex:bad decrypt$'); + // Test RSA encryption/decryption { const input = 'I AM THE WALRUS'; @@ -34,13 +37,13 @@ const dsaKeyPemEncrypted = fs.readFileSync( let encryptedBuffer = crypto.publicEncrypt(rsaPubPem, bufferToEncrypt); let decryptedBuffer = crypto.privateDecrypt(rsaKeyPem, encryptedBuffer); - assert.strictEqual(input, decryptedBuffer.toString()); + assert.strictEqual(decryptedBuffer.toString(), input); let decryptedBufferWithPassword = crypto.privateDecrypt({ key: rsaKeyPemEncrypted, passphrase: 'password' }, encryptedBuffer); - assert.strictEqual(input, decryptedBufferWithPassword.toString()); + assert.strictEqual(decryptedBufferWithPassword.toString(), input); encryptedBuffer = crypto.publicEncrypt({ key: rsaKeyPemEncrypted, @@ -51,7 +54,7 @@ const dsaKeyPemEncrypted = fs.readFileSync( key: rsaKeyPemEncrypted, passphrase: 'password' }, encryptedBuffer); - assert.strictEqual(input, decryptedBufferWithPassword.toString()); + assert.strictEqual(decryptedBufferWithPassword.toString(), input); encryptedBuffer = crypto.privateEncrypt({ key: rsaKeyPemEncrypted, @@ -62,53 +65,53 @@ const dsaKeyPemEncrypted = fs.readFileSync( key: rsaKeyPemEncrypted, passphrase: Buffer.from('password') }, encryptedBuffer); - assert.strictEqual(input, decryptedBufferWithPassword.toString()); + assert.strictEqual(decryptedBufferWithPassword.toString(), input); encryptedBuffer = crypto.publicEncrypt(certPem, bufferToEncrypt); decryptedBuffer = crypto.privateDecrypt(keyPem, encryptedBuffer); - assert.strictEqual(input, decryptedBuffer.toString()); + assert.strictEqual(decryptedBuffer.toString(), input); encryptedBuffer = crypto.publicEncrypt(keyPem, bufferToEncrypt); decryptedBuffer = crypto.privateDecrypt(keyPem, encryptedBuffer); - assert.strictEqual(input, decryptedBuffer.toString()); + assert.strictEqual(decryptedBuffer.toString(), input); encryptedBuffer = crypto.privateEncrypt(keyPem, bufferToEncrypt); decryptedBuffer = crypto.publicDecrypt(keyPem, encryptedBuffer); - assert.strictEqual(input, decryptedBuffer.toString()); + assert.strictEqual(decryptedBuffer.toString(), input); - assert.throws(function() { + assert.throws(() => { crypto.privateDecrypt({ key: rsaKeyPemEncrypted, passphrase: 'wrong' }, bufferToEncrypt); - }); + }, decryptError); - assert.throws(function() { + assert.throws(() => { crypto.publicEncrypt({ key: rsaKeyPemEncrypted, passphrase: 'wrong' }, encryptedBuffer); - }); + }, decryptError); encryptedBuffer = crypto.privateEncrypt({ key: rsaKeyPemEncrypted, passphrase: Buffer.from('password') }, bufferToEncrypt); - assert.throws(function() { + assert.throws(() => { crypto.publicDecrypt({ key: rsaKeyPemEncrypted, passphrase: [].concat.apply([], Buffer.from('password')) }, encryptedBuffer); - }); + }, decryptError); } function test_rsa(padding) { - const input = Buffer - .allocUnsafe(padding === 'RSA_NO_PADDING' ? 1024 / 8 : 32); + const size = (padding === 'RSA_NO_PADDING') ? 1024 / 8 : 32; + const input = Buffer.allocUnsafe(size); for (let i = 0; i < input.length; i++) input[i] = (i * 7 + 11) & 0xff; const bufferToEncrypt = Buffer.from(input); @@ -124,7 +127,7 @@ function test_rsa(padding) { key: rsaKeyPem, padding: padding }, encryptedBuffer); - assert.strictEqual(input.toString(), decryptedBuffer.toString()); + assert.deepStrictEqual(decryptedBuffer, input); } test_rsa('RSA_NO_PADDING'); @@ -137,14 +140,16 @@ let rsaVerify = crypto.createVerify('RSA-SHA1'); assert.ok(rsaSign); assert.ok(rsaVerify); +const expectedSignature = + '5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' + + '8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' + + 'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' + + '60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' + + '40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6'; + rsaSign.update(rsaPubPem); let rsaSignature = rsaSign.sign(rsaKeyPem, 'hex'); -assert.strictEqual(rsaSignature, - '5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' + - '8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' + - 'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' + - '60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' + - '40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6'); +assert.strictEqual(rsaSignature, expectedSignature); rsaVerify.update(rsaPubPem); assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true); @@ -152,16 +157,11 @@ assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true); // Test RSA key signing/verification with encrypted key rsaSign = crypto.createSign('RSA-SHA1'); rsaSign.update(rsaPubPem); -assert.doesNotThrow(function() { +assert.doesNotThrow(() => { const signOptions = { key: rsaKeyPemEncrypted, passphrase: 'password' }; rsaSignature = rsaSign.sign(signOptions, 'hex'); }); -assert.strictEqual(rsaSignature, - '5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' + - '8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' + - 'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' + - '60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' + - '40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6'); +assert.strictEqual(rsaSignature, expectedSignature); rsaVerify = crypto.createVerify('RSA-SHA1'); rsaVerify.update(rsaPubPem); @@ -169,10 +169,10 @@ assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true); rsaSign = crypto.createSign('RSA-SHA1'); rsaSign.update(rsaPubPem); -assert.throws(function() { +assert.throws(() => { const signOptions = { key: rsaKeyPemEncrypted, passphrase: 'wrong' }; rsaSign.sign(signOptions, 'hex'); -}); +}, decryptError); // // Test RSA signing and verification @@ -197,7 +197,7 @@ assert.throws(function() { sign.update(input); const output = sign.sign(privateKey, 'hex'); - assert.strictEqual(output, signature); + assert.strictEqual(signature, output); const verify = crypto.createVerify('RSA-SHA256'); verify.update(input); @@ -233,9 +233,9 @@ const input = 'I AM THE WALRUS'; { const sign = crypto.createSign('DSS1'); sign.update(input); - assert.throws(function() { + assert.throws(() => { sign.sign({ key: dsaKeyPemEncrypted, passphrase: 'wrong' }, 'hex'); - }); + }, decryptError); } { @@ -245,7 +245,7 @@ const input = 'I AM THE WALRUS'; sign.update(input); let signature; - assert.doesNotThrow(function() { + assert.doesNotThrow(() => { const signOptions = { key: dsaKeyPemEncrypted, passphrase: 'password' }; signature = sign.sign(signOptions, 'hex'); }); diff --git a/test/parallel/test-fs-make-callback.js b/test/parallel/test-fs-make-callback.js index 2dc9874a546574..034b22f789104b 100644 --- a/test/parallel/test-fs-make-callback.js +++ b/test/parallel/test-fs-make-callback.js @@ -1,28 +1,28 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const fs = require('fs'); +const cbTypeError = /^TypeError: "callback" argument must be a function$/; +const callbackThrowValues = [null, true, false, 0, 1, 'foo', /foo/, [], {}]; -function test(cb) { +const { sep } = require('path'); + +common.refreshTmpDir(); + +function testMakeCallback(cb) { return function() { - // fs.stat() calls makeCallback() on its second argument - fs.stat(__filename, cb); + // fs.mkdtemp() calls makeCallback() on its third argument + fs.mkdtemp(`${common.tmpDir}${sep}`, {}, cb); }; } -// Verify the case where a callback function is provided -assert.doesNotThrow(test(function() {})); +// Passing undefined/nothing calls rethrow() internally +assert.doesNotThrow(testMakeCallback()); -// Passing undefined calls rethrow() internally, which is fine -assert.doesNotThrow(test(undefined)); +function invalidCallbackThrowsTests() { + callbackThrowValues.forEach((value) => { + assert.throws(testMakeCallback(value), cbTypeError); + }); +} -// Anything else should throw -assert.throws(test(null)); -assert.throws(test(true)); -assert.throws(test(false)); -assert.throws(test(1)); -assert.throws(test(0)); -assert.throws(test('foo')); -assert.throws(test(/foo/)); -assert.throws(test([])); -assert.throws(test({})); +invalidCallbackThrowsTests(); diff --git a/test/parallel/test-fs-makeStatsCallback.js b/test/parallel/test-fs-makeStatsCallback.js new file mode 100644 index 00000000000000..67e6b18dd3d4f7 --- /dev/null +++ b/test/parallel/test-fs-makeStatsCallback.js @@ -0,0 +1,27 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); +const cbTypeError = /^TypeError: "callback" argument must be a function$/; +const callbackThrowValues = [null, true, false, 0, 1, 'foo', /foo/, [], {}]; + +function testMakeStatsCallback(cb) { + return function() { + // fs.stat() calls makeStatsCallback() on its second argument + fs.stat(__filename, cb); + }; +} + +// Verify the case where a callback function is provided +assert.doesNotThrow(testMakeStatsCallback(common.noop)); + +// Passing undefined/nothing calls rethrow() internally +assert.doesNotThrow(testMakeStatsCallback()); + +function invalidCallbackThrowsTests() { + callbackThrowValues.forEach((value) => { + assert.throws(testMakeStatsCallback(value), cbTypeError); + }); +} + +invalidCallbackThrowsTests(); diff --git a/test/parallel/test-fs-open-flags.js b/test/parallel/test-fs-open-flags.js index 2c8eceb4095ceb..c59e0ab31c1667 100644 --- a/test/parallel/test-fs-open-flags.js +++ b/test/parallel/test-fs-open-flags.js @@ -39,20 +39,27 @@ assert.strictEqual(fs._stringToFlags('xa+'), ('+ +a +r +w rw wa war raw r++ a++ w++ x +x x+ rx rx+ wxx wax xwx xxx') .split(' ') .forEach(function(flags) { - assert.throws(function() { fs._stringToFlags(flags); }); + assert.throws( + () => fs._stringToFlags(flags), + new RegExp(`^Error: Unknown file open flag: ${escapeRegExp(flags)}`) + ); }); assert.throws( () => fs._stringToFlags({}), - /Unknown file open flag: \[object Object\]/ + /^Error: Unknown file open flag: \[object Object\]$/ ); assert.throws( () => fs._stringToFlags(true), - /Unknown file open flag: true/ + /^Error: Unknown file open flag: true$/ ); assert.throws( () => fs._stringToFlags(null), - /Unknown file open flag: null/ + /Error: Unknown file open flag: null$/ ); + +function escapeRegExp(string) { + return string.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&'); +} diff --git a/test/parallel/test-fs-realpath.js b/test/parallel/test-fs-realpath.js index 3152ff80a38688..9efef0a2281b02 100644 --- a/test/parallel/test-fs-realpath.js +++ b/test/parallel/test-fs-realpath.js @@ -190,21 +190,25 @@ function test_cyclic_link_protection(callback) { common.skip('symlink test (no privs)'); return runNextTest(); } - const entry = common.tmpDir + '/cycles/realpath-3a'; + const entry = path.join(common.tmpDir, '/cycles/realpath-3a'); [ [entry, '../cycles/realpath-3b'], - [common.tmpDir + '/cycles/realpath-3b', '../cycles/realpath-3c'], - [common.tmpDir + '/cycles/realpath-3c', '../cycles/realpath-3a'] + [path.join(common.tmpDir, '/cycles/realpath-3b'), '../cycles/realpath-3c'], + [path.join(common.tmpDir, '/cycles/realpath-3c'), '../cycles/realpath-3a'] ].forEach(function(t) { try { fs.unlinkSync(t[0]); } catch (e) {} fs.symlinkSync(t[1], t[0], 'dir'); unlink.push(t[0]); }); - assert.throws(function() { fs.realpathSync(entry); }); - asynctest(fs.realpath, [entry], callback, function(err, result) { - assert.ok(err && true); - return true; - }); + assert.throws(() => { + fs.realpathSync(entry); + }, /^Error: ELOOP: too many symbolic links encountered, stat /); + asynctest( + fs.realpath, [entry], callback, common.mustCall(function(err, result) { + assert.strictEqual(err.path, entry); + assert.strictEqual(result, undefined); + return true; + })); } function test_cyclic_link_overprotection(callback) { diff --git a/test/parallel/test-http-mutable-headers.js b/test/parallel/test-http-mutable-headers.js index 8f9d597efbec92..2a4b0dc6c1c99b 100644 --- a/test/parallel/test-http-mutable-headers.js +++ b/test/parallel/test-http-mutable-headers.js @@ -22,10 +22,18 @@ const cookies = [ const s = http.createServer(function(req, res) { switch (test) { case 'headers': - assert.throws(function() { res.setHeader(); }); - assert.throws(function() { res.setHeader('someHeader'); }); - assert.throws(function() { res.getHeader(); }); - assert.throws(function() { res.removeHeader(); }); + assert.throws(() => { + res.setHeader(); + }, /^TypeError: Header name must be a valid HTTP Token \["undefined"\]$/); + assert.throws(() => { + res.setHeader('someHeader'); + }, /^Error: "value" required in setHeader\("someHeader", value\)$/); + assert.throws(() => { + res.getHeader(); + }, /^Error: "name" argument is required for getHeader\(name\)$/); + assert.throws(() => { + res.removeHeader(); + }, /^Error: "name" argument is required for removeHeader\(name\)$/); res.setHeader('x-test-header', 'testing'); res.setHeader('X-TEST-HEADER2', 'testing'); diff --git a/test/parallel/test-repl-context.js b/test/parallel/test-repl-context.js index 2287a247d81bb2..a2f04214311c13 100644 --- a/test/parallel/test-repl-context.js +++ b/test/parallel/test-repl-context.js @@ -22,5 +22,5 @@ function testContext(repl) { assert.strictEqual(context.global, context); // ensure that the repl console instance does not have a setter - assert.throws(() => context.console = 'foo'); + assert.throws(() => context.console = 'foo', TypeError); } diff --git a/test/parallel/test-vm-new-script-this-context.js b/test/parallel/test-vm-new-script-this-context.js index 3df5dc1b5020d5..62aecfed28af4f 100644 --- a/test/parallel/test-vm-new-script-this-context.js +++ b/test/parallel/test-vm-new-script-this-context.js @@ -14,7 +14,7 @@ console.error('thrown error'); script = new Script('throw new Error(\'test\');'); assert.throws(function() { script.runInThisContext(script); -}); +}, /^Error: test$/); global.hello = 5; script = new Script('hello = 2');