Skip to content

Commit

Permalink
test: refactor test-dns
Browse files Browse the repository at this point in the history
* track callback invocations with common.mustCall() and
  common.mustNotCall()
* remove test in test/internet/test-dns.js that is duplicated in
  test/parallel/test-dns.js
* move tests that might perform a DNS query from test
  test/parallel/test-dns.js to test/internet/test-dns.js

PR-URL: #13163
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
Trott authored and jasnell committed May 28, 2017
1 parent 7fe5303 commit a985ed6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 48 deletions.
22 changes: 9 additions & 13 deletions test/internet/test-dns.js
Original file line number Original file line 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) { TEST(function test_lookup_ip_all(done) {
const req = dns.lookup('127.0.0.1', {all: true}, function(err, ips, family) { const req = dns.lookup('127.0.0.1', {all: true}, function(err, ips, family) {
assert.ifError(err); assert.ifError(err);
Expand Down Expand Up @@ -578,3 +565,12 @@ process.on('exit', function() {
assert.strictEqual(expected, completed); assert.strictEqual(expected, completed);
assert.ok(getaddrinfoCallbackCalled); 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 Original file line Diff line number Diff line change
Expand Up @@ -91,34 +91,47 @@ assert.doesNotThrow(() => dns.setServers([]));
assert.deepStrictEqual(dns.getServers(), []); assert.deepStrictEqual(dns.getServers(), []);


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


// dns.lookup should accept falsey and string values // dns.lookup should accept only falsey and string values
const errorReg = {
/^TypeError: Invalid arguments: hostname must be a string or falsey$/; 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. * 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. * flags are either === 1 or even.
*/ */
assert.throws(() => { assert.throws(() => {
dns.lookup('www.google.com', { hints: (dns.V4MAPPED | dns.ADDRCONFIG) + 1 }, dns.lookup('nodejs.org', { hints: (dns.V4MAPPED | dns.ADDRCONFIG) + 1 },
common.noop); common.mustNotCall());
}, /^TypeError: Invalid argument: hints must use valid flags$/); }, /^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$/); /^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$/); /^TypeError: Invalid arguments: callback must be passed$/);


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

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

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


assert.doesNotThrow(() => { assert.doesNotThrow(() => {
dns.lookup('', { dns.lookup('', {
family: 6, family: 6,
hints: dns.ADDRCONFIG 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(() => { assert.doesNotThrow(() => {
dns.lookup('', { dns.lookup('', {
hints: dns.ADDRCONFIG | dns.V4MAPPED hints: dns.ADDRCONFIG | dns.V4MAPPED
}, common.noop); }, common.mustCall());
}); });


assert.throws(() => dns.lookupService('0.0.0.0'), assert.throws(() => dns.lookupService('0.0.0.0'),
/^Error: Invalid arguments$/); /^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$/); /^TypeError: "host" argument needs to be a valid IP address$/);


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

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

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


assert.throws( 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"$/ /^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"$/); /^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"$/); /^TypeError: "port" should be >= 0 and < 65536, got "test"$/);


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

0 comments on commit a985ed6

Please sign in to comment.