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 test-dns #13163

Closed
wants to merge 1 commit into from
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
22 changes: 9 additions & 13 deletions test/internet/test-dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,19 +402,6 @@ TEST(function test_lookup_failure(done) {
});


TEST(function test_lookup_null(done) {
const req = dns.lookup(null, function(err, ip, family) {
assert.ifError(err);
assert.strictEqual(ip, null);
assert.strictEqual(family, 4);

done();
});

checkWrap(req);
});


TEST(function test_lookup_ip_all(done) {
const req = dns.lookup('127.0.0.1', {all: true}, function(err, ips, family) {
assert.ifError(err);
Expand Down Expand Up @@ -578,3 +565,12 @@ process.on('exit', function() {
assert.strictEqual(expected, completed);
assert.ok(getaddrinfoCallbackCalled);
});


assert.doesNotThrow(() => dns.lookup('nodejs.org', 6, common.mustCall()));

assert.doesNotThrow(() => dns.lookup('nodejs.org', {}, common.mustCall()));

assert.doesNotThrow(() => dns.lookupService('0.0.0.0', '0', common.mustCall()));

assert.doesNotThrow(() => dns.lookupService('0.0.0.0', 0, common.mustCall()));
77 changes: 42 additions & 35 deletions test/parallel/test-dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,34 +91,47 @@ assert.doesNotThrow(() => dns.setServers([]));
assert.deepStrictEqual(dns.getServers(), []);

assert.throws(() => {
dns.resolve('test.com', [], common.noop);
dns.resolve('test.com', [], common.mustNotCall());
}, function(err) {
return !(err instanceof TypeError);
}, 'Unexpected error');

// dns.lookup should accept falsey and string values
const errorReg =
/^TypeError: Invalid arguments: hostname must be a string or falsey$/;
// dns.lookup should accept only falsey and string values
{
const errorReg =
/^TypeError: Invalid arguments: hostname must be a string or falsey$/;

assert.throws(() => dns.lookup({}, common.noop), errorReg);
assert.throws(() => dns.lookup({}, common.mustNotCall()), errorReg);

assert.throws(() => dns.lookup([], common.noop), errorReg);
assert.throws(() => dns.lookup([], common.mustNotCall()), errorReg);

assert.throws(() => dns.lookup(true, common.noop), errorReg);
assert.throws(() => dns.lookup(true, common.mustNotCall()), errorReg);

assert.throws(() => dns.lookup(1, common.noop), errorReg);
assert.throws(() => dns.lookup(1, common.mustNotCall()), errorReg);

assert.throws(() => dns.lookup(common.noop, common.noop), errorReg);
assert.throws(() => dns.lookup(common.mustNotCall(), common.mustNotCall()),
errorReg);
}

assert.doesNotThrow(() => dns.lookup('', common.noop));
// dns.lookup should accept falsey values
{
const checkCallback = (err, address, family) => {
assert.ifError(err);
assert.strictEqual(address, null);
assert.strictEqual(family, 4);
};

assert.doesNotThrow(() => dns.lookup(null, common.noop));
assert.doesNotThrow(() => dns.lookup('', common.mustCall(checkCallback)));

assert.doesNotThrow(() => dns.lookup(undefined, common.noop));
assert.doesNotThrow(() => dns.lookup(null, common.mustCall(checkCallback)));

assert.doesNotThrow(() => dns.lookup(0, common.noop));
assert.doesNotThrow(() => dns.lookup(undefined,
common.mustCall(checkCallback)));

assert.doesNotThrow(() => dns.lookup(NaN, common.noop));
assert.doesNotThrow(() => dns.lookup(0, common.mustCall(checkCallback)));

assert.doesNotThrow(() => dns.lookup(NaN, common.mustCall(checkCallback)));
}

/*
* Make sure that dns.lookup throws if hints does not represent a valid flag.
Expand All @@ -130,59 +143,53 @@ assert.doesNotThrow(() => dns.lookup(NaN, common.noop));
* flags are either === 1 or even.
*/
assert.throws(() => {
dns.lookup('www.google.com', { hints: (dns.V4MAPPED | dns.ADDRCONFIG) + 1 },
common.noop);
dns.lookup('nodejs.org', { hints: (dns.V4MAPPED | dns.ADDRCONFIG) + 1 },
common.mustNotCall());
}, /^TypeError: Invalid argument: hints must use valid flags$/);

assert.throws(() => dns.lookup('www.google.com'),
assert.throws(() => dns.lookup('nodejs.org'),
/^TypeError: Invalid arguments: callback must be passed$/);

assert.throws(() => dns.lookup('www.google.com', 4),
assert.throws(() => dns.lookup('nodejs.org', 4),
/^TypeError: Invalid arguments: callback must be passed$/);

assert.doesNotThrow(() => dns.lookup('www.google.com', 6, common.noop));

assert.doesNotThrow(() => dns.lookup('www.google.com', {}, common.noop));

assert.doesNotThrow(() => dns.lookup('', {family: 4, hints: 0}, common.noop));
assert.doesNotThrow(() => dns.lookup('', {family: 4, hints: 0},
common.mustCall()));

assert.doesNotThrow(() => {
dns.lookup('', {
family: 6,
hints: dns.ADDRCONFIG
}, common.noop);
}, common.mustCall());
});

assert.doesNotThrow(() => dns.lookup('', {hints: dns.V4MAPPED}, common.noop));
assert.doesNotThrow(() => dns.lookup('', {hints: dns.V4MAPPED},
common.mustCall()));

assert.doesNotThrow(() => {
dns.lookup('', {
hints: dns.ADDRCONFIG | dns.V4MAPPED
}, common.noop);
}, common.mustCall());
});

assert.throws(() => dns.lookupService('0.0.0.0'),
/^Error: Invalid arguments$/);

assert.throws(() => dns.lookupService('fasdfdsaf', 0, common.noop),
assert.throws(() => dns.lookupService('fasdfdsaf', 0, common.mustNotCall()),
/^TypeError: "host" argument needs to be a valid IP address$/);

assert.doesNotThrow(() => dns.lookupService('0.0.0.0', '0', common.noop));

assert.doesNotThrow(() => dns.lookupService('0.0.0.0', 0, common.noop));

assert.throws(() => dns.lookupService('0.0.0.0', null, common.noop),
assert.throws(() => dns.lookupService('0.0.0.0', null, common.mustNotCall()),
/^TypeError: "port" should be >= 0 and < 65536, got "null"$/);

assert.throws(
() => dns.lookupService('0.0.0.0', undefined, common.noop),
() => dns.lookupService('0.0.0.0', undefined, common.mustNotCall()),
/^TypeError: "port" should be >= 0 and < 65536, got "undefined"$/
);

assert.throws(() => dns.lookupService('0.0.0.0', 65538, common.noop),
assert.throws(() => dns.lookupService('0.0.0.0', 65538, common.mustNotCall()),
/^TypeError: "port" should be >= 0 and < 65536, got "65538"$/);

assert.throws(() => dns.lookupService('0.0.0.0', 'test', common.noop),
assert.throws(() => dns.lookupService('0.0.0.0', 'test', common.mustNotCall()),
/^TypeError: "port" should be >= 0 and < 65536, got "test"$/);

assert.throws(() => dns.lookupService('0.0.0.0', 80, null),
Expand Down