Skip to content

Commit 87543f0

Browse files
KeeRealBethGriggs
authored andcommitted
test: improve dns lookup coverage
Adding tests covering promises-related code paths. PR-URL: #30777 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Ben Coe <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 0688c99 commit 87543f0

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
Lines 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+
constcommon=require('../common');
4+
constassert=require('assert');
5+
const{ internalBinding }=require('internal/test/binding');
6+
constcares=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+
letgetaddrinfoStub=null;
11+
cares.getaddrinfo=(req)=>getaddrinfoStub(req);
12+
13+
constdnsPromises=require('dns').promises;
14+
15+
functiongetaddrinfoNegative(){
16+
returnfunctiongetaddrinfoNegativeHandler(req){
17+
constoriginalReject=req.reject;
18+
req.resolve=common.mustNotCall();
19+
req.reject=common.mustCall(originalReject);
20+
req.oncomplete(internalBinding('uv').UV_ENOMEM);
21+
};
22+
}
23+
24+
functiongetaddrinfoPositive(addresses){
25+
returnfunctiongetaddrinfo_positive(req){
26+
constoriginalResolve=req.resolve;
27+
req.reject=common.mustNotCall();
28+
req.resolve=common.mustCall(originalResolve);
29+
req.oncomplete(null,addresses);
30+
};
31+
}
32+
33+
asyncfunctionlookupPositive(){
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(awaitfactory(),expectation);
63+
});
64+
}
65+
66+
asyncfunctionlookupNegative(){
67+
getaddrinfoStub=getaddrinfoNegative();
68+
constexpected={
69+
code: 'ENOMEM',
70+
hostname: 'example.com',
71+
syscall: 'getaddrinfo'
72+
};
73+
returnassert.rejects(dnsPromises.lookup('example.com'),expected);
74+
}
75+
76+
asyncfunctionlookupallPositive(){
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(awaitfactory(),expectation);
118+
});
119+
}
120+
121+
asyncfunctionlookupallNegative(){
122+
getaddrinfoStub=getaddrinfoNegative();
123+
constexpected={
124+
code: 'ENOMEM',
125+
hostname: 'example.com',
126+
syscall: 'getaddrinfo'
127+
};
128+
returnassert.rejects(dnsPromises.lookup('example.com',{all: true}),
129+
expected);
130+
}
131+
132+
(async()=>{
133+
awaitPromise.all([
134+
lookupPositive(),
135+
lookupNegative(),
136+
lookupallPositive(),
137+
lookupallNegative()
138+
]).then(common.mustCall());
139+
})();

0 commit comments

Comments
(0)