Skip to content

Commit

Permalink
test: use internet.addresses in internet tests
Browse files Browse the repository at this point in the history
PR-URL: nodejs#16390
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
joyeecheung committed Mar 30, 2018
1 parent 239c02d commit 9fb711a
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 114 deletions.
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
76 changes: 41 additions & 35 deletions test/internet/test-dns-ipv6.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');
if (!common.hasIPv6)
common.skip('this test, no IPv6 support');

Expand Down Expand Up @@ -38,53 +39,57 @@ function checkWrap(req) {
}

TEST(function test_resolve6(done) {
const req = dns.resolve6('ipv6.google.com',
common.mustCall((err, ips) => {
assert.ifError(err);
const req = dns.resolve6(
addresses.INET6_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(isIPv6(ips[i]));
for (let i = 0; i < ips.length; i++)
assert.ok(isIPv6(ips[i]));

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

checkWrap(req);
});

TEST(function test_reverse_ipv6(done) {
const req = dns.reverse('2001:4860:4860::8888',
common.mustCall((err, domains) => {
assert.ifError(err);
const req = dns.reverse(
addresses.INET6_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(typeof domains[i] === 'string');
for (let i = 0; i < domains.length; i++)
assert.ok(typeof domains[i] === 'string');

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

checkWrap(req);
});

TEST(function test_lookup_ipv6_explicit(done) {
const req = dns.lookup('ipv6.google.com', 6,
common.mustCall((err, ip, family) => {
assert.ifError(err);
assert.ok(isIPv6(ip));
assert.strictEqual(family, 6);
const req = dns.lookup(
addresses.INET6_HOST,
6,
common.mustCall((err, ip, family) => {
assert.ifError(err);
assert.ok(isIPv6(ip));
assert.strictEqual(family, 6);

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

checkWrap(req);
});

/* This ends up just being too problematic to test
TEST(function test_lookup_ipv6_implicit(done) {
var req = dns.lookup('ipv6.google.com', function(err, ip, family) {
var req = dns.lookup(addresses.INET6_HOST, function(err, ip, family) {
assert.ifError(err);
assert.ok(net.isIPv6(ip));
assert.strictEqual(family, 6);
Expand All @@ -97,7 +102,7 @@ TEST(function test_lookup_ipv6_implicit(done) {
*/

TEST(function test_lookup_ipv6_explicit_object(done) {
const req = dns.lookup('ipv6.google.com', {
const req = dns.lookup(addresses.INET6_HOST, {
family: 6
}, common.mustCall((err, ip, family) => {
assert.ifError(err);
Expand All @@ -111,7 +116,7 @@ TEST(function test_lookup_ipv6_explicit_object(done) {
});

TEST(function test_lookup_ipv6_hint(done) {
const req = dns.lookup('www.google.com', {
const req = dns.lookup(addresses.INET6_HOST, {
family: 6,
hints: dns.V4MAPPED
}, common.mustCall((err, ip, family) => {
Expand All @@ -120,7 +125,7 @@ TEST(function test_lookup_ipv6_hint(done) {
if (common.isFreeBSD) {
assert(err instanceof Error);
assert.strictEqual(err.code, 'EAI_BADFLAGS');
assert.strictEqual(err.hostname, 'www.google.com');
assert.strictEqual(err.hostname, addresses.INET_HOST);
assert.ok(/getaddrinfo EAI_BADFLAGS/.test(err.message));
done();
return;
Expand All @@ -139,21 +144,22 @@ TEST(function test_lookup_ipv6_hint(done) {
});

TEST(function test_lookup_ip_ipv6(done) {
const req = dns.lookup('::1',
common.mustCall((err, ip, family) => {
assert.ifError(err);
assert.ok(isIPv6(ip));
assert.strictEqual(family, 6);
const req = dns.lookup(
'::1',
common.mustCall((err, ip, family) => {
assert.ifError(err);
assert.ok(isIPv6(ip));
assert.strictEqual(family, 6);

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

checkWrap(req);
});

TEST(function test_lookup_all_ipv6(done) {
const req = dns.lookup(
'www.google.com',
addresses.INET6_HOST,
{ all: true, family: 6 },
common.mustCall((err, ips) => {
assert.ifError(err);
Expand Down
11 changes: 7 additions & 4 deletions test/internet/test-dns-setserver-in-callback-of-resolve4.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
// a crash or not. If it doesn't crash, the test succeeded.

const common = require('../common');
const { addresses } = require('../common/internet');
const dns = require('dns');

dns.resolve4('google.com', common.mustCall(function(/* err, nameServers */) {
dns.setServers([ '8.8.8.8' ]);
}));
dns.resolve4(
addresses.INET4_HOST,
common.mustCall(function(/* err, nameServers */) {
dns.setServers([ addresses.DNS4_SERVER ]);
}));

// Test https://github.com/nodejs/node/issues/14734
dns.resolve4('google.com', common.mustCall());
dns.resolve4(addresses.INET4_HOST, common.mustCall());
Loading

0 comments on commit 9fb711a

Please sign in to comment.