• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3const common = require('../common');
4const assert = require('assert');
5const { internalBinding } = require('internal/test/binding');
6const cares = internalBinding('cares_wrap');
7const { promisify } = require('util');
8
9// Test that `dns.setDefaultResultOrder()` and
10// `dns.promises.setDefaultResultOrder()` works as expected.
11
12const originalGetaddrinfo = cares.getaddrinfo;
13const calls = [];
14cares.getaddrinfo = common.mustCallAtLeast((...args) => {
15  calls.push(args);
16  originalGetaddrinfo(...args);
17}, 1);
18
19const dns = require('dns');
20const dnsPromises = dns.promises;
21
22let verbatim;
23
24// We want to test the parameter of verbatim only so that we
25// ignore possible errors here.
26function allowFailed(fn) {
27  return fn.catch((_err) => {
28    //
29  });
30}
31
32assert.throws(() => dns.setDefaultResultOrder('my_order'), {
33  code: 'ERR_INVALID_ARG_VALUE',
34});
35assert.throws(() => dns.promises.setDefaultResultOrder('my_order'), {
36  code: 'ERR_INVALID_ARG_VALUE',
37});
38assert.throws(() => dns.setDefaultResultOrder(4), {
39  code: 'ERR_INVALID_ARG_VALUE',
40});
41assert.throws(() => dns.promises.setDefaultResultOrder(4), {
42  code: 'ERR_INVALID_ARG_VALUE',
43});
44
45(async () => {
46  let callsLength = 0;
47  const checkParameter = (expected) => {
48    assert.strictEqual(calls.length, callsLength + 1);
49    verbatim = calls[callsLength][4];
50    assert.strictEqual(verbatim, expected);
51    callsLength += 1;
52  };
53
54  dns.setDefaultResultOrder('verbatim');
55  await allowFailed(promisify(dns.lookup)('example.org'));
56  checkParameter(true);
57  await allowFailed(dnsPromises.lookup('example.org'));
58  checkParameter(true);
59  await allowFailed(promisify(dns.lookup)('example.org', {}));
60  checkParameter(true);
61  await allowFailed(dnsPromises.lookup('example.org', {}));
62  checkParameter(true);
63
64  dns.setDefaultResultOrder('ipv4first');
65  await allowFailed(promisify(dns.lookup)('example.org'));
66  checkParameter(false);
67  await allowFailed(dnsPromises.lookup('example.org'));
68  checkParameter(false);
69  await allowFailed(promisify(dns.lookup)('example.org', {}));
70  checkParameter(false);
71  await allowFailed(dnsPromises.lookup('example.org', {}));
72  checkParameter(false);
73
74  dns.promises.setDefaultResultOrder('verbatim');
75  await allowFailed(promisify(dns.lookup)('example.org'));
76  checkParameter(true);
77  await allowFailed(dnsPromises.lookup('example.org'));
78  checkParameter(true);
79  await allowFailed(promisify(dns.lookup)('example.org', {}));
80  checkParameter(true);
81  await allowFailed(dnsPromises.lookup('example.org', {}));
82  checkParameter(true);
83
84  dns.promises.setDefaultResultOrder('ipv4first');
85  await allowFailed(promisify(dns.lookup)('example.org'));
86  checkParameter(false);
87  await allowFailed(dnsPromises.lookup('example.org'));
88  checkParameter(false);
89  await allowFailed(promisify(dns.lookup)('example.org', {}));
90  checkParameter(false);
91  await allowFailed(dnsPromises.lookup('example.org', {}));
92  checkParameter(false);
93})().then(common.mustCall());
94