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 ciphers: 'RSA@SECLEVEL=0', 22 key: fixtures.readKey('agent2-key.pem'), 23 cert: fixtures.readKey('agent2-cert.pem') 24}; 25 26const server = tls.createServer(serverConfig, common.mustCall(clientConfigs.length)) 27.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()); 38 this.on('close', common.mustCall(function() { 39 assert.strictEqual(this.getProtocol(), null); 40 })).end(); 41 if (++connected === clientConfigs.length) 42 server.close(); 43 })); 44 }); 45}); 46