11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
61cb0ef41Sopenharmony_ciconst cares = internalBinding('cares_wrap');
71cb0ef41Sopenharmony_ciconst { promisify } = require('util');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci// Test that `dns.setDefaultResultOrder()` and
101cb0ef41Sopenharmony_ci// `dns.promises.setDefaultResultOrder()` works as expected.
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst originalGetaddrinfo = cares.getaddrinfo;
131cb0ef41Sopenharmony_ciconst calls = [];
141cb0ef41Sopenharmony_cicares.getaddrinfo = common.mustCallAtLeast((...args) => {
151cb0ef41Sopenharmony_ci  calls.push(args);
161cb0ef41Sopenharmony_ci  originalGetaddrinfo(...args);
171cb0ef41Sopenharmony_ci}, 1);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst dns = require('dns');
201cb0ef41Sopenharmony_ciconst dnsPromises = dns.promises;
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_cilet verbatim;
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci// We want to test the parameter of verbatim only so that we
251cb0ef41Sopenharmony_ci// ignore possible errors here.
261cb0ef41Sopenharmony_cifunction allowFailed(fn) {
271cb0ef41Sopenharmony_ci  return fn.catch((_err) => {
281cb0ef41Sopenharmony_ci    //
291cb0ef41Sopenharmony_ci  });
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciassert.throws(() => dns.setDefaultResultOrder('my_order'), {
331cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_VALUE',
341cb0ef41Sopenharmony_ci});
351cb0ef41Sopenharmony_ciassert.throws(() => dns.promises.setDefaultResultOrder('my_order'), {
361cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_VALUE',
371cb0ef41Sopenharmony_ci});
381cb0ef41Sopenharmony_ciassert.throws(() => dns.setDefaultResultOrder(4), {
391cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_VALUE',
401cb0ef41Sopenharmony_ci});
411cb0ef41Sopenharmony_ciassert.throws(() => dns.promises.setDefaultResultOrder(4), {
421cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_VALUE',
431cb0ef41Sopenharmony_ci});
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci(async () => {
461cb0ef41Sopenharmony_ci  let callsLength = 0;
471cb0ef41Sopenharmony_ci  const checkParameter = (expected) => {
481cb0ef41Sopenharmony_ci    assert.strictEqual(calls.length, callsLength + 1);
491cb0ef41Sopenharmony_ci    verbatim = calls[callsLength][4];
501cb0ef41Sopenharmony_ci    assert.strictEqual(verbatim, expected);
511cb0ef41Sopenharmony_ci    callsLength += 1;
521cb0ef41Sopenharmony_ci  };
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  dns.setDefaultResultOrder('verbatim');
551cb0ef41Sopenharmony_ci  await allowFailed(promisify(dns.lookup)('example.org'));
561cb0ef41Sopenharmony_ci  checkParameter(true);
571cb0ef41Sopenharmony_ci  await allowFailed(dnsPromises.lookup('example.org'));
581cb0ef41Sopenharmony_ci  checkParameter(true);
591cb0ef41Sopenharmony_ci  await allowFailed(promisify(dns.lookup)('example.org', {}));
601cb0ef41Sopenharmony_ci  checkParameter(true);
611cb0ef41Sopenharmony_ci  await allowFailed(dnsPromises.lookup('example.org', {}));
621cb0ef41Sopenharmony_ci  checkParameter(true);
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  dns.setDefaultResultOrder('ipv4first');
651cb0ef41Sopenharmony_ci  await allowFailed(promisify(dns.lookup)('example.org'));
661cb0ef41Sopenharmony_ci  checkParameter(false);
671cb0ef41Sopenharmony_ci  await allowFailed(dnsPromises.lookup('example.org'));
681cb0ef41Sopenharmony_ci  checkParameter(false);
691cb0ef41Sopenharmony_ci  await allowFailed(promisify(dns.lookup)('example.org', {}));
701cb0ef41Sopenharmony_ci  checkParameter(false);
711cb0ef41Sopenharmony_ci  await allowFailed(dnsPromises.lookup('example.org', {}));
721cb0ef41Sopenharmony_ci  checkParameter(false);
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  dns.promises.setDefaultResultOrder('verbatim');
751cb0ef41Sopenharmony_ci  await allowFailed(promisify(dns.lookup)('example.org'));
761cb0ef41Sopenharmony_ci  checkParameter(true);
771cb0ef41Sopenharmony_ci  await allowFailed(dnsPromises.lookup('example.org'));
781cb0ef41Sopenharmony_ci  checkParameter(true);
791cb0ef41Sopenharmony_ci  await allowFailed(promisify(dns.lookup)('example.org', {}));
801cb0ef41Sopenharmony_ci  checkParameter(true);
811cb0ef41Sopenharmony_ci  await allowFailed(dnsPromises.lookup('example.org', {}));
821cb0ef41Sopenharmony_ci  checkParameter(true);
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  dns.promises.setDefaultResultOrder('ipv4first');
851cb0ef41Sopenharmony_ci  await allowFailed(promisify(dns.lookup)('example.org'));
861cb0ef41Sopenharmony_ci  checkParameter(false);
871cb0ef41Sopenharmony_ci  await allowFailed(dnsPromises.lookup('example.org'));
881cb0ef41Sopenharmony_ci  checkParameter(false);
891cb0ef41Sopenharmony_ci  await allowFailed(promisify(dns.lookup)('example.org', {}));
901cb0ef41Sopenharmony_ci  checkParameter(false);
911cb0ef41Sopenharmony_ci  await allowFailed(dnsPromises.lookup('example.org', {}));
921cb0ef41Sopenharmony_ci  checkParameter(false);
931cb0ef41Sopenharmony_ci})().then(common.mustCall());
94