Skip to content

Commit

Permalink
test: fix flaky IPv6/localhost tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Trott committed Jul 3, 2016
1 parent 7cbbec5 commit 00f9b60
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 37 deletions.
59 changes: 40 additions & 19 deletions test/parallel/test-https-connect-address-family.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,50 @@ if (!common.hasCrypto) {
return;
}

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

if (!common.hasIPv6) {
common.skip('no IPv6 support');
return;
}

const ciphers = 'AECDH-NULL-SHA';
https.createServer({ ciphers }, function(req, res) {
this.close();
res.end();
}).listen(common.PORT, '::1', function() {
const options = {
host: 'localhost',
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);
this.destroy();
const assert = require('assert');
const https = require('https');
const dns = require('dns');

// In a more perfect world, we could just use `localhost` for the host.
// Alas, we live in a world where some distributions do not map localhost
// to ::1 by default. See https://github.com/nodejs/node/issues/7288

var index = 0;
checkForLocalhost(common.localIPv6Hosts[index]);

function checkForLocalhost(host) {
dns.lookup(host, 6, (err, address) => {
if (!err)
return runTest(host);
index = index + 1;
if (index < common.localIPv6Hosts.length)
return checkForLocalhost(common.localIPv6Hosts[index]);
common.fail('Could not find an IPv6 host for ::1');
});
}

const runTest = common.mustCall(function runTest(host) {
const ciphers = 'AECDH-NULL-SHA';
https.createServer({ ciphers }, common.mustCall(function(req, res) {
this.close();
res.end();
})).listen(common.PORT, '::1', common.mustCall(function() {
const options = {
host,
port: common.PORT,
family: 6,
ciphers,
rejectUnauthorized: false,
};
// Will fail with ECONNREFUSED if the address family is not honored.
https.get(options, common.mustCall(common.mustCall(function() {
assert.strictEqual('::1', this.socket.remoteAddress);
this.destroy();
})));
}));
});
57 changes: 39 additions & 18 deletions test/parallel/test-tls-connect-address-family.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,49 @@ if (!common.hasCrypto) {
return;
}

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

if (!common.hasIPv6) {
common.skip('no IPv6 support');
return;
}

const ciphers = 'AECDH-NULL-SHA';
tls.createServer({ ciphers }, function() {
this.close();
}).listen(common.PORT, '::1', function() {
const options = {
host: 'localhost',
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);
this.destroy();
const assert = require('assert');
const tls = require('tls');
const dns = require('dns');

// In a more perfect world, we could just use `localhost` for the host.
// Alas, we live in a world where some distributions do not map localhost
// to ::1 by default. See https://github.com/nodejs/node/issues/7288

var index = 0;
checkForLocalhost(common.localIPv6Hosts[index]);

function checkForLocalhost(host) {
dns.lookup(host, 6, (err, address) => {
if (!err)
return runTest(host);
index = index + 1;
if (index < common.localIPv6Hosts.length)
return checkForLocalhost(common.localIPv6Hosts[index]);
common.fail('Could not find an IPv6 host for ::1');
});
}

const runTest = common.mustCall(function(host) {
const ciphers = 'AECDH-NULL-SHA';
tls.createServer({ ciphers }, common.mustCall(function() {
this.close();
})).listen(common.PORT, '::1', common.mustCall(function() {
const options = {
host,
port: common.PORT,
family: 6,
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);
this.destroy();
}));
}));
});

0 comments on commit 00f9b60

Please sign in to comment.