1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const net = require('net'); 5 6// EADDRINUSE is expected to occur on FreeBSD 7// Ref: https://github.com/nodejs/node/issues/13055 8const expectedErrorCodes = ['ECONNREFUSED', 'EADDRINUSE']; 9 10const optionsIPv4 = { 11 port: common.PORT, 12 family: 4, 13 localPort: common.PORT + 1, 14 localAddress: common.localhostIPv4 15}; 16 17const optionsIPv6 = { 18 host: '::1', 19 family: 6, 20 port: common.PORT + 2, 21 localPort: common.PORT + 3, 22 localAddress: '::1', 23}; 24 25function onError(err, options) { 26 assert.ok(expectedErrorCodes.includes(err.code)); 27 assert.strictEqual(err.syscall, 'connect'); 28 assert.strictEqual(err.localPort, options.localPort); 29 assert.strictEqual(err.localAddress, options.localAddress); 30 assert.strictEqual( 31 err.message, 32 `connect ${err.code} ${err.address}:${err.port} ` + 33 `- Local (${err.localAddress}:${err.localPort})` 34 ); 35} 36 37const clientIPv4 = net.connect(optionsIPv4); 38clientIPv4.on('error', common.mustCall((err) => onError(err, optionsIPv4))); 39 40if (!common.hasIPv6) { 41 common.printSkipMessage('ipv6 part of test, no IPv6 support'); 42 return; 43} 44 45const clientIPv6 = net.connect(optionsIPv6); 46clientIPv6.on('error', common.mustCall((err) => onError(err, optionsIPv6))); 47