1'use strict'; 2const common = require('../common'); 3const { addresses } = require('../common/internet'); 4if (!common.hasIPv6) 5 common.skip('this test, no IPv6 support'); 6 7const assert = require('assert'); 8const dns = require('dns'); 9const net = require('net'); 10const dnsPromises = dns.promises; 11const isIPv6 = net.isIPv6; 12 13let running = false; 14const queue = []; 15 16function TEST(f) { 17 function next() { 18 const f = queue.shift(); 19 if (f) { 20 running = true; 21 console.log(f.name); 22 f(done); 23 } 24 } 25 26 function done() { 27 running = false; 28 process.nextTick(next); 29 } 30 31 queue.push(f); 32 33 if (!running) { 34 next(); 35 } 36} 37 38function checkWrap(req) { 39 assert.ok(typeof req === 'object'); 40} 41 42TEST(async function test_resolve6(done) { 43 function validateResult(res) { 44 assert.ok(res.length > 0); 45 46 for (let i = 0; i < res.length; i++) { 47 assert.ok(isIPv6(res[i])); 48 } 49 } 50 51 validateResult(await dnsPromises.resolve6(addresses.INET6_HOST)); 52 53 const req = dns.resolve6( 54 addresses.INET6_HOST, 55 common.mustSucceed((ips) => { 56 validateResult(ips); 57 done(); 58 })); 59 60 checkWrap(req); 61}); 62 63TEST(async function test_reverse_ipv6(done) { 64 function validateResult(res) { 65 assert.ok(res.length > 0); 66 67 for (let i = 0; i < res.length; i++) { 68 assert.ok(typeof res[i] === 'string'); 69 } 70 } 71 72 validateResult(await dnsPromises.reverse(addresses.INET6_IP)); 73 74 const req = dns.reverse( 75 addresses.INET6_IP, 76 common.mustSucceed((domains) => { 77 validateResult(domains); 78 done(); 79 })); 80 81 checkWrap(req); 82}); 83 84TEST(async function test_lookup_ipv6_explicit(done) { 85 function validateResult(res) { 86 assert.ok(isIPv6(res.address)); 87 assert.strictEqual(res.family, 6); 88 } 89 90 validateResult(await dnsPromises.lookup(addresses.INET6_HOST, 6)); 91 92 const req = dns.lookup( 93 addresses.INET6_HOST, 94 6, 95 common.mustSucceed((ip, family) => { 96 validateResult({ address: ip, family }); 97 done(); 98 })); 99 100 checkWrap(req); 101}); 102 103// This ends up just being too problematic to test 104// TEST(function test_lookup_ipv6_implicit(done) { 105// var req = dns.lookup(addresses.INET6_HOST, function(err, ip, family) { 106// assert.ifError(err); 107// assert.ok(net.isIPv6(ip)); 108// assert.strictEqual(family, 6); 109 110// done(); 111// }); 112 113// checkWrap(req); 114// }); 115 116TEST(async function test_lookup_ipv6_explicit_object(done) { 117 function validateResult(res) { 118 assert.ok(isIPv6(res.address)); 119 assert.strictEqual(res.family, 6); 120 } 121 122 validateResult(await dnsPromises.lookup(addresses.INET6_HOST, { family: 6 })); 123 124 const req = dns.lookup(addresses.INET6_HOST, { 125 family: 6 126 }, common.mustSucceed((ip, family) => { 127 validateResult({ address: ip, family }); 128 done(); 129 })); 130 131 checkWrap(req); 132}); 133 134TEST(function test_lookup_ipv6_hint(done) { 135 const req = dns.lookup(addresses.INET6_HOST, { 136 family: 6, 137 hints: dns.V4MAPPED 138 }, common.mustCall((err, ip, family) => { 139 if (err) { 140 // FreeBSD does not support V4MAPPED 141 if (common.isFreeBSD) { 142 assert(err instanceof Error); 143 assert.strictEqual(err.code, 'EAI_BADFLAGS'); 144 assert.strictEqual(err.hostname, addresses.INET_HOST); 145 assert.ok(/getaddrinfo EAI_BADFLAGS/.test(err.message)); 146 done(); 147 return; 148 } 149 150 assert.ifError(err); 151 } 152 153 assert.ok(isIPv6(ip)); 154 assert.strictEqual(family, 6); 155 156 done(); 157 })); 158 159 checkWrap(req); 160}); 161 162TEST(async function test_lookup_ip_ipv6(done) { 163 function validateResult(res) { 164 assert.ok(isIPv6(res.address)); 165 assert.strictEqual(res.family, 6); 166 } 167 168 validateResult(await dnsPromises.lookup('::1')); 169 170 const req = dns.lookup( 171 '::1', 172 common.mustSucceed((ip, family) => { 173 validateResult({ address: ip, family }); 174 done(); 175 })); 176 177 checkWrap(req); 178}); 179 180TEST(async function test_lookup_all_ipv6(done) { 181 function validateResult(res) { 182 assert.ok(Array.isArray(res)); 183 assert.ok(res.length > 0); 184 185 res.forEach((ip) => { 186 assert.ok(isIPv6(ip.address), 187 `Invalid IPv6: ${ip.address.toString()}`); 188 assert.strictEqual(ip.family, 6); 189 }); 190 } 191 192 validateResult(await dnsPromises.lookup(addresses.INET6_HOST, { 193 all: true, 194 family: 6 195 })); 196 197 const req = dns.lookup( 198 addresses.INET6_HOST, 199 { all: true, family: 6 }, 200 common.mustSucceed((ips) => { 201 validateResult(ips); 202 done(); 203 }) 204 ); 205 206 checkWrap(req); 207}); 208 209TEST(function test_lookupservice_ip_ipv6(done) { 210 const req = dns.lookupService( 211 '::1', 80, 212 common.mustCall((err, host, service) => { 213 if (err) { 214 // Not skipping the test, rather checking an alternative result, 215 // i.e. that ::1 may not be configured (e.g. in /etc/hosts) 216 assert.strictEqual(err.code, 'ENOTFOUND'); 217 return done(); 218 } 219 assert.strictEqual(typeof host, 'string'); 220 assert(host); 221 assert(['http', 'www', '80'].includes(service)); 222 done(); 223 }) 224 ); 225 226 checkWrap(req); 227}); 228 229// Disabled because it appears to be not working on Linux. 230// TEST(function test_lookup_localhost_ipv6(done) { 231// var req = dns.lookup('localhost', 6, function(err, ip, family) { 232// assert.ifError(err); 233// assert.ok(net.isIPv6(ip)); 234// assert.strictEqual(family, 6); 235// 236// done(); 237// }); 238// 239// checkWrap(req); 240// }); 241