diff options
author | Rebecca Turner <me@re-becca.org> | 2018-05-24 23:31:26 +0300 |
---|---|---|
committer | Rebecca Turner <me@re-becca.org> | 2018-05-24 23:31:38 +0300 |
commit | 0483f5c5deaf18c968a128657923103e49f4e67a (patch) | |
tree | 210f154da2f60938471d96a844dcb32cbda09e2a /node_modules/ip | |
parent | 6c294614d800cb95a49ef1c422fbdde65b0731a2 (diff) |
Flatten dependencies and add dev deps to git
Diffstat (limited to 'node_modules/ip')
-rw-r--r-- | node_modules/ip/.jscsrc | 46 | ||||
-rw-r--r-- | node_modules/ip/.npmignore | 2 | ||||
-rw-r--r-- | node_modules/ip/.travis.yml | 15 | ||||
-rw-r--r-- | node_modules/ip/README.md | 90 | ||||
-rw-r--r-- | node_modules/ip/lib/ip.js | 416 | ||||
-rw-r--r-- | node_modules/ip/package.json | 55 | ||||
-rw-r--r-- | node_modules/ip/test/api-test.js | 407 |
7 files changed, 1031 insertions, 0 deletions
diff --git a/node_modules/ip/.jscsrc b/node_modules/ip/.jscsrc new file mode 100644 index 000000000..dbaae2057 --- /dev/null +++ b/node_modules/ip/.jscsrc @@ -0,0 +1,46 @@ +{ + "disallowKeywordsOnNewLine": [ "else" ], + "disallowMixedSpacesAndTabs": true, + "disallowMultipleLineStrings": true, + "disallowMultipleVarDecl": true, + "disallowNewlineBeforeBlockStatements": true, + "disallowQuotedKeysInObjects": true, + "disallowSpaceAfterObjectKeys": true, + "disallowSpaceAfterPrefixUnaryOperators": true, + "disallowSpaceBeforePostfixUnaryOperators": true, + "disallowSpacesInCallExpression": true, + "disallowTrailingComma": true, + "disallowTrailingWhitespace": true, + "disallowYodaConditions": true, + + "requireCommaBeforeLineBreak": true, + "requireOperatorBeforeLineBreak": true, + "requireSpaceAfterBinaryOperators": true, + "requireSpaceAfterKeywords": [ "if", "for", "while", "else", "try", "catch" ], + "requireSpaceAfterLineComment": true, + "requireSpaceBeforeBinaryOperators": true, + "requireSpaceBeforeBlockStatements": true, + "requireSpaceBeforeKeywords": [ "else", "catch" ], + "requireSpaceBeforeObjectValues": true, + "requireSpaceBetweenArguments": true, + "requireSpacesInAnonymousFunctionExpression": { + "beforeOpeningCurlyBrace": true + }, + "requireSpacesInFunctionDeclaration": { + "beforeOpeningCurlyBrace": true + }, + "requireSpacesInFunctionExpression": { + "beforeOpeningCurlyBrace": true + }, + "requireSpacesInConditionalExpression": true, + "requireSpacesInForStatement": true, + "requireSpacesInsideArrayBrackets": "all", + "requireSpacesInsideObjectBrackets": "all", + "requireDotNotation": true, + + "maximumLineLength": 80, + "validateIndentation": 2, + "validateLineBreaks": "LF", + "validateParameterSeparator": ", ", + "validateQuoteMarks": "'" +} diff --git a/node_modules/ip/.npmignore b/node_modules/ip/.npmignore new file mode 100644 index 000000000..1ca957177 --- /dev/null +++ b/node_modules/ip/.npmignore @@ -0,0 +1,2 @@ +node_modules/ +npm-debug.log diff --git a/node_modules/ip/.travis.yml b/node_modules/ip/.travis.yml new file mode 100644 index 000000000..a3a8fad6b --- /dev/null +++ b/node_modules/ip/.travis.yml @@ -0,0 +1,15 @@ +sudo: false +language: node_js +node_js: + - "0.8" + - "0.10" + - "0.12" + - "4" + - "6" + +before_install: + - travis_retry npm install -g npm@2.14.5 + - travis_retry npm install + +script: + - npm test diff --git a/node_modules/ip/README.md b/node_modules/ip/README.md new file mode 100644 index 000000000..22e5819ff --- /dev/null +++ b/node_modules/ip/README.md @@ -0,0 +1,90 @@ +# IP +[![](https://badge.fury.io/js/ip.svg)](https://www.npmjs.com/package/ip) + +IP address utilities for node.js + +## Installation + +### npm +```shell +npm install ip +``` + +### git + +```shell +git clone https://github.com/indutny/node-ip.git +``` + +## Usage +Get your ip address, compare ip addresses, validate ip addresses, etc. + +```js +var ip = require('ip'); + +ip.address() // my ip address +ip.isEqual('::1', '::0:1'); // true +ip.toBuffer('127.0.0.1') // Buffer([127, 0, 0, 1]) +ip.toString(new Buffer([127, 0, 0, 1])) // 127.0.0.1 +ip.fromPrefixLen(24) // 255.255.255.0 +ip.mask('192.168.1.134', '255.255.255.0') // 192.168.1.0 +ip.cidr('192.168.1.134/26') // 192.168.1.128 +ip.not('255.255.255.0') // 0.0.0.255 +ip.or('192.168.1.134', '0.0.0.255') // 192.168.1.255 +ip.isPrivate('127.0.0.1') // true +ip.isV4Format('127.0.0.1'); // true +ip.isV6Format('::ffff:127.0.0.1'); // true + +// operate on buffers in-place +var buf = new Buffer(128); +var offset = 64; +ip.toBuffer('127.0.0.1', buf, offset); // [127, 0, 0, 1] at offset 64 +ip.toString(buf, offset, 4); // '127.0.0.1' + +// subnet information +ip.subnet('192.168.1.134', '255.255.255.192') +// { networkAddress: '192.168.1.128', +// firstAddress: '192.168.1.129', +// lastAddress: '192.168.1.190', +// broadcastAddress: '192.168.1.191', +// subnetMask: '255.255.255.192', +// subnetMaskLength: 26, +// numHosts: 62, +// length: 64, +// contains: function(addr){...} } +ip.cidrSubnet('192.168.1.134/26') +// Same as previous. + +// range checking +ip.cidrSubnet('192.168.1.134/26').contains('192.168.1.190') // true + + +// ipv4 long conversion +ip.toLong('127.0.0.1'); // 2130706433 +ip.fromLong(2130706433); // '127.0.0.1' +``` + +### License + +This software is licensed under the MIT License. + +Copyright Fedor Indutny, 2012. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to permit +persons to whom the Software is furnished to do so, subject to the +following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/node_modules/ip/lib/ip.js b/node_modules/ip/lib/ip.js new file mode 100644 index 000000000..c1799a8c5 --- /dev/null +++ b/node_modules/ip/lib/ip.js @@ -0,0 +1,416 @@ +'use strict'; + +var ip = exports; +var Buffer = require('buffer').Buffer; +var os = require('os'); + +ip.toBuffer = function(ip, buff, offset) { + offset = ~~offset; + + var result; + + if (this.isV4Format(ip)) { + result = buff || new Buffer(offset + 4); + ip.split(/\./g).map(function(byte) { + result[offset++] = parseInt(byte, 10) & 0xff; + }); + } else if (this.isV6Format(ip)) { + var sections = ip.split(':', 8); + + var i; + for (i = 0; i < sections.length; i++) { + var isv4 = this.isV4Format(sections[i]); + var v4Buffer; + + if (isv4) { + v4Buffer = this.toBuffer(sections[i]); + sections[i] = v4Buffer.slice(0, 2).toString('hex'); + } + + if (v4Buffer && ++i < 8) { + sections.splice(i, 0, v4Buffer.slice(2, 4).toString('hex')); + } + } + + if (sections[0] === '') { + while (sections.length < 8) sections.unshift('0'); + } else if (sections[sections.length - 1] === '') { + while (sections.length < 8) sections.push('0'); + } else if (sections.length < 8) { + for (i = 0; i < sections.length && sections[i] !== ''; i++); + var argv = [ i, 1 ]; + for (i = 9 - sections.length; i > 0; i--) { + argv.push('0'); + } + sections.splice.apply(sections, argv); + } + + result = buff || new Buffer(offset + 16); + for (i = 0; i < sections.length; i++) { + var word = parseInt(sections[i], 16); + result[offset++] = (word >> 8) & 0xff; + result[offset++] = word & 0xff; + } + } + + if (!result) { + throw Error('Invalid ip address: ' + ip); + } + + return result; +}; + +ip.toString = function(buff, offset, length) { + offset = ~~offset; + length = length || (buff.length - offset); + + var result = []; + if (length === 4) { + // IPv4 + for (var i = 0; i < length; i++) { + result.push(buff[offset + i]); + } + result = result.join('.'); + } else if (length === 16) { + // IPv6 + for (var i = 0; i < length; i += 2) { + result.push(buff.readUInt16BE(offset + i).toString(16)); + } + result = result.join(':'); + result = result.replace(/(^|:)0(:0)*:0(:|$)/, '$1::$3'); + result = result.replace(/:{3,4}/, '::'); + } + + return result; +}; + +var ipv4Regex = /^(\d{1,3}\.){3,3}\d{1,3}$/; +var ipv6Regex = + /^(::)?(((\d{1,3}\.){3}(\d{1,3}){1})?([0-9a-f]){0,4}:{0,2}){1,8}(::)?$/i; + +ip.isV4Format = function(ip) { + return ipv4Regex.test(ip); +}; + +ip.isV6Format = function(ip) { + return ipv6Regex.test(ip); +}; +function _normalizeFamily(family) { + return family ? family.toLowerCase() : 'ipv4'; +} + +ip.fromPrefixLen = function(prefixlen, family) { + if (prefixlen > 32) { + family = 'ipv6'; + } else { + family = _normalizeFamily(family); + } + + var len = 4; + if (family === 'ipv6') { + len = 16; + } + var buff = new Buffer(len); + + for (var i = 0, n = buff.length; i < n; ++i) { + var bits = 8; + if (prefixlen < 8) { + bits = prefixlen; + } + prefixlen -= bits; + + buff[i] = ~(0xff >> bits) & 0xff; + } + + return ip.toString(buff); +}; + +ip.mask = function(addr, mask) { + addr = ip.toBuffer(addr); + mask = ip.toBuffer(mask); + + var result = new Buffer(Math.max(addr.length, mask.length)); + + var i = 0; + // Same protocol - do bitwise and + if (addr.length === mask.length) { + for (i = 0; i < addr.length; i++) { + result[i] = addr[i] & mask[i]; + } + } else if (mask.length === 4) { + // IPv6 address and IPv4 mask + // (Mask low bits) + for (i = 0; i < mask.length; i++) { + result[i] = addr[addr.length - 4 + i] & mask[i]; + } + } else { + // IPv6 mask and IPv4 addr + for (var i = 0; i < result.length - 6; i++) { + result[i] = 0; + } + + // ::ffff:ipv4 + result[10] = 0xff; + result[11] = 0xff; + for (i = 0; i < addr.length; i++) { + result[i + 12] = addr[i] & mask[i + 12]; + } + i = i + 12; + } + for (; i < result.length; i++) + result[i] = 0; + + return ip.toString(result); +}; + +ip.cidr = function(cidrString) { + var cidrParts = cidrString.split('/'); + + var addr = cidrParts[0]; + if (cidrParts.length !== 2) + throw new Error('invalid CIDR subnet: ' + addr); + + var mask = ip.fromPrefixLen(parseInt(cidrParts[1], 10)); + + return ip.mask(addr, mask); +}; + +ip.subnet = function(addr, mask) { + var networkAddress = ip.toLong(ip.mask(addr, mask)); + + // Calculate the mask's length. + var maskBuffer = ip.toBuffer(mask); + var maskLength = 0; + + for (var i = 0; i < maskBuffer.length; i++) { + if (maskBuffer[i] === 0xff) { + maskLength += 8; + } else { + var octet = maskBuffer[i] & 0xff; + while (octet) { + octet = (octet << 1) & 0xff; + maskLength++; + } + } + } + + var numberOfAddresses = Math.pow(2, 32 - maskLength); + + return { + networkAddress: ip.fromLong(networkAddress), + firstAddress: numberOfAddresses <= 2 ? + ip.fromLong(networkAddress) : + ip.fromLong(networkAddress + 1), + lastAddress: numberOfAddresses <= 2 ? + ip.fromLong(networkAddress + numberOfAddresses - 1) : + ip.fromLong(networkAddress + numberOfAddresses - 2), + broadcastAddress: ip.fromLong(networkAddress + numberOfAddresses - 1), + subnetMask: mask, + subnetMaskLength: maskLength, + numHosts: numberOfAddresses <= 2 ? + numberOfAddresses : numberOfAddresses - 2, + length: numberOfAddresses, + contains: function(other) { + return networkAddress === ip.toLong(ip.mask(other, mask)); + } + }; +}; + +ip.cidrSubnet = function(cidrString) { + var cidrParts = cidrString.split('/'); + + var addr = cidrParts[0]; + if (cidrParts.length !== 2) + throw new Error('invalid CIDR subnet: ' + addr); + + var mask = ip.fromPrefixLen(parseInt(cidrParts[1], 10)); + + return ip.subnet(addr, mask); +}; + +ip.not = function(addr) { + var buff = ip.toBuffer(addr); + for (var i = 0; i < buff.length; i++) { + buff[i] = 0xff ^ buff[i]; + } + return ip.toString(buff); +}; + +ip.or = function(a, b) { + a = ip.toBuffer(a); + b = ip.toBuffer(b); + + // same protocol + if (a.length === b.length) { + for (var i = 0; i < a.length; ++i) { + a[i] |= b[i]; + } + return ip.toString(a); + + // mixed protocols + } else { + var buff = a; + var other = b; + if (b.length > a.length) { + buff = b; + other = a; + } + + var offset = buff.length - other.length; + for (var i = offset; i < buff.length; ++i) { + buff[i] |= other[i - offset]; + } + + return ip.toString(buff); + } +}; + +ip.isEqual = function(a, b) { + a = ip.toBuffer(a); + b = ip.toBuffer(b); + + // Same protocol + if (a.length === b.length) { + for (var i = 0; i < a.length; i++) { + if (a[i] !== b[i]) return false; + } + return true; + } + + // Swap + if (b.length === 4) { + var t = b; + b = a; + a = t; + } + + // a - IPv4, b - IPv6 + for (var i = 0; i < 10; i++) { + if (b[i] !== 0) return false; + } + + var word = b.readUInt16BE(10); + if (word !== 0 && word !== 0xffff) return false; + + for (var i = 0; i < 4; i++) { + if (a[i] !== b[i + 12]) return false; + } + + return true; +}; + +ip.isPrivate = function(addr) { + return /^(::f{4}:)?10\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i + .test(addr) || + /^(::f{4}:)?192\.168\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) || + /^(::f{4}:)?172\.(1[6-9]|2\d|30|31)\.([0-9]{1,3})\.([0-9]{1,3})$/i + .test(addr) || + /^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) || + /^(::f{4}:)?169\.254\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) || + /^f[cd][0-9a-f]{2}:/i.test(addr) || + /^fe80:/i.test(addr) || + /^::1$/.test(addr) || + /^::$/.test(addr); +}; + +ip.isPublic = function(addr) { + return !ip.isPrivate(addr); +}; + +ip.isLoopback = function(addr) { + return /^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/ + .test(addr) || + /^fe80::1$/.test(addr) || + /^::1$/.test(addr) || + /^::$/.test(addr); +}; + +ip.loopback = function(family) { + // + // Default to `ipv4` + // + family = _normalizeFamily(family); + + if (family !== 'ipv4' && family !== 'ipv6') { + throw new Error('family must be ipv4 or ipv6'); + } + + return family === 'ipv4' ? '127.0.0.1' : 'fe80::1'; +}; + +// +// ### function address (name, family) +// #### @name {string|'public'|'private'} **Optional** Name or security +// of the network interface. +// #### @family {ipv4|ipv6} **Optional** IP family of the address (defaults +// to ipv4). +// +// Returns the address for the network interface on the current system with +// the specified `name`: +// * String: First `family` address of the interface. +// If not found see `undefined`. +// * 'public': the first public ip address of family. +// * 'private': the first private ip address of family. +// * undefined: First address with `ipv4` or loopback address `127.0.0.1`. +// +ip.address = function(name, family) { + var interfaces = os.networkInterfaces(); + var all; + + // + // Default to `ipv4` + // + family = _normalizeFamily(family); + + // + // If a specific network interface has been named, + // return the address. + // + if (name && name !== 'private' && name !== 'public') { + var res = interfaces[name].filter(function(details) { + var itemFamily = details.family.toLowerCase(); + return itemFamily === family; + }); + if (res.length === 0) + return undefined; + return res[0].address; + } + + var all = Object.keys(interfaces).map(function (nic) { + // + // Note: name will only be `public` or `private` + // when this is called. + // + var addresses = interfaces[nic].filter(function (details) { + details.family = details.family.toLowerCase(); + if (details.family !== family || ip.isLoopback(details.address)) { + return false; + } else if (!name) { + return true; + } + + return name === 'public' ? ip.isPrivate(details.address) : + ip.isPublic(details.address); + }); + + return addresses.length ? addresses[0].address : undefined; + }).filter(Boolean); + + return !all.length ? ip.loopback(family) : all[0]; +}; + +ip.toLong = function(ip) { + var ipl = 0; + ip.split('.').forEach(function(octet) { + ipl <<= 8; + ipl += parseInt(octet); + }); + return(ipl >>> 0); +}; + +ip.fromLong = function(ipl) { + return ((ipl >>> 24) + '.' + + (ipl >> 16 & 255) + '.' + + (ipl >> 8 & 255) + '.' + + (ipl & 255) ); +}; diff --git a/node_modules/ip/package.json b/node_modules/ip/package.json new file mode 100644 index 000000000..fb58d8c04 --- /dev/null +++ b/node_modules/ip/package.json @@ -0,0 +1,55 @@ +{ + "_from": "ip@^1.1.5", + "_id": "ip@1.1.5", + "_inBundle": false, + "_integrity": "sha1-vd7XARQpCCjAoDnnLvJfWq7ENUo=", + "_location": "/ip", + "_phantomChildren": {}, + "_requested": { + "type": "range", + "registry": true, + "raw": "ip@^1.1.5", + "name": "ip", + "escapedName": "ip", + "rawSpec": "^1.1.5", + "saveSpec": null, + "fetchSpec": "^1.1.5" + }, + "_requiredBy": [ + "/npm-profile/socks", + "/npm-registry-fetch/socks", + "/socks" + ], + "_resolved": "https://registry.npmjs.org/ip/-/ip-1.1.5.tgz", + "_shasum": "bdded70114290828c0a039e72ef25f5aaec4354a", + "_spec": "ip@^1.1.5", + "_where": "/Users/rebecca/code/npm/node_modules/socks", + "author": { + "name": "Fedor Indutny", + "email": "fedor@indutny.com" + }, + "bugs": { + "url": "https://github.com/indutny/node-ip/issues" + }, + "bundleDependencies": false, + "deprecated": false, + "description": "[![](https://badge.fury.io/js/ip.svg)](https://www.npmjs.com/package/ip)", + "devDependencies": { + "jscs": "^2.1.1", + "jshint": "^2.8.0", + "mocha": "~1.3.2" + }, + "homepage": "https://github.com/indutny/node-ip", + "license": "MIT", + "main": "lib/ip", + "name": "ip", + "repository": { + "type": "git", + "url": "git+ssh://git@github.com/indutny/node-ip.git" + }, + "scripts": { + "fix": "jscs lib/*.js test/*.js --fix", + "test": "jscs lib/*.js test/*.js && jshint lib/*.js && mocha --reporter spec test/*-test.js" + }, + "version": "1.1.5" +} diff --git a/node_modules/ip/test/api-test.js b/node_modules/ip/test/api-test.js new file mode 100644 index 000000000..2e390f986 --- /dev/null +++ b/node_modules/ip/test/api-test.js @@ -0,0 +1,407 @@ +'use strict'; + +var ip = require('..'); +var assert = require('assert'); +var net = require('net'); +var os = require('os'); + +describe('IP library for node.js', function() { + describe('toBuffer()/toString() methods', function() { + it('should convert to buffer IPv4 address', function() { + var buf = ip.toBuffer('127.0.0.1'); + assert.equal(buf.toString('hex'), '7f000001'); + assert.equal(ip.toString(buf), '127.0.0.1'); + }); + + it('should convert to buffer IPv4 address in-place', function() { + var buf = new Buffer(128); + var offset = 64; + ip.toBuffer('127.0.0.1', buf, offset); + assert.equal(buf.toString('hex', offset, offset + 4), '7f000001'); + assert.equal(ip.toString(buf, offset, 4), '127.0.0.1'); + }); + + it('should convert to buffer IPv6 address', function() { + var buf = ip.toBuffer('::1'); + assert(/(00){15,15}01/.test(buf.toString('hex'))); + assert.equal(ip.toString(buf), '::1'); + assert.equal(ip.toString(ip.toBuffer('1::')), '1::'); + assert.equal(ip.toString(ip.toBuffer('abcd::dcba')), 'abcd::dcba'); + }); + + it('should convert to buffer IPv6 address in-place', function() { + var buf = new Buffer(128); + var offset = 64; + ip.toBuffer('::1', buf, offset); + assert(/(00){15,15}01/.test(buf.toString('hex', offset, offset + 16))); + assert.equal(ip.toString(buf, offset, 16), '::1'); + assert.equal(ip.toString(ip.toBuffer('1::', buf, offset), + offset, 16), '1::'); + assert.equal(ip.toString(ip.toBuffer('abcd::dcba', buf, offset), + offset, 16), 'abcd::dcba'); + }); + + it('should convert to buffer IPv6 mapped IPv4 address', function() { + var buf = ip.toBuffer('::ffff:127.0.0.1'); + assert.equal(buf.toString('hex'), '00000000000000000000ffff7f000001'); + assert.equal(ip.toString(buf), '::ffff:7f00:1'); + + buf = ip.toBuffer('ffff::127.0.0.1'); + assert.equal(buf.toString('hex'), 'ffff000000000000000000007f000001'); + assert.equal(ip.toString(buf), 'ffff::7f00:1'); + + buf = ip.toBuffer('0:0:0:0:0:ffff:127.0.0.1'); + assert.equal(buf.toString('hex'), '00000000000000000000ffff7f000001'); + assert.equal(ip.toString(buf), '::ffff:7f00:1'); + }); + }); + + describe('fromPrefixLen() method', function() { + it('should create IPv4 mask', function() { + assert.equal(ip.fromPrefixLen(24), '255.255.255.0'); + }); + it('should create IPv6 mask', function() { + assert.equal(ip.fromPrefixLen(64), 'ffff:ffff:ffff:ffff::'); + }); + it('should create IPv6 mask explicitly', function() { + assert.equal(ip.fromPrefixLen(24, 'IPV6'), 'ffff:ff00::'); + }); + }); + + describe('not() method', function() { + it('should reverse bits in address', function() { + assert.equal(ip.not('255.255.255.0'), '0.0.0.255'); + }); + }); + + describe('or() method', function() { + it('should or bits in ipv4 addresses', function() { + assert.equal(ip.or('0.0.0.255', '192.168.1.10'), '192.168.1.255'); + }); + it('should or bits in ipv6 addresses', function() { + assert.equal(ip.or('::ff', '::abcd:dcba:abcd:dcba'), + '::abcd:dcba:abcd:dcff'); + }); + it('should or bits in mixed addresses', function() { + assert.equal(ip.or('0.0.0.255', '::abcd:dcba:abcd:dcba'), + '::abcd:dcba:abcd:dcff'); + }); + }); + + describe('mask() method', function() { + it('should mask bits in address', function() { + assert.equal(ip.mask('192.168.1.134', '255.255.255.0'), '192.168.1.0'); + assert.equal(ip.mask('192.168.1.134', '::ffff:ff00'), '::ffff:c0a8:100'); + }); + + it('should not leak data', function() { + for (var i = 0; i < 10; i++) + assert.equal(ip.mask('::1', '0.0.0.0'), '::'); + }); + }); + + describe('subnet() method', function() { + // Test cases calculated with http://www.subnet-calculator.com/ + var ipv4Subnet = ip.subnet('192.168.1.134', '255.255.255.192'); + + it('should compute ipv4 network address', function() { + assert.equal(ipv4Subnet.networkAddress, '192.168.1.128'); + }); + + it('should compute ipv4 network\'s first address', function() { + assert.equal(ipv4Subnet.firstAddress, '192.168.1.129'); + }); + + it('should compute ipv4 network\'s last address', function() { + assert.equal(ipv4Subnet.lastAddress, '192.168.1.190'); + }); + + it('should compute ipv4 broadcast address', function() { + assert.equal(ipv4Subnet.broadcastAddress, '192.168.1.191'); + }); + + it('should compute ipv4 subnet number of addresses', function() { + assert.equal(ipv4Subnet.length, 64); + }); + + it('should compute ipv4 subnet number of addressable hosts', function() { + assert.equal(ipv4Subnet.numHosts, 62); + }); + + it('should compute ipv4 subnet mask', function() { + assert.equal(ipv4Subnet.subnetMask, '255.255.255.192'); + }); + + it('should compute ipv4 subnet mask\'s length', function() { + assert.equal(ipv4Subnet.subnetMaskLength, 26); + }); + + it('should know whether a subnet contains an address', function() { + assert.equal(ipv4Subnet.contains('192.168.1.180'), true); + }); + + it('should know whether a subnet does not contain an address', function() { + assert.equal(ipv4Subnet.contains('192.168.1.195'), false); + }); + }); + + describe('subnet() method with mask length 32', function() { + // Test cases calculated with http://www.subnet-calculator.com/ + var ipv4Subnet = ip.subnet('192.168.1.134', '255.255.255.255'); + it('should compute ipv4 network\'s first address', function() { + assert.equal(ipv4Subnet.firstAddress, '192.168.1.134'); + }); + + it('should compute ipv4 network\'s last address', function() { + assert.equal(ipv4Subnet.lastAddress, '192.168.1.134'); + }); + + it('should compute ipv4 subnet number of addressable hosts', function() { + assert.equal(ipv4Subnet.numHosts, 1); + }); + }); + + describe('subnet() method with mask length 31', function() { + // Test cases calculated with http://www.subnet-calculator.com/ + var ipv4Subnet = ip.subnet('192.168.1.134', '255.255.255.254'); + it('should compute ipv4 network\'s first address', function() { + assert.equal(ipv4Subnet.firstAddress, '192.168.1.134'); + }); + + it('should compute ipv4 network\'s last address', function() { + assert.equal(ipv4Subnet.lastAddress, '192.168.1.135'); + }); + + it('should compute ipv4 subnet number of addressable hosts', function() { + assert.equal(ipv4Subnet.numHosts, 2); + }); + }); + + describe('cidrSubnet() method', function() { + // Test cases calculated with http://www.subnet-calculator.com/ + var ipv4Subnet = ip.cidrSubnet('192.168.1.134/26'); + + it('should compute an ipv4 network address', function() { + assert.equal(ipv4Subnet.networkAddress, '192.168.1.128'); + }); + + it('should compute an ipv4 network\'s first address', function() { + assert.equal(ipv4Subnet.firstAddress, '192.168.1.129'); + }); + + it('should compute an ipv4 network\'s last address', function() { + assert.equal(ipv4Subnet.lastAddress, '192.168.1.190'); + }); + + it('should compute an ipv4 broadcast address', function() { + assert.equal(ipv4Subnet.broadcastAddress, '192.168.1.191'); + }); + + it('should compute an ipv4 subnet number of addresses', function() { + assert.equal(ipv4Subnet.length, 64); + }); + + it('should compute an ipv4 subnet number of addressable hosts', function() { + assert.equal(ipv4Subnet.numHosts, 62); + }); + + it('should compute an ipv4 subnet mask', function() { + assert.equal(ipv4Subnet.subnetMask, '255.255.255.192'); + }); + + it('should compute an ipv4 subnet mask\'s length', function() { + assert.equal(ipv4Subnet.subnetMaskLength, 26); + }); + + it('should know whether a subnet contains an address', function() { + assert.equal(ipv4Subnet.contains('192.168.1.180'), true); + }); + + it('should know whether a subnet contains an address', function() { + assert.equal(ipv4Subnet.contains('192.168.1.195'), false); + }); + + }); + + describe('cidr() method', function() { + it('should mask address in CIDR notation', function() { + assert.equal(ip.cidr('192.168.1.134/26'), '192.168.1.128'); + assert.equal(ip.cidr('2607:f0d0:1002:51::4/56'), '2607:f0d0:1002::'); + }); + }); + + describe('isEqual() method', function() { + it('should check if addresses are equal', function() { + assert(ip.isEqual('127.0.0.1', '::7f00:1')); + assert(!ip.isEqual('127.0.0.1', '::7f00:2')); + assert(ip.isEqual('127.0.0.1', '::ffff:7f00:1')); + assert(!ip.isEqual('127.0.0.1', '::ffaf:7f00:1')); + assert(ip.isEqual('::ffff:127.0.0.1', '::ffff:127.0.0.1')); + assert(ip.isEqual('::ffff:127.0.0.1', '127.0.0.1')); + }); + }); + + + describe('isPrivate() method', function() { + it('should check if an address is localhost', function() { + assert.equal(ip.isPrivate('127.0.0.1'), true); + }); + + it('should check if an address is from a 192.168.x.x network', function() { + assert.equal(ip.isPrivate('192.168.0.123'), true); + assert.equal(ip.isPrivate('192.168.122.123'), true); + assert.equal(ip.isPrivate('192.162.1.2'), false); + }); + + it('should check if an address is from a 172.16.x.x network', function() { + assert.equal(ip.isPrivate('172.16.0.5'), true); + assert.equal(ip.isPrivate('172.16.123.254'), true); + assert.equal(ip.isPrivate('171.16.0.5'), false); + assert.equal(ip.isPrivate('172.25.232.15'), true); + assert.equal(ip.isPrivate('172.15.0.5'), false); + assert.equal(ip.isPrivate('172.32.0.5'), false); + }); + + it('should check if an address is from a 169.254.x.x network', function() { + assert.equal(ip.isPrivate('169.254.2.3'), true); + assert.equal(ip.isPrivate('169.254.221.9'), true); + assert.equal(ip.isPrivate('168.254.2.3'), false); + }); + + it('should check if an address is from a 10.x.x.x network', function() { + assert.equal(ip.isPrivate('10.0.2.3'), true); + assert.equal(ip.isPrivate('10.1.23.45'), true); + assert.equal(ip.isPrivate('12.1.2.3'), false); + }); + + it('should check if an address is from a private IPv6 network', function() { + assert.equal(ip.isPrivate('fd12:3456:789a:1::1'), true); + assert.equal(ip.isPrivate('fe80::f2de:f1ff:fe3f:307e'), true); + assert.equal(ip.isPrivate('::ffff:10.100.1.42'), true); + assert.equal(ip.isPrivate('::FFFF:172.16.200.1'), true); + assert.equal(ip.isPrivate('::ffff:192.168.0.1'), true); + }); + + it('should check if an address is from the internet', function() { + assert.equal(ip.isPrivate('165.225.132.33'), false); // joyent.com + }); + + it('should check if an address is a loopback IPv6 address', function() { + assert.equal(ip.isPrivate('::'), true); + assert.equal(ip.isPrivate('::1'), true); + assert.equal(ip.isPrivate('fe80::1'), true); + }); + }); + + describe('loopback() method', function() { + describe('undefined', function() { + it('should respond with 127.0.0.1', function() { + assert.equal(ip.loopback(), '127.0.0.1') + }); + }); + + describe('ipv4', function() { + it('should respond with 127.0.0.1', function() { + assert.equal(ip.loopback('ipv4'), '127.0.0.1') + }); + }); + + describe('ipv6', function() { + it('should respond with fe80::1', function() { + assert.equal(ip.loopback('ipv6'), 'fe80::1') + }); + }); + }); + + describe('isLoopback() method', function() { + describe('127.0.0.1', function() { + it('should respond with true', function() { + assert.ok(ip.isLoopback('127.0.0.1')) + }); + }); + + describe('127.8.8.8', function () { + it('should respond with true', function () { + assert.ok(ip.isLoopback('127.8.8.8')) + }); + }); + + describe('8.8.8.8', function () { + it('should respond with false', function () { + assert.equal(ip.isLoopback('8.8.8.8'), false); + }); + }); + + describe('fe80::1', function() { + it('should respond with true', function() { + assert.ok(ip.isLoopback('fe80::1')) + }); + }); + + describe('::1', function() { + it('should respond with true', function() { + assert.ok(ip.isLoopback('::1')) + }); + }); + + describe('::', function() { + it('should respond with true', function() { + assert.ok(ip.isLoopback('::')) + }); + }); + }); + + describe('address() method', function() { + describe('undefined', function() { + it('should respond with a private ip', function() { + assert.ok(ip.isPrivate(ip.address())); + }); + }); + + describe('private', function() { + [ undefined, 'ipv4', 'ipv6' ].forEach(function(family) { + describe(family, function() { + it('should respond with a private ip', function() { + assert.ok(ip.isPrivate(ip.address('private', family))); + }); + }); + }); + }); + + var interfaces = os.networkInterfaces(); + + Object.keys(interfaces).forEach(function(nic) { + describe(nic, function() { + [ undefined, 'ipv4' ].forEach(function(family) { + describe(family, function() { + it('should respond with an ipv4 address', function() { + var addr = ip.address(nic, family); + assert.ok(!addr || net.isIPv4(addr)); + }); + }); + }); + + describe('ipv6', function() { + it('should respond with an ipv6 address', function() { + var addr = ip.address(nic, 'ipv6'); + assert.ok(!addr || net.isIPv6(addr)); + }); + }) + }); + }); + }); + + describe('toLong() method', function() { + it('should respond with a int', function() { + assert.equal(ip.toLong('127.0.0.1'), 2130706433); + assert.equal(ip.toLong('255.255.255.255'), 4294967295); + }); + }); + + describe('fromLong() method', function() { + it('should repond with ipv4 address', function() { + assert.equal(ip.fromLong(2130706433), '127.0.0.1'); + assert.equal(ip.fromLong(4294967295), '255.255.255.255'); + }); + }) +}); |