1
0
Fork 0
forked from svrjs/svrjs

Lint out the codebase

This commit is contained in:
Dorian Niemiec 2024-08-25 19:17:54 +02:00
parent 0ea53fdf96
commit 85debce9c9
3 changed files with 87 additions and 48 deletions

View file

@ -453,7 +453,7 @@ if (ips.length == 0) {
}
// Server IP address
var host = ips[(ips.length) - 1];
var host = ips[ips.length - 1];
if (!host) host = "[offline]";
// TODO: Public IP address-related

View file

@ -1,7 +1,9 @@
function calculateBroadcastIPv4FromCidr(ipWithCidr) {
// Check if CIDR notation is valid, if it's not, return null
if (!ipWithCidr) return null;
const ipCA = ipWithCidr.match(/^((?:(?:25[0-5]|(?:2[0-4]|1\d|[1-9]|)\d)\.?\b){4})\/([0-2][0-9]|3[0-2]|[0-9])$/);
const ipCA = ipWithCidr.match(
/^((?:(?:25[0-5]|(?:2[0-4]|1\d|[1-9]|)\d)\.?\b){4})\/([0-2][0-9]|3[0-2]|[0-9])$/,
);
if (!ipCA) return null;
// Extract IP and mask (numeric format)
@ -24,7 +26,9 @@ function calculateBroadcastIPv4FromCidr(ipWithCidr) {
function calculateNetworkIPv4FromCidr(ipWithCidr) {
// Check if CIDR notation is valid, if it's not, return null
if (!ipWithCidr) return null;
const ipCA = ipWithCidr.match(/^((?:(?:25[0-5]|(?:2[0-4]|1\d|[1-9]|)\d)\.?\b){4})\/([0-2][0-9]|3[0-2]|[0-9])$/);
const ipCA = ipWithCidr.match(
/^((?:(?:25[0-5]|(?:2[0-4]|1\d|[1-9]|)\d)\.?\b){4})\/([0-2][0-9]|3[0-2]|[0-9])$/,
);
if (!ipCA) return null;
// Extract IP and mask (numeric format)

View file

@ -1,49 +1,84 @@
const {
calculateBroadcastIPv4FromCidr,
calculateNetworkIPv4FromCidr,
} = require('../../src/utils/ipSubnetUtils');
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');
expect(calculateBroadcastIPv4FromCidr('192.168.1.0/25')).toBe('192.168.1.127');
expect(calculateBroadcastIPv4FromCidr('192.168.1.0/26')).toBe('192.168.1.63');
expect(calculateBroadcastIPv4FromCidr('192.168.1.0/27')).toBe('192.168.1.31');
expect(calculateBroadcastIPv4FromCidr('192.168.1.0/28')).toBe('192.168.1.15');
expect(calculateBroadcastIPv4FromCidr('192.168.1.0/29')).toBe('192.168.1.7');
expect(calculateBroadcastIPv4FromCidr('192.168.1.0/30')).toBe('192.168.1.3');
expect(calculateBroadcastIPv4FromCidr('192.168.1.0/31')).toBe('192.168.1.1');
expect(calculateBroadcastIPv4FromCidr('192.168.1.0/32')).toBe('192.168.1.0');
});
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);
});
calculateBroadcastIPv4FromCidr,
calculateNetworkIPv4FromCidr,
} = require("../../src/utils/ipSubnetUtils");
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",
);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/25")).toBe(
"192.168.1.127",
);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/26")).toBe(
"192.168.1.63",
);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/27")).toBe(
"192.168.1.31",
);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/28")).toBe(
"192.168.1.15",
);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/29")).toBe(
"192.168.1.7",
);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/30")).toBe(
"192.168.1.3",
);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/31")).toBe(
"192.168.1.1",
);
expect(calculateBroadcastIPv4FromCidr("192.168.1.0/32")).toBe(
"192.168.1.0",
);
});
describe('calculateNetworkIPv4FromCidr', () => {
test('should return the network address for a given CIDR', () => {
expect(calculateNetworkIPv4FromCidr('192.168.1.0/24')).toBe('192.168.1.0');
expect(calculateNetworkIPv4FromCidr('192.168.1.0/25')).toBe('192.168.1.0');
expect(calculateNetworkIPv4FromCidr('192.168.1.0/26')).toBe('192.168.1.0');
expect(calculateNetworkIPv4FromCidr('192.168.1.0/27')).toBe('192.168.1.0');
expect(calculateNetworkIPv4FromCidr('192.168.1.0/28')).toBe('192.168.1.0');
expect(calculateNetworkIPv4FromCidr('192.168.1.0/29')).toBe('192.168.1.0');
expect(calculateNetworkIPv4FromCidr('192.168.1.0/30')).toBe('192.168.1.0');
expect(calculateNetworkIPv4FromCidr('192.168.1.0/31')).toBe('192.168.1.0');
expect(calculateNetworkIPv4FromCidr('192.168.1.0/32')).toBe('192.168.1.0');
});
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);
});
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);
});
});
describe("calculateNetworkIPv4FromCidr", () => {
test("should return the network address for a given CIDR", () => {
expect(calculateNetworkIPv4FromCidr("192.168.1.0/24")).toBe(
"192.168.1.0",
);
expect(calculateNetworkIPv4FromCidr("192.168.1.0/25")).toBe(
"192.168.1.0",
);
expect(calculateNetworkIPv4FromCidr("192.168.1.0/26")).toBe(
"192.168.1.0",
);
expect(calculateNetworkIPv4FromCidr("192.168.1.0/27")).toBe(
"192.168.1.0",
);
expect(calculateNetworkIPv4FromCidr("192.168.1.0/28")).toBe(
"192.168.1.0",
);
expect(calculateNetworkIPv4FromCidr("192.168.1.0/29")).toBe(
"192.168.1.0",
);
expect(calculateNetworkIPv4FromCidr("192.168.1.0/30")).toBe(
"192.168.1.0",
);
expect(calculateNetworkIPv4FromCidr("192.168.1.0/31")).toBe(
"192.168.1.0",
);
expect(calculateNetworkIPv4FromCidr("192.168.1.0/32")).toBe(
"192.168.1.0",
);
});
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);
});
});
});