Skip to content

Commit c2bb4b2

Browse files
refackMylesBorins
authored andcommitted
test: bypass dns for IPv6 net tests
PR-URL: #16976 Refs: #16248 Reviewed-By: Joyee Cheung <[email protected]>
1 parent 3f363d3 commit c2bb4b2

File tree

2 files changed

+35
-61
lines changed

2 files changed

+35
-61
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
'use strict';
2+
3+
// Test that the family option of https.get is honored.
4+
25
const common = require('../common');
36
if (!common.hasCrypto)
47
common.skip('missing crypto');
@@ -9,21 +12,28 @@ if (!common.hasIPv6)
912
const assert = require('assert');
1013
const fixtures = require('../common/fixtures');
1114
const https = require('https');
12-
const dns = require('dns');
1315

14-
function runTest() {
16+
{
17+
// Test that `https` machinery passes host name, and receives IP.
18+
const hostAddrIPv6 = '::1';
19+
const HOSTNAME = 'dummy';
1520
https.createServer({
1621
cert: fixtures.readKey('agent1-cert.pem'),
1722
key: fixtures.readKey('agent1-key.pem'),
1823
}, common.mustCall(function(req, res) {
1924
this.close();
2025
res.end();
21-
})).listen(0, '::1', common.mustCall(function() {
26+
})).listen(0, hostAddrIPv6, common.mustCall(function() {
2227
const options = {
23-
host: 'localhost',
28+
host: HOSTNAME,
2429
port: this.address().port,
2530
family: 6,
2631
rejectUnauthorized: false,
32+
lookup: common.mustCall((addr, opt, cb) => {
33+
assert.strictEqual(addr, HOSTNAME);
34+
assert.strictEqual(opt.family, 6);
35+
cb(null, hostAddrIPv6, opt.family);
36+
})
2737
};
2838
// Will fail with ECONNREFUSED if the address family is not honored.
2939
https.get(options, common.mustCall(function() {
@@ -32,17 +42,3 @@ function runTest() {
3242
}));
3343
}));
3444
}
35-
36-
dns.lookup('localhost', { family: 6, all: true }, (err, addresses) => {
37-
if (err) {
38-
if (err.code === 'ENOTFOUND' || err.code === 'EAI_AGAIN')
39-
common.skip('localhost does not resolve to ::1');
40-
41-
throw err;
42-
}
43-
44-
if (addresses.some((val) => val.address === '::1'))
45-
runTest();
46-
else
47-
common.skip('localhost does not resolve to ::1');
48-
});

test/parallel/test-net-connect-options-ipv6.js

+21-43
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
// Test that the family option of net.connect is honored.
23+
2224
'use strict';
2325
const common = require('../common');
2426
if (!common.hasIPv6)
@@ -27,63 +29,39 @@ if (!common.hasIPv6)
2729
const assert = require('assert');
2830
const net = require('net');
2931

30-
const hosts = common.localIPv6Hosts;
31-
let hostIdx = 0;
32-
let host = hosts[hostIdx];
33-
let localhostTries = 10;
32+
const hostAddrIPv6 = '::1';
33+
const HOSTNAME = 'dummy';
3434

35-
const server = net.createServer({ allowHalfOpen: true }, function(socket) {
35+
const server = net.createServer({ allowHalfOpen: true }, (socket) => {
3636
socket.resume();
3737
socket.on('end', common.mustCall());
3838
socket.end();
3939
});
4040

41-
server.listen(0, '::1', tryConnect);
42-
4341
function tryConnect() {
44-
const client = net.connect({
45-
host: host,
42+
const connectOpt = {
43+
host: HOSTNAME,
4644
port: server.address().port,
4745
family: 6,
48-
allowHalfOpen: true
49-
}, function() {
50-
console.error('client connect cb');
46+
allowHalfOpen: true,
47+
lookup: common.mustCall((addr, opt, cb) => {
48+
assert.strictEqual(addr, HOSTNAME);
49+
assert.strictEqual(opt.family, 6);
50+
cb(null, hostAddrIPv6, opt.family);
51+
})
52+
};
53+
// No `mustCall`, since test could skip, and it's the only path to `close`.
54+
const client = net.connect(connectOpt, () => {
5155
client.resume();
52-
client.on('end', common.mustCall(function() {
56+
client.on('end', () => {
57+
// Wait for next uv tick and make sure the socket stream is writable.
5358
setTimeout(function() {
5459
assert(client.writable);
5560
client.end();
5661
}, 10);
57-
}));
58-
client.on('close', function() {
59-
server.close();
6062
});
61-
}).on('error', function(err) {
62-
// ENOTFOUND means we don't have the requested address. In this
63-
// case we try the next one in the list and if we run out of
64-
// candidates we assume IPv6 is not supported on the
65-
// machine and skip the test.
66-
// EAI_AGAIN means we tried to remotely resolve the address and
67-
// timed out or hit some intermittent connectivity issue with the
68-
// dns server. Although we are looking for local loopback addresses
69-
// we may go remote since the list we search includes addresses that
70-
// cover more than is available on any one distribution. The
71-
// net is that if we get an EAI_AGAIN we were looking for an
72-
// address which does not exist in this distribution so the error
73-
// is not significant and we should just move on and try the
74-
// next address in the list.
75-
if ((err.syscall === 'getaddrinfo') && ((err.code === 'ENOTFOUND') ||
76-
(err.code === 'EAI_AGAIN'))) {
77-
if (host !== 'localhost' || --localhostTries === 0)
78-
host = hosts[++hostIdx];
79-
if (host)
80-
tryConnect();
81-
else {
82-
server.close();
83-
common.skip('no IPv6 localhost support');
84-
}
85-
return;
86-
}
87-
throw err;
63+
client.on('close', () => server.close());
8864
});
8965
}
66+
67+
server.listen(0, hostAddrIPv6, tryConnect);

0 commit comments

Comments
 (0)