Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5cb1744

Browse filesBrowse files
KeeRealtargos
authored andcommitted
test: improve dns lookup coverage
Adding tests covering promises-related code paths. PR-URL: #30777 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Ben Coe <bencoe@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent c554c0e commit 5cb1744
Copy full SHA for 5cb1744

File tree

Expand file treeCollapse file tree

1 file changed

+139
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+139
-0
lines changed
Open diff view settings
Collapse file
+139Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const { internalBinding } = require('internal/test/binding');
6+
const 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.
10+
let getaddrinfoStub = null;
11+
cares.getaddrinfo = (req) => getaddrinfoStub(req);
12+
13+
const dnsPromises = require('dns').promises;
14+
15+
function 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+
24+
function 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+
33+
async 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+
66+
async 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+
76+
async 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+
121+
async 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+
]).then(common.mustCall());
139+
})();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.