• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3if (!common.hasCrypto)
4  common.skip('missing crypto');
5
6// This test ensures that `getProtocol` returns the right protocol
7// from a TLS connection
8
9const assert = require('assert');
10const tls = require('tls');
11const fixtures = require('../common/fixtures');
12
13const clientConfigs = [
14  {
15    secureProtocol: 'TLSv1_method',
16    version: 'TLSv1',
17    ciphers: (common.hasOpenSSL31 ? 'DEFAULT:@SECLEVEL=0' : 'DEFAULT')
18  }, {
19    secureProtocol: 'TLSv1_1_method',
20    version: 'TLSv1.1',
21    ciphers: (common.hasOpenSSL31 ? 'DEFAULT:@SECLEVEL=0' : 'DEFAULT')
22  }, {
23    secureProtocol: 'TLSv1_2_method',
24    version: 'TLSv1.2'
25  },
26];
27
28const serverConfig = {
29  secureProtocol: 'TLS_method',
30  ciphers: 'RSA@SECLEVEL=0',
31  key: fixtures.readKey('agent2-key.pem'),
32  cert: fixtures.readKey('agent2-cert.pem')
33};
34
35const server = tls.createServer(serverConfig, common.mustCall(clientConfigs.length))
36.listen(0, common.localhostIPv4, function() {
37  let connected = 0;
38  clientConfigs.forEach(function(v) {
39    tls.connect({
40      host: common.localhostIPv4,
41      port: server.address().port,
42      ciphers: v.ciphers,
43      rejectUnauthorized: false,
44      secureProtocol: v.secureProtocol
45    }, common.mustCall(function() {
46      assert.strictEqual(this.getProtocol(), v.version);
47      this.on('end', common.mustCall());
48      this.on('close', common.mustCall(function() {
49        assert.strictEqual(this.getProtocol(), null);
50      })).end();
51      if (++connected === clientConfigs.length)
52        server.close();
53    }));
54  });
55});
56