1'use strict'; 2const common = require('../common'); 3const fixtures = require('../common/fixtures'); 4 5// This tests that both tls and net will ignore host and port if path is 6// provided. 7 8if (!common.hasCrypto) 9 common.skip('missing crypto'); 10 11const tmpdir = require('../common/tmpdir'); 12tmpdir.refresh(); 13 14const tls = require('tls'); 15const net = require('net'); 16const assert = require('assert'); 17 18function libName(lib) { 19 return lib === net ? 'net' : 'tls'; 20} 21 22function mkServer(lib, tcp, cb) { 23 const handler = (socket) => { 24 socket.write(`${libName(lib)}:${ 25 server.address().port || server.address() 26 }`); 27 socket.end(); 28 }; 29 const args = [handler]; 30 if (lib === tls) { 31 args.unshift({ 32 cert: fixtures.readKey('rsa_cert.crt'), 33 key: fixtures.readKey('rsa_private.pem') 34 }); 35 } 36 const server = lib.createServer(...args); 37 server.listen(tcp ? 0 : common.PIPE, common.mustCall(() => cb(server))); 38} 39 40function testLib(lib, cb) { 41 mkServer(lib, true, (tcpServer) => { 42 mkServer(lib, false, (unixServer) => { 43 const client = lib.connect({ 44 path: unixServer.address(), 45 port: tcpServer.address().port, 46 host: 'localhost', 47 rejectUnauthorized: false 48 }, () => { 49 const bufs = []; 50 client.on('data', common.mustCall((d) => { 51 bufs.push(d); 52 })); 53 client.on('end', common.mustCall(() => { 54 const resp = Buffer.concat(bufs).toString(); 55 assert.strictEqual(resp, `${libName(lib)}:${unixServer.address()}`); 56 tcpServer.close(); 57 unixServer.close(); 58 cb(); 59 })); 60 }); 61 }); 62 }); 63} 64 65testLib(net, common.mustCall(() => testLib(tls, common.mustCall()))); 66