• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const { addresses } = require('../common/internet');
4const assert = require('assert');
5const dns = require('dns');
6const net = require('net');
7const util = require('util');
8const isIPv4 = net.isIPv4;
9
10const dnsPromises = dns.promises;
11let running = false;
12const queue = [];
13
14function TEST(f) {
15  function next() {
16    const f = queue.shift();
17    if (f) {
18      running = true;
19      console.log(f.name);
20      f(done);
21    }
22  }
23
24  function done() {
25    running = false;
26    process.nextTick(next);
27  }
28
29  queue.push(f);
30
31  if (!running) {
32    next();
33  }
34}
35
36function checkWrap(req) {
37  assert.ok(typeof req === 'object');
38}
39
40TEST(async function test_resolve4(done) {
41  function validateResult(res) {
42    assert.ok(res.length > 0);
43
44    for (let i = 0; i < res.length; i++) {
45      assert.ok(isIPv4(res[i]));
46    }
47  }
48
49  validateResult(await dnsPromises.resolve4(addresses.INET4_HOST));
50
51  const req = dns.resolve4(
52    addresses.INET4_HOST,
53    common.mustSucceed((ips) => {
54      validateResult(ips);
55      done();
56    }));
57
58  checkWrap(req);
59});
60
61TEST(async function test_reverse_ipv4(done) {
62  function validateResult(res) {
63    assert.ok(res.length > 0);
64
65    for (let i = 0; i < res.length; i++) {
66      assert.ok(res[i]);
67      assert.ok(typeof res[i] === 'string');
68    }
69  }
70
71  validateResult(await dnsPromises.reverse(addresses.INET4_IP));
72
73  const req = dns.reverse(
74    addresses.INET4_IP,
75    common.mustSucceed((domains) => {
76      validateResult(domains);
77      done();
78    }));
79
80  checkWrap(req);
81});
82
83TEST(async function test_lookup_ipv4_explicit(done) {
84  function validateResult(res) {
85    assert.ok(net.isIPv4(res.address));
86    assert.strictEqual(res.family, 4);
87  }
88
89  validateResult(await dnsPromises.lookup(addresses.INET4_HOST, 4));
90
91  const req = dns.lookup(
92    addresses.INET4_HOST, 4,
93    common.mustSucceed((ip, family) => {
94      validateResult({ address: ip, family });
95      done();
96    }));
97
98  checkWrap(req);
99});
100
101TEST(async function test_lookup_ipv4_implicit(done) {
102  function validateResult(res) {
103    assert.ok(net.isIPv4(res.address));
104    assert.strictEqual(res.family, 4);
105  }
106
107  validateResult(await dnsPromises.lookup(addresses.INET4_HOST));
108
109  const req = dns.lookup(
110    addresses.INET4_HOST,
111    common.mustSucceed((ip, family) => {
112      validateResult({ address: ip, family });
113      done();
114    }));
115
116  checkWrap(req);
117});
118
119TEST(async function test_lookup_ipv4_explicit_object(done) {
120  function validateResult(res) {
121    assert.ok(net.isIPv4(res.address));
122    assert.strictEqual(res.family, 4);
123  }
124
125  validateResult(await dnsPromises.lookup(addresses.INET4_HOST, { family: 4 }));
126
127  const req = dns.lookup(addresses.INET4_HOST, {
128    family: 4
129  }, common.mustSucceed((ip, family) => {
130    validateResult({ address: ip, family });
131    done();
132  }));
133
134  checkWrap(req);
135});
136
137TEST(async function test_lookup_ipv4_hint_addrconfig(done) {
138  function validateResult(res) {
139    assert.ok(net.isIPv4(res.address));
140    assert.strictEqual(res.family, 4);
141  }
142
143  validateResult(await dnsPromises.lookup(addresses.INET4_HOST, {
144    hints: dns.ADDRCONFIG
145  }));
146
147  const req = dns.lookup(addresses.INET4_HOST, {
148    hints: dns.ADDRCONFIG
149  }, common.mustSucceed((ip, family) => {
150    validateResult({ address: ip, family });
151    done();
152  }));
153
154  checkWrap(req);
155});
156
157TEST(async function test_lookup_ip_ipv4(done) {
158  function validateResult(res) {
159    assert.strictEqual(res.address, '127.0.0.1');
160    assert.strictEqual(res.family, 4);
161  }
162
163  validateResult(await dnsPromises.lookup('127.0.0.1'));
164
165  const req = dns.lookup('127.0.0.1',
166                         common.mustSucceed((ip, family) => {
167                           validateResult({ address: ip, family });
168                           done();
169                         }));
170
171  checkWrap(req);
172});
173
174TEST(async function test_lookup_localhost_ipv4(done) {
175  function validateResult(res) {
176    assert.strictEqual(res.address, '127.0.0.1');
177    assert.strictEqual(res.family, 4);
178  }
179
180  validateResult(await dnsPromises.lookup('localhost', 4));
181
182  const req = dns.lookup('localhost', 4,
183                         common.mustSucceed((ip, family) => {
184                           validateResult({ address: ip, family });
185                           done();
186                         }));
187
188  checkWrap(req);
189});
190
191TEST(async function test_lookup_all_ipv4(done) {
192  function validateResult(res) {
193    assert.ok(Array.isArray(res));
194    assert.ok(res.length > 0);
195
196    res.forEach((ip) => {
197      assert.ok(isIPv4(ip.address));
198      assert.strictEqual(ip.family, 4);
199    });
200  }
201
202  validateResult(await dnsPromises.lookup(addresses.INET4_HOST, {
203    all: true,
204    family: 4
205  }));
206
207  const req = dns.lookup(
208    addresses.INET4_HOST,
209    { all: true, family: 4 },
210    common.mustSucceed((ips) => {
211      validateResult(ips);
212      done();
213    })
214  );
215
216  checkWrap(req);
217});
218
219TEST(async function test_lookupservice_ip_ipv4(done) {
220  function validateResult(res) {
221    assert.strictEqual(typeof res.hostname, 'string');
222    assert(res.hostname);
223    assert(['http', 'www', '80'].includes(res.service));
224  }
225
226  validateResult(await dnsPromises.lookupService('127.0.0.1', 80));
227
228  const req = dns.lookupService(
229    '127.0.0.1', 80,
230    common.mustSucceed((hostname, service) => {
231      validateResult({ hostname, service });
232      done();
233    })
234  );
235
236  checkWrap(req);
237});
238
239TEST(function test_lookupservice_ip_ipv4_promise(done) {
240  util.promisify(dns.lookupService)('127.0.0.1', 80)
241    .then(common.mustCall(({ hostname, service }) => {
242      assert.strictEqual(typeof hostname, 'string');
243      assert(hostname.length > 0);
244      assert(['http', 'www', '80'].includes(service));
245      done();
246    }));
247});
248