From 1018e7d23f579f90948664b66752fee58b23ef40 Mon Sep 17 00:00:00 2001 From: Henry Rawas Date: Thu, 7 Jul 2011 11:23:27 -0700 Subject: [PATCH] isIP test --- lib/net_uv.js | 4 +++- test/simple/test-net-isip.js | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/net_uv.js b/lib/net_uv.js index e62e3caa95..fded05d4a7 100644 --- a/lib/net_uv.js +++ b/lib/net_uv.js @@ -563,6 +563,8 @@ Server.prototype.close = function() { // TODO: isIP should be moved to the DNS code. Putting it here now because // this is what the legacy system did. +// NOTE: This does not accept IPv6 with an IPv4 dotted address at the end, +// and it does not detect more than one double : in a string. exports.isIP = function(input) { if (!input) { return 4; @@ -575,7 +577,7 @@ exports.isIP = function(input) { } } return 4; - } else if (/^(::)?([a-fA-F0-9]+(::?)?)*$/.test(input)) { + } else if (/^::|^::1|^([a-fA-F0-9]{1,4}::?){1,7}([a-fA-F0-9]{1,4})$/.test(input)) { return 6; } else { return 0; diff --git a/test/simple/test-net-isip.js b/test/simple/test-net-isip.js index c226f1a9d3..feb0805c36 100644 --- a/test/simple/test-net-isip.js +++ b/test/simple/test-net-isip.js @@ -32,6 +32,10 @@ assert.equal(net.isIP('1050:0:0:0:5:600:300c:326b'), 6); assert.equal(net.isIP('2001:252:0:1::2008:6'), 6); assert.equal(net.isIP('2001:dead:beef:1::2008:6'), 6); assert.equal(net.isIP('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'), 6); +assert.equal(net.isIP('::1'), 6); +assert.equal(net.isIP('::'), 6); +assert.equal(net.isIP('0000:0000:0000:0000:0000:0000:00001:0000'), 0); +assert.equal(net.isIP('0'), 0); assert.equal(net.isIPv4('127.0.0.1'), true); assert.equal(net.isIPv4('example.com'), false);