11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This tests the errors thrown from TLSSocket.prototype.setServername
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciif (!common.hasCrypto)
91cb0ef41Sopenharmony_ci  common.skip('missing crypto');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst assert = require('assert');
121cb0ef41Sopenharmony_ciconst { connect, TLSSocket } = require('tls');
131cb0ef41Sopenharmony_ciconst makeDuplexPair = require('../common/duplexpair');
141cb0ef41Sopenharmony_ciconst { clientSide, serverSide } = makeDuplexPair();
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst key = fixtures.readKey('agent1-key.pem');
171cb0ef41Sopenharmony_ciconst cert = fixtures.readKey('agent1-cert.pem');
181cb0ef41Sopenharmony_ciconst ca = fixtures.readKey('ca1-cert.pem');
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst client = connect({
211cb0ef41Sopenharmony_ci  socket: clientSide,
221cb0ef41Sopenharmony_ci  ca,
231cb0ef41Sopenharmony_ci  host: 'agent1'  // Hostname from certificate
241cb0ef41Sopenharmony_ci});
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci[undefined, null, 1, true, {}].forEach((value) => {
271cb0ef41Sopenharmony_ci  assert.throws(() => {
281cb0ef41Sopenharmony_ci    client.setServername(value);
291cb0ef41Sopenharmony_ci  }, {
301cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
311cb0ef41Sopenharmony_ci    message: 'The "name" argument must be of type string.' +
321cb0ef41Sopenharmony_ci             common.invalidArgTypeHelper(value)
331cb0ef41Sopenharmony_ci  });
341cb0ef41Sopenharmony_ci});
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciconst server = new TLSSocket(serverSide, {
371cb0ef41Sopenharmony_ci  isServer: true,
381cb0ef41Sopenharmony_ci  key,
391cb0ef41Sopenharmony_ci  cert,
401cb0ef41Sopenharmony_ci  ca
411cb0ef41Sopenharmony_ci});
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ciassert.throws(() => {
441cb0ef41Sopenharmony_ci  server.setServername('localhost');
451cb0ef41Sopenharmony_ci}, {
461cb0ef41Sopenharmony_ci  code: 'ERR_TLS_SNI_FROM_SERVER',
471cb0ef41Sopenharmony_ci  message: 'Cannot issue SNI from a TLS server-side socket'
481cb0ef41Sopenharmony_ci});
49