test: try other ipv6 localhost alternatives
authorBrian White <mscdex@mscdex.net>
Thu, 17 Dec 2015 06:16:46 +0000 (01:16 -0500)
committerMyles Borins <mborins@us.ibm.com>
Tue, 19 Jan 2016 19:52:26 +0000 (11:52 -0800)
PR-URL: https://github.com/nodejs/node/pull/4325
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
test/common.js
test/parallel/test-net-connect-options-ipv6.js

index adf4247..0e824ef 100644 (file)
@@ -79,6 +79,19 @@ var opensslCli = null;
 var inFreeBSDJail = null;
 var localhostIPv4 = null;
 
+exports.localIPv6Hosts = [
+  // Debian/Ubuntu
+  'ip6-localhost',
+  'ip6-loopback',
+
+  // SUSE
+  'ipv6-localhost',
+  'ipv6-loopback',
+
+  // Typically universal
+  'localhost',
+];
+
 Object.defineProperty(exports, 'inFreeBSDJail', {
   get: function() {
     if (inFreeBSDJail !== null) return inFreeBSDJail;
index f0f7bc6..623b2ef 100644 (file)
@@ -1,8 +1,8 @@
 'use strict';
-var common = require('../common');
-var assert = require('assert');
-var net = require('net');
-var dns = require('dns');
+const common = require('../common');
+const assert = require('assert');
+const net = require('net');
+const dns = require('dns');
 
 if (!common.hasIPv6) {
   console.log('1..0 # Skipped: no IPv6 support');
@@ -12,46 +12,60 @@ if (!common.hasIPv6) {
 var serverGotEnd = false;
 var clientGotEnd = false;
 
-dns.lookup('localhost', 6, function(err) {
-  if (err) {
-    console.error('Looks like IPv6 is not really supported');
-    console.error(err);
-    return;
-  }
+const hosts = common.localIPv6Hosts;
+var hostIdx = 0;
+var host = hosts[hostIdx];
+var localhostTries = 10;
 
-  var server = net.createServer({allowHalfOpen: true}, function(socket) {
-    socket.resume();
-    socket.on('end', function() {
-      serverGotEnd = true;
-    });
-    socket.end();
+const server = net.createServer({allowHalfOpen: true}, function(socket) {
+  socket.resume();
+  socket.on('end', function() {
+    serverGotEnd = true;
   });
+  socket.end();
+});
 
-  server.listen(common.PORT, '::1', function() {
-    var client = net.connect({
-      host: 'localhost',
-      port: common.PORT,
-      family: 6,
-      allowHalfOpen: true
-    }, function() {
-      console.error('client connect cb');
-      client.resume();
-      client.on('end', function() {
-        clientGotEnd = true;
-        setTimeout(function() {
-          assert(client.writable);
-          client.end();
-        }, 10);
-      });
-      client.on('close', function() {
-        server.close();
-      });
+server.listen(common.PORT, '::1', tryConnect);
+
+function tryConnect() {
+  const client = net.connect({
+    host: host,
+    port: common.PORT,
+    family: 6,
+    allowHalfOpen: true
+  }, function() {
+    console.error('client connect cb');
+    client.resume();
+    client.on('end', function() {
+      clientGotEnd = true;
+      setTimeout(function() {
+        assert(client.writable);
+        client.end();
+      }, 10);
+    });
+    client.on('close', function() {
+      server.close();
     });
+  }).on('error', function(err) {
+    if (err.syscall === 'getaddrinfo' && err.code === 'ENOTFOUND') {
+      if (host !== 'localhost' || --localhostTries === 0)
+        host = hosts[++hostIdx];
+      if (host)
+        tryConnect();
+      else {
+        console.log('1..0 # Skipped: no IPv6 localhost support');
+        process.removeListener('exit', onExit);
+        server.close();
+      }
+      return;
+    }
+    throw err;
   });
+}
 
-  process.on('exit', function() {
-    console.error('exit', serverGotEnd, clientGotEnd);
-    assert(serverGotEnd);
-    assert(clientGotEnd);
-  });
-});
+process.on('exit', onExit);
+function onExit() {
+  console.error('exit', serverGotEnd, clientGotEnd);
+  assert(serverGotEnd);
+  assert(clientGotEnd);
+}