11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst net = require('net'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_cifunction check(addressType, cb) { 71cb0ef41Sopenharmony_ci const server = net.createServer(function(client) { 81cb0ef41Sopenharmony_ci client.end(); 91cb0ef41Sopenharmony_ci server.close(); 101cb0ef41Sopenharmony_ci cb && cb(); 111cb0ef41Sopenharmony_ci }); 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci const address = addressType === 4 ? common.localhostIPv4 : '::1'; 141cb0ef41Sopenharmony_ci server.listen(0, address, common.mustCall(function() { 151cb0ef41Sopenharmony_ci net.connect({ 161cb0ef41Sopenharmony_ci port: this.address().port, 171cb0ef41Sopenharmony_ci host: 'localhost', 181cb0ef41Sopenharmony_ci family: addressType, 191cb0ef41Sopenharmony_ci lookup: lookup 201cb0ef41Sopenharmony_ci }).on('lookup', common.mustCall(function(err, ip, type) { 211cb0ef41Sopenharmony_ci assert.strictEqual(err, null); 221cb0ef41Sopenharmony_ci assert.strictEqual(address, ip); 231cb0ef41Sopenharmony_ci assert.strictEqual(type, addressType); 241cb0ef41Sopenharmony_ci })); 251cb0ef41Sopenharmony_ci })); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci function lookup(host, dnsopts, cb) { 281cb0ef41Sopenharmony_ci dnsopts.family = addressType; 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci if (addressType === 4) { 311cb0ef41Sopenharmony_ci process.nextTick(function() { 321cb0ef41Sopenharmony_ci if (dnsopts.all) { 331cb0ef41Sopenharmony_ci cb(null, [{ address: common.localhostIPv4, family: 4 }]); 341cb0ef41Sopenharmony_ci } else { 351cb0ef41Sopenharmony_ci cb(null, common.localhostIPv4, 4); 361cb0ef41Sopenharmony_ci } 371cb0ef41Sopenharmony_ci }); 381cb0ef41Sopenharmony_ci } else { 391cb0ef41Sopenharmony_ci process.nextTick(function() { 401cb0ef41Sopenharmony_ci if (dnsopts.all) { 411cb0ef41Sopenharmony_ci cb(null, [{ address: '::1', family: 6 }]); 421cb0ef41Sopenharmony_ci } else { 431cb0ef41Sopenharmony_ci cb(null, '::1', 6); 441cb0ef41Sopenharmony_ci } 451cb0ef41Sopenharmony_ci }); 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci } 481cb0ef41Sopenharmony_ci} 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_cicheck(4, function() { 511cb0ef41Sopenharmony_ci common.hasIPv6 && check(6); 521cb0ef41Sopenharmony_ci}); 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci// Verify that bad lookup() IPs are handled. 551cb0ef41Sopenharmony_ci{ 561cb0ef41Sopenharmony_ci net.connect({ 571cb0ef41Sopenharmony_ci host: 'localhost', 581cb0ef41Sopenharmony_ci port: 80, 591cb0ef41Sopenharmony_ci lookup(host, dnsopts, cb) { 601cb0ef41Sopenharmony_ci if (dnsopts.all) { 611cb0ef41Sopenharmony_ci cb(null, [{ address: undefined, family: 4 }]); 621cb0ef41Sopenharmony_ci } else { 631cb0ef41Sopenharmony_ci cb(null, undefined, 4); 641cb0ef41Sopenharmony_ci } 651cb0ef41Sopenharmony_ci } 661cb0ef41Sopenharmony_ci }).on('error', common.expectsError({ code: 'ERR_INVALID_IP_ADDRESS' })); 671cb0ef41Sopenharmony_ci} 68