Skip to content

Commit

Permalink
improved equality check on prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
massimocandela committed Jan 21, 2024
1 parent ba0310f commit 051e199
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ipUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,10 @@ const ip = {
const components1 = this.getIpAndCidr(prefix1);
const components2 = this.getIpAndCidr(prefix2);

return components1[1] === components2[1] && this._isEqualIP(components1[0], components2[0], af);
const ip1 = this._applyNetmask(this._expandIP(components1[0], af), components1[1], af);
const ip2 = this._applyNetmask(this._expandIP(components2[0], af), components2[1], af);

return components1[1] === components2[1] && ip1 === ip2;
},

getAddressFamily: function(ip) {
Expand Down
13 changes: 13 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,21 @@ describe("Tests", function() {
expect(ipUtils.isEqualPrefix("2001:db8:123::/48", "2001:db8:123::/48")).to.equal(true);
expect(ipUtils.isEqualPrefix("2001:db8:123::/48", "2001:db8:123::/49")).to.equal(false);
expect(ipUtils.isEqualPrefix("2001:db8::/48", "2001:db8:0000::/48")).to.equal(true);
expect(ipUtils.isEqualPrefix("2001:db8::/48", "2001:db9::/48")).to.equal(false);
expect(ipUtils.isEqualPrefix("2001:db8::/48", "2001:db8:0000::/48")).to.equal(true);
expect(ipUtils.isEqualPrefix("2001:db8::/48", "2001:db8:0001::/48")).to.equal(false);
expect(ipUtils.isEqualPrefix("2001:db8::/48", "2001:db8::/47")).to.equal(false);
expect(ipUtils.isEqualPrefix("2001:db8::/47", "2001:0db8:0001:ffff:ffff:ffff:ffff:ffff/47")).to.equal(true);
expect(ipUtils.isEqualPrefix("2001:db8::/47", "2001:0db8:0002:ffff:ffff:ffff:ffff:ffff/47")).to.equal(false);

expect(ipUtils.isEqualPrefix("127.0.0.0/8", "127.0.0.0/8")).to.equal(true);
expect(ipUtils.isEqualPrefix("127.0.0.0/8", "130.0.0.0/8")).to.equal(false);
expect(ipUtils.isEqualPrefix("127.1.1.1/24", "127.0.0.8/24")).to.equal(false);
expect(ipUtils.isEqualPrefix("127.1.1.1/24", "127.1.1.8/24")).to.equal(true);
expect(ipUtils.isEqualPrefix("127.1.1.1/24", "127.1.1.8/23")).to.equal(false);
expect(ipUtils.isEqualPrefix("127.1.1.1/22", "127.1.2.8/22")).to.equal(true);
expect(ipUtils.isEqualPrefix("127.1.1.1/22", "127.1.3.8/22")).to.equal(true);
expect(ipUtils.isEqualPrefix("127.1.1.1/22", "127.1.4.8/22")).to.equal(false);
expect(ipUtils._isEqualPrefix("127/8", "127.0.0.0/8")).to.equal(true);

expect(ipUtils._expandIP("127")).to.equal("127.0.0.0");
Expand Down

0 comments on commit 051e199

Please sign in to comment.