• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const dnstools = require('../common/dns');
4const dns = require('dns');
5const assert = require('assert');
6const dgram = require('dgram');
7const dnsPromises = dns.promises;
8
9const answers = [
10  { type: 'A', address: '1.2.3.4', ttl: 123 },
11  { type: 'AAAA', address: '::42', ttl: 123 },
12  { type: 'MX', priority: 42, exchange: 'foobar.com', ttl: 124 },
13  { type: 'NS', value: 'foobar.org', ttl: 457 },
14  { type: 'TXT', entries: [ 'v=spf1 ~all', 'xyz\0foo' ] },
15  { type: 'PTR', value: 'baz.org', ttl: 987 },
16  {
17    type: 'SOA',
18    nsname: 'ns1.example.com',
19    hostmaster: 'admin.example.com',
20    serial: 156696742,
21    refresh: 900,
22    retry: 900,
23    expire: 1800,
24    minttl: 60
25  },
26  {
27    type: 'CAA',
28    critical: 128,
29    issue: 'platynum.ch'
30  },
31];
32
33const server = dgram.createSocket('udp4');
34
35server.on('message', common.mustCall((msg, { address, port }) => {
36  const parsed = dnstools.parseDNSPacket(msg);
37  const domain = parsed.questions[0].domain;
38  assert.strictEqual(domain, 'example.org');
39
40  server.send(dnstools.writeDNSPacket({
41    id: parsed.id,
42    questions: parsed.questions,
43    answers: answers.map((answer) => Object.assign({ domain }, answer)),
44  }), port, address);
45}, 2));
46
47server.bind(0, common.mustCall(async () => {
48  const address = server.address();
49  dns.setServers([`127.0.0.1:${address.port}`]);
50
51  validateResults(await dnsPromises.resolveAny('example.org'));
52
53  dns.resolveAny('example.org', common.mustSucceed((res) => {
54    validateResults(res);
55    server.close();
56  }));
57}));
58
59function validateResults(res) {
60  // TTL values are only provided for A and AAAA entries.
61  assert.deepStrictEqual(res.map(maybeRedactTTL), answers.map(maybeRedactTTL));
62}
63
64function maybeRedactTTL(r) {
65  const ret = { ...r };
66  if (!['A', 'AAAA'].includes(r.type))
67    delete ret.ttl;
68  return ret;
69}
70