11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst net = require('net');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// EADDRINUSE is expected to occur on FreeBSD
71cb0ef41Sopenharmony_ci// Ref: https://github.com/nodejs/node/issues/13055
81cb0ef41Sopenharmony_ciconst expectedErrorCodes = ['ECONNREFUSED', 'EADDRINUSE'];
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst optionsIPv4 = {
111cb0ef41Sopenharmony_ci  port: common.PORT,
121cb0ef41Sopenharmony_ci  family: 4,
131cb0ef41Sopenharmony_ci  localPort: common.PORT + 1,
141cb0ef41Sopenharmony_ci  localAddress: common.localhostIPv4
151cb0ef41Sopenharmony_ci};
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst optionsIPv6 = {
181cb0ef41Sopenharmony_ci  host: '::1',
191cb0ef41Sopenharmony_ci  family: 6,
201cb0ef41Sopenharmony_ci  port: common.PORT + 2,
211cb0ef41Sopenharmony_ci  localPort: common.PORT + 3,
221cb0ef41Sopenharmony_ci  localAddress: '::1',
231cb0ef41Sopenharmony_ci};
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cifunction onError(err, options) {
261cb0ef41Sopenharmony_ci  assert.ok(expectedErrorCodes.includes(err.code));
271cb0ef41Sopenharmony_ci  assert.strictEqual(err.syscall, 'connect');
281cb0ef41Sopenharmony_ci  assert.strictEqual(err.localPort, options.localPort);
291cb0ef41Sopenharmony_ci  assert.strictEqual(err.localAddress, options.localAddress);
301cb0ef41Sopenharmony_ci  assert.strictEqual(
311cb0ef41Sopenharmony_ci    err.message,
321cb0ef41Sopenharmony_ci    `connect ${err.code} ${err.address}:${err.port} ` +
331cb0ef41Sopenharmony_ci    `- Local (${err.localAddress}:${err.localPort})`
341cb0ef41Sopenharmony_ci  );
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciconst clientIPv4 = net.connect(optionsIPv4);
381cb0ef41Sopenharmony_ciclientIPv4.on('error', common.mustCall((err) => onError(err, optionsIPv4)));
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ciif (!common.hasIPv6) {
411cb0ef41Sopenharmony_ci  common.printSkipMessage('ipv6 part of test, no IPv6 support');
421cb0ef41Sopenharmony_ci  return;
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciconst clientIPv6 = net.connect(optionsIPv6);
461cb0ef41Sopenharmony_ciclientIPv6.on('error', common.mustCall((err) => onError(err, optionsIPv6)));
47