summaryrefslogtreecommitdiff
path: root/test/parallel/test-dns-resolveany-bad-ancount.js
blob: 71fcbe03cd58f1537820598b498728d947a0efc5 (plain)
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
'use strict';
const common = require('../common');
const dnstools = require('../common/dns');
const dns = require('dns');
const assert = require('assert');
const dgram = require('dgram');
const dnsPromises = dns.promises;

const server = dgram.createSocket('udp4');

server.on('message', common.mustCall((msg, { address, port }) => {
  const parsed = dnstools.parseDNSPacket(msg);
  const domain = parsed.questions[0].domain;
  assert.strictEqual(domain, 'example.org');

  const buf = dnstools.writeDNSPacket({
    id: parsed.id,
    questions: parsed.questions,
    answers: { type: 'A', address: '1.2.3.4', ttl: 123, domain },
  });
  // Overwrite the # of answers with 2, which is incorrect.
  buf.writeUInt16LE(2, 6);
  server.send(buf, port, address);
}, 2));

server.bind(0, common.mustCall(async () => {
  const address = server.address();
  dns.setServers([`127.0.0.1:${address.port}`]);

  dnsPromises.resolveAny('example.org')
    .then(common.mustNotCall())
    .catch(common.expectsError({
      code: 'EBADRESP',
      syscall: 'queryAny',
      hostname: 'example.org'
    }));

  dns.resolveAny('example.org', common.mustCall((err) => {
    assert.strictEqual(err.code, 'EBADRESP');
    assert.strictEqual(err.syscall, 'queryAny');
    assert.strictEqual(err.hostname, 'example.org');
    const descriptor = Object.getOwnPropertyDescriptor(err, 'message');
    // The error message should be non-enumerable.
    assert.strictEqual(descriptor.enumerable, false);
    server.close();
  }));
}));