sorry i ever left you dear mocha node:test has two major issues: * --bail does not work and requires strange modules and incantations. I was able to work around this with a custom module. * the test reporter reports _after_ the suite is run. this makes debugging really hard. the debugs that we print all happen before the test suite summary. poor design overall.
111 lines
4.3 KiB
JavaScript
111 lines
4.3 KiB
JavaScript
import { describe, it } from 'mocha';
|
|
import assert from 'node:assert/strict';
|
|
import ipaddr from '../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}`, () => assert.equal(ipaddr.isValid(goodIPv4), true));
|
|
}
|
|
|
|
for (const badIPv4 of badIPv4s) {
|
|
it(`isValid returns false ${badIPv4}`, () => assert.equal(ipaddr.isValid(badIPv4), 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}`, () => assert.equal(ipaddr.isValidCIDR(goodCIDR), true));
|
|
}
|
|
|
|
for (const badCIDR of badCIDRs) {
|
|
it(`isValidCIDR returns false ${badCIDR}`, () => assert.equal(ipaddr.isValidCIDR(badCIDR), false));
|
|
}
|
|
|
|
it('isEqual', function () {
|
|
assert.equal(ipaddr.isEqual('1.2.3.4', '1.2.3.4'), true);
|
|
assert.equal(ipaddr.isEqual('1.2.3.4', '1.2.3.5'), false);
|
|
assert.equal(ipaddr.isEqual('1.2.3.4', '1.2.3.'), false);
|
|
});
|
|
|
|
it('includes', function () {
|
|
assert.equal(ipaddr.includes('1.2.3.0/8', '1.2.3.4'), true);
|
|
assert.equal(ipaddr.includes('1.2.0.0/16', '1.3.0.0'), 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}`, () => assert.equal(ipaddr.isValid(goodIPv6), true));
|
|
}
|
|
|
|
for (const badIPv6 of badIPv6s) {
|
|
it(`isValid returns false ${badIPv6}`, () => assert.equal(ipaddr.isValid(badIPv6), false));
|
|
}
|
|
|
|
const goodCIDRs = [
|
|
'2001:db8::/32',
|
|
'::/128'
|
|
];
|
|
|
|
const badCIDRs = [
|
|
'2001:db8::/129',
|
|
'::a/abc'
|
|
];
|
|
|
|
for (const goodCIDR of goodCIDRs) {
|
|
it(`isValidCIDR returns true ${goodCIDR}`, () => assert.equal(ipaddr.isValidCIDR(goodCIDR), true));
|
|
}
|
|
|
|
for (const badCIDR of badCIDRs) {
|
|
it(`isValidCIDR returns false ${badCIDR}`, () => assert.equal(ipaddr.isValidCIDR(badCIDR), false));
|
|
}
|
|
|
|
it('isEqual', function () {
|
|
assert.equal(ipaddr.isEqual('2001:0db8:85a3:0000:0000:8a2e:0370:7334', '2001:0db8:85a3:0000:0000:8a2e:0370:7334'), true); // same
|
|
assert.equal(ipaddr.isEqual('2001:0db8:85a3:0000:0000:8a2e:0370:7334', '2001:0db8:85a3::0000:8a2e:0370:7334'), true); // shorthand
|
|
assert.equal(ipaddr.isEqual('2001:db8:85A3:0000:0000:8a2e:0370:7334', '2001:0db8:85a3::0000:8a2e:370:7334'), true); // casing change and no 0 prefix
|
|
assert.equal(ipaddr.isEqual('2001:db8:85A3:0000:0000:8a2e:0370:7334', '1.2.3.4'), false);
|
|
assert.equal(ipaddr.isEqual('::ffff:1.2.3.4', '1.2.3.4'), true); // ipv6 mapped ipv4
|
|
assert.equal(ipaddr.isEqual('2002:0db8:85a3:0000:0000:8a2e:0370:7334', '2001:0db8:85a3:0000:0000:8a2e:0370:7334'), false);
|
|
});
|
|
|
|
it('includes', function () {
|
|
assert.equal(ipaddr.includes('2001:0db8:85a3::/64', '2001:0db8:85a3:0000:0000:8a2e:0370:7334'), true);
|
|
assert.equal(ipaddr.includes('2001:0db8:85a3::/64', '2002:0db8:85a3:0000:0000:8a2e:0370:7334'), false);
|
|
assert.equal(ipaddr.includes('::ffff:0:0/96', '1.2.3.4'), true); // ipv6 mapped ipv4
|
|
});
|
|
});
|
|
});
|