11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst dnstools = require('../common/dns');
41cb0ef41Sopenharmony_ciconst dns = require('dns');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst dgram = require('dgram');
71cb0ef41Sopenharmony_ciconst dnsPromises = dns.promises;
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst server = dgram.createSocket('udp4');
101cb0ef41Sopenharmony_ciconst resolver = new dns.Resolver({ timeout: 100, tries: 1 });
111cb0ef41Sopenharmony_ciconst resolverPromises = new dnsPromises.Resolver({ timeout: 100, tries: 1 });
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciserver.on('message', common.mustCall((msg, { address, port }) => {
141cb0ef41Sopenharmony_ci  const parsed = dnstools.parseDNSPacket(msg);
151cb0ef41Sopenharmony_ci  const domain = parsed.questions[0].domain;
161cb0ef41Sopenharmony_ci  assert.strictEqual(domain, 'example.org');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  const buf = dnstools.writeDNSPacket({
191cb0ef41Sopenharmony_ci    id: parsed.id,
201cb0ef41Sopenharmony_ci    questions: parsed.questions,
211cb0ef41Sopenharmony_ci    answers: { type: 'A', address: '1.2.3.4', ttl: 123, domain },
221cb0ef41Sopenharmony_ci  });
231cb0ef41Sopenharmony_ci  // Overwrite the # of answers with 2, which is incorrect. The response is
241cb0ef41Sopenharmony_ci  // discarded in c-ares >= 1.21.0. This is the reason why a small timeout is
251cb0ef41Sopenharmony_ci  // used in the `Resolver` constructor. See
261cb0ef41Sopenharmony_ci  // https://github.com/nodejs/node/pull/50743#issue-1994909204
271cb0ef41Sopenharmony_ci  buf.writeUInt16LE(2, 6);
281cb0ef41Sopenharmony_ci  server.send(buf, port, address);
291cb0ef41Sopenharmony_ci}, 2));
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciserver.bind(0, common.mustCall(async () => {
321cb0ef41Sopenharmony_ci  const address = server.address();
331cb0ef41Sopenharmony_ci  resolver.setServers([`127.0.0.1:${address.port}`]);
341cb0ef41Sopenharmony_ci  resolverPromises.setServers([`127.0.0.1:${address.port}`]);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  resolverPromises.resolveAny('example.org')
371cb0ef41Sopenharmony_ci    .then(common.mustNotCall())
381cb0ef41Sopenharmony_ci    .catch(common.expectsError({
391cb0ef41Sopenharmony_ci      // May return EBADRESP or ETIMEOUT
401cb0ef41Sopenharmony_ci      code: /^(?:EBADRESP|ETIMEOUT)$/,
411cb0ef41Sopenharmony_ci      syscall: 'queryAny',
421cb0ef41Sopenharmony_ci      hostname: 'example.org'
431cb0ef41Sopenharmony_ci    }));
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  resolver.resolveAny('example.org', common.mustCall((err) => {
461cb0ef41Sopenharmony_ci    assert.notStrictEqual(err.code, 'SUCCESS');
471cb0ef41Sopenharmony_ci    assert.strictEqual(err.syscall, 'queryAny');
481cb0ef41Sopenharmony_ci    assert.strictEqual(err.hostname, 'example.org');
491cb0ef41Sopenharmony_ci    const descriptor = Object.getOwnPropertyDescriptor(err, 'message');
501cb0ef41Sopenharmony_ci    // The error message should be non-enumerable.
511cb0ef41Sopenharmony_ci    assert.strictEqual(descriptor.enumerable, false);
521cb0ef41Sopenharmony_ci    server.close();
531cb0ef41Sopenharmony_ci  }));
541cb0ef41Sopenharmony_ci}));
55