• 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');
7
8// Stub `getaddrinfo` to proxy its call dynamic stub. This has to be done before
9// we load the `dns` module to guarantee that the `dns` module uses the stub.
10let getaddrinfoStub = null;
11cares.getaddrinfo = (req) => getaddrinfoStub(req);
12
13const dnsPromises = require('dns').promises;
14
15function getaddrinfoNegative() {
16  return function getaddrinfoNegativeHandler(req) {
17    const originalReject = req.reject;
18    req.resolve = common.mustNotCall();
19    req.reject = common.mustCall(originalReject);
20    req.oncomplete(internalBinding('uv').UV_ENOMEM);
21  };
22}
23
24function getaddrinfoPositive(addresses) {
25  return function getaddrinfo_positive(req) {
26    const originalResolve = req.resolve;
27    req.reject = common.mustNotCall();
28    req.resolve = common.mustCall(originalResolve);
29    req.oncomplete(null, addresses);
30  };
31}
32
33async function lookupPositive() {
34  [
35    {
36      stub: getaddrinfoPositive(['::1']),
37      factory: () => dnsPromises.lookup('example.com'),
38      expectation: { address: '::1', family: 6 }
39    },
40    {
41      stub: getaddrinfoPositive(['127.0.0.1']),
42      factory: () => dnsPromises.lookup('example.com'),
43      expectation: { address: '127.0.0.1', family: 4 }
44    },
45    {
46      stub: getaddrinfoPositive(['127.0.0.1'], { family: 4 }),
47      factory: () => dnsPromises.lookup('example.com'),
48      expectation: { address: '127.0.0.1', family: 4 }
49    },
50    {
51      stub: getaddrinfoPositive(['some-address']),
52      factory: () => dnsPromises.lookup('example.com'),
53      expectation: { address: 'some-address', family: 0 }
54    },
55    {
56      stub: getaddrinfoPositive(['some-address2']),
57      factory: () => dnsPromises.lookup('example.com', { family: 6 }),
58      expectation: { address: 'some-address2', family: 6 }
59    },
60  ].forEach(async ({ stub, factory, expectation }) => {
61    getaddrinfoStub = stub;
62    assert.deepStrictEqual(await factory(), expectation);
63  });
64}
65
66async function lookupNegative() {
67  getaddrinfoStub = getaddrinfoNegative();
68  const expected = {
69    code: 'ENOMEM',
70    hostname: 'example.com',
71    syscall: 'getaddrinfo'
72  };
73  return assert.rejects(dnsPromises.lookup('example.com'), expected);
74}
75
76async function lookupallPositive() {
77  [
78    {
79      stub: getaddrinfoPositive(['::1', '::2']),
80      factory: () => dnsPromises.lookup('example', { all: true }),
81      expectation: [
82        { address: '::1', family: 6 },
83        { address: '::2', family: 6 },
84      ]
85    },
86    {
87      stub: getaddrinfoPositive(['::1', '::2']),
88      factory: () => dnsPromises.lookup('example', { all: true, family: 4 }),
89      expectation: [
90        { address: '::1', family: 4 },
91        { address: '::2', family: 4 },
92      ]
93    },
94    {
95      stub: getaddrinfoPositive(['127.0.0.1', 'some-address']),
96      factory: () => dnsPromises.lookup('example', { all: true }),
97      expectation: [
98        { address: '127.0.0.1', family: 4 },
99        { address: 'some-address', family: 0 },
100      ]
101    },
102    {
103      stub: getaddrinfoPositive(['127.0.0.1', 'some-address']),
104      factory: () => dnsPromises.lookup('example', { all: true, family: 6 }),
105      expectation: [
106        { address: '127.0.0.1', family: 6 },
107        { address: 'some-address', family: 6 },
108      ]
109    },
110    {
111      stub: getaddrinfoPositive([]),
112      factory: () => dnsPromises.lookup('example', { all: true }),
113      expectation: []
114    },
115  ].forEach(async ({ stub, factory, expectation }) => {
116    getaddrinfoStub = stub;
117    assert.deepStrictEqual(await factory(), expectation);
118  });
119}
120
121async function lookupallNegative() {
122  getaddrinfoStub = getaddrinfoNegative();
123  const expected = {
124    code: 'ENOMEM',
125    hostname: 'example.com',
126    syscall: 'getaddrinfo'
127  };
128  return assert.rejects(dnsPromises.lookup('example.com', { all: true }),
129                        expected);
130}
131
132(async () => {
133  await Promise.all([
134    lookupPositive(),
135    lookupNegative(),
136    lookupallPositive(),
137    lookupallNegative(),
138  ]);
139})().then(common.mustCall());
140