• 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 server = dgram.createSocket('udp4');
10
11server.on('message', common.mustCall((msg, { address, port }) => {
12  const parsed = dnstools.parseDNSPacket(msg);
13  const domain = parsed.questions[0].domain;
14  assert.strictEqual(domain, 'example.org');
15
16  const buf = dnstools.writeDNSPacket({
17    id: parsed.id,
18    questions: parsed.questions,
19    answers: { type: 'A', address: '1.2.3.4', ttl: 123, domain },
20  });
21  // Overwrite the # of answers with 2, which is incorrect.
22  buf.writeUInt16LE(2, 6);
23  server.send(buf, port, address);
24}, 2));
25
26server.bind(0, common.mustCall(async () => {
27  const address = server.address();
28  dns.setServers([`127.0.0.1:${address.port}`]);
29
30  dnsPromises.resolveAny('example.org')
31    .then(common.mustNotCall())
32    .catch(common.expectsError({
33      code: 'EBADRESP',
34      syscall: 'queryAny',
35      hostname: 'example.org'
36    }));
37
38  dns.resolveAny('example.org', common.mustCall((err) => {
39    assert.strictEqual(err.code, 'EBADRESP');
40    assert.strictEqual(err.syscall, 'queryAny');
41    assert.strictEqual(err.hostname, 'example.org');
42    const descriptor = Object.getOwnPropertyDescriptor(err, 'message');
43    // The error message should be non-enumerable.
44    assert.strictEqual(descriptor.enumerable, false);
45    server.close();
46  }));
47}));
48