replace ipaddr.js
This commit is contained in:
113
src/test/ipaddr-test.js
Normal file
113
src/test/ipaddr-test.js
Normal file
@@ -0,0 +1,113 @@
|
||||
'use strict';
|
||||
|
||||
/* global it, describe */
|
||||
|
||||
const expect = require('expect.js'),
|
||||
ipaddr = require('../ipaddr.js');
|
||||
|
||||
describe('ipaddr', function () {
|
||||
describe('IPv4', function () {
|
||||
const goodIPv4s = [ '1.2.3.4', '0.235.255.123' ];
|
||||
const badIPv4s = [ '1.2.3', '1.2.3.256', '-1.2.3.4', '1e2.5.6.7', 'x.7.8.9', '1..2.4' ];
|
||||
|
||||
for (const goodIPv4 of goodIPv4s) {
|
||||
it(`isValid returns true ${goodIPv4}`, () => expect(ipaddr.isValid(goodIPv4)).to.be(true));
|
||||
}
|
||||
|
||||
for (const badIPv4 of badIPv4s) {
|
||||
it(`isValid returns false ${badIPv4}`, () => expect(ipaddr.isValid(badIPv4)).to.be(false));
|
||||
}
|
||||
|
||||
const goodCIDRs = [
|
||||
'192.168.1.0/24'
|
||||
];
|
||||
|
||||
const badCIDRs = [
|
||||
'192.168.1.0/-1',
|
||||
'192.168.1.0/',
|
||||
'192.168.1.0/33',
|
||||
'192.168.1.0/1e2'
|
||||
];
|
||||
|
||||
for (const goodCIDR of goodCIDRs) {
|
||||
it(`isValidCIDR returns true ${goodCIDR}`, () => expect(ipaddr.isValidCIDR(goodCIDR)).to.be(true));
|
||||
}
|
||||
|
||||
for (const badCIDR of badCIDRs) {
|
||||
it(`isValidCIDR returns false ${badCIDR}`, () => expect(ipaddr.isValidCIDR(badCIDR)).to.be(false));
|
||||
}
|
||||
|
||||
it('isEqual', function () {
|
||||
expect(ipaddr.isEqual('1.2.3.4', '1.2.3.4')).to.be(true);
|
||||
expect(ipaddr.isEqual('1.2.3.4', '1.2.3.5')).to.be(false);
|
||||
expect(ipaddr.isEqual('1.2.3.4', '1.2.3.')).to.be(false);
|
||||
});
|
||||
|
||||
it('includes', function () {
|
||||
expect(ipaddr.includes('1.2.3.0/8', '1.2.3.4')).to.be(true);
|
||||
expect(ipaddr.includes('1.2.0.0/16', '1.3.0.0')).to.be(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('IPv6', function () {
|
||||
const goodIPv6s = [
|
||||
'2001:0db8:85a3:0000:0000:8a2e:0370:7334',
|
||||
'::1',
|
||||
'2001:db8::ff00:42:8329',
|
||||
'::ffff:1.2.3.4',
|
||||
'2001:db8:85a3::8a2e:370:7334',
|
||||
'2001:0db8:85a3::',
|
||||
'2000::',
|
||||
'2002:c0a8:101::42',
|
||||
'::a:b'
|
||||
];
|
||||
const badIPv6s = [
|
||||
'2001:db8::85a3::8a2e:370:7334', // too many ::
|
||||
'2001:db8::85a3:8g2e:370:7334', // invalid hex
|
||||
'12345::1', // segment too long
|
||||
'::a:b:c:d:e:f:f:f:f', // too many segments
|
||||
':192:168:0:1',
|
||||
];
|
||||
|
||||
for (const goodIPv6 of goodIPv6s) {
|
||||
it(`isValid returns true ${goodIPv6}`, () => expect(ipaddr.isValid(goodIPv6)).to.be(true));
|
||||
}
|
||||
|
||||
for (const badIPv6 of badIPv6s) {
|
||||
it(`isValid returns false ${badIPv6}`, () => expect(ipaddr.isValid(badIPv6)).to.be(false));
|
||||
}
|
||||
|
||||
const goodCIDRs = [
|
||||
'2001:db8::/32',
|
||||
'::/128'
|
||||
];
|
||||
|
||||
const badCIDRs = [
|
||||
'2001:db8::/129',
|
||||
'::a/abc'
|
||||
];
|
||||
|
||||
for (const goodCIDR of goodCIDRs) {
|
||||
it(`isValidCIDR returns true ${goodCIDR}`, () => expect(ipaddr.isValidCIDR(goodCIDR)).to.be(true));
|
||||
}
|
||||
|
||||
for (const badCIDR of badCIDRs) {
|
||||
it(`isValidCIDR returns false ${badCIDR}`, () => expect(ipaddr.isValidCIDR(badCIDR)).to.be(false));
|
||||
}
|
||||
|
||||
it('isEqual', function () {
|
||||
expect(ipaddr.isEqual('2001:0db8:85a3:0000:0000:8a2e:0370:7334', '2001:0db8:85a3:0000:0000:8a2e:0370:7334')).to.be(true); // same
|
||||
expect(ipaddr.isEqual('2001:0db8:85a3:0000:0000:8a2e:0370:7334', '2001:0db8:85a3::0000:8a2e:0370:7334')).to.be(true); // shorthand
|
||||
expect(ipaddr.isEqual('2001:db8:85A3:0000:0000:8a2e:0370:7334', '2001:0db8:85a3::0000:8a2e:370:7334')).to.be(true); // casing change and no 0 prefix
|
||||
expect(ipaddr.isEqual('2001:db8:85A3:0000:0000:8a2e:0370:7334', '1.2.3.4')).to.be(false);
|
||||
expect(ipaddr.isEqual('::ffff:1.2.3.4', '1.2.3.4')).to.be(true); // ipv6 mapped ipv4
|
||||
expect(ipaddr.isEqual('2002:0db8:85a3:0000:0000:8a2e:0370:7334', '2001:0db8:85a3:0000:0000:8a2e:0370:7334')).to.be(false);
|
||||
});
|
||||
|
||||
it('includes', function () {
|
||||
expect(ipaddr.includes('2001:0db8:85a3::/64', '2001:0db8:85a3:0000:0000:8a2e:0370:7334')).to.be(true);
|
||||
expect(ipaddr.includes('2001:0db8:85a3::/64', '2002:0db8:85a3:0000:0000:8a2e:0370:7334')).to.be(false);
|
||||
expect(ipaddr.includes('::ffff:0:0/96', '1.2.3.4')).to.be(true); // ipv6 mapped ipv4
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user