Skip to content

Commit 00f9b60

Browse files
committed
test: fix flaky IPv6/localhost tests
Fixes: #7288
1 parent 7cbbec5 commit 00f9b60

2 files changed

Lines changed: 79 additions & 37 deletions

File tree

test/parallel/test-https-connect-address-family.js

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,50 @@ if (!common.hasCrypto) {
55
return;
66
}
77

8-
const assert = require('assert');
9-
const https = require('https');
10-
118
if (!common.hasIPv6) {
129
common.skip('no IPv6 support');
1310
return;
1411
}
1512

16-
const ciphers = 'AECDH-NULL-SHA';
17-
https.createServer({ ciphers }, function(req, res) {
18-
this.close();
19-
res.end();
20-
}).listen(common.PORT, '::1', function() {
21-
const options = {
22-
host: 'localhost',
23-
port: common.PORT,
24-
family: 6,
25-
ciphers: ciphers,
26-
rejectUnauthorized: false,
27-
};
28-
// Will fail with ECONNREFUSED if the address family is not honored.
29-
https.get(options, common.mustCall(function() {
30-
assert.strictEqual('::1', this.socket.remoteAddress);
31-
this.destroy();
13+
const assert = require('assert');
14+
const https = require('https');
15+
const dns = require('dns');
16+
17+
// In a more perfect world, we could just use `localhost` for the host.
18+
// Alas, we live in a world where some distributions do not map localhost
19+
// to ::1 by default. See https://github.com/nodejs/node/issues/7288
20+
21+
var index = 0;
22+
checkForLocalhost(common.localIPv6Hosts[index]);
23+
24+
function checkForLocalhost(host) {
25+
dns.lookup(host, 6, (err, address) => {
26+
if (!err)
27+
return runTest(host);
28+
index = index + 1;
29+
if (index < common.localIPv6Hosts.length)
30+
return checkForLocalhost(common.localIPv6Hosts[index]);
31+
common.fail('Could not find an IPv6 host for ::1');
32+
});
33+
}
34+
35+
const runTest = common.mustCall(function runTest(host) {
36+
const ciphers = 'AECDH-NULL-SHA';
37+
https.createServer({ ciphers }, common.mustCall(function(req, res) {
38+
this.close();
39+
res.end();
40+
})).listen(common.PORT, '::1', common.mustCall(function() {
41+
const options = {
42+
host,
43+
port: common.PORT,
44+
family: 6,
45+
ciphers,
46+
rejectUnauthorized: false,
47+
};
48+
// Will fail with ECONNREFUSED if the address family is not honored.
49+
https.get(options, common.mustCall(common.mustCall(function() {
50+
assert.strictEqual('::1', this.socket.remoteAddress);
51+
this.destroy();
52+
})));
3253
}));
3354
});

test/parallel/test-tls-connect-address-family.js

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,49 @@ if (!common.hasCrypto) {
55
return;
66
}
77

8-
const assert = require('assert');
9-
const tls = require('tls');
10-
118
if (!common.hasIPv6) {
129
common.skip('no IPv6 support');
1310
return;
1411
}
1512

16-
const ciphers = 'AECDH-NULL-SHA';
17-
tls.createServer({ ciphers }, function() {
18-
this.close();
19-
}).listen(common.PORT, '::1', function() {
20-
const options = {
21-
host: 'localhost',
22-
port: common.PORT,
23-
family: 6,
24-
ciphers: ciphers,
25-
rejectUnauthorized: false,
26-
};
27-
// Will fail with ECONNREFUSED if the address family is not honored.
28-
tls.connect(options).once('secureConnect', common.mustCall(function() {
29-
assert.strictEqual('::1', this.remoteAddress);
30-
this.destroy();
13+
const assert = require('assert');
14+
const tls = require('tls');
15+
const dns = require('dns');
16+
17+
// In a more perfect world, we could just use `localhost` for the host.
18+
// Alas, we live in a world where some distributions do not map localhost
19+
// to ::1 by default. See https://github.com/nodejs/node/issues/7288
20+
21+
var index = 0;
22+
checkForLocalhost(common.localIPv6Hosts[index]);
23+
24+
function checkForLocalhost(host) {
25+
dns.lookup(host, 6, (err, address) => {
26+
if (!err)
27+
return runTest(host);
28+
index = index + 1;
29+
if (index < common.localIPv6Hosts.length)
30+
return checkForLocalhost(common.localIPv6Hosts[index]);
31+
common.fail('Could not find an IPv6 host for ::1');
32+
});
33+
}
34+
35+
const runTest = common.mustCall(function(host) {
36+
const ciphers = 'AECDH-NULL-SHA';
37+
tls.createServer({ ciphers }, common.mustCall(function() {
38+
this.close();
39+
})).listen(common.PORT, '::1', common.mustCall(function() {
40+
const options = {
41+
host,
42+
port: common.PORT,
43+
family: 6,
44+
ciphers,
45+
rejectUnauthorized: false,
46+
};
47+
// Will fail with ECONNREFUSED if the address family is not honored.
48+
tls.connect(options).once('secureConnect', common.mustCall(function() {
49+
assert.strictEqual('::1', this.remoteAddress);
50+
this.destroy();
51+
}));
3152
}));
3253
});

0 commit comments

Comments
 (0)