1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
|
// Flags: --expose-internals
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
const common = require('../common');
const { addresses } = require('../common/internet');
const { internalBinding } = require('internal/test/binding');
const assert = require('assert');
const dns = require('dns');
const net = require('net');
const isIPv4 = net.isIPv4;
const isIPv6 = net.isIPv6;
const util = require('util');
const dnsPromises = dns.promises;
let expected = 0;
let completed = 0;
let running = false;
const queue = [];
function TEST(f) {
function next() {
const f = queue.shift();
if (f) {
running = true;
console.log(f.name);
f(done);
}
}
function done() {
running = false;
completed++;
process.nextTick(next);
}
expected++;
queue.push(f);
if (!running) {
next();
}
}
function checkWrap(req) {
assert.strictEqual(typeof req, 'object');
}
TEST(function test_reverse_bogus(done) {
dnsPromises.reverse('bogus ip')
.then(common.mustNotCall())
.catch(common.expectsError({ errno: 'EINVAL' }));
assert.throws(() => {
dns.reverse('bogus ip', common.mustNotCall());
}, /^Error: getHostByAddr EINVAL bogus ip$/);
done();
});
TEST(async function test_resolve4_ttl(done) {
function validateResult(result) {
assert.ok(result.length > 0);
for (const item of result) {
assert.strictEqual(typeof item, 'object');
assert.strictEqual(typeof item.ttl, 'number');
assert.strictEqual(typeof item.address, 'string');
assert.ok(item.ttl > 0);
assert.ok(isIPv4(item.address));
}
}
validateResult(await dnsPromises.resolve4(addresses.INET4_HOST, {
ttl: true
}));
const req = dns.resolve4(addresses.INET4_HOST, {
ttl: true
}, function(err, result) {
assert.ifError(err);
validateResult(result);
done();
});
checkWrap(req);
});
TEST(async function test_resolve6_ttl(done) {
function validateResult(result) {
assert.ok(result.length > 0);
for (const item of result) {
assert.strictEqual(typeof item, 'object');
assert.strictEqual(typeof item.ttl, 'number');
assert.strictEqual(typeof item.address, 'string');
assert.ok(item.ttl > 0);
assert.ok(isIPv6(item.address));
}
}
validateResult(await dnsPromises.resolve6(addresses.INET6_HOST, {
ttl: true
}));
const req = dns.resolve6(addresses.INET6_HOST, {
ttl: true
}, function(err, result) {
assert.ifError(err);
validateResult(result);
done();
});
checkWrap(req);
});
TEST(async function test_resolveMx(done) {
function validateResult(result) {
assert.ok(result.length > 0);
for (const item of result) {
assert.strictEqual(typeof item, 'object');
assert.ok(item.exchange);
assert.strictEqual(typeof item.exchange, 'string');
assert.strictEqual(typeof item.priority, 'number');
}
}
validateResult(await dnsPromises.resolveMx(addresses.MX_HOST));
const req = dns.resolveMx(addresses.MX_HOST, function(err, result) {
assert.ifError(err);
validateResult(result);
done();
});
checkWrap(req);
});
TEST(function test_resolveMx_failure(done) {
dnsPromises.resolveMx(addresses.INVALID_HOST)
.then(common.mustNotCall())
.catch(common.expectsError({ errno: 'ENOTFOUND' }));
const req = dns.resolveMx(addresses.INVALID_HOST, function(err, result) {
assert.ok(err instanceof Error);
assert.strictEqual(err.errno, 'ENOTFOUND');
assert.strictEqual(result, undefined);
done();
});
checkWrap(req);
});
TEST(async function test_resolveNs(done) {
function validateResult(result) {
assert.ok(result.length > 0);
for (const item of result) {
assert.ok(item);
assert.strictEqual(typeof item, 'string');
}
}
validateResult(await dnsPromises.resolveNs(addresses.NS_HOST));
const req = dns.resolveNs(addresses.NS_HOST, function(err, names) {
assert.ifError(err);
validateResult(names);
done();
});
checkWrap(req);
});
TEST(function test_resolveNs_failure(done) {
dnsPromises.resolveNs(addresses.INVALID_HOST)
.then(common.mustNotCall())
.catch(common.expectsError({ errno: 'ENOTFOUND' }));
const req = dns.resolveNs(addresses.INVALID_HOST, function(err, result) {
assert.ok(err instanceof Error);
assert.strictEqual(err.errno, 'ENOTFOUND');
assert.strictEqual(result, undefined);
done();
});
checkWrap(req);
});
TEST(async function test_resolveSrv(done) {
function validateResult(result) {
assert.ok(result.length > 0);
for (const item of result) {
assert.strictEqual(typeof item, 'object');
assert.ok(item.name);
assert.strictEqual(typeof item.name, 'string');
assert.strictEqual(typeof item.port, 'number');
assert.strictEqual(typeof item.priority, 'number');
assert.strictEqual(typeof item.weight, 'number');
}
}
validateResult(await dnsPromises.resolveSrv(addresses.SRV_HOST));
const req = dns.resolveSrv(addresses.SRV_HOST, function(err, result) {
assert.ifError(err);
validateResult(result);
done();
});
checkWrap(req);
});
TEST(function test_resolveSrv_failure(done) {
dnsPromises.resolveSrv(addresses.INVALID_HOST)
.then(common.mustNotCall())
.catch(common.expectsError({ errno: 'ENOTFOUND' }));
const req = dns.resolveSrv(addresses.INVALID_HOST, function(err, result) {
assert.ok(err instanceof Error);
assert.strictEqual(err.errno, 'ENOTFOUND');
assert.strictEqual(result, undefined);
done();
});
checkWrap(req);
});
TEST(async function test_resolvePtr(done) {
function validateResult(result) {
assert.ok(result.length > 0);
for (const item of result) {
assert.ok(item);
assert.strictEqual(typeof item, 'string');
}
}
validateResult(await dnsPromises.resolvePtr(addresses.PTR_HOST));
const req = dns.resolvePtr(addresses.PTR_HOST, function(err, result) {
assert.ifError(err);
validateResult(result);
done();
});
checkWrap(req);
});
TEST(function test_resolvePtr_failure(done) {
dnsPromises.resolvePtr(addresses.INVALID_HOST)
.then(common.mustNotCall())
.catch(common.expectsError({ errno: 'ENOTFOUND' }));
const req = dns.resolvePtr(addresses.INVALID_HOST, function(err, result) {
assert.ok(err instanceof Error);
assert.strictEqual(err.errno, 'ENOTFOUND');
assert.strictEqual(result, undefined);
done();
});
checkWrap(req);
});
TEST(async function test_resolveNaptr(done) {
function validateResult(result) {
assert.ok(result.length > 0);
for (const item of result) {
assert.strictEqual(typeof item, 'object');
assert.strictEqual(typeof item.flags, 'string');
assert.strictEqual(typeof item.service, 'string');
assert.strictEqual(typeof item.regexp, 'string');
assert.strictEqual(typeof item.replacement, 'string');
assert.strictEqual(typeof item.order, 'number');
assert.strictEqual(typeof item.preference, 'number');
}
}
validateResult(await dnsPromises.resolveNaptr(addresses.NAPTR_HOST));
const req = dns.resolveNaptr(addresses.NAPTR_HOST, function(err, result) {
assert.ifError(err);
validateResult(result);
done();
});
checkWrap(req);
});
TEST(function test_resolveNaptr_failure(done) {
dnsPromises.resolveNaptr(addresses.INVALID_HOST)
.then(common.mustNotCall())
.catch(common.expectsError({ errno: 'ENOTFOUND' }));
const req = dns.resolveNaptr(addresses.INVALID_HOST, function(err, result) {
assert.ok(err instanceof Error);
assert.strictEqual(err.errno, 'ENOTFOUND');
assert.strictEqual(result, undefined);
done();
});
checkWrap(req);
});
TEST(async function test_resolveSoa(done) {
function validateResult(result) {
assert.strictEqual(typeof result, 'object');
assert.strictEqual(typeof result.nsname, 'string');
assert.ok(result.nsname.length > 0);
assert.strictEqual(typeof result.hostmaster, 'string');
assert.ok(result.hostmaster.length > 0);
assert.strictEqual(typeof result.serial, 'number');
assert.ok((result.serial > 0) && (result.serial < 4294967295));
assert.strictEqual(typeof result.refresh, 'number');
assert.ok((result.refresh > 0) && (result.refresh < 2147483647));
assert.strictEqual(typeof result.retry, 'number');
assert.ok((result.retry > 0) && (result.retry < 2147483647));
assert.strictEqual(typeof result.expire, 'number');
assert.ok((result.expire > 0) && (result.expire < 2147483647));
assert.strictEqual(typeof result.minttl, 'number');
assert.ok((result.minttl >= 0) && (result.minttl < 2147483647));
}
validateResult(await dnsPromises.resolveSoa(addresses.SOA_HOST));
const req = dns.resolveSoa(addresses.SOA_HOST, function(err, result) {
assert.ifError(err);
validateResult(result);
done();
});
checkWrap(req);
});
TEST(function test_resolveSoa_failure(done) {
dnsPromises.resolveSoa(addresses.INVALID_HOST)
.then(common.mustNotCall())
.catch(common.expectsError({ errno: 'ENOTFOUND' }));
const req = dns.resolveSoa(addresses.INVALID_HOST, function(err, result) {
assert.ok(err instanceof Error);
assert.strictEqual(err.errno, 'ENOTFOUND');
assert.strictEqual(result, undefined);
done();
});
checkWrap(req);
});
TEST(async function test_resolveCname(done) {
function validateResult(result) {
assert.ok(result.length > 0);
for (const item of result) {
assert.ok(item);
assert.strictEqual(typeof item, 'string');
}
}
validateResult(await dnsPromises.resolveCname(addresses.CNAME_HOST));
const req = dns.resolveCname(addresses.CNAME_HOST, function(err, names) {
assert.ifError(err);
validateResult(names);
done();
});
checkWrap(req);
});
TEST(function test_resolveCname_failure(done) {
dnsPromises.resolveCname(addresses.INVALID_HOST)
.then(common.mustNotCall())
.catch(common.expectsError({ errno: 'ENOTFOUND' }));
const req = dns.resolveCname(addresses.INVALID_HOST, function(err, result) {
assert.ok(err instanceof Error);
assert.strictEqual(err.errno, 'ENOTFOUND');
assert.strictEqual(result, undefined);
done();
});
checkWrap(req);
});
TEST(async function test_resolveTxt(done) {
function validateResult(result) {
assert.ok(Array.isArray(result[0]));
assert.strictEqual(result.length, 1);
assert(result[0][0].startsWith('v=spf1'));
}
validateResult(await dnsPromises.resolveTxt(addresses.TXT_HOST));
const req = dns.resolveTxt(addresses.TXT_HOST, function(err, records) {
assert.ifError(err);
validateResult(records);
done();
});
checkWrap(req);
});
TEST(function test_resolveTxt_failure(done) {
dnsPromises.resolveTxt(addresses.INVALID_HOST)
.then(common.mustNotCall())
.catch(common.expectsError({ errno: 'ENOTFOUND' }));
const req = dns.resolveTxt(addresses.INVALID_HOST, function(err, result) {
assert.ok(err instanceof Error);
assert.strictEqual(err.errno, 'ENOTFOUND');
assert.strictEqual(result, undefined);
done();
});
checkWrap(req);
});
TEST(function test_lookup_failure(done) {
dnsPromises.lookup(addresses.INVALID_HOST, 4)
.then(common.mustNotCall())
.catch(common.expectsError({ errno: dns.NOTFOUND }));
const req = dns.lookup(addresses.INVALID_HOST, 4, (err) => {
assert.ok(err instanceof Error);
assert.strictEqual(err.errno, dns.NOTFOUND);
assert.strictEqual(err.errno, 'ENOTFOUND');
assert.ok(!/ENOENT/.test(err.message));
assert.ok(err.message.includes(addresses.INVALID_HOST));
done();
});
checkWrap(req);
});
TEST(async function test_lookup_ip_all(done) {
function validateResult(result) {
assert.ok(Array.isArray(result));
assert.ok(result.length > 0);
assert.strictEqual(result[0].address, '127.0.0.1');
assert.strictEqual(result[0].family, 4);
}
validateResult(await dnsPromises.lookup('127.0.0.1', { all: true }));
const req = dns.lookup(
'127.0.0.1',
{ all: true },
function(err, ips, family) {
assert.ifError(err);
assert.strictEqual(family, undefined);
validateResult(ips);
done();
}
);
checkWrap(req);
});
TEST(function test_lookup_ip_all_promise(done) {
const req = util.promisify(dns.lookup)('127.0.0.1', { all: true })
.then(function(ips) {
assert.ok(Array.isArray(ips));
assert.ok(ips.length > 0);
assert.strictEqual(ips[0].address, '127.0.0.1');
assert.strictEqual(ips[0].family, 4);
done();
});
checkWrap(req);
});
TEST(function test_lookup_ip_promise(done) {
util.promisify(dns.lookup)('127.0.0.1')
.then(function({ address, family }) {
assert.strictEqual(address, '127.0.0.1');
assert.strictEqual(family, 4);
done();
});
});
TEST(async function test_lookup_null_all(done) {
assert.deepStrictEqual(await dnsPromises.lookup(null, { all: true }), []);
const req = dns.lookup(null, { all: true }, (err, ips) => {
assert.ifError(err);
assert.ok(Array.isArray(ips));
assert.strictEqual(ips.length, 0);
done();
});
checkWrap(req);
});
TEST(async function test_lookup_all_mixed(done) {
function validateResult(result) {
assert.ok(Array.isArray(result));
assert.ok(result.length > 0);
result.forEach(function(ip) {
if (isIPv4(ip.address))
assert.strictEqual(ip.family, 4);
else if (isIPv6(ip.address))
assert.strictEqual(ip.family, 6);
else
assert.fail('unexpected IP address');
});
}
validateResult(await dnsPromises.lookup(addresses.INET_HOST, { all: true }));
const req = dns.lookup(addresses.INET_HOST, {
all: true
}, function(err, ips) {
assert.ifError(err);
validateResult(ips);
done();
});
checkWrap(req);
});
TEST(function test_lookupservice_invalid(done) {
dnsPromises.lookupService('1.2.3.4', 80)
.then(common.mustNotCall())
.catch(common.expectsError({ code: 'ENOTFOUND' }));
const req = dns.lookupService('1.2.3.4', 80, (err) => {
assert(err instanceof Error);
assert.strictEqual(err.code, 'ENOTFOUND');
assert.ok(/1\.2\.3\.4/.test(err.message));
done();
});
checkWrap(req);
});
TEST(function test_reverse_failure(done) {
dnsPromises.reverse('203.0.113.0')
.then(common.mustNotCall())
.catch(common.expectsError({
code: 'ENOTFOUND',
hostname: '203.0.113.0'
}));
// 203.0.113.0/24 are addresses reserved for (RFC) documentation use only
const req = dns.reverse('203.0.113.0', function(err) {
assert(err instanceof Error);
assert.strictEqual(err.code, 'ENOTFOUND'); // Silly error code...
assert.strictEqual(err.hostname, '203.0.113.0');
assert.ok(/203\.0\.113\.0/.test(err.message));
done();
});
checkWrap(req);
});
TEST(function test_lookup_failure(done) {
dnsPromises.lookup(addresses.INVALID_HOST)
.then(common.mustNotCall())
.catch(common.expectsError({
code: 'ENOTFOUND',
hostname: addresses.INVALID_HOST
}));
const req = dns.lookup(addresses.INVALID_HOST, (err) => {
assert(err instanceof Error);
assert.strictEqual(err.code, 'ENOTFOUND'); // Silly error code...
assert.strictEqual(err.hostname, addresses.INVALID_HOST);
assert.ok(err.message.includes(addresses.INVALID_HOST));
done();
});
checkWrap(req);
});
TEST(function test_resolve_failure(done) {
const req = dns.resolve4(addresses.INVALID_HOST, (err) => {
assert(err instanceof Error);
switch (err.code) {
case 'ENOTFOUND':
case 'ESERVFAIL':
break;
default:
assert.strictEqual(err.code, 'ENOTFOUND'); // Silly error code...
break;
}
assert.strictEqual(err.hostname, addresses.INVALID_HOST);
assert.ok(err.message.includes(addresses.INVALID_HOST));
done();
});
checkWrap(req);
});
let getaddrinfoCallbackCalled = false;
console.log(`looking up ${addresses.INET4_HOST}..`);
const cares = internalBinding('cares_wrap');
const req = new cares.GetAddrInfoReqWrap();
cares.getaddrinfo(req, addresses.INET4_HOST, 4,
/* hints */ 0, /* verbatim */ true);
req.oncomplete = function(err, domains) {
assert.strictEqual(err, 0);
console.log(`${addresses.INET4_HOST} = ${domains}`);
assert.ok(Array.isArray(domains));
assert.ok(domains.length >= 1);
assert.strictEqual(typeof domains[0], 'string');
getaddrinfoCallbackCalled = true;
};
process.on('exit', function() {
console.log(`${completed} tests completed`);
assert.strictEqual(running, false);
assert.strictEqual(completed, expected);
assert.ok(getaddrinfoCallbackCalled);
});
// Should not throw.
dns.lookup(addresses.INET6_HOST, 6, common.mustCall());
dns.lookup(addresses.INET_HOST, {}, common.mustCall());
dns.lookupService('0.0.0.0', '0', common.mustCall());
dns.lookupService('0.0.0.0', 0, common.mustCall());
(async function() {
await dnsPromises.lookup(addresses.INET6_HOST, 6);
await dnsPromises.lookup(addresses.INET_HOST, {});
})();
|