Skip to content

Commit 852313a

Browse files
committed
test: try other ipv6 localhost alternatives
PR-URL: #4325 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 25776f3 commit 852313a

File tree

2 files changed

+68
-41
lines changed

2 files changed

+68
-41
lines changed

test/common.js

+13
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ var opensslCli = null;
8181
var inFreeBSDJail = null;
8282
var localhostIPv4 = null;
8383

84+
exports.localIPv6Hosts = [
85+
// Debian/Ubuntu
86+
'ip6-localhost',
87+
'ip6-loopback',
88+
89+
// SUSE
90+
'ipv6-localhost',
91+
'ipv6-loopback',
92+
93+
// Typically universal
94+
'localhost',
95+
];
96+
8497
Object.defineProperty(exports, 'inFreeBSDJail', {
8598
get: function() {
8699
if (inFreeBSDJail !== null) return inFreeBSDJail;
+55-41
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
2-
var common = require('../common');
3-
var assert = require('assert');
4-
var net = require('net');
5-
var dns = require('dns');
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const net = require('net');
5+
const dns = require('dns');
66

77
if (!common.hasIPv6) {
88
console.log('1..0 # Skipped: no IPv6 support');
@@ -12,46 +12,60 @@ if (!common.hasIPv6) {
1212
var serverGotEnd = false;
1313
var clientGotEnd = false;
1414

15-
dns.lookup('localhost', 6, function(err) {
16-
if (err) {
17-
console.error('Looks like IPv6 is not really supported');
18-
console.error(err);
19-
return;
20-
}
15+
const hosts = common.localIPv6Hosts;
16+
var hostIdx = 0;
17+
var host = hosts[hostIdx];
18+
var localhostTries = 10;
2119

22-
var server = net.createServer({allowHalfOpen: true}, function(socket) {
23-
socket.resume();
24-
socket.on('end', function() {
25-
serverGotEnd = true;
26-
});
27-
socket.end();
20+
const server = net.createServer({allowHalfOpen: true}, function(socket) {
21+
socket.resume();
22+
socket.on('end', function() {
23+
serverGotEnd = true;
2824
});
25+
socket.end();
26+
});
2927

30-
server.listen(common.PORT, '::1', function() {
31-
var client = net.connect({
32-
host: 'localhost',
33-
port: common.PORT,
34-
family: 6,
35-
allowHalfOpen: true
36-
}, function() {
37-
console.error('client connect cb');
38-
client.resume();
39-
client.on('end', function() {
40-
clientGotEnd = true;
41-
setTimeout(function() {
42-
assert(client.writable);
43-
client.end();
44-
}, 10);
45-
});
46-
client.on('close', function() {
47-
server.close();
48-
});
28+
server.listen(common.PORT, '::1', tryConnect);
29+
30+
function tryConnect() {
31+
const client = net.connect({
32+
host: host,
33+
port: common.PORT,
34+
family: 6,
35+
allowHalfOpen: true
36+
}, function() {
37+
console.error('client connect cb');
38+
client.resume();
39+
client.on('end', function() {
40+
clientGotEnd = true;
41+
setTimeout(function() {
42+
assert(client.writable);
43+
client.end();
44+
}, 10);
45+
});
46+
client.on('close', function() {
47+
server.close();
4948
});
49+
}).on('error', function(err) {
50+
if (err.syscall === 'getaddrinfo' && err.code === 'ENOTFOUND') {
51+
if (host !== 'localhost' || --localhostTries === 0)
52+
host = hosts[++hostIdx];
53+
if (host)
54+
tryConnect();
55+
else {
56+
console.log('1..0 # Skipped: no IPv6 localhost support');
57+
process.removeListener('exit', onExit);
58+
server.close();
59+
}
60+
return;
61+
}
62+
throw err;
5063
});
64+
}
5165

52-
process.on('exit', function() {
53-
console.error('exit', serverGotEnd, clientGotEnd);
54-
assert(serverGotEnd);
55-
assert(clientGotEnd);
56-
});
57-
});
66+
process.on('exit', onExit);
67+
function onExit() {
68+
console.error('exit', serverGotEnd, clientGotEnd);
69+
assert(serverGotEnd);
70+
assert(clientGotEnd);
71+
}

0 commit comments

Comments
 (0)