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

test: refactor the code in test-dns-ipv4 #10200

Closed
wants to merge 1 commit into from
Closed
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
143 changes: 70 additions & 73 deletions test/internet/test-dns-ipv4.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const dns = require('dns');
const net = require('net');
const isIPv4 = net.isIPv4;

let expected = 0;
let completed = 0;
let running = false;
const queue = [];

function TEST(f) {
function next() {
var f = queue.shift();
const f = queue.shift();
if (f) {
running = true;
console.log(f.name);
Expand All @@ -22,11 +20,9 @@ function TEST(f) {

function done() {
running = false;
completed++;
process.nextTick(next);
}

expected++;
queue.push(f);

if (!running) {
Expand All @@ -39,149 +35,150 @@ function checkWrap(req) {
}

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

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

for (var 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) {
var req = dns.reverse('8.8.8.8', function(err, domains) {
if (err) throw err;
const req = dns.reverse('8.8.8.8',
common.mustCall((err, domains) => {
assert.ifError(err);

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

for (var 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) {
var req = dns.lookup('www.google.com', 4, function(err, ip, family) {
if (err) throw err;
assert.ok(net.isIPv4(ip));
assert.strictEqual(family, 4);
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);

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

checkWrap(req);
});

TEST(function test_lookup_ipv4_implicit(done) {
var req = dns.lookup('www.google.com', function(err, ip, family) {
if (err) throw err;
assert.ok(net.isIPv4(ip));
assert.strictEqual(family, 4);
const req = dns.lookup('www.google.com',
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) {
var req = dns.lookup('www.google.com', {
const req = dns.lookup('www.google.com', {
family: 4
}, function(err, ip, family) {
if (err) throw err;
}, common.mustCall((err, ip, family) => {
assert.ifError(err);
assert.ok(net.isIPv4(ip));
assert.strictEqual(family, 4);

done();
});
}));

checkWrap(req);
});

TEST(function test_lookup_ipv4_hint_addrconfig(done) {
var req = dns.lookup('www.google.com', {
const req = dns.lookup('www.google.com', {
hints: dns.ADDRCONFIG
}, function(err, ip, family) {
if (err) throw err;
}, common.mustCall((err, ip, family) => {
assert.ifError(err);
assert.ok(net.isIPv4(ip));
assert.strictEqual(family, 4);

done();
});
}));

checkWrap(req);
});

TEST(function test_lookup_ip_ipv4(done) {
var req = dns.lookup('127.0.0.1', function(err, ip, family) {
if (err) throw err;
assert.strictEqual(ip, '127.0.0.1');
assert.strictEqual(family, 4);
const req = dns.lookup('127.0.0.1',
common.mustCall((err, ip, family) => {
assert.ifError(err);
assert.strictEqual(ip, '127.0.0.1');
assert.strictEqual(family, 4);

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

checkWrap(req);
});

TEST(function test_lookup_localhost_ipv4(done) {
var req = dns.lookup('localhost', 4, function(err, ip, family) {
if (err) throw err;
assert.strictEqual(ip, '127.0.0.1');
assert.strictEqual(family, 4);
const req = dns.lookup('localhost', 4,
common.mustCall((err, ip, family) => {
assert.ifError(err);
assert.strictEqual(ip, '127.0.0.1');
assert.strictEqual(family, 4);

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

checkWrap(req);
});

TEST(function test_lookup_all_ipv4(done) {
var req = dns.lookup(
const req = dns.lookup(
'www.google.com',
{all: true, family: 4},
function(err, ips) {
if (err) throw err;
common.mustCall((err, ips) => {
assert.ifError(err);
assert.ok(Array.isArray(ips));
assert.ok(ips.length > 0);

ips.forEach(function(ip) {
ips.forEach((ip) => {
assert.ok(isIPv4(ip.address));
assert.strictEqual(ip.family, 4);
});

done();
}
);
));

checkWrap(req);
});

TEST(function test_lookupservice_ip_ipv4(done) {
var req = dns.lookupService('127.0.0.1', 80, function(err, host, service) {
if (err) throw err;
assert.equal(typeof host, 'string');
assert(host);
assert(['http', 'www', '80'].includes(service));
done();
});
const req = dns.lookupService('127.0.0.1', 80,
common.mustCall((err, host, service) => {
assert.ifError(err);
assert.strictEqual(typeof host, 'string');
assert(host);
assert(['http', 'www', '80'].includes(service));
done();
}));

checkWrap(req);
});

process.on('exit', function() {
console.log(completed + ' tests completed');
assert.equal(running, false);
assert.strictEqual(expected, completed);
});