summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcjihrig <cjihrig@gmail.com>2014-08-15 10:40:09 -0400
committerTrevor Norris <trev.norris@gmail.com>2014-08-20 13:51:09 -0700
commit5086d6ef94d1ce2b67193781052be84bb6913e63 (patch)
tree5565430038b87847fb31a869d47828ae4ddc3ea3
parent437c2f438335541084136480a9a5308075dd3dfd (diff)
downloadnodejs-5086d6ef94d1ce2b67193781052be84bb6913e63.tar.gz
nodejs-5086d6ef94d1ce2b67193781052be84bb6913e63.tar.bz2
nodejs-5086d6ef94d1ce2b67193781052be84bb6913e63.zip
dns: throw if hostname is not string or falsey
Fix assertion failure from poor argument parsing logic introduced in 6ea5d16. Add tests to make sure arguments are properly parsed. Fixes: 6ea5d16 "dns: always set variable family in lookup()" Reviewed-by: Trevor Norris <trev.norris@gmail.com>
-rw-r--r--lib/dns.js4
-rw-r--r--test/simple/test-dns.js41
2 files changed, 44 insertions, 1 deletions
diff --git a/lib/dns.js b/lib/dns.js
index 288373b45..c61e6e9fc 100644
--- a/lib/dns.js
+++ b/lib/dns.js
@@ -105,7 +105,9 @@ exports.lookup = function lookup(hostname, options, callback) {
var family = -1;
// Parse arguments
- if (typeof options === 'function') {
+ if (hostname && typeof hostname !== 'string') {
+ throw TypeError('invalid arguments: hostname must be a string or falsey');
+ } else if (typeof options === 'function') {
callback = options;
family = 0;
} else if (typeof callback !== 'function') {
diff --git a/test/simple/test-dns.js b/test/simple/test-dns.js
index c62a3c889..405bcf0a0 100644
--- a/test/simple/test-dns.js
+++ b/test/simple/test-dns.js
@@ -69,6 +69,47 @@ assert.throws(function() {
return !(err instanceof TypeError);
}, 'Unexpected error');
+// dns.lookup should accept falsey and string values
+assert.throws(function() {
+ dns.lookup({}, noop);
+}, 'invalid arguments: hostname must be a string or falsey');
+
+assert.throws(function() {
+ dns.lookup([], noop);
+}, 'invalid arguments: hostname must be a string or falsey');
+
+assert.throws(function() {
+ dns.lookup(true, noop);
+}, 'invalid arguments: hostname must be a string or falsey');
+
+assert.throws(function() {
+ dns.lookup(1, noop);
+}, 'invalid arguments: hostname must be a string or falsey');
+
+assert.throws(function() {
+ dns.lookup(noop, noop);
+}, 'invalid arguments: hostname must be a string or falsey');
+
+assert.doesNotThrow(function() {
+ dns.lookup('', noop);
+});
+
+assert.doesNotThrow(function() {
+ dns.lookup(null, noop);
+});
+
+assert.doesNotThrow(function() {
+ dns.lookup(undefined, noop);
+});
+
+assert.doesNotThrow(function() {
+ dns.lookup(0, noop);
+});
+
+assert.doesNotThrow(function() {
+ dns.lookup(NaN, noop);
+});
+
/*
* Make sure that dns.lookup throws if hints does not represent a valid flag.
* (dns.V4MAPPED | dns.ADDRCONFIG) + 1 is invalid because: