11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.hasCrypto)
41cb0ef41Sopenharmony_ci  common.skip('missing crypto');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// This test ensures that `getProtocol` returns the right protocol
71cb0ef41Sopenharmony_ci// from a TLS connection
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst tls = require('tls');
111cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst clientConfigs = [
141cb0ef41Sopenharmony_ci  {
151cb0ef41Sopenharmony_ci    secureProtocol: 'TLSv1_method',
161cb0ef41Sopenharmony_ci    version: 'TLSv1',
171cb0ef41Sopenharmony_ci    ciphers: (common.hasOpenSSL31 ? 'DEFAULT:@SECLEVEL=0' : 'DEFAULT')
181cb0ef41Sopenharmony_ci  }, {
191cb0ef41Sopenharmony_ci    secureProtocol: 'TLSv1_1_method',
201cb0ef41Sopenharmony_ci    version: 'TLSv1.1',
211cb0ef41Sopenharmony_ci    ciphers: (common.hasOpenSSL31 ? 'DEFAULT:@SECLEVEL=0' : 'DEFAULT')
221cb0ef41Sopenharmony_ci  }, {
231cb0ef41Sopenharmony_ci    secureProtocol: 'TLSv1_2_method',
241cb0ef41Sopenharmony_ci    version: 'TLSv1.2'
251cb0ef41Sopenharmony_ci  },
261cb0ef41Sopenharmony_ci];
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciconst serverConfig = {
291cb0ef41Sopenharmony_ci  secureProtocol: 'TLS_method',
301cb0ef41Sopenharmony_ci  ciphers: 'RSA@SECLEVEL=0',
311cb0ef41Sopenharmony_ci  key: fixtures.readKey('agent2-key.pem'),
321cb0ef41Sopenharmony_ci  cert: fixtures.readKey('agent2-cert.pem')
331cb0ef41Sopenharmony_ci};
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ciconst server = tls.createServer(serverConfig, common.mustCall(clientConfigs.length))
361cb0ef41Sopenharmony_ci.listen(0, common.localhostIPv4, function() {
371cb0ef41Sopenharmony_ci  let connected = 0;
381cb0ef41Sopenharmony_ci  clientConfigs.forEach(function(v) {
391cb0ef41Sopenharmony_ci    tls.connect({
401cb0ef41Sopenharmony_ci      host: common.localhostIPv4,
411cb0ef41Sopenharmony_ci      port: server.address().port,
421cb0ef41Sopenharmony_ci      ciphers: v.ciphers,
431cb0ef41Sopenharmony_ci      rejectUnauthorized: false,
441cb0ef41Sopenharmony_ci      secureProtocol: v.secureProtocol
451cb0ef41Sopenharmony_ci    }, common.mustCall(function() {
461cb0ef41Sopenharmony_ci      assert.strictEqual(this.getProtocol(), v.version);
471cb0ef41Sopenharmony_ci      this.on('end', common.mustCall());
481cb0ef41Sopenharmony_ci      this.on('close', common.mustCall(function() {
491cb0ef41Sopenharmony_ci        assert.strictEqual(this.getProtocol(), null);
501cb0ef41Sopenharmony_ci      })).end();
511cb0ef41Sopenharmony_ci      if (++connected === clientConfigs.length)
521cb0ef41Sopenharmony_ci        server.close();
531cb0ef41Sopenharmony_ci    }));
541cb0ef41Sopenharmony_ci  });
551cb0ef41Sopenharmony_ci});
56