ip-filtering-tree
Version:
On memory database indexed by IP addresses.
955 lines (889 loc) • 163 kB
JavaScript
var chai = require('chai'),
should = chai.should();
const IPFilteringTree = require('../index').IPFilteringTree;
describe('IPFilteringTree', () => {
var dict = undefined;
const I_IPV4_DATA = 0;
const I_IPV4_LENGTH_OF_SUBNETMASK = 1;
const I_IPV4_LENGTH_OF_CHILD_SUBNETMASK = 2;
const I_IPV4_REF_CHILD_NODE = 3;
function assertTheNode(node, data, subnetLength, subnetLengthOfChild, indexesOfChildNodes) {
should.equal(node[I_IPV4_DATA], data);
should.equal(node[I_IPV4_LENGTH_OF_SUBNETMASK], subnetLength);
should.equal(node[I_IPV4_LENGTH_OF_CHILD_SUBNETMASK], subnetLengthOfChild);
should.equal(Object.keys(node[I_IPV4_REF_CHILD_NODE]).length, indexesOfChildNodes.length);
for(var i = 0; i < indexesOfChildNodes.length; ++i) {
should.exist(node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary(indexesOfChildNodes[i])]);
}
}
beforeEach(function() {
dict = new IPFilteringTree();
});
describe('#iPv4StringToBinary', () => {
it('should convert "0.0.0.0" to 0', () => {
dict.iPv4StringToBinary('0.0.0.0').should.equal(0);
});
it('should convert "255.255.255.255", to -1', () => {
// 255.255.255.255 -> 11111111 11111111 11111111 11111111 -> -1
dict.iPv4StringToBinary('255.255.255.255').should.equal(-1);
});
it('should convert "255.255.255.0" to -256', () => {
// 255.255.255.0 -> 11111111 11111111 11111111 00000000 -> -256
dict.iPv4StringToBinary('255.255.255.0').should.equal(-256);
});
it('should convert "255.255.0.0" to -65536', () => {
// 255.255.0.0 -> 11111111 11111111 00000000 00000000 -> -65536
dict.iPv4StringToBinary('255.255.0.0').should.equal(-65536);
});
it('should convert "255.0.0.0" to -16777216', () => {
// 255.0.0.0 -> 11111111 00000000 00000000 00000000 -> -16777216
dict.iPv4StringToBinary('255.0.0.0').should.equal(-16777216);
});
it('should convert "192.168.1.0" to -1062731520', () => {
// 192.168.1.0 -> 11000000 10101000 00000001 00000000 -> -1062731520
dict.iPv4StringToBinary('192.168.1.0').should.equal(-1062731520);
});
it('should convert "192.168.1.1" to -1062731519', () => {
// 192.168.1.0 -> 11000000 10101000 00000001 00000001 -> -1062731519
dict.iPv4StringToBinary('192.168.1.1').should.equal(-1062731519);
});
it('should convert "172.16.0.0" to -1408237568', () => {
// 172.16.0.0 -> 10101100 00010000 00000000 00000000 -> -1408237568
dict.iPv4StringToBinary('172.16.0.0').should.equal(-1408237568);
});
it('should convert "10.0.0.0" to 167772160', () => {
// 10.0.0.0 -> 00001010 00000000 00000000 00000000 -> 167772160
dict.iPv4StringToBinary('10.0.0.0').should.equal(167772160);
});
it('should throws exception when the invalid format of IPv4 was specified.', () => {
(() => { dict.iPv4StringToBinary(''); }).should.throw(TypeError, 'Format of IPv4 address "" is illegal');
(() => { dict.iPv4StringToBinary('foo'); }).should.throw(TypeError, 'Format of IPv4 address "foo" is illegal');
(() => { dict.iPv4StringToBinary('0.0.0.0.0'); }).should.throw(TypeError, 'Format of IPv4 address "0.0.0.0.0" is illegal');
(() => { dict.iPv4StringToBinary('0.0.0'); }).should.throw(TypeError, 'Format of IPv4 address "0.0.0" is illegal');
(() => { dict.iPv4StringToBinary('0.0.0.'); }).should.throw(TypeError, 'Format of IPv4 address "0.0.0." is illegal');
(() => { dict.iPv4StringToBinary('.255.255.255'); }).should.throw(TypeError, 'Format of IPv4 address ".255.255.255" is illegal');
(() => { dict.iPv4StringToBinary('foo.255.255.255'); }).should.throw(TypeError, 'Format of IPv4 address "foo.255.255.255" is illegal');
(() => { dict.iPv4StringToBinary('0.0.0.256'); }).should.throw(TypeError, 'Format of IPv4 address "0.0.0.256" is illegal');
(() => { dict.iPv4StringToBinary('0.0.0.-1'); }).should.throw(TypeError, 'Format of IPv4 address "0.0.0.-1" is illegal');
});
});
describe('#stringifyFromBinIPv4', () => {
it('should stringify 0 to "0.0.0.0"', () => {
dict.stringifyFromBinIPv4(0).should.equal('0.0.0.0');
});
it('should stringify 0 to "255.255.255.255"', () => {
dict.stringifyFromBinIPv4(-1).should.equal('255.255.255.255');
});
it('should stringify 0 to "255.255.255.0"', () => {
dict.stringifyFromBinIPv4(-256).should.equal('255.255.255.0');
});
it('should stringify 0 to "255.255.0.0"', () => {
dict.stringifyFromBinIPv4(-65536).should.equal('255.255.0.0');
});
it('should stringify 0 to "255.0.0.0"', () => {
dict.stringifyFromBinIPv4(-16777216).should.equal('255.0.0.0');
});
it('should stringify 0 to "192.168.1.0"', () => {
dict.stringifyFromBinIPv4(-1062731520).should.equal('192.168.1.0');
});
it('should stringify 0 to "172.16.0.0"', () => {
dict.stringifyFromBinIPv4(-1408237568).should.equal('172.16.0.0');
});
it('should stringify 0 to "10.0.0.0"', () => {
dict.stringifyFromBinIPv4(167772160).should.equal('10.0.0.0');
});
});
describe('#hasGlueNodeOnly', () => {
it('should return false if no node are existed', () => {
var node = ["dummy", 0, undefined, {}];
dict.hasGlueNodeOnly(node).should.equal(false);
});
it('should return true if the node is glue', () => {
var node = [
"dummy", 0, 8, {
0: [undefined, 8, 16, { 100: ["dummy", 16, undefined, {}] }]
}
];
dict.hasGlueNodeOnly(node).should.equal(true);
});
it('should return true if the nodes are glue', () => {
var node = [
"dummy", 0, 8, {
0: [undefined, 8, 16, { 100: ["dummy", 16, undefined, {}] }],
1: [undefined, 8, 16, { 100: ["dummy", 16, undefined, {}] }]
}
];
dict.hasGlueNodeOnly(node).should.equal(true);
});
it('should return false if the node is data', () => {
var node = [
"dummy", 0, 8, {
02: ["dymmy", 8, 16, { 100: ["dummy", 16, undefined, {}] }]
}
];
dict.hasGlueNodeOnly(node).should.equal(false);
});
it('should return false if all node are NOT glue', () => {
var node = [
"dummy", 0, 8, {
0: [undefined, 8, 16, { 100: ["dummy", 16, undefined, {}] }],
1: [undefined, 8, 16, { 100: ["dummy", 16, undefined, {}] }],
2: ["dymmy", 8, 16, { 100: ["dummy", 16, undefined, {}] }]
}
];
dict.hasGlueNodeOnly(node).should.equal(false);
});
});
describe('#getBinIPv4NetAddr', function() {
it('should get the network address from the binary IPv4 address', () => {
dict.getBinIPv4NetAddr(-1, 32).should.equal(-1); // 255.255.255.255
dict.getBinIPv4NetAddr(-1, 24).should.equal(-256); // 255.255.255.0
dict.getBinIPv4NetAddr(-1, 16).should.equal(-65536); // 255.255.0.0
dict.getBinIPv4NetAddr(-1, 8).should.equal(-16777216); // 255.0.0.0
dict.getBinIPv4NetAddr(-1, 0).should.equal(0); // 0.0.0.0
dict.getBinIPv4NetAddr(-1, 31).should.equal(-2); // 255.255.255.254
});
});
describe('#createNewOneNode', function() {
it('should create a new node with specified parameters', () => {
var result = dict.createNewOneNode({a: 0}, 16, 24, {b: 1});
result[I_IPV4_DATA].a.should.equal(0);
result[I_IPV4_LENGTH_OF_SUBNETMASK].should.equal(16);
result[I_IPV4_LENGTH_OF_CHILD_SUBNETMASK].should.equal(24);
result[I_IPV4_REF_CHILD_NODE].b.should.equal(1);
});
});
describe('#createGlueNodes', () => {
it('should create a sinble glue node', () => {
/*
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+-+-----------------------+
| 2) 192.168.1.0/24(d) |
+-------------------------+
> below >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
| New
+-+-----------------------+
| 192.168.0.0/16(g) |
+-------------------------+
|
+-+-----------------------+
| 192.168.1.0/24(d) |
+-------------------------+
*/
dict.push("0.0.0.0", 0, "Data of 0.0.0.0/0");
dict.push("192.168.1.0", 24, "Data of 192.168.1.0/24");
var node = dict.getRootNode();
dict.createGlueNodes(node, 16);
assertTheNode(node, 'Data of 0.0.0.0/0', 0, 16, ['192.168.0.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node, undefined, 16, 24, ['192.168.1.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.1.0')];
assertTheNode(node, 'Data of 192.168.1.0/24', 24, undefined, []);
});
it('should create a single glue node over the glue node', () => {
/*
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+-+-----------------------+
| 192.168.0.0/16(g) |
+-------------------------+
|
+-+-----------------------+
| 192.168.1.0/24(d) |
+-------------------------+
> below >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
| New
+-+-----------------------+
| 192.0.0.0/8(g) |
+-------------------------+
|
+-+-----------------------+
| 192.168.0.0/16(g) |
+-------------------------+
|
+-+-----------------------+
| 192.168.1.0/24(d) |
+-------------------------+
*/
dict.push("0.0.0.0", 0, "Data of 0.0.0.0/0");
dict.push("192.168.1.0", 24, "Data of 192.168.1.0/24");
var node = dict.getRootNode();
dict.createGlueNodes(node, 16);
dict.createGlueNodes(node, 8);
assertTheNode(node, 'Data of 0.0.0.0/0', 0, 8, ['192.0.0.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.0.0.0')];
assertTheNode(node, undefined, 8, 16, ['192.168.0.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node, undefined, 16, 24, ['192.168.1.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.1.0')];
assertTheNode(node, 'Data of 192.168.1.0/24', 24, undefined, []);
});
it('should create a single glue node under the middle of data node', () => {
/*
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+-+-----------------------+
| 192.168.0.0/16(d) |
+-------------------------+
|
+-+-----------------------+
| 192.168.129.0/24(d) |
+-------------------------+
> below >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+-+-----------------------+
| 192.168.0.0/16(d) |
+-+-----------------------+
| New
+-+-----------------------+
| 192.168.128.0/17(g) |
+-+-----------------------+
|
+-+-----------------------+
| 192.168.129.0/24(d) |
+-------------------------+
*/
dict.push("0.0.0.0", 0, "Data of 0.0.0.0/0");
dict.push("192.168.0.0", 16, "Data of 192.168.0.0/16");
dict.push("192.168.129.0", 24, "Data of 192.168.129.0/24");
var node = dict.getRootNode();
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
dict.createGlueNodes(node, 17);
node = dict.getRootNode();
assertTheNode(node, 'Data of 0.0.0.0/0', 0, 16, ['192.168.0.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node, 'Data of 192.168.0.0/16', 16, 17, ['192.168.128.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.128.0')];
assertTheNode(node, undefined, 17, 24, ['192.168.129.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.129.0')];
assertTheNode(node, 'Data of 192.168.129.0/24', 24, undefined, []);
});
it('should create 1 glue node over the 2 data nodes', () => {
/*
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+---------------------------+
| |
+-+-----------------------+ +-+-----------------------+
| 192.168.128.0/24(d) | | 192.168.0.0/24(d) |
+-------------------------+ +-------------------------+
> below >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
| New
+-+-----------------------+
| 192.168.0.0/16(g) |
+-+-----------------------+
|
+---------------------------+
| |
+-+-----------------------+ +-+-----------------------+
| 192.168.128.0/24(d) | | 192.168.0.0/24(d) |
+-------------------------+ +-------------------------+
*/
dict.push("0.0.0.0", 0, "Data of 0.0.0.0/0");
dict.push("192.168.128.0", 24, "Data of 192.168.128.0/24");
dict.push("192.168.0.0", 24, "Data of 192.168.0.0/24");
var node = dict.getRootNode();
dict.createGlueNodes(node, 16);
assertTheNode(node, 'Data of 0.0.0.0/0', 0, 16, ['192.168.0.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node, undefined, 16, 24, ['192.168.128.0', '192.168.0.0']);
var node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.128.0')];
assertTheNode(node1, 'Data of 192.168.128.0/24', 24, undefined, []);
node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node1, 'Data of 192.168.0.0/24', 24, undefined, []);
});
it('should create 2 glue node that has 1 child data node', () => {
/*
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+---------------------------+
| |
+-+-----------------------+ +-+-----------------------+
| 192.168.128.0/24(d) | | 192.168.0.0/24(d) |
+-------------------------+ +-------------------------+
> below >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+---------------------------+
| |
+-+-----------------------+ +-+-----------------------+
| 192.168.128.0/17(g) | | 192.168.0.0/17(g) | <- New
+-------------------------+ +-------------------------+
| |
+-+-----------------------+ +-+-----------------------+
| 192.168.128.0/24(d) | | 192.168.0.0/24(d) |
+-------------------------+ +-------------------------+
*/
dict.push("0.0.0.0", 0, "Data of 0.0.0.0/0");
dict.push("192.168.128.0", 24, "Data of 192.168.128.0/24");
dict.push("192.168.0.0", 24, "Data of 192.168.0.0/24");
var node = dict.getRootNode();
dict.createGlueNodes(node, 17);
assertTheNode(node, 'Data of 0.0.0.0/0', 0, 17, ['192.168.128.0', '192.168.0.0']);
var node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.128.0')];
assertTheNode(node1, undefined, 17, 24, ['192.168.128.0']);
var node2 = node1[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.128.0')];
assertTheNode(node2, 'Data of 192.168.128.0/24', 24, undefined, []);
node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node1, undefined, 17, 24, ['192.168.0.0']);
node2 = node1[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node2, 'Data of 192.168.0.0/24', 24, undefined, []);
});
it('should create 2 glue node that has 2 child data node and has 1 child data node', () => {
/*
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+---------------------------+---------------------------+
| | |
+-+-----------------------+ +-+-----------------------+ +-+-----------------------+
| 192.168.172.0/24(d) | | 192.168.128.0/24(d) | | 192.168.0.0/24(d) |
+-------------------------+ +-------------------------+ +-------------------------+
> below >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+-------------------------------------------------------+
| New | New
+-+-----------------------+ +-+-----------------------+
| 192.168.128.0/17(g) | | 192.168.0.0/17(g) |
+-------------------------+ +-------------------------+
| |
+---------------------------+ |
| | |
+-+-----------------------+ +-+-----------------------+ +-+-----------------------+
| 192.168.172.0/24(d) | | 192.168.128.0/24(d) | | 192.168.0.0/24(d) |
+-------------------------+ +-------------------------+ +-------------------------+
*/
dict.push("0.0.0.0", 0, "Data of 0.0.0.0/0");
dict.push("192.168.172.0", 24, "Data of 192.168.172.0/24");
dict.push("192.168.128.0", 24, "Data of 192.168.128.0/24");
dict.push("192.168.0.0", 24, "Data of 192.168.0.0/24");
var node = dict.getRootNode();
dict.createGlueNodes(node, 17);
assertTheNode(node, 'Data of 0.0.0.0/0', 0, 17, ['192.168.128.0', '192.168.0.0']);
var node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.128.0')];
assertTheNode(node1, undefined, 17, 24, ['192.168.172.0', '192.168.128.0']);
var node2 = node1[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.172.0')];
assertTheNode(node2, 'Data of 192.168.172.0/24', 24, undefined, []);
node2 = node1[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.128.0')];
assertTheNode(node2, 'Data of 192.168.128.0/24', 24, undefined, []);
node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node1, undefined, 17, 24, ['192.168.0.0']);
node2 = node1[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node2, 'Data of 192.168.0.0/24', 24, undefined, []);
});
it('should create 2 glue node that has 1 child data node and has 2 child data node', () => {
/*
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+---------------------------+---------------------------+
| | |
+-+-----------------------+ +-+-----------------------+ +-+-----------------------+
| 192.168.128.0/24(d) | | 192.168.64.0/24(d) | | 192.168.0.0/24(d) |
+-------------------------+ +-------------------------+ +-------------------------+
> below >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+---------------------------+
| New | New
+-+-----------------------+ +-+-----------------------+
| 192.168.128.0/17(g) | | 192.168.0.0/17(g) |
+-------------------------+ +-+-----------------------+
| |
| +---------------------------+
| | |
+-+-----------------------+ +-+-----------------------+ +-+-----------------------+
| 192.168.128.0/24(d) | | 192.168.64.0/24(d) | | 192.168.0.0/24(d) |
+-------------------------+ +-------------------------+ +-------------------------+
*/
dict.push("0.0.0.0", 0, "Data of 0.0.0.0/0");
dict.push("192.168.128.0", 24, "Data of 192.168.128.0/24");
dict.push("192.168.64.0", 24, "Data of 192.168.64.0/24");
dict.push("192.168.0.0", 24, "Data of 192.168.0.0/24");
var node = dict.getRootNode();
dict.createGlueNodes(node, 17);
assertTheNode(node, 'Data of 0.0.0.0/0', 0, 17, ['192.168.128.0', '192.168.0.0']);
var node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.128.0')];
assertTheNode(node1, undefined, 17, 24, ['192.168.128.0']);
var node2 = node1[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.128.0')];
assertTheNode(node2, 'Data of 192.168.128.0/24', 24, undefined, []);
node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node1, undefined, 17, 24, ['192.168.0.0', '192.168.64.0']);
node2 = node1[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node2, 'Data of 192.168.0.0/24', 24, undefined, []);
node2 = node1[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.64.0')];
assertTheNode(node2, 'Data of 192.168.64.0/24', 24, undefined, []);
});
it('should create 2 glue node that has 2 child data node and has 2 child data node', () => {
/*
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+---------------------------+---------------------------+---------------------------+
| | | |
+-+-----------------------+ +-+-----------------------+ +-+-----------------------+ +-+-----------------------+
| 192.168.172.0/24(d) | | 192.168.128.0/24(d) | | 192.168.64.0/24(d) | | 192.168.0.0/24(d) |
+-------------------------+ +-------------------------+ +-------------------------+ +-------------------------+
> below >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+---------------------------+---------------------------+
| New | New
+-+-----------------------+ +-+-----------------------+
| 192.168.128.0/17(g) | | 192.168.0.0/17(g) |
+-+-----------------------+ +-+-----------------------+
| |
+---------------------------+ +---------------------------+
| | | |
+-+-----------------------+ +-+-----------------------+ +-+-----------------------+ +-+-----------------------+
| 192.168.172.0/24(d) | | 192.168.128.0/24(d) | | 192.168.64.0/24(d) | | 192.168.0.0/24(d) |
+-------------------------+ +-------------------------+ +-------------------------+ +-------------------------+
*/
dict.push("0.0.0.0", 0, "Data of 0.0.0.0/0");
dict.push("192.168.172.0", 24, "Data of 192.168.172.0/24");
dict.push("192.168.128.0", 24, "Data of 192.168.128.0/24");
dict.push("192.168.64.0", 24, "Data of 192.168.64.0/24");
dict.push("192.168.0.0", 24, "Data of 192.168.0.0/24");
var node = dict.getRootNode();
dict.createGlueNodes(node, 17);
assertTheNode(node, 'Data of 0.0.0.0/0', 0, 17, ['192.168.128.0', '192.168.0.0']);
var node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.128.0')];
assertTheNode(node1, undefined, 17, 24, ['192.168.172.0', '192.168.128.0']);
var node2 = node1[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.172.0')];
assertTheNode(node2, 'Data of 192.168.172.0/24', 24, undefined, []);
node2 = node1[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.128.0')];
assertTheNode(node2, 'Data of 192.168.128.0/24', 24, undefined, []);
node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node1, undefined, 17, 24, ['192.168.0.0', '192.168.64.0']);
node2 = node1[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node2, 'Data of 192.168.0.0/24', 24, undefined, []);
node2 = node1[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.64.0')];
assertTheNode(node2, 'Data of 192.168.64.0/24', 24, undefined, []);
});
});
describe('#push', () => {
/*
(g): glue node
(d): data node
*/
it('should be empty node when no data has been pushed', () => {
/* (Structure of tree)
+-------------------------+
| 0.0.0.0/0(g) |
+-------------------------+
*/
var node = dict.getRootNode();
should.equal(node[I_IPV4_DATA], undefined);
node[I_IPV4_LENGTH_OF_SUBNETMASK].should.equal(0);
should.equal(node[I_IPV4_LENGTH_OF_CHILD_SUBNETMASK], undefined);
Object.keys(node[I_IPV4_REF_CHILD_NODE]).length.should.equal(0);
});
it('should not be able to push undefined data', () => {
(() => {dict.push("0.0.0.0", 0, undefined)}).should.throw(TypeError, "Cannot push undefined as a data to the tree");
});
it('should be able to push a root node 0.0.0.0/0', () => {
/*
+-------------------------+
| 0.0.0.0/0(d) |
+-------------------------+
*/
dict.push("0.0.0.0", 0, "Data of 0.0.0.0/0");
var node = dict.getRootNode();
assertTheNode(node, 'Data of 0.0.0.0/0', 0, undefined, []);
});
it('should be able to push a single node 128.0.0.0/1', () => {
/*
+-------------------------+
| 0.0.0.0/0(g) |
+-+-----------------------+
|
+-+-----------------------+
| 1)128.0.0.0/1(d) |
+-------------------------+
*/
dict.push("128.0.0.0", 1, "Data of 128.0.0.0/1");
var node = dict.getRootNode();
assertTheNode(node, undefined, 0, 1, ['128.0.0.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('128.0.0.0')];
assertTheNode(node, 'Data of 128.0.0.0/1', 1, undefined, []);
});
it('should be able to push nodes 128.0.0.0/1, 0.0.0.0/1', () => {
/*
+-------------------------+
| 0.0.0.0/0(g) |
+-+-----------------------+
|
+---------------------------+
| |
+-+-----------------------+ +-+-----------------------+
| 1)128.0.0.0/1(d) | | 2)0.0.0.0/1(d) |
+-------------------------+ +-------------------------+
*/
dict.push("128.0.0.0", 1, "Data of 128.0.0.0/1");
dict.push("0.0.0.0", 1, "Data of 0.0.0.0/1");
var node = dict.getRootNode();
assertTheNode(node, undefined, 0, 1, ['128.0.0.0', '0.0.0.0']);
var node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('128.0.0.0')];
assertTheNode(node1, 'Data of 128.0.0.0/1', 1, undefined, []);
node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('0.0.0.0')];
assertTheNode(node1, 'Data of 0.0.0.0/1', 1, undefined, []);
});
it('should be able to push a node 255.255.255.255/32', () => {
/*
+-------------------------+
| 0.0.0.0/0(g) |
+-+-----------------------+
|
+-+-----------------------+
| 1)255.255.255.255/32(d) |
+-------------------------+
*/
dict.push("255.255.255.255", 32, "Data of 255.255.255.255/32");
var node = dict.getRootNode();
assertTheNode(node, undefined, 0, 32, ['255.255.255.255']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('255.255.255.255')];
assertTheNode(node, 'Data of 255.255.255.255/32', 32, undefined, []);
});
it('should be able to push nodes 255.255.255.255/32, 255.255.255.254/32', () => {
/*
+-------------------------+
| 0.0.0.0/0(g) |
+-+-----------------------+
|
+---------------------------+
| |
+-+-----------------------+ +-+-----------------------+
| 1)255.255.255.255/32(d) | | 2)255.255.255.254/32(d) |
+-------------------------+ +-------------------------+
*/
dict.push("255.255.255.255", 32, "Data of 255.255.255.255/32");
dict.push("255.255.255.254", 32, "Data of 255.255.255.254/32");
var node = dict.getRootNode();
assertTheNode(node, undefined, 0, 32, ['255.255.255.255', '255.255.255.254']);
var node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('255.255.255.255')];
assertTheNode(node1, 'Data of 255.255.255.255/32', 32, undefined, []);
node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('255.255.255.254')];
assertTheNode(node1, 'Data of 255.255.255.254/32', 32, undefined, []);
});
it('should be able to push a node 192.168.1.0/24', () => {
/*
+-------------------------+
| 0.0.0.0/0(g) |
+-+-----------------------+
|
+-+-----------------------+
| 1)192.168.1.0/24(d) |
+-------------------------+
*/
dict.push("192.168.1.0", 24, "Data of 192.168.1.0/24");
var node = dict.getRootNode();
assertTheNode(node, undefined, 0, 24, ['192.168.1.0']);
node = node[I_IPV4_REF_CHILD_NODE][-1062731520]; // -1062731520 -> 192.168.0.1
assertTheNode(node, 'Data of 192.168.1.0/24', 24, undefined, []);
});
it('should be able to push nodes 192.168.1.0/24, 192.168.2.0/24', () => {
/*
+-------------------------+
| 0.0.0.0/0(g) |
+-+-----------------------+
|
+---------------------------+
| |
+-+-----------------------+ +-+-----------------------+
| 1) 192.168.1.0/24(d) | | 2) 192.168.2.0/24(d) |
+-------------------------+ +-------------------------+
*/
dict.push("0.0.0.0", 0, "Data of 0.0.0.0/0");
dict.push("192.168.1.0", 24, "Data of 192.168.1.0/24");
dict.push("192.168.2.0", 24, "Data of 192.168.2.0/24");
var node = dict.getRootNode();
assertTheNode(node, 'Data of 0.0.0.0/0', 0, 24, ['192.168.1.0', '192.168.2.0']);
var node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.1.0')];
assertTheNode(node1, 'Data of 192.168.1.0/24', 24, undefined, []);
node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.2.0')];
assertTheNode(node1, 'Data of 192.168.2.0/24', 24, undefined, []);
});
it('should be able to push nodes 192.168.0.0/16, 192.168.1.0/24', () => {
/*
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+-+-----------------------+
| 1) 192.168.0.0/16(d) |
+-------------------------+
|
+-+-----------------------+
| 2) 192.168.1.0/24(d) |
+-------------------------+
*/
dict.push("0.0.0.0", 0, "Data of 0.0.0.0/0");
dict.push("192.168.0.0", 16, "Data of 192.168.0.0/16");
dict.push("192.168.1.0", 24, "Data of 192.168.1.0/24");
var node1 = dict.getRootNode();
assertTheNode(node1, 'Data of 0.0.0.0/0', 0, 16, ['192.168.0.0']);
node1 = node1[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node1, 'Data of 192.168.0.0/16', 16, 24, ['192.168.1.0']);
node1 = node1[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.1.0')];
assertTheNode(node1, 'Data of 192.168.1.0/24', 24, undefined, []);
});
it('should be able to push nodes 192.168.0.0/16, 192.168.1.0/24, 192.168.2.0/24', () => {
/*
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+-+-----------------------+
| 1) 192.168.0.0/16(d) |
+-------------------------+
|
+---------------------------+
| |
+-+-----------------------+ +-+-----------------------+
| 2) 192.168.1.0/24(d) | | 3) 192.168.2.0/24(d) |
+-------------------------+ +-------------------------+
*/
dict.push("0.0.0.0", 0, "Data of 0.0.0.0/0");
dict.push("192.168.0.0", 16, "Data of 192.168.0.0/16");
dict.push("192.168.1.0", 24, "Data of 192.168.1.0/24");
dict.push("192.168.2.0", 24, "Data of 192.168.2.0/24");
var node = dict.getRootNode();
assertTheNode(node, 'Data of 0.0.0.0/0', 0, 16, ['192.168.0.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node, 'Data of 192.168.0.0/16', 16, 24, ['192.168.1.0', '192.168.2.0']);
var node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.1.0')];
assertTheNode(node1, 'Data of 192.168.1.0/24', 24, undefined, []);
node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.2.0')];
assertTheNode(node1, 'Data of 192.168.2.0/24', 24, undefined, []);
});
it('should be able to push nodes 192.168.0.0/16, 192.168.1.0/24, 172.16.0.0/16', () => {
/*
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+---------------------------+
| |
+-+-----------------------+ +-+-----------------------+
| 1) 192.168.0.0/16(d) | | 3) 172.16.0.0/16(d) |
+-------------------------+ +-------------------------+
|
+-+-----------------------+
| 2) 192.168.1.0/24(d) |
+-------------------------+
*/
dict.push("0.0.0.0", 0, "Data of 0.0.0.0/0");
dict.push("192.168.0.0", 16, "Data of 192.168.0.0/16");
dict.push("192.168.1.0", 24, "Data of 192.168.1.0/24");
dict.push("172.16.0.0", 16, "Data of 172.16.0.0/16");
var node = dict.getRootNode();
assertTheNode(node, 'Data of 0.0.0.0/0', 0, 16, ['192.168.0.0', '172.16.0.0']);
var node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node1, 'Data of 192.168.0.0/16', 16, 24, ['192.168.1.0']);
node1 = node1[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.1.0')];
assertTheNode(node1, 'Data of 192.168.1.0/24', 24, undefined, []);
node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('172.16.0.0')];
assertTheNode(node1, 'Data of 172.16.0.0/16', 16, undefined, []);
});
it('should be able to push nodes 192.168.1.0/24, 192.168.0.0/16', () => {
/*
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+-+-----------------------+
| 2) 192.168.0.0/16(d) |
+-------------------------+
|
+-+-----------------------+
| 1) 192.168.1.0/24(d) |
+-------------------------+
*/
dict.push("0.0.0.0", 0, "Data of 0.0.0.0/0");
dict.push("192.168.1.0", 24, "Data of 192.168.1.0/24");
dict.push("192.168.0.0", 16, "Data of 192.168.0.0/16");
var node = dict.getRootNode();
assertTheNode(node, 'Data of 0.0.0.0/0', 0, 16, ['192.168.0.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node, 'Data of 192.168.0.0/16', 16, 24, ['192.168.1.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.1.0')];
assertTheNode(node, 'Data of 192.168.1.0/24', 24, undefined, []);
});
it('should be able to push nodes 192.168.1.0/24, 192.168.0.0/16, 192.168.2.0/24', () => {
/*
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+-+-----------------------+
| 2) 192.168.0.0/16(d) |
+-------------------------+
|
+---------------------------+
| |
+-+-----------------------+ +-+-----------------------+
| 1) 192.168.1.0/24(d) | | 3) 192.168.2.0/24(d) |
+-------------------------+ +-------------------------+
*/
dict.push("0.0.0.0", 0, "Data of 0.0.0.0/0");
dict.push("192.168.1.0", 24, "Data of 192.168.1.0/24");
dict.push("192.168.2.0", 24, "Data of 192.168.2.0/24");
dict.push("192.168.0.0", 16, "Data of 192.168.0.0/16");
var node = dict.getRootNode();
assertTheNode(node, 'Data of 0.0.0.0/0', 0, 16, ['192.168.0.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node, 'Data of 192.168.0.0/16', 16, 24, ['192.168.1.0', '192.168.2.0']);
var node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.1.0')];
assertTheNode(node1, 'Data of 192.168.1.0/24', 24, undefined, []);
node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.2.0')];
assertTheNode(node1, 'Data of 192.168.2.0/24', 24, undefined, []);
});
it('should be able to push nodes 192.168.1.0/24, 192.168.0.0/16, 172.16.0.0/16', () => {
/*
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+---------------------------+
| |
+-+-----------------------+ +-+-----------------------+
| 2) 192.168.0.0/16(d) | | 3) 172.16.0.0/16(d) |
+-------------------------+ +-------------------------+
|
+-+-----------------------+
| 1) 192.168.1.0/24(d) |
+-------------------------+
*/
dict.push("0.0.0.0", 0, "Data of 0.0.0.0/0");
dict.push("192.168.1.0", 24, "Data of 192.168.1.0/24");
dict.push("192.168.0.0", 16, "Data of 192.168.0.0/16");
dict.push("172.16.0.0", 16, "Data of 172.16.0.0/16");
var node = dict.getRootNode();
assertTheNode(node, 'Data of 0.0.0.0/0', 0, 16, ['192.168.0.0', '172.16.0.0']);
var node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node1, 'Data of 192.168.0.0/16', 16, 24, ['192.168.1.0']);
node1 = node1[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.1.0')];
assertTheNode(node1, 'Data of 192.168.1.0/24', 24, undefined, []);
node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('172.16.0.0')];
assertTheNode(node1, 'Data of 172.16.0.0/16', 16, undefined, []);
});
it('should be able to push nodes 192.168.1.0/24, 192.168.0.0/16, 192.0.0.0/8', () => {
/*
+-------------------------+
| 0.0.0.0/0(d) |
+-+-----------------------+
|
+-+-----------------------+
| 3) 192.0.0.0/8(d) |
+-------------------------+
|
+-+-----------------------+
| 2) 192.168.0.0/16(d) |
+-------------------------+
|
+-+-----------------------+
| 1) 192.168.1.0/24(d) |
+-------------------------+
*/
dict.push("0.0.0.0", 0, "Data of 0.0.0.0/0");
dict.push("192.168.1.0", 24, "Data of 192.168.1.0/24");
dict.push("192.168.0.0", 16, "Data of 192.168.0.0/16");
dict.push("192.0.0.0", 8, "Data of 192.0.0.0/8");
var node = dict.getRootNode();
assertTheNode(node, "Data of 0.0.0.0/0", 0, 8, ['192.0.0.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.0.0.0')];
assertTheNode(node, "Data of 192.0.0.0/8", 8, 16, ['192.168.0.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node, "Data of 192.168.0.0/16", 16, 24, ['192.168.1.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.1.0')];
assertTheNode(node, "Data of 192.168.1.0/24", 24, undefined, []);
});
it('should be able to push nodes 192.168.1.0/24, 192.0.0.0/8, 192.168.0.0/16', () => {
/*
+-------------------------+
| 0.0.0.0/0(g) |
+-+-----------------------+
|
+-+-----------------------+
| 2) 192.0.0.0/8(d) |
+-------------------------+
|
+-+-----------------------+
| 3) 192.168.0.0/16(d) |
+-------------------------+
|
+-+-----------------------+
| 1) 192.168.1.0/24(d) |
+-------------------------+
*/
dict.push("0.0.0.0", 0, "Data of 0.0.0.0/0");
dict.push("192.168.1.0", 24, "Data of 192.168.1.0/24");
dict.push("192.0.0.0", 8, "Data of 192.0.0.0/8");
dict.push("192.168.0.0", 16, "Data of 192.168.0.0/16");
var node = dict.getRootNode();
assertTheNode(node, "Data of 0.0.0.0/0", 0, 8, ['192.0.0.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.0.0.0')];
assertTheNode(node, "Data of 192.0.0.0/8", 8, 16, ['192.168.0.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.0.0')];
assertTheNode(node, "Data of 192.168.0.0/16", 16, 24, ['192.168.1.0']);
node = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('192.168.1.0')];
assertTheNode(node, "Data of 192.168.1.0/24", 24, undefined, []);
});
it('should be able to push nodes 192.168.1.0/24, 172.16.0.0/16', () => {
/*
+-------------------------+
| 0.0.0.0/0(g) |
+-+-----------------------+
|
+---------------------------+
| Create a glue node |
+-+-----------------------+ +-+-----------------------+
| 192.168.0.0/16(g) | | 2) 172.16.0.0/16(d) |
+-------------------------+ +-------------------------+
|
+-+-----------------------+
| 1) 192.168.1.0/24(d) |
+-------------------------+
*/
dict.push("192.168.1.0", 24, "Data of 192.168.1.0/24");
dict.push("172.16.0.0", 16, "Data of 172.16.0.0/16");
var node = dict.getRootNode();
assertTheNode(node, undefined, 0, 16, ['192.168.0.0', '172.16.0.0']);
var node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4StringToBinary('172.16.0.0')];
assertTheNode(node1, 'Data of 172.16.0.0/16', 16, undefined, []);
node1 = node[I_IPV4_REF_CHILD_NODE][dict.iPv4