Skip to content

test: find IPv6 address from localhost names #7806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var fs = require('fs');
var assert = require('assert');
var os = require('os');
var child_process = require('child_process');
const dns = require('dns');
const stream = require('stream');
const util = require('util');
const Timer = process.binding('timer_wrap').Timer;
Expand Down Expand Up @@ -89,6 +90,7 @@ exports.tmpDir = path.join(testRoot, exports.tmpDirName);
var opensslCli = null;
var inFreeBSDJail = null;
var localhostIPv4 = null;
var localhostIPv6 = null;

exports.localIPv6Hosts = ['localhost'];
if (process.platform === 'linux') {
Expand All @@ -106,6 +108,26 @@ if (process.platform === 'linux') {
];
}

exports.getLocalIPv6Address = function getLocalIPv6Address(callback) {
const err = new Error('Unable to determine IPv6 address of localhost');

if (typeof localhostIPv6 === 'object' && localhostIPv6 !== null)
return process.nextTick(() => callback(null, localhostIPv6));
else if (typeof localhostIPv6 === 'undefined')
return process.nextTick(() => callback(err));

Promise.all(exports.localIPv6Hosts.map((host) => new Promise((res, _) => {
dns.lookup(host, {family: 6},
(_, addr) => res({hostname: host, address: addr || ''}));
}))).then((addresses) => {
localhostIPv6 = addresses.filter((d) => d.address)[0];
if (localhostIPv6 === undefined)
callback(err);
else
callback(null, localhostIPv6);
});
};

Object.defineProperty(exports, 'inFreeBSDJail', {
get: function() {
if (inFreeBSDJail !== null) return inFreeBSDJail;
Expand Down
7 changes: 0 additions & 7 deletions test/parallel/parallel.status
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ test-tick-processor : PASS,FLAKY
[$system==linux]
test-tick-processor : PASS,FLAKY

# Flaky until https://github.com/nodejs/build/issues/415 is resolved.
# On some of the buildbots, AAAA queries for localhost don't resolve
# to an address and neither do any of the alternatives from the
# localIPv6Hosts list from test/common.js.
test-https-connect-address-family : PASS,FLAKY
test-tls-connect-address-family : PASS,FLAKY

[$system==macos]

[$system==solaris] # Also applies to SmartOS
Expand Down
20 changes: 7 additions & 13 deletions test/parallel/test-https-connect-address-family.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,29 @@ if (!common.hasIPv6) {

const assert = require('assert');
const https = require('https');
const dns = require('dns');

function runTest() {
function runTest(hostname, ipv6) {
const ciphers = 'AECDH-NULL-SHA';
https.createServer({ ciphers }, common.mustCall(function(req, res) {
this.close();
res.end();
})).listen(common.PORT, '::1', common.mustCall(function() {
})).listen(common.PORT, ipv6, common.mustCall(function() {
const options = {
host: 'localhost',
host: hostname,
port: common.PORT,
family: 6,
ciphers: ciphers,
rejectUnauthorized: false,
};
// Will fail with ECONNREFUSED if the address family is not honored.
https.get(options, common.mustCall(function() {
assert.strictEqual('::1', this.socket.remoteAddress);
assert.strictEqual(ipv6, this.socket.remoteAddress);
this.destroy();
}));
}));
}

dns.lookup('localhost', {family: 6, all: true}, (err, addresses) => {
if (err)
throw err;

if (addresses.some((val) => val.address === '::1'))
runTest();
else
common.skip('localhost does not resolve to ::1');
common.getLocalIPv6Address((err, address) => {
if (err) return common.skip(err.message);
runTest(address.hostname, address.address);
});
20 changes: 7 additions & 13 deletions test/parallel/test-tls-connect-address-family.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,28 @@ if (!common.hasIPv6) {

const assert = require('assert');
const tls = require('tls');
const dns = require('dns');

function runTest() {
function runTest(hostname, ipv6) {
const ciphers = 'AECDH-NULL-SHA';
tls.createServer({ ciphers }, common.mustCall(function() {
this.close();
})).listen(common.PORT, '::1', common.mustCall(function() {
})).listen(common.PORT, ipv6, common.mustCall(function() {
const options = {
host: 'localhost',
host: hostname,
port: common.PORT,
family: 6,
ciphers: ciphers,
rejectUnauthorized: false,
};
// Will fail with ECONNREFUSED if the address family is not honored.
tls.connect(options).once('secureConnect', common.mustCall(function() {
assert.strictEqual('::1', this.remoteAddress);
assert.strictEqual(ipv6, this.remoteAddress);
this.destroy();
}));
}));
}

dns.lookup('localhost', {family: 6, all: true}, (err, addresses) => {
if (err)
throw err;

if (addresses.some((val) => val.address === '::1'))
runTest();
else
common.skip('localhost does not resolve to ::1');
common.getLocalIPv6Address((err, address) => {
if (err) return common.skip(err.message);
runTest(address.hostname, address.address);
});