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 30 if (addressType === 4) { 31 process.nextTick(function() { 32 if (dnsopts.all) { 33 cb(null, [{ address: common.localhostIPv4, family: 4 }]); 34 } else { 35 cb(null, common.localhostIPv4, 4); 36 } 37 }); 38 } else { 39 process.nextTick(function() { 40 if (dnsopts.all) { 41 cb(null, [{ address: '::1', family: 6 }]); 42 } else { 43 cb(null, '::1', 6); 44 } 45 }); 46 } 47 } 48} 49 50check(4, function() { 51 common.hasIPv6 && check(6); 52}); 53 54// Verify that bad lookup() IPs are handled. 55{ 56 net.connect({ 57 host: 'localhost', 58 port: 80, 59 lookup(host, dnsopts, cb) { 60 if (dnsopts.all) { 61 cb(null, [{ address: undefined, family: 4 }]); 62 } else { 63 cb(null, undefined, 4); 64 } 65 } 66 }).on('error', common.expectsError({ code: 'ERR_INVALID_IP_ADDRESS' })); 67} 68