1
0
Fork 0
forked from svrjs/svrjs
This repository has been archived on 2024-11-10. You can view files and clone it, but cannot push or open issues or pull requests.
svrjs/tests/utils/ipBlockList.test.js

105 lines
3.3 KiB
JavaScript

const ipBlockList = require("../../src/utils/ipBlockList.js");
describe("IP block list functionality", () => {
let blockList;
beforeEach(() => {
blockList = ipBlockList([]);
});
test("should add and check IPv4 address", () => {
blockList.add("192.168.1.1");
expect(blockList.check("192.168.1.1")).toBe(true);
expect(blockList.check("192.168.1.2")).toBe(false);
});
test("should add and check IPv6 address", () => {
blockList.add("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
expect(blockList.check("2001:0db8:85a3:0000:0000:8a2e:0370:7334")).toBe(
true,
);
expect(blockList.check("2001:0db8:85a3:0000:0000:8a2e:0370:7335")).toBe(
false,
);
});
test("should add and check IPv4 CIDR block", () => {
blockList.add("192.168.1.0/24");
expect(blockList.check("192.168.1.1")).toBe(true);
expect(blockList.check("192.168.1.255")).toBe(true);
expect(blockList.check("192.168.2.1")).toBe(false);
});
test("should add and check IPv6 CIDR block", () => {
blockList.add("2001:0db8:85a3::/64");
expect(blockList.check("2001:0db8:85a3:0000:0000:8a2e:0370:7334")).toBe(
true,
);
expect(blockList.check("2001:0db8:85a3:0000:0000:8a2e:0370:7335")).toBe(
true,
);
expect(blockList.check("2001:0db8:85a4:0000:0000:8a2e:0370:7334")).toBe(
false,
);
});
test("should remove IPv4 address", () => {
blockList.add("192.168.1.1");
expect(blockList.check("192.168.1.1")).toBe(true);
blockList.remove("192.168.1.1");
expect(blockList.check("192.168.1.1")).toBe(false);
});
test("should remove IPv6 address", () => {
blockList.add("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
expect(blockList.check("2001:0db8:85a3:0000:0000:8a2e:0370:7334")).toBe(
true,
);
blockList.remove("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
expect(blockList.check("2001:0db8:85a3:0000:0000:8a2e:0370:7334")).toBe(
false,
);
});
test("should remove IPv4 CIDR block", () => {
blockList.add("192.168.1.0/24");
expect(blockList.check("192.168.1.1")).toBe(true);
blockList.remove("192.168.1.0/24");
expect(blockList.check("192.168.1.1")).toBe(false);
});
test("should remove IPv6 CIDR block", () => {
blockList.add("2001:0db8:85a3::/64");
expect(blockList.check("2001:0db8:85a3:0000:0000:8a2e:0370:7334")).toBe(
true,
);
blockList.remove("2001:0db8:85a3::/64");
expect(blockList.check("2001:0db8:85a3:0000:0000:8a2e:0370:7334")).toBe(
false,
);
});
test("should handle edge cases for IPv4 addresses", () => {
blockList.add("192.168.1.0/30");
expect(blockList.check("192.168.1.1")).toBe(true);
expect(blockList.check("192.168.1.2")).toBe(true);
expect(blockList.check("192.168.1.3")).toBe(true);
expect(blockList.check("192.168.1.4")).toBe(false);
});
test("should handle edge cases for IPv6 addresses", () => {
blockList.add("2001:0db8:85a3::/64");
expect(blockList.check("2001:0db8:85a2:ffff:ffff:ffff:ffff:ffff")).toBe(
false,
);
expect(blockList.check("2001:0db8:85a3:0000:0000:0000:0000:0000")).toBe(
true,
);
expect(blockList.check("2001:0db8:85a3:0000:ffff:ffff:ffff:ffff")).toBe(
true,
);
expect(blockList.check("2001:0db8:85a3:0001:0000:0000:0000:0000")).toBe(
false,
);
});
});