1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const net = require('net'); 5 6function check(addressType, cb) { 7 const server = net.createServer(function(client) { 8 client.end(); 9 server.close(); 10 cb && cb(); 11 }); 12 13 const address = addressType === 4 ? common.localhostIPv4 : '::1'; 14 server.listen(0, address, common.mustCall(function() { 15 net.connect({ 16 port: this.address().port, 17 host: 'localhost', 18 family: addressType, 19 lookup: lookup 20 }).on('lookup', common.mustCall(function(err, ip, type) { 21 assert.strictEqual(err, null); 22 assert.strictEqual(address, ip); 23 assert.strictEqual(type, addressType); 24 })); 25 })); 26 27 function lookup(host, dnsopts, cb) { 28 dnsopts.family = addressType; 29 if (addressType === 4) { 30 process.nextTick(function() { 31 cb(null, common.localhostIPv4, 4); 32 }); 33 } else { 34 process.nextTick(function() { 35 cb(null, '::1', 6); 36 }); 37 } 38 } 39} 40 41check(4, function() { 42 common.hasIPv6 && check(6); 43}); 44 45// Verify that bad lookup() IPs are handled. 46{ 47 net.connect({ 48 host: 'localhost', 49 port: 80, 50 lookup(host, dnsopts, cb) { 51 cb(null, undefined, 4); 52 } 53 }).on('error', common.expectsError({ code: 'ERR_INVALID_IP_ADDRESS' })); 54} 55