Browse Source

isIP test

v0.7.4-release
Henry Rawas 14 years ago
committed by Ryan Dahl
parent
commit
1018e7d23f
  1. 4
      lib/net_uv.js
  2. 4
      test/simple/test-net-isip.js

4
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 // TODO: isIP should be moved to the DNS code. Putting it here now because
// this is what the legacy system did. // 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) { exports.isIP = function(input) {
if (!input) { if (!input) {
return 4; return 4;
@ -575,7 +577,7 @@ exports.isIP = function(input) {
} }
} }
return 4; 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; return 6;
} else { } else {
return 0; return 0;

4
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:252:0:1::2008:6'), 6);
assert.equal(net.isIP('2001:dead:beef: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('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('127.0.0.1'), true);
assert.equal(net.isIPv4('example.com'), false); assert.equal(net.isIPv4('example.com'), false);

Loading…
Cancel
Save