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 { secureProtocol: 'TLSv1_method', version: 'TLSv1' }, 15 { secureProtocol: 'TLSv1_1_method', version: 'TLSv1.1' }, 16 { secureProtocol: 'TLSv1_2_method', version: 'TLSv1.2' }, 17]; 18 19const serverConfig = { 20 secureProtocol: 'TLS_method', 21 key: fixtures.readKey('agent2-key.pem'), 22 cert: fixtures.readKey('agent2-cert.pem') 23}; 24 25const server = tls.createServer(serverConfig, common.mustCall(function() { 26 27}, clientConfigs.length)).listen(0, common.localhostIPv4, function() { 28 let connected = 0; 29 clientConfigs.forEach(function(v) { 30 tls.connect({ 31 host: common.localhostIPv4, 32 port: server.address().port, 33 rejectUnauthorized: false, 34 secureProtocol: v.secureProtocol 35 }, common.mustCall(function() { 36 assert.strictEqual(this.getProtocol(), v.version); 37 this.on('end', common.mustCall(function() { 38 assert.strictEqual(this.getProtocol(), null); 39 })).end(); 40 if (++connected === clientConfigs.length) 41 server.close(); 42 })); 43 }); 44}); 45