0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-tls-basic-validations.js
Ruben Bridgewater e038d6a1cd
test: refactor common.expectsError
This completely refactors the `expectsError` behavior: so far it's
almost identical to `assert.throws(fn, object)` in case it was used
with a function as first argument. It had a magical property check
that allowed to verify a functions `type` in case `type` was passed
used in the validation object. This pattern is now completely removed
and `assert.throws()` should be used instead.

The main intent for `common.expectsError()` is to verify error cases
for callback based APIs. This is now more flexible by accepting all
validation possibilites that `assert.throws()` accepts as well. No
magical properties exist anymore. This reduces surprising behavior
for developers who are not used to the Node.js core code base.

This has the side effect that `common` is used significantly less
frequent.

PR-URL: https://github.com/nodejs/node/pull/31092
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
2019-12-31 15:54:20 +01:00

133 lines
3.4 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const tls = require('tls');
assert.throws(
() => tls.createSecureContext({ ciphers: 1 }),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "options.ciphers" property must be of type string.' +
' Received type number (1)'
});
assert.throws(
() => tls.createServer({ ciphers: 1 }),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "options.ciphers" property must be of type string.' +
' Received type number (1)'
});
assert.throws(
() => tls.createSecureContext({ key: 'dummykey', passphrase: 1 }),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'Pass phrase must be a string'
});
assert.throws(
() => tls.createServer({ key: 'dummykey', passphrase: 1 }),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'Pass phrase must be a string'
});
assert.throws(
() => tls.createServer({ ecdhCurve: 1 }),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'ECDH curve name must be a string'
});
assert.throws(
() => tls.createServer({ handshakeTimeout: 'abcd' }),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "options.handshakeTimeout" property must be of type number.' +
" Received type string ('abcd')"
}
);
assert.throws(
() => tls.createServer({ sessionTimeout: 'abcd' }),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'Session timeout must be a 32-bit integer'
});
assert.throws(
() => tls.createServer({ ticketKeys: 'abcd' }),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'Ticket keys must be a buffer'
});
assert.throws(() => tls.createServer({ ticketKeys: Buffer.alloc(0) }),
/TypeError: Ticket keys length must be 48 bytes/);
assert.throws(
() => tls.createSecurePair({}),
{
message: 'context must be a SecureContext',
code: 'ERR_TLS_INVALID_CONTEXT',
name: 'TypeError',
}
);
{
const buffer = Buffer.from('abcd');
const out = {};
tls.convertALPNProtocols(buffer, out);
out.ALPNProtocols.write('efgh');
assert(buffer.equals(Buffer.from('abcd')));
assert(out.ALPNProtocols.equals(Buffer.from('efgh')));
}
{
const arrayBufferViewStr = 'abcd';
const inputBuffer = Buffer.from(arrayBufferViewStr.repeat(8), 'utf8');
for (const expectView of common.getArrayBufferViews(inputBuffer)) {
const out = {};
tls.convertALPNProtocols(expectView, out);
assert(out.ALPNProtocols.equals(Buffer.from(expectView)));
}
}
{
const protocols = [(new String('a')).repeat(500)];
const out = {};
assert.throws(
() => tls.convertALPNProtocols(protocols, out),
{
code: 'ERR_OUT_OF_RANGE',
message: 'The byte length of the protocol at index 0 exceeds the ' +
'maximum length. It must be <= 255. Received 500'
}
);
}
assert.throws(() => { tls.createSecureContext({ minVersion: 'fhqwhgads' }); },
{
code: 'ERR_TLS_INVALID_PROTOCOL_VERSION',
name: 'TypeError'
});
assert.throws(() => { tls.createSecureContext({ maxVersion: 'fhqwhgads' }); },
{
code: 'ERR_TLS_INVALID_PROTOCOL_VERSION',
name: 'TypeError'
});