|
21 | 21 |
|
22 | 22 | 'use strict';
|
23 | 23 | const common = require('../common');
|
| 24 | +const dnstools = require('../common/dns'); |
24 | 25 | const assert = require('assert');
|
25 | 26 |
|
26 | 27 | const dns = require('dns');
|
27 | 28 | const dnsPromises = dns.promises;
|
| 29 | +const dgram = require('dgram'); |
28 | 30 |
|
29 | 31 | const existing = dns.getServers();
|
30 | 32 | assert(existing.length > 0);
|
@@ -326,3 +328,113 @@ common.expectsError(() => {
|
326 | 328 | assert.deepStrictEqual(err.message, 'queryMx ENOTFOUND foo.onion');
|
327 | 329 | });
|
328 | 330 | }
|
| 331 | + |
| 332 | +{ |
| 333 | + const cases = [ |
| 334 | + { method: 'resolveAny', |
| 335 | + answers: [ |
| 336 | + { type: 'A', address: '1.2.3.4', ttl: 3333333333 }, |
| 337 | + { type: 'AAAA', address: '::42', ttl: 3333333333 }, |
| 338 | + { type: 'MX', priority: 42, exchange: 'foobar.com', ttl: 3333333333 }, |
| 339 | + { type: 'NS', value: 'foobar.org', ttl: 3333333333 }, |
| 340 | + { type: 'PTR', value: 'baz.org', ttl: 3333333333 }, |
| 341 | + { |
| 342 | + type: 'SOA', |
| 343 | + nsname: 'ns1.example.com', |
| 344 | + hostmaster: 'admin.example.com', |
| 345 | + serial: 3210987654, |
| 346 | + refresh: 900, |
| 347 | + retry: 900, |
| 348 | + expire: 1800, |
| 349 | + minttl: 3333333333 |
| 350 | + }, |
| 351 | + ] |
| 352 | + }, |
| 353 | + |
| 354 | + { method: 'resolve4', |
| 355 | + options: { ttl: true }, |
| 356 | + answers: [ { type: 'A', address: '1.2.3.4', ttl: 3333333333 } ] |
| 357 | + }, |
| 358 | + |
| 359 | + { method: 'resolve6', |
| 360 | + options: { ttl: true }, |
| 361 | + answers: [ { type: 'AAAA', address: '::42', ttl: 3333333333 } ] |
| 362 | + }, |
| 363 | + |
| 364 | + { method: 'resolveSoa', |
| 365 | + answers: [ |
| 366 | + { |
| 367 | + type: 'SOA', |
| 368 | + nsname: 'ns1.example.com', |
| 369 | + hostmaster: 'admin.example.com', |
| 370 | + serial: 3210987654, |
| 371 | + refresh: 900, |
| 372 | + retry: 900, |
| 373 | + expire: 1800, |
| 374 | + minttl: 3333333333 |
| 375 | + } |
| 376 | + ] |
| 377 | + }, |
| 378 | + ]; |
| 379 | + |
| 380 | + const server = dgram.createSocket('udp4'); |
| 381 | + |
| 382 | + server.on('message', common.mustCall((msg, { address, port }) => { |
| 383 | + const parsed = dnstools.parseDNSPacket(msg); |
| 384 | + const domain = parsed.questions[0].domain; |
| 385 | + assert.strictEqual(domain, 'example.org'); |
| 386 | + |
| 387 | + server.send(dnstools.writeDNSPacket({ |
| 388 | + id: parsed.id, |
| 389 | + questions: parsed.questions, |
| 390 | + answers: cases[0].answers.map( |
| 391 | + (answer) => Object.assign({ domain }, answer) |
| 392 | + ), |
| 393 | + }), port, address); |
| 394 | + }, cases.length * 2)); |
| 395 | + |
| 396 | + server.bind(0, common.mustCall(() => { |
| 397 | + const address = server.address(); |
| 398 | + dns.setServers([`127.0.0.1:${address.port}`]); |
| 399 | + |
| 400 | + function validateResults(res) { |
| 401 | + if (!Array.isArray(res)) |
| 402 | + res = [res]; |
| 403 | + |
| 404 | + assert.deepStrictEqual(res.map(tweakEntry), |
| 405 | + cases[0].answers.map(tweakEntry)); |
| 406 | + } |
| 407 | + |
| 408 | + function tweakEntry(r) { |
| 409 | + const ret = { ...r }; |
| 410 | + |
| 411 | + const { method } = cases[0]; |
| 412 | + |
| 413 | + // TTL values are only provided for A and AAAA entries. |
| 414 | + if (!['A', 'AAAA'].includes(ret.type) && !/^resolve(4|6)?$/.test(method)) |
| 415 | + delete ret.ttl; |
| 416 | + |
| 417 | + if (method !== 'resolveAny') |
| 418 | + delete ret.type; |
| 419 | + |
| 420 | + return ret; |
| 421 | + } |
| 422 | + |
| 423 | + (async function nextCase() { |
| 424 | + if (cases.length === 0) |
| 425 | + return server.close(); |
| 426 | + |
| 427 | + const { method, options } = cases[0]; |
| 428 | + |
| 429 | + validateResults(await dnsPromises[method]('example.org', options)); |
| 430 | + |
| 431 | + dns[method]('example.org', options, common.mustCall((err, res) => { |
| 432 | + assert.ifError(err); |
| 433 | + validateResults(res); |
| 434 | + cases.shift(); |
| 435 | + nextCase(); |
| 436 | + })); |
| 437 | + })(); |
| 438 | + |
| 439 | + })); |
| 440 | +} |
0 commit comments