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/ipSubnetUtils.test.js

85 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

const {
2024-08-25 19:17:54 +02:00
calculateBroadcastIPv4FromCidr,
calculateNetworkIPv4FromCidr
} = require("../../src/utils/ipSubnetUtils.js");
2024-08-25 19:17:54 +02:00
describe("IPv4 subnet utilties", () => {
describe("calculateBroadcastIPv4FromCidr", () => {
test("should return the broadcast address for a given CIDR", () => {
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/24")).toBe(
"192.168.1.255"
2024-08-25 19:17:54 +02:00
);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/25")).toBe(
"192.168.1.127"
2024-08-25 19:17:54 +02:00
);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/26")).toBe(
"192.168.1.63"
2024-08-25 19:17:54 +02:00
);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/27")).toBe(
"192.168.1.31"
2024-08-25 19:17:54 +02:00
);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/28")).toBe(
"192.168.1.15"
2024-08-25 19:17:54 +02:00
);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/29")).toBe(
"192.168.1.7"
2024-08-25 19:17:54 +02:00
);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/30")).toBe(
"192.168.1.3"
2024-08-25 19:17:54 +02:00
);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/31")).toBe(
"192.168.1.1"
2024-08-25 19:17:54 +02:00
);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/32")).toBe(
"192.168.1.0"
2024-08-25 19:17:54 +02:00
);
});
2024-08-25 19:17:54 +02:00
test("should return null for invalid CIDR notation", () => {
expect(calculateBroadcastIPv4FromCidr(null)).toBe(null);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0")).toBe(null);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/")).toBe(null);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/abc")).toBe(null);
});
});
2024-08-25 19:17:54 +02:00
describe("calculateNetworkIPv4FromCidr", () => {
test("should return the network address for a given CIDR", () => {
expect(calculateNetworkIPv4FromCidr("192.168.1.0/24")).toBe(
"192.168.1.0"
2024-08-25 19:17:54 +02:00
);
expect(calculateNetworkIPv4FromCidr("192.168.1.0/25")).toBe(
"192.168.1.0"
2024-08-25 19:17:54 +02:00
);
expect(calculateNetworkIPv4FromCidr("192.168.1.0/26")).toBe(
"192.168.1.0"
2024-08-25 19:17:54 +02:00
);
expect(calculateNetworkIPv4FromCidr("192.168.1.0/27")).toBe(
"192.168.1.0"
2024-08-25 19:17:54 +02:00
);
expect(calculateNetworkIPv4FromCidr("192.168.1.0/28")).toBe(
"192.168.1.0"
2024-08-25 19:17:54 +02:00
);
expect(calculateNetworkIPv4FromCidr("192.168.1.0/29")).toBe(
"192.168.1.0"
2024-08-25 19:17:54 +02:00
);
expect(calculateNetworkIPv4FromCidr("192.168.1.0/30")).toBe(
"192.168.1.0"
2024-08-25 19:17:54 +02:00
);
expect(calculateNetworkIPv4FromCidr("192.168.1.0/31")).toBe(
"192.168.1.0"
2024-08-25 19:17:54 +02:00
);
expect(calculateNetworkIPv4FromCidr("192.168.1.0/32")).toBe(
"192.168.1.0"
2024-08-25 19:17:54 +02:00
);
});
test("should return null for invalid CIDR notation", () => {
expect(calculateNetworkIPv4FromCidr(null)).toBe(null);
expect(calculateNetworkIPv4FromCidr("192.168.1.0")).toBe(null);
expect(calculateNetworkIPv4FromCidr("192.168.1.0/")).toBe(null);
expect(calculateNetworkIPv4FromCidr("192.168.1.0/abc")).toBe(null);
});
});
});