11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// This tests that both tls and net will ignore host and port if path is
61cb0ef41Sopenharmony_ci// provided.
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciif (!common.hasCrypto)
91cb0ef41Sopenharmony_ci  common.skip('missing crypto');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
121cb0ef41Sopenharmony_citmpdir.refresh();
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst tls = require('tls');
151cb0ef41Sopenharmony_ciconst net = require('net');
161cb0ef41Sopenharmony_ciconst assert = require('assert');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cifunction libName(lib) {
191cb0ef41Sopenharmony_ci  return lib === net ? 'net' : 'tls';
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_cifunction mkServer(lib, tcp, cb) {
231cb0ef41Sopenharmony_ci  const handler = (socket) => {
241cb0ef41Sopenharmony_ci    socket.write(`${libName(lib)}:${
251cb0ef41Sopenharmony_ci      server.address().port || server.address()
261cb0ef41Sopenharmony_ci    }`);
271cb0ef41Sopenharmony_ci    socket.end();
281cb0ef41Sopenharmony_ci  };
291cb0ef41Sopenharmony_ci  const args = [handler];
301cb0ef41Sopenharmony_ci  if (lib === tls) {
311cb0ef41Sopenharmony_ci    args.unshift({
321cb0ef41Sopenharmony_ci      cert: fixtures.readKey('rsa_cert.crt'),
331cb0ef41Sopenharmony_ci      key: fixtures.readKey('rsa_private.pem')
341cb0ef41Sopenharmony_ci    });
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci  const server = lib.createServer(...args);
371cb0ef41Sopenharmony_ci  server.listen(tcp ? 0 : common.PIPE, common.mustCall(() => cb(server)));
381cb0ef41Sopenharmony_ci}
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_cifunction testLib(lib, cb) {
411cb0ef41Sopenharmony_ci  mkServer(lib, true, (tcpServer) => {
421cb0ef41Sopenharmony_ci    mkServer(lib, false, (unixServer) => {
431cb0ef41Sopenharmony_ci      const client = lib.connect({
441cb0ef41Sopenharmony_ci        path: unixServer.address(),
451cb0ef41Sopenharmony_ci        port: tcpServer.address().port,
461cb0ef41Sopenharmony_ci        host: 'localhost',
471cb0ef41Sopenharmony_ci        rejectUnauthorized: false
481cb0ef41Sopenharmony_ci      }, () => {
491cb0ef41Sopenharmony_ci        const bufs = [];
501cb0ef41Sopenharmony_ci        client.on('data', common.mustCall((d) => {
511cb0ef41Sopenharmony_ci          bufs.push(d);
521cb0ef41Sopenharmony_ci        }));
531cb0ef41Sopenharmony_ci        client.on('end', common.mustCall(() => {
541cb0ef41Sopenharmony_ci          const resp = Buffer.concat(bufs).toString();
551cb0ef41Sopenharmony_ci          assert.strictEqual(resp, `${libName(lib)}:${unixServer.address()}`);
561cb0ef41Sopenharmony_ci          tcpServer.close();
571cb0ef41Sopenharmony_ci          unixServer.close();
581cb0ef41Sopenharmony_ci          cb();
591cb0ef41Sopenharmony_ci        }));
601cb0ef41Sopenharmony_ci      });
611cb0ef41Sopenharmony_ci    });
621cb0ef41Sopenharmony_ci  });
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_citestLib(net, common.mustCall(() => testLib(tls, common.mustCall())));
66