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 answers = [
101cb0ef41Sopenharmony_ci  { type: 'A', address: '1.2.3.4', ttl: 123 },
111cb0ef41Sopenharmony_ci  { type: 'AAAA', address: '::42', ttl: 123 },
121cb0ef41Sopenharmony_ci  { type: 'MX', priority: 42, exchange: 'foobar.com', ttl: 124 },
131cb0ef41Sopenharmony_ci  { type: 'NS', value: 'foobar.org', ttl: 457 },
141cb0ef41Sopenharmony_ci  { type: 'TXT', entries: [ 'v=spf1 ~all xyz\0foo' ] },
151cb0ef41Sopenharmony_ci  { type: 'PTR', value: 'baz.org', ttl: 987 },
161cb0ef41Sopenharmony_ci  {
171cb0ef41Sopenharmony_ci    type: 'SOA',
181cb0ef41Sopenharmony_ci    nsname: 'ns1.example.com',
191cb0ef41Sopenharmony_ci    hostmaster: 'admin.example.com',
201cb0ef41Sopenharmony_ci    serial: 156696742,
211cb0ef41Sopenharmony_ci    refresh: 900,
221cb0ef41Sopenharmony_ci    retry: 900,
231cb0ef41Sopenharmony_ci    expire: 1800,
241cb0ef41Sopenharmony_ci    minttl: 60
251cb0ef41Sopenharmony_ci  },
261cb0ef41Sopenharmony_ci  {
271cb0ef41Sopenharmony_ci    type: 'CAA',
281cb0ef41Sopenharmony_ci    critical: 128,
291cb0ef41Sopenharmony_ci    issue: 'platynum.ch'
301cb0ef41Sopenharmony_ci  },
311cb0ef41Sopenharmony_ci];
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciconst server = dgram.createSocket('udp4');
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ciserver.on('message', common.mustCall((msg, { address, port }) => {
361cb0ef41Sopenharmony_ci  const parsed = dnstools.parseDNSPacket(msg);
371cb0ef41Sopenharmony_ci  const domain = parsed.questions[0].domain;
381cb0ef41Sopenharmony_ci  assert.strictEqual(domain, 'example.org');
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  server.send(dnstools.writeDNSPacket({
411cb0ef41Sopenharmony_ci    id: parsed.id,
421cb0ef41Sopenharmony_ci    questions: parsed.questions,
431cb0ef41Sopenharmony_ci    answers: answers.map((answer) => Object.assign({ domain }, answer)),
441cb0ef41Sopenharmony_ci  }), port, address);
451cb0ef41Sopenharmony_ci}, 2));
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciserver.bind(0, common.mustCall(async () => {
481cb0ef41Sopenharmony_ci  const address = server.address();
491cb0ef41Sopenharmony_ci  dns.setServers([`127.0.0.1:${address.port}`]);
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  validateResults(await dnsPromises.resolveAny('example.org'));
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  dns.resolveAny('example.org', common.mustSucceed((res) => {
541cb0ef41Sopenharmony_ci    validateResults(res);
551cb0ef41Sopenharmony_ci    server.close();
561cb0ef41Sopenharmony_ci  }));
571cb0ef41Sopenharmony_ci}));
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_cifunction validateResults(res) {
601cb0ef41Sopenharmony_ci  // TTL values are only provided for A and AAAA entries.
611cb0ef41Sopenharmony_ci  assert.deepStrictEqual(res.map(maybeRedactTTL), answers.map(maybeRedactTTL));
621cb0ef41Sopenharmony_ci}
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_cifunction maybeRedactTTL(r) {
651cb0ef41Sopenharmony_ci  const ret = { ...r };
661cb0ef41Sopenharmony_ci  if (!['A', 'AAAA'].includes(r.type))
671cb0ef41Sopenharmony_ci    delete ret.ttl;
681cb0ef41Sopenharmony_ci  return ret;
691cb0ef41Sopenharmony_ci}
70