From 9ffebeab48e2e0b61dc0817430f089b6a1482ea7 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 11 Jan 2018 01:42:08 +0800 Subject: tls: migrate argument type-checking errors * Throw ERR_INVALID_ARG_TYPE from public APIs * Assert argument types in bindings instead of throwing errors PR-URL: https://github.com/nodejs/node/pull/18125 Reviewed-By: Anna Henningsen Reviewed-By: Fedor Indutny Reviewed-By: James M Snell --- test/parallel/test-tls-basic-validations.js | 9 +++++++-- test/parallel/test-tls-error-servername.js | 30 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-tls-error-servername.js (limited to 'test') diff --git a/test/parallel/test-tls-basic-validations.js b/test/parallel/test-tls-basic-validations.js index fc2743ce04..501cfd91d0 100644 --- a/test/parallel/test-tls-basic-validations.js +++ b/test/parallel/test-tls-basic-validations.js @@ -39,8 +39,13 @@ assert.throws(() => tls.createServer({ ticketKeys: 'abcd' }), assert.throws(() => tls.createServer({ ticketKeys: Buffer.alloc(0) }), /TypeError: Ticket keys length must be 48 bytes/); -assert.throws(() => tls.createSecurePair({}), - /TypeError: Second argument should be a SecureContext instance/); +common.expectsError( + () => tls.createSecurePair({}), + { + code: 'ERR_ASSERTION', + message: 'context.context must be a NativeSecureContext' + } +); { const buffer = Buffer.from('abcd'); diff --git a/test/parallel/test-tls-error-servername.js b/test/parallel/test-tls-error-servername.js new file mode 100644 index 0000000000..5c1960fce3 --- /dev/null +++ b/test/parallel/test-tls-error-servername.js @@ -0,0 +1,30 @@ +'use strict'; + +// This tests the errors thrown from TLSSocket.prototype.setServername + +const common = require('../common'); +const fixtures = require('../common/fixtures'); + +if (!common.hasCrypto) + common.skip('missing crypto'); + +const { connect } = require('tls'); +const makeDuplexPair = require('../common/duplexpair'); +const { clientSide } = makeDuplexPair(); + +const ca = fixtures.readKey('ca1-cert.pem'); + +const client = connect({ + socket: clientSide, + ca, + host: 'agent1' // Hostname from certificate +}); + +[undefined, null, 1, true, {}].forEach((value) => { + common.expectsError(() => { + client.setServername(value); + }, { + code: 'ERR_INVALID_ARG_TYPE', + message: 'The "name" argument must be of type string' + }); +}); -- cgit v1.2.3