refactor network helper for e2e tests
This commit is contained in:
@@ -9,17 +9,20 @@ interface DHCPConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_SUBNET_MASK = '255.255.255.0';
|
const DEFAULT_SUBNET_MASK = '255.255.255.0';
|
||||||
|
const DEFAULT_SUBNET_MASK_OCTETS = DEFAULT_SUBNET_MASK.split('.').map(Number);
|
||||||
|
|
||||||
function checkIsIPv4(addr: NetworkInterfaceInfo): boolean {
|
function checkIsIPv4(addr: NetworkInterfaceInfo): boolean {
|
||||||
return addr.family === 'IPv4' && !addr.internal;
|
return addr.family === 'IPv4' && !addr.internal;
|
||||||
}
|
}
|
||||||
|
|
||||||
function calculateNetwork(ip: number[], mask: number[]): number[] {
|
function calculateNetwork(ip: number[], mask: number[]): number[] {
|
||||||
|
// Calculate the network address by applying the bitwise AND operation.
|
||||||
// eslint-disable-next-line no-bitwise
|
// eslint-disable-next-line no-bitwise
|
||||||
return ip.map((octet, i) => octet & mask[i]);
|
return ip.map((octet, i) => octet & mask[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function calculateBroadcast(network: number[], mask: number[]): number[] {
|
function calculateBroadcast(network: number[], mask: number[]): number[] {
|
||||||
|
// Calculate the broadcast address by ORing the network address with the inverted mask.
|
||||||
// eslint-disable-next-line no-bitwise
|
// eslint-disable-next-line no-bitwise
|
||||||
return network.map((octet, i) => octet | (~mask[i] & 255));
|
return network.map((octet, i) => octet | (~mask[i] & 255));
|
||||||
}
|
}
|
||||||
@@ -27,30 +30,28 @@ function calculateBroadcast(network: number[], mask: number[]): number[] {
|
|||||||
export function getDHCPConfig(): DHCPConfig {
|
export function getDHCPConfig(): DHCPConfig {
|
||||||
const interfaces = networkInterfaces();
|
const interfaces = networkInterfaces();
|
||||||
|
|
||||||
|
// Select the first interface that has a valid non-internal IPv4 address.
|
||||||
const ipV4Interface = Object.entries(interfaces)
|
const ipV4Interface = Object.entries(interfaces)
|
||||||
.map(([name, addresses]) => ({
|
.map(([name, addresses]) => ({ name, addresses }))
|
||||||
name,
|
.find((i) => i.addresses?.some(checkIsIPv4));
|
||||||
addresses,
|
|
||||||
}))
|
|
||||||
.find((i) => {
|
|
||||||
return i.addresses?.some((addr) => checkIsIPv4(addr));
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!ipV4Interface) {
|
if (!ipV4Interface) {
|
||||||
throw new Error('No suitable network interface found');
|
throw new Error('No suitable network interface found');
|
||||||
}
|
}
|
||||||
|
|
||||||
const ipv4Address = ipV4Interface.addresses.find((addr) => checkIsIPv4(addr));
|
// Get the first valid IPv4 address from the interface.
|
||||||
|
const ipv4Address = ipV4Interface.addresses.find(checkIsIPv4);
|
||||||
|
|
||||||
const ip = ipv4Address.address.split('.').map(Number);
|
const ip = ipv4Address.address.split('.').map(Number);
|
||||||
const mask = ipv4Address.netmask?.split('.').map(Number) || DEFAULT_SUBNET_MASK.split('.');
|
const mask = ipv4Address.netmask?.split('.').map(Number) || DEFAULT_SUBNET_MASK_OCTETS;
|
||||||
|
|
||||||
const network = calculateNetwork(ip, mask);
|
const network = calculateNetwork(ip, mask);
|
||||||
|
|
||||||
// Calculate first and last usable addresses (excluding network and broadcast)
|
// Calculate first usable address (network address + 1)
|
||||||
const rangeStart = [...network];
|
const rangeStart = [...network];
|
||||||
rangeStart[3] = network[3] + 1;
|
rangeStart[3] = network[3] + 1;
|
||||||
|
|
||||||
|
// Calculate broadcast address and then the last usable address (broadcast - 1)
|
||||||
const broadcast = calculateBroadcast(network, mask);
|
const broadcast = calculateBroadcast(network, mask);
|
||||||
const rangeEnd = [...broadcast];
|
const rangeEnd = [...broadcast];
|
||||||
rangeEnd[3] = broadcast[3] - 1;
|
rangeEnd[3] = broadcast[3] - 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user