• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.mustCall((err, ips) => {
56      assert.ifError(err);
57      validateResult(ips);
58      done();
59    }));
60
61  checkWrap(req);
62});
63
64TEST(async function test_reverse_ipv6(done) {
65  function validateResult(res) {
66    assert.ok(res.length > 0);
67
68    for (let i = 0; i < res.length; i++) {
69      assert.ok(typeof res[i] === 'string');
70    }
71  }
72
73  validateResult(await dnsPromises.reverse(addresses.INET6_IP));
74
75  const req = dns.reverse(
76    addresses.INET6_IP,
77    common.mustCall((err, domains) => {
78      assert.ifError(err);
79      validateResult(domains);
80      done();
81    }));
82
83  checkWrap(req);
84});
85
86TEST(async function test_lookup_ipv6_explicit(done) {
87  function validateResult(res) {
88    assert.ok(isIPv6(res.address));
89    assert.strictEqual(res.family, 6);
90  }
91
92  validateResult(await dnsPromises.lookup(addresses.INET6_HOST, 6));
93
94  const req = dns.lookup(
95    addresses.INET6_HOST,
96    6,
97    common.mustCall((err, ip, family) => {
98      assert.ifError(err);
99      validateResult({ address: ip, family });
100      done();
101    }));
102
103  checkWrap(req);
104});
105
106/* This ends up just being too problematic to test
107TEST(function test_lookup_ipv6_implicit(done) {
108  var req = dns.lookup(addresses.INET6_HOST, function(err, ip, family) {
109    assert.ifError(err);
110    assert.ok(net.isIPv6(ip));
111    assert.strictEqual(family, 6);
112
113    done();
114  });
115
116  checkWrap(req);
117});
118*/
119
120TEST(async function test_lookup_ipv6_explicit_object(done) {
121  function validateResult(res) {
122    assert.ok(isIPv6(res.address));
123    assert.strictEqual(res.family, 6);
124  }
125
126  validateResult(await dnsPromises.lookup(addresses.INET6_HOST, { family: 6 }));
127
128  const req = dns.lookup(addresses.INET6_HOST, {
129    family: 6
130  }, common.mustCall((err, ip, family) => {
131    assert.ifError(err);
132    validateResult({ address: ip, family });
133    done();
134  }));
135
136  checkWrap(req);
137});
138
139TEST(function test_lookup_ipv6_hint(done) {
140  const req = dns.lookup(addresses.INET6_HOST, {
141    family: 6,
142    hints: dns.V4MAPPED
143  }, common.mustCall((err, ip, family) => {
144    if (err) {
145      // FreeBSD does not support V4MAPPED
146      if (common.isFreeBSD) {
147        assert(err instanceof Error);
148        assert.strictEqual(err.code, 'EAI_BADFLAGS');
149        assert.strictEqual(err.hostname, addresses.INET_HOST);
150        assert.ok(/getaddrinfo EAI_BADFLAGS/.test(err.message));
151        done();
152        return;
153      }
154
155      assert.ifError(err);
156    }
157
158    assert.ok(isIPv6(ip));
159    assert.strictEqual(family, 6);
160
161    done();
162  }));
163
164  checkWrap(req);
165});
166
167TEST(async function test_lookup_ip_ipv6(done) {
168  function validateResult(res) {
169    assert.ok(isIPv6(res.address));
170    assert.strictEqual(res.family, 6);
171  }
172
173  validateResult(await dnsPromises.lookup('::1'));
174
175  const req = dns.lookup(
176    '::1',
177    common.mustCall((err, ip, family) => {
178      assert.ifError(err);
179      validateResult({ address: ip, family });
180      done();
181    }));
182
183  checkWrap(req);
184});
185
186TEST(async function test_lookup_all_ipv6(done) {
187  function validateResult(res) {
188    assert.ok(Array.isArray(res));
189    assert.ok(res.length > 0);
190
191    res.forEach((ip) => {
192      assert.ok(isIPv6(ip.address),
193                `Invalid IPv6: ${ip.address.toString()}`);
194      assert.strictEqual(ip.family, 6);
195    });
196  }
197
198  validateResult(await dnsPromises.lookup(addresses.INET6_HOST, {
199    all: true,
200    family: 6
201  }));
202
203  const req = dns.lookup(
204    addresses.INET6_HOST,
205    { all: true, family: 6 },
206    common.mustCall((err, ips) => {
207      assert.ifError(err);
208      validateResult(ips);
209      done();
210    })
211  );
212
213  checkWrap(req);
214});
215
216TEST(function test_lookupservice_ip_ipv6(done) {
217  const req = dns.lookupService(
218    '::1', 80,
219    common.mustCall((err, host, service) => {
220      if (err) {
221        // Not skipping the test, rather checking an alternative result,
222        // i.e. that ::1 may not be configured (e.g. in /etc/hosts)
223        assert.strictEqual(err.code, 'ENOTFOUND');
224        return done();
225      }
226      assert.strictEqual(typeof host, 'string');
227      assert(host);
228      assert(['http', 'www', '80'].includes(service));
229      done();
230    })
231  );
232
233  checkWrap(req);
234});
235
236/* Disabled because it appears to be not working on linux. */
237/* TEST(function test_lookup_localhost_ipv6(done) {
238  var req = dns.lookup('localhost', 6, function(err, ip, family) {
239    assert.ifError(err);
240    assert.ok(net.isIPv6(ip));
241    assert.strictEqual(family, 6);
242
243    done();
244  });
245
246  checkWrap(req);
247}); */
248