Comparing version 2.0.0 to 2.0.1
// Generated by CoffeeScript 1.12.7 | ||
(function() { | ||
var Netmask, ip2long, long2ip; | ||
var Netmask, atob, chr, chr0, chrA, chra, ip2long, long2ip; | ||
@@ -15,30 +15,77 @@ long2ip = function(long) { | ||
ip2long = function(ip) { | ||
var b, byte, i, j, len; | ||
b = (ip + '').split('.'); | ||
if (b.length === 0 || b.length > 4) { | ||
var b, c, i, j, n, ref; | ||
b = []; | ||
for (i = j = 0; j <= 3; i = ++j) { | ||
if (ip.length === 0) { | ||
break; | ||
} | ||
if (i > 0) { | ||
if (ip[0] !== '.') { | ||
throw new Error('Invalid IP'); | ||
} | ||
ip = ip.substring(1); | ||
} | ||
ref = atob(ip), n = ref[0], c = ref[1]; | ||
ip = ip.substring(c); | ||
b.push(n); | ||
} | ||
if (ip.length !== 0) { | ||
throw new Error('Invalid IP'); | ||
} | ||
for (i = j = 0, len = b.length; j < len; i = ++j) { | ||
byte = b[i]; | ||
if (byte && byte[0] === '0') { | ||
if (byte.length > 2 && (byte[1] === 'x' || byte[1] === 'x')) { | ||
byte = parseInt(byte, 16); | ||
while (b.length < 4) { | ||
b.unshift(0); | ||
} | ||
return (b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3]) >>> 0; | ||
}; | ||
chr = function(b) { | ||
return b.charCodeAt(0); | ||
}; | ||
chr0 = chr('0'); | ||
chra = chr('a'); | ||
chrA = chr('A'); | ||
atob = function(s) { | ||
var base, dmax, i, n, start; | ||
n = 0; | ||
base = 10; | ||
dmax = '9'; | ||
i = 0; | ||
if (s.length > 1 && s[i] === '0') { | ||
if (s[i + 1] === 'x' || s[i + 1] === 'X') { | ||
i += 2; | ||
base = 16; | ||
} else if ('0' <= s[i + 1] && s[i + 1] <= '7') { | ||
i++; | ||
base = 8; | ||
dmax = '7'; | ||
} | ||
} | ||
start = i; | ||
while (s.length > 0) { | ||
if ('0' <= s[i] && s[i] <= dmax) { | ||
n = n * base + (chr(s[i]) - chr0); | ||
} else if (base === 16) { | ||
if ('a' <= s[i] && s[i] <= 'f') { | ||
n = n * base + (10 + chr(s[i]) - chra); | ||
} else if ('A' <= s[i] && s[i] <= 'F') { | ||
n = n * base + (10 + chr(s[i]) - chrA); | ||
} else { | ||
byte = parseInt(byte, 8); | ||
break; | ||
} | ||
} else { | ||
byte = parseInt(byte, 10); | ||
break; | ||
} | ||
if (isNaN(byte)) { | ||
throw new Error("Invalid byte: " + byte); | ||
if (n > 0xFF) { | ||
throw new Error('byte overflow'); | ||
} | ||
if (byte < 0 || byte > 255) { | ||
throw new Error("Invalid byte: " + byte); | ||
} | ||
b[i] = byte; | ||
i++; | ||
} | ||
while (b.length < 4) { | ||
b.unshift(0); | ||
if (i === start) { | ||
throw new Error('empty octet'); | ||
} | ||
return (b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3]) >>> 0; | ||
return [n, i]; | ||
}; | ||
@@ -45,0 +92,0 @@ |
{ | ||
"author": "Olivier Poitrey <rs@dailymotion.com>", | ||
"author": "Olivier Poitrey <rs@rhapsodyk.net>", | ||
"name": "netmask", | ||
"description": "Parse and lookup IP network blocks", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"homepage": "https://github.com/rs/node-netmask", | ||
@@ -26,3 +26,3 @@ "bugs": "https://github.com/rs/node-netmask/issues", | ||
"prepublish": "coffee -c lib/*.coffee", | ||
"test": "vows --spec test/*" | ||
"test": "coffee -c lib/*.coffee && vows --spec test/* && mocha tests/*" | ||
}, | ||
@@ -34,4 +34,5 @@ "engines": { | ||
"coffee-script": ">=1.2.0", | ||
"mocha": "^8.3.2", | ||
"vows": "*" | ||
} | ||
} |
@@ -63,2 +63,7 @@ Netmask | ||
Run all tests (vows plus mocha) | ||
------------------------------- | ||
$ npm test | ||
License | ||
@@ -65,0 +70,0 @@ ------- |
@@ -1,24 +0,103 @@ | ||
/* some troubles with vows | ||
here is some mocha test | ||
/* It is important to test our Javascript output as well as our coffeescript, | ||
* since code that is transpiled may be slightly different in effect from the | ||
* original. | ||
* | ||
* Run these tests (against lib/netmask.js, not lib/netmask.coffee directly) | ||
* using mocha, after re-generating lib/netmask.js including your changes: | ||
* | ||
* mocha tests/netmask.js | ||
*/ | ||
npm install | ||
mocha tests/netmask.js | ||
*/ | ||
var assert = require('assert'); | ||
const assert = require('assert'); | ||
const Netmask = require('../').Netmask; | ||
var Netmask = require('../').Netmask; | ||
describe('Netmask', () => { | ||
describe('can build a block', () => { | ||
let block = new Netmask('10.1.2.0/24'); | ||
var block = new Netmask('10.1.2.0/24'); | ||
var b1 = new Netmask('10.1.2.10/29'); | ||
var b2 = new Netmask('10.1.2.10/31'); | ||
var b3 = new Netmask('10.1.2.20/32'); | ||
it('should contain a sub-block', () => { | ||
let block1 = new Netmask('10.1.2.10/29'); | ||
assert(block.contains(block1)); | ||
}); | ||
console.log('first : '+b2.base); | ||
console.log('broadcast : '+b2.broadcast); | ||
console.log('last : ' + b2.last); | ||
it('should contain another sub-block', () => { | ||
let block2 = new Netmask('10.1.2.10/31'); | ||
assert(block.contains(block2)); | ||
}); | ||
describe("Netmask contains bug", function() { | ||
assert.equal(block.contains(b1),true); | ||
assert.equal(block.contains(b2),true); | ||
assert.equal(block.contains(b3),true); | ||
}); | ||
it('should contain a third sub-block', () => { | ||
let block3 = new Netmask('10.1.2.20/32'); | ||
assert(block.contains(block3)); | ||
}); | ||
}); | ||
describe('when presented with an octet which is not a number', () => { | ||
let block = new Netmask('192.168.0.0/29') | ||
it('should throw', () => { | ||
assert.throws(() => block.contains('192.168.~.4'), Error); | ||
}); | ||
}); | ||
describe('can handle hexadecimal, octal, & decimal octets in input IP', () => { | ||
let block1 = new Netmask('31.0.0.0/19'); | ||
let block2 = new Netmask('127.0.0.0/8'); | ||
let block3 = new Netmask('255.0.0.1/12'); | ||
let block4 = new Netmask('10.0.0.1/8'); | ||
let block5 = new Netmask('1.0.0.1/4'); | ||
describe('octal', () => { | ||
it('block 31.0.0.0/19 does not contain 031.0.5.5', () => { | ||
assert(!block1.contains('031.0.5.5')); | ||
}); | ||
it('block 127.0.0.0/8 contains 0177.0.0.2 (127.0.0.2)', () => { | ||
assert(block2.contains('0177.0.0.2')); | ||
}); | ||
it('block 255.0.0.1/12 does not contain 0255.0.0.2 (173.0.0.2)', () => { | ||
assert(!block3.contains('0255.0.0.2')); | ||
}); | ||
it('block 10.0.0.1/8 contains 012.0.0.255 (10.0.0.255)', () => { | ||
assert(block4.contains('012.0.0.255')); | ||
}); | ||
it('block 1.0.0.1/4 contains 01.02.03.04', () => { | ||
assert(block5.contains('01.02.03.04')); | ||
}); | ||
}); | ||
describe('hexadecimal', () => { | ||
it('block 31.0.0.0/19 does not contain 0x31.0.5.5', () => { | ||
assert(!block1.contains('0x31.0.5.5')); | ||
}); | ||
it('block 127.0.0.0/8 contains 0x7f.0.0.0x2 (127.0.0.2)', () => { | ||
assert(block2.contains('0x7f.0.0.0x2')); | ||
}); | ||
it('block 255.0.0.1/12 contains 0xff.0.0.2', () => { | ||
assert(block3.contains('0xff.0.0.2')); | ||
}); | ||
it('block 10.0.0.1/8 does not contain 0x10.0.0.255', () => { | ||
assert(!block4.contains('0x10.0.0.255')); | ||
}); | ||
it('block 1.0.0.1/4 contains 0x1.0x2.0x3.0x4', () => { | ||
assert(block5.contains('0x1.0x2.0x3.0x4')); | ||
}); | ||
}); | ||
describe('decimal', () => { | ||
it('block 31.0.0.0/19 contains 31.0.5.5', () => { | ||
assert(block1.contains('31.0.5.5')); | ||
}); | ||
it('block 127.0.0.0/8 does not contain 128.0.0.2', () =>{ | ||
assert(!block2.contains('128.0.0.2')); | ||
}); | ||
it('block 255.0.0.1/12 contains 255.0.0.2', () => { | ||
assert(block3.contains('255.0.0.2')); | ||
}); | ||
it('block 10.0.0.1/8 contains 10.0.0.255', () => { | ||
assert(block4.contains('10.0.0.255')); | ||
}); | ||
it('block 1.0.0.1/4 contains 1.2.3.4', () => { | ||
assert(block5.contains('1.2.3.4')); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28306
9
261
83
3