Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v8.x backport] 16390, 18547, 17296 (internet test flake fixes) #19706

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This directory contains modules used to test the Node.js implementation.
* [DNS module](#dns-module)
* [Duplex pair helper](#duplex-pair-helper)
* [Fixtures module](#fixtures-module)
* [Internet module](#internet-module)
* [tmpdir module](#tmpdir-module)
* [WPT module](#wpt-module)

Expand Down Expand Up @@ -403,7 +404,26 @@ called before the callback is invoked.

## DNS Module

The `DNS` module provides a naïve DNS parser/serializer.
The `DNS` module provides utilities related to the `dns` built-in module.

### errorLookupMock(code, syscall)

* `code` [<String>] Defaults to `dns.mockedErrorCode`.
* `syscall` [<String>] Defaults to `dns.mockedSysCall`.
* return [<Function>]


A mock for the `lookup` option of `net.connect()` that would result in an error
with the `code` and the `syscall` specified. Returns a function that has the
same signature as `dns.lookup()`.

### mockedErrorCode

The default `code` of errors generated by `errorLookupMock`.

### mockedSysCall

The default `syscall` of errors generated by `errorLookupMock`.

### readDomainFromPacket(buffer, offset)

Expand Down Expand Up @@ -483,6 +503,40 @@ Returns the result of
Returns the result of
`fs.readFileSync(path.join(fixtures.fixturesDir, 'keys', arg), 'enc')`.

## Internet Module

The `common/internet` module provides utilities for working with
internet-related tests.

### internet.addresses

* [<Object>]
* `INET_HOST` [<String>] A generic host that has registered common
DNS records, supports both IPv4 and IPv6, and provides basic HTTP/HTTPS
services
* `INET4_HOST` [<String>] A host that provides IPv4 services
* `INET6_HOST` [<String>] A host that provides IPv6 services
* `INET4_IP` [<String>] An accessible IPv4 IP, defaults to the
Google Public DNS IPv4 address
* `INET6_IP` [<String>] An accessible IPv6 IP, defaults to the
Google Public DNS IPv6 address
* `INVALID_HOST` [<String>] An invalid host that cannot be resolved
* `MX_HOST` [<String>] A host with MX records registered
* `SRV_HOST` [<String>] A host with SRV records registered
* `PTR_HOST` [<String>] A host with PTR records registered
* `NAPTR_HOST` [<String>] A host with NAPTR records registered
* `SOA_HOST` [<String>] A host with SOA records registered
* `CNAME_HOST` [<String>] A host with CNAME records registered
* `NS_HOST` [<String>] A host with NS records registered
* `TXT_HOST` [<String>] A host with TXT records registered
* `DNS4_SERVER` [<String>] An accessible IPv4 DNS server
* `DNS6_SERVER` [<String>] An accessible IPv6 DNS server

A set of addresses for internet-related tests. All properties are configurable
via `NODE_TEST_*` environment variables. For example, to configure
`internet.addresses.INET_HOST`, set the environment
vairable `NODE_TEST_INET_HOST` to a specified host.

## tmpdir Module

The `tmpdir` module supports the use of a temporary directory for testing.
Expand Down
28 changes: 25 additions & 3 deletions test/common/dns.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-disable required-modules */
'use strict';

// Naïve DNS parser/serializer.

const assert = require('assert');
const os = require('os');

Expand All @@ -22,6 +20,8 @@ const classes = {
IN: 1
};

// Naïve DNS parser/serializer.

function readDomainFromPacket(buffer, offset) {
assert.ok(offset < buffer.length);
const length = buffer[offset];
Expand Down Expand Up @@ -287,4 +287,26 @@ function writeDNSPacket(parsed) {
}));
}

module.exports = { types, classes, writeDNSPacket, parseDNSPacket };
const mockedErrorCode = 'ENOTFOUND';
const mockedSysCall = 'getaddrinfo';

function errorLookupMock(code = mockedErrorCode, syscall = mockedSysCall) {
return function lookupWithError(host, dnsopts, cb) {
const err = new Error(`${syscall} ${code} ${host}`);
err.code = code;
err.errno = code;
err.syscall = syscall;
err.hostname = host;
cb(err);
};
}

module.exports = {
types,
classes,
writeDNSPacket,
parseDNSPacket,
errorLookupMock,
mockedErrorCode,
mockedSysCall
};
54 changes: 54 additions & 0 deletions test/common/internet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* eslint-disable required-modules */
'use strict';

// Utilities for internet-related tests

const addresses = {
// A generic host that has registered common DNS records,
// supports both IPv4 and IPv6, and provides basic HTTP/HTTPS services
INET_HOST: 'nodejs.org',
// A host that provides IPv4 services
INET4_HOST: 'nodejs.org',
// A host that provides IPv6 services
INET6_HOST: 'nodejs.org',
// An accessible IPv4 IP,
// defaults to the Google Public DNS IPv4 address
INET4_IP: '8.8.8.8',
// An accessible IPv6 IP,
// defaults to the Google Public DNS IPv6 address
INET6_IP: '2001:4860:4860::8888',
// An invalid host that cannot be resolved
// See https://tools.ietf.org/html/rfc2606#section-2
INVALID_HOST: 'something.invalid',
// A host with MX records registered
MX_HOST: 'nodejs.org',
// A host with SRV records registered
SRV_HOST: '_jabber._tcp.google.com',
// A host with PTR records registered
PTR_HOST: '8.8.8.8.in-addr.arpa',
// A host with NAPTR records registered
NAPTR_HOST: 'sip2sip.info',
// A host with SOA records registered
SOA_HOST: 'nodejs.org',
// A host with CNAME records registered
CNAME_HOST: 'blog.nodejs.org',
// A host with NS records registered
NS_HOST: 'nodejs.org',
// A host with TXT records registered
TXT_HOST: 'nodejs.org',
// An accessible IPv4 DNS server
DNS4_SERVER: '8.8.8.8',
// An accessible IPv4 DNS server
DNS6_SERVER: '2001:4860:4860::8888'
};

for (const key of Object.keys(addresses)) {
const envName = `NODE_TEST_${key}`;
if (process.env[envName]) {
addresses[key] = process.env[envName];
}
}

module.exports = {
addresses
};
3 changes: 0 additions & 3 deletions test/internet/test-dns-any.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ const checkers = {
checkTXT(r) {
assert.ok(Array.isArray(r.entries));
assert.ok(r.entries.length > 0);
r.entries.forEach((txt) => {
assert(txt.startsWith('v=spf1'));
});
assert.strictEqual(r.type, 'TXT');
},
checkSOA(r) {
Expand Down
7 changes: 4 additions & 3 deletions test/internet/test-dns-cares-domains.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
require('../common');
const common = require('../common');
const { addresses } = require('../common/internet');
const assert = require('assert');
const dns = require('dns');
const domain = require('domain');
Expand All @@ -20,8 +21,8 @@ const methods = [
methods.forEach(function(method) {
const d = domain.create();
d.run(function() {
dns[method]('google.com', function() {
dns[method](addresses.INET_HOST, common.mustCall(() => {
assert.strictEqual(process.domain, d, `${method} retains domain`);
});
}));
});
});
77 changes: 41 additions & 36 deletions test/internet/test-dns-ipv4.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const common = require('../common');
const { addresses } = require('../common/internet');
const assert = require('assert');
const dns = require('dns');
const net = require('net');
Expand Down Expand Up @@ -38,68 +39,72 @@ function checkWrap(req) {
}

TEST(function test_resolve4(done) {
const req = dns.resolve4('www.google.com',
common.mustCall((err, ips) => {
assert.ifError(err);
const req = dns.resolve4(
addresses.INET4_HOST,
common.mustCall((err, ips) => {
assert.ifError(err);

assert.ok(ips.length > 0);
assert.ok(ips.length > 0);

for (let i = 0; i < ips.length; i++) {
assert.ok(isIPv4(ips[i]));
}
for (let i = 0; i < ips.length; i++) {
assert.ok(isIPv4(ips[i]));
}

done();
}));
done();
}));

checkWrap(req);
});

TEST(function test_reverse_ipv4(done) {
const req = dns.reverse('8.8.8.8',
common.mustCall((err, domains) => {
assert.ifError(err);
const req = dns.reverse(
addresses.INET4_IP,
common.mustCall((err, domains) => {
assert.ifError(err);

assert.ok(domains.length > 0);
assert.ok(domains.length > 0);

for (let i = 0; i < domains.length; i++) {
assert.ok(domains[i]);
assert.ok(typeof domains[i] === 'string');
}
for (let i = 0; i < domains.length; i++) {
assert.ok(domains[i]);
assert.ok(typeof domains[i] === 'string');
}

done();
}));
done();
}));

checkWrap(req);
});

TEST(function test_lookup_ipv4_explicit(done) {
const req = dns.lookup('www.google.com', 4,
common.mustCall((err, ip, family) => {
assert.ifError(err);
assert.ok(net.isIPv4(ip));
assert.strictEqual(family, 4);
const req = dns.lookup(
addresses.INET4_HOST, 4,
common.mustCall((err, ip, family) => {
assert.ifError(err);
assert.ok(net.isIPv4(ip));
assert.strictEqual(family, 4);

done();
}));
done();
}));

checkWrap(req);
});

TEST(function test_lookup_ipv4_implicit(done) {
const req = dns.lookup('www.google.com',
common.mustCall((err, ip, family) => {
assert.ifError(err);
assert.ok(net.isIPv4(ip));
assert.strictEqual(family, 4);
const req = dns.lookup(
addresses.INET4_HOST,
common.mustCall((err, ip, family) => {
assert.ifError(err);
assert.ok(net.isIPv4(ip));
assert.strictEqual(family, 4);

done();
}));
done();
}));

checkWrap(req);
});

TEST(function test_lookup_ipv4_explicit_object(done) {
const req = dns.lookup('www.google.com', {
const req = dns.lookup(addresses.INET4_HOST, {
family: 4
}, common.mustCall((err, ip, family) => {
assert.ifError(err);
Expand All @@ -113,7 +118,7 @@ TEST(function test_lookup_ipv4_explicit_object(done) {
});

TEST(function test_lookup_ipv4_hint_addrconfig(done) {
const req = dns.lookup('www.google.com', {
const req = dns.lookup(addresses.INET4_HOST, {
hints: dns.ADDRCONFIG
}, common.mustCall((err, ip, family) => {
assert.ifError(err);
Expand Down Expand Up @@ -154,7 +159,7 @@ TEST(function test_lookup_localhost_ipv4(done) {

TEST(function test_lookup_all_ipv4(done) {
const req = dns.lookup(
'www.google.com',
addresses.INET4_HOST,
{ all: true, family: 4 },
common.mustCall((err, ips) => {
assert.ifError(err);
Expand Down
Loading