net: make isIP() return 0 on empty input

pull/24503/head
rsolomo 2012-05-13 19:17:51 -07:00 committed by Ben Noordhuis
parent 38542f76a9
commit d40415912f
2 changed files with 5 additions and 4 deletions

View File

@ -1030,7 +1030,7 @@ Server.prototype.listenFD = function(fd, type) {
// and it does not detect more than one double : in a string.
exports.isIP = function(input) {
if (!input) {
return 4;
return 0;
} else if (/^(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)$/.test(input)) {
var parts = input.split('.');
for (var i = 0; i < parts.length; i++) {

View File

@ -36,12 +36,13 @@ assert.equal(net.isIP('::1'), 6);
assert.equal(net.isIP('::'), 6);
assert.equal(net.isIP('0000:0000:0000:0000:0000:0000:12345:0000'), 0);
assert.equal(net.isIP('0'), 0);
assert.equal(net.isIP(), 0);
assert.equal(net.isIP(""), 0);
assert.equal(net.isIPv4('127.0.0.1'), true);
assert.equal(net.isIPv4('example.com'), false);
assert.equal(net.isIPv4('2001:252:0:1::2008:6'), false);
assert.equal(net.isIPv6('127.0.0.1'), false);
assert.equal(net.isIPv4('example.com'), false);
assert.equal(net.isIPv6('2001:252:0:1::2008:6'), true);
assert.equal(net.isIPv6('example.com'), false);
assert.equal(net.isIPv6('2001:252:0:1::2008:6'), true);