isIP test
authorHenry Rawas <henryr@schakra.com>
Thu, 7 Jul 2011 18:23:27 +0000 (11:23 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Fri, 8 Jul 2011 17:36:16 +0000 (10:36 -0700)
lib/net_uv.js
test/simple/test-net-isip.js

index e62e3ca..fded05d 100644 (file)
@@ -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;
index c226f1a..feb0805 100644 (file)
@@ -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);