diff --git a/src/lib/components/tools/PXEProfileBuilder.svelte b/src/lib/components/tools/PXEProfileBuilder.svelte new file mode 100644 index 0000000..71cc258 --- /dev/null +++ b/src/lib/components/tools/PXEProfileBuilder.svelte @@ -0,0 +1,714 @@ + + + + ex.label} + getDescription={(ex) => ex.description} + selectedIndex={selectedExampleIndex} + /> + +
+
+

Profile Configuration

+
+
+
+ + +
+ +
+ + + Hostname or IP address of the TFTP server +
+ +
+ + + + Auto-detect uses Option 93 to serve different bootfiles based on client firmware + +
+
+
+ +
+
+

Bootfiles (Option 67)

+

+ Configure bootfile names for different client architectures. At least one bootfile is required. +

+
+
+ {#if profile.architecture === 'bios' || profile.architecture === 'auto'} +
+ + + For legacy BIOS systems (Arch Type 0x0000) +
+ {/if} + + {#if profile.architecture === 'uefi-x64' || profile.architecture === 'auto'} +
+ + + For UEFI x86-64 systems (Arch Type 0x0007) +
+ {/if} + + {#if profile.architecture === 'uefi-x86' || profile.architecture === 'auto'} +
+ + + For UEFI IA32 systems (Arch Type 0x0006) +
+ {/if} + + {#if profile.architecture === 'uefi-arm64' || profile.architecture === 'auto'} +
+ + + For UEFI ARM 64-bit systems (Arch Type 0x000b) +
+ {/if} + + {#if profile.architecture === 'uefi-arm32' || profile.architecture === 'auto'} +
+ + + For UEFI ARM 32-bit systems (Arch Type 0x000a) +
+ {/if} +
+
+ + {#if validationErrors.length > 0} +
+

Validation Errors

+ {#each validationErrors as error, i (i)} +
+ + {error} +
+ {/each} +
+ {/if} + + {#if result} +
+ +
+
+

Network Settings (Optional)

+

Customize network values for configuration examples below

+
+
+
+
+ + +
+ +
+ + +
+
+ +
+
+ + +
+ +
+ + +
+
+ +
+
+ + +
+ +
+ + +
+
+
+ + {#if networkValidationErrors.length > 0} +
+

Network Settings Errors

+ {#each networkValidationErrors as error, i (i)} +
+ + {error} +
+ {/each} +
+ {/if} +
+ {/if} + + {#if result && networkValidationErrors.length === 0} +
+

Profile Summary

+
+
Profile Name: {result.profile.name}
+
Architecture Mode: {result.profile.architecture}
+
TFTP Server: {result.profile.tftpServer}
+ {#if result.profile.biosBootfile} +
BIOS Bootfile: {result.profile.biosBootfile}
+ {/if} + {#if result.profile.uefiX64Bootfile} +
UEFI x64 Bootfile: {result.profile.uefiX64Bootfile}
+ {/if} + {#if result.profile.uefiX86Bootfile} +
UEFI x86 Bootfile: {result.profile.uefiX86Bootfile}
+ {/if} + {#if result.profile.uefiArm64Bootfile} +
UEFI ARM64 Bootfile: {result.profile.uefiArm64Bootfile}
+ {/if} + {#if result.profile.uefiArm32Bootfile} +
UEFI ARM32 Bootfile: {result.profile.uefiArm32Bootfile}
+ {/if} +
+
+ +
+

DHCP Server Configuration

+ + {#if result.examples.iscDhcpd} +
+
+

ISC dhcpd Configuration

+ +
+
{result.examples.iscDhcpd}
+
+ {/if} + + {#if result.examples.keaDhcp4} +
+
+

Kea DHCPv4 Configuration

+ +
+
{result.examples.keaDhcp4}
+
+ {/if} +
+ +
+

PXE Boot Architecture Detection

+

+ DHCP Option 93 (Client System Architecture Type) allows the DHCP server to detect the client's firmware type and + serve the appropriate bootfile. Common architecture types: +

+
    +
  • 0x0000 - Intel x86PC (Legacy BIOS)
  • +
  • 0x0006 - EFI IA32 (32-bit UEFI)
  • +
  • 0x0007 - EFI BC (64-bit UEFI, most common)
  • +
  • 0x000a - EFI ARM 32-bit
  • +
  • 0x000b - EFI ARM 64-bit
  • +
+

+ When using auto-detect mode, the DHCP server will examine Option 93 in the client's DHCPDISCOVER message and + respond with the appropriate bootfile for that architecture. +

+
+ {/if} +
+ + diff --git a/src/lib/constants/icon-map.ts b/src/lib/constants/icon-map.ts index 45abdee..d00be5f 100644 --- a/src/lib/constants/icon-map.ts +++ b/src/lib/constants/icon-map.ts @@ -311,6 +311,8 @@ export const iconMap: Record = { tools: ``, 'code-file': ``, compass: ``, + 'hard-drive': ``, + cpu: ``, // 'mail-check': ``, // 'eye': ``, // 'star': ``, diff --git a/src/lib/constants/nav.ts b/src/lib/constants/nav.ts index 4f70842..584ce3e 100644 --- a/src/lib/constants/nav.ts +++ b/src/lib/constants/nav.ts @@ -1013,6 +1013,42 @@ export const SUB_NAV: Record = { 'configuration', ], }, + { + href: makePath('/dhcp/options/pxe-profile'), + label: 'PXE Profile Generator', + description: + 'Generate PXE boot profiles with automatic UEFI/BIOS detection using Options 93/94 for architecture-aware bootfile selection', + icon: 'tools', + keywords: [ + 'dhcp', + 'pxe', + 'profile', + 'boot', + 'uefi', + 'bios', + 'option', + '93', + '94', + '66', + '67', + 'architecture', + 'detection', + 'bootfile', + 'tftp', + 'network', + 'generator', + 'isc', + 'dhcpd', + 'kea', + 'configuration', + 'preset', + 'ipxe', + 'wds', + 'windows', + 'deployment', + 'firmware', + ], + }, ], '/dns': [ { diff --git a/src/lib/utils/dhcp-pxe-profile.ts b/src/lib/utils/dhcp-pxe-profile.ts new file mode 100644 index 0000000..691ea12 --- /dev/null +++ b/src/lib/utils/dhcp-pxe-profile.ts @@ -0,0 +1,451 @@ +/** + * DHCP PXE Profile Generator + * Generates PXE boot profiles with UEFI/BIOS detection using Options 93/94 + * - Option 93: Client System Architecture Type + * - Option 94: Client Network Interface Identifier + * - Option 66: TFTP Server Name + * - Option 67: Bootfile Name + * Utilities for generating conditional PXE configurations + */ + +export type ArchitectureType = 'bios' | 'uefi-x64' | 'uefi-x86' | 'uefi-arm64' | 'uefi-arm32' | 'auto'; + +export interface PXEProfile { + name: string; + tftpServer?: string; // Option 66 + architecture: ArchitectureType; + biosBootfile?: string; // Bootfile for BIOS (Option 67) + uefiX64Bootfile?: string; // Bootfile for UEFI x64 + uefiX86Bootfile?: string; // Bootfile for UEFI x86 + uefiArm64Bootfile?: string; // Bootfile for UEFI ARM64 + uefiArm32Bootfile?: string; // Bootfile for UEFI ARM32 + network?: { + subnet?: string; + netmask?: string; + rangeStart?: string; + rangeEnd?: string; + gateway?: string; + dns?: string; + }; +} + +export interface PXEResult { + profile: PXEProfile; + examples: { + iscDhcpd?: string; + keaDhcp4?: string; + }; +} + +/** + * Architecture type codes from RFC 4578 + */ +export const ARCH_CODES = { + BIOS: 0x0000, // Intel x86PC + UEFI_X64: 0x0007, // EFI BC (x86-64) + UEFI_X86: 0x0006, // EFI IA32 + UEFI_ARM64: 0x000b, // EFI ARM 64-bit + UEFI_ARM32: 0x000a, // EFI ARM 32-bit +} as const; + +/** + * Preset PXE profiles + */ +export const PXE_PRESETS: PXEProfile[] = [ + { + name: 'Standard PXE (BIOS only)', + architecture: 'bios', + tftpServer: 'pxe.example.com', + biosBootfile: 'pxelinux.0', + }, + { + name: 'UEFI x64 only', + architecture: 'uefi-x64', + tftpServer: 'pxe.example.com', + uefiX64Bootfile: 'bootx64.efi', + }, + { + name: 'Auto-detect (BIOS + UEFI x64)', + architecture: 'auto', + tftpServer: 'pxe.example.com', + biosBootfile: 'pxelinux.0', + uefiX64Bootfile: 'bootx64.efi', + }, + { + name: 'Full Multi-Architecture', + architecture: 'auto', + tftpServer: 'pxe.example.com', + biosBootfile: 'pxelinux.0', + uefiX64Bootfile: 'bootx64.efi', + uefiX86Bootfile: 'bootia32.efi', + uefiArm64Bootfile: 'bootaa64.efi', + uefiArm32Bootfile: 'bootarm.efi', + }, + { + name: 'iPXE Chain Loading', + architecture: 'auto', + tftpServer: '192.168.1.10', + biosBootfile: 'undionly.kpxe', + uefiX64Bootfile: 'ipxe-x64.efi', + }, + { + name: 'Windows Deployment (WDS)', + architecture: 'auto', + tftpServer: 'wds.example.com', + biosBootfile: 'boot\\x86\\wdsnbp.com', + uefiX64Bootfile: 'boot\\x64\\wdsmgfw.efi', + }, +]; + +/** + * Validate PXE profile + */ +export function validatePXEProfile(profile: PXEProfile): string[] { + const errors: string[] = []; + + if (!profile.name?.trim()) { + errors.push('Profile name is required'); + } + + if (!profile.tftpServer?.trim()) { + errors.push('TFTP server is required'); + } + + // Check that at least one bootfile is configured + const hasBootfile = + profile.biosBootfile?.trim() || + profile.uefiX64Bootfile?.trim() || + profile.uefiX86Bootfile?.trim() || + profile.uefiArm64Bootfile?.trim() || + profile.uefiArm32Bootfile?.trim(); + + if (!hasBootfile) { + errors.push('At least one bootfile must be configured'); + } + + // Validate architecture-specific requirements + if (profile.architecture === 'bios' && !profile.biosBootfile?.trim()) { + errors.push('BIOS bootfile is required for BIOS architecture'); + } + + if (profile.architecture === 'uefi-x64' && !profile.uefiX64Bootfile?.trim()) { + errors.push('UEFI x64 bootfile is required for UEFI x64 architecture'); + } + + if (profile.architecture === 'uefi-x86' && !profile.uefiX86Bootfile?.trim()) { + errors.push('UEFI x86 bootfile is required for UEFI x86 architecture'); + } + + if (profile.architecture === 'uefi-arm64' && !profile.uefiArm64Bootfile?.trim()) { + errors.push('UEFI ARM64 bootfile is required for UEFI ARM64 architecture'); + } + + if (profile.architecture === 'uefi-arm32' && !profile.uefiArm32Bootfile?.trim()) { + errors.push('UEFI ARM32 bootfile is required for UEFI ARM32 architecture'); + } + + return errors; +} + +/** + * Validate network settings + */ +export function validateNetworkSettings(network?: PXEProfile['network']): string[] { + if (!network) return []; + + const errors: string[] = []; + + // Basic IPv4 validation + const ipv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/; + + if (network.subnet && !ipv4Regex.test(network.subnet)) { + errors.push('Invalid subnet format'); + } + + if (network.netmask && !ipv4Regex.test(network.netmask)) { + errors.push('Invalid netmask format'); + } + + if (network.rangeStart && !ipv4Regex.test(network.rangeStart)) { + errors.push('Invalid range start format'); + } + + if (network.rangeEnd && !ipv4Regex.test(network.rangeEnd)) { + errors.push('Invalid range end format'); + } + + if (network.gateway && !ipv4Regex.test(network.gateway)) { + errors.push('Invalid gateway format'); + } + + if (network.dns && !ipv4Regex.test(network.dns)) { + errors.push('Invalid DNS server format'); + } + + return errors; +} + +/** + * Generate ISC dhcpd configuration + */ +function generateIscDhcpdConfig(profile: PXEProfile): string { + const lines: string[] = []; + + // Use network settings or defaults + const subnet = profile.network?.subnet?.trim() || '192.168.1.0'; + const netmask = profile.network?.netmask?.trim() || '255.255.255.0'; + const rangeStart = profile.network?.rangeStart?.trim() || '192.168.1.100'; + const rangeEnd = profile.network?.rangeEnd?.trim() || '192.168.1.200'; + const gateway = profile.network?.gateway?.trim() || '192.168.1.1'; + const dns = profile.network?.dns?.trim() || '8.8.8.8'; + + lines.push(`# PXE Profile: ${profile.name}`); + lines.push('# ISC dhcpd configuration for PXE boot with architecture detection'); + lines.push(''); + lines.push('# Define option space for PXE'); + lines.push('option space PXE;'); + lines.push('option PXE.mtftp-ip code 1 = ip-address;'); + lines.push('option PXE.mtftp-cport code 2 = unsigned integer 16;'); + lines.push('option PXE.mtftp-sport code 3 = unsigned integer 16;'); + lines.push('option PXE.mtftp-tmout code 4 = unsigned integer 8;'); + lines.push('option PXE.mtftp-delay code 5 = unsigned integer 8;'); + lines.push('option arch code 93 = unsigned integer 16;'); + lines.push(''); + lines.push(`subnet ${subnet} netmask ${netmask} {`); + lines.push(` range ${rangeStart} ${rangeEnd};`); + lines.push(` option routers ${gateway};`); + lines.push(` option domain-name-servers ${dns};`); + lines.push(' '); + + if (profile.architecture === 'auto') { + lines.push(' # Auto-detect architecture and set bootfile accordingly'); + lines.push(' class "pxeclients" {'); + lines.push(' match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";'); + lines.push(` next-server ${profile.tftpServer};`); + lines.push(' '); + + const bootfiles: { arch: string; code: number; file?: string }[] = []; + if (profile.biosBootfile) { + bootfiles.push({ arch: 'BIOS (x86)', code: ARCH_CODES.BIOS, file: profile.biosBootfile }); + } + if (profile.uefiX64Bootfile) { + bootfiles.push({ + arch: 'UEFI x64', + code: ARCH_CODES.UEFI_X64, + file: profile.uefiX64Bootfile, + }); + } + if (profile.uefiX86Bootfile) { + bootfiles.push({ + arch: 'UEFI x86', + code: ARCH_CODES.UEFI_X86, + file: profile.uefiX86Bootfile, + }); + } + if (profile.uefiArm64Bootfile) { + bootfiles.push({ + arch: 'UEFI ARM64', + code: ARCH_CODES.UEFI_ARM64, + file: profile.uefiArm64Bootfile, + }); + } + if (profile.uefiArm32Bootfile) { + bootfiles.push({ + arch: 'UEFI ARM32', + code: ARCH_CODES.UEFI_ARM32, + file: profile.uefiArm32Bootfile, + }); + } + + bootfiles.forEach((bf, idx) => { + if (idx === 0) { + lines.push(` if option arch = ${bf.code.toString(16).padStart(2, '0')} {`); + } else { + lines.push(` } elsif option arch = ${bf.code.toString(16).padStart(2, '0')} {`); + } + lines.push(` # ${bf.arch}`); + lines.push(` filename "${bf.file}";`); + }); + + if (bootfiles.length > 0) { + lines.push(' } else {'); + lines.push(' # Default to BIOS if architecture not recognized'); + lines.push(` filename "${profile.biosBootfile || 'pxelinux.0'}";`); + lines.push(' }'); + } + + lines.push(' }'); + } else { + // Single architecture mode + lines.push(' class "pxeclients" {'); + lines.push(' match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";'); + lines.push(` next-server ${profile.tftpServer};`); + + let bootfile = ''; + if (profile.architecture === 'bios') bootfile = profile.biosBootfile || ''; + else if (profile.architecture === 'uefi-x64') bootfile = profile.uefiX64Bootfile || ''; + else if (profile.architecture === 'uefi-x86') bootfile = profile.uefiX86Bootfile || ''; + else if (profile.architecture === 'uefi-arm64') bootfile = profile.uefiArm64Bootfile || ''; + else if (profile.architecture === 'uefi-arm32') bootfile = profile.uefiArm32Bootfile || ''; + + lines.push(` filename "${bootfile}";`); + lines.push(' }'); + } + + lines.push('}'); + + return lines.join('\n'); +} + +/** + * Generate Kea DHCPv4 configuration + */ +function generateKeaDhcp4Config(profile: PXEProfile): string { + const lines: string[] = []; + + // Use network settings or defaults + const subnet = profile.network?.subnet?.trim() || '192.168.1.0'; + const netmask = profile.network?.netmask?.trim() || '255.255.255.0'; + const rangeStart = profile.network?.rangeStart?.trim() || '192.168.1.100'; + const rangeEnd = profile.network?.rangeEnd?.trim() || '192.168.1.200'; + const gateway = profile.network?.gateway?.trim() || '192.168.1.1'; + const dns = profile.network?.dns?.trim() || '8.8.8.8'; + + // Calculate CIDR notation from netmask + const cidrBits = + netmask + .split('.') + .map((octet) => parseInt(octet, 10).toString(2).padStart(8, '0')) + .join('') + .split('1').length - 1; + const cidr = `${subnet}/${cidrBits}`; + + lines.push(`// PXE Profile: ${profile.name}`); + lines.push('// Kea DHCPv4 configuration for PXE boot with architecture detection'); + lines.push('{'); + lines.push(' "Dhcp4": {'); + lines.push(' "client-classes": ['); + + if (profile.architecture === 'auto') { + const classes: { name: string; test: string; bootfile?: string }[] = []; + + if (profile.biosBootfile) { + classes.push({ + name: 'BIOS', + test: 'option[93].hex == 0x0000', + bootfile: profile.biosBootfile, + }); + } + if (profile.uefiX64Bootfile) { + classes.push({ + name: 'UEFI_X64', + test: 'option[93].hex == 0x0007', + bootfile: profile.uefiX64Bootfile, + }); + } + if (profile.uefiX86Bootfile) { + classes.push({ + name: 'UEFI_X86', + test: 'option[93].hex == 0x0006', + bootfile: profile.uefiX86Bootfile, + }); + } + if (profile.uefiArm64Bootfile) { + classes.push({ + name: 'UEFI_ARM64', + test: 'option[93].hex == 0x000b', + bootfile: profile.uefiArm64Bootfile, + }); + } + if (profile.uefiArm32Bootfile) { + classes.push({ + name: 'UEFI_ARM32', + test: 'option[93].hex == 0x000a', + bootfile: profile.uefiArm32Bootfile, + }); + } + + classes.forEach((cls, idx) => { + lines.push(' {'); + lines.push(` "name": "${cls.name}",`); + lines.push(` "test": "${cls.test}",`); + lines.push(` "boot-file-name": "${cls.bootfile}"`); + lines.push(idx < classes.length - 1 ? ' },' : ' }'); + }); + } + + lines.push(' ],'); + lines.push(' "subnet4": ['); + lines.push(' {'); + lines.push(` "subnet": "${cidr}",`); + lines.push(' "pools": ['); + lines.push(' {'); + lines.push(` "pool": "${rangeStart} - ${rangeEnd}"`); + lines.push(' }'); + lines.push(' ],'); + lines.push(` "next-server": "${profile.tftpServer}",`); + + if (profile.architecture !== 'auto') { + let bootfile = ''; + if (profile.architecture === 'bios') bootfile = profile.biosBootfile || ''; + else if (profile.architecture === 'uefi-x64') bootfile = profile.uefiX64Bootfile || ''; + else if (profile.architecture === 'uefi-x86') bootfile = profile.uefiX86Bootfile || ''; + else if (profile.architecture === 'uefi-arm64') bootfile = profile.uefiArm64Bootfile || ''; + else if (profile.architecture === 'uefi-arm32') bootfile = profile.uefiArm32Bootfile || ''; + + lines.push(` "boot-file-name": "${bootfile}",`); + } + + lines.push(' "option-data": ['); + lines.push(' {'); + lines.push(' "name": "routers",'); + lines.push(` "data": "${gateway}"`); + lines.push(' },'); + lines.push(' {'); + lines.push(' "name": "domain-name-servers",'); + lines.push(` "data": "${dns}"`); + lines.push(' }'); + lines.push(' ]'); + lines.push(' }'); + lines.push(' ]'); + lines.push(' }'); + lines.push('}'); + + return lines.join('\n'); +} + +/** + * Generate PXE profile configuration + */ +export function generatePXEProfile(profile: PXEProfile): PXEResult { + const profileErrors = validatePXEProfile(profile); + if (profileErrors.length > 0) { + throw new Error(profileErrors.join('; ')); + } + + const networkErrors = validateNetworkSettings(profile.network); + if (networkErrors.length > 0) { + throw new Error(networkErrors.join('; ')); + } + + return { + profile, + examples: { + iscDhcpd: generateIscDhcpdConfig(profile), + keaDhcp4: generateKeaDhcp4Config(profile), + }, + }; +} + +/** + * Get default PXE profile + */ +export function getDefaultPXEProfile(): PXEProfile { + return { + name: 'Standard PXE Profile', + architecture: 'auto', + tftpServer: 'pxe.example.com', + biosBootfile: 'pxelinux.0', + uefiX64Bootfile: 'bootx64.efi', + }; +} diff --git a/src/routes/dhcp/options/pxe-profile/+page.svelte b/src/routes/dhcp/options/pxe-profile/+page.svelte new file mode 100644 index 0000000..84f491e --- /dev/null +++ b/src/routes/dhcp/options/pxe-profile/+page.svelte @@ -0,0 +1,5 @@ + + + diff --git a/tests/unit/utils/dhcp-pxe-profile.test.ts b/tests/unit/utils/dhcp-pxe-profile.test.ts new file mode 100644 index 0000000..fea5469 --- /dev/null +++ b/tests/unit/utils/dhcp-pxe-profile.test.ts @@ -0,0 +1,451 @@ +import { describe, it, expect } from 'vitest'; +import { + type PXEProfile, + generatePXEProfile, + validatePXEProfile, + validateNetworkSettings, + PXE_PRESETS, + getDefaultPXEProfile, + ARCH_CODES, +} from '$lib/utils/dhcp-pxe-profile'; + +describe('PXE Profile Generator', () => { + describe('validatePXEProfile', () => { + it('should validate a correct profile', () => { + const profile: PXEProfile = { + name: 'Test Profile', + tftpServer: '192.168.1.10', + architecture: 'auto', + biosBootfile: 'pxelinux.0', + uefiX64Bootfile: 'bootx64.efi', + }; + + const errors = validatePXEProfile(profile); + expect(errors).toEqual([]); + }); + + it('should require profile name', () => { + const profile: PXEProfile = { + name: '', + tftpServer: '192.168.1.10', + architecture: 'bios', + biosBootfile: 'pxelinux.0', + }; + + const errors = validatePXEProfile(profile); + expect(errors).toContain('Profile name is required'); + }); + + it('should require TFTP server', () => { + const profile: PXEProfile = { + name: 'Test', + tftpServer: '', + architecture: 'bios', + biosBootfile: 'pxelinux.0', + }; + + const errors = validatePXEProfile(profile); + expect(errors).toContain('TFTP server is required'); + }); + + it('should require at least one bootfile', () => { + const profile: PXEProfile = { + name: 'Test', + tftpServer: '192.168.1.10', + architecture: 'auto', + }; + + const errors = validatePXEProfile(profile); + expect(errors).toContain('At least one bootfile must be configured'); + }); + + it('should validate BIOS architecture requires BIOS bootfile', () => { + const profile: PXEProfile = { + name: 'Test', + tftpServer: '192.168.1.10', + architecture: 'bios', + }; + + const errors = validatePXEProfile(profile); + expect(errors).toContain('BIOS bootfile is required for BIOS architecture'); + }); + + it('should validate UEFI x64 architecture requires UEFI x64 bootfile', () => { + const profile: PXEProfile = { + name: 'Test', + tftpServer: '192.168.1.10', + architecture: 'uefi-x64', + }; + + const errors = validatePXEProfile(profile); + expect(errors).toContain('UEFI x64 bootfile is required for UEFI x64 architecture'); + }); + + it('should validate UEFI x86 architecture requires UEFI x86 bootfile', () => { + const profile: PXEProfile = { + name: 'Test', + tftpServer: '192.168.1.10', + architecture: 'uefi-x86', + }; + + const errors = validatePXEProfile(profile); + expect(errors).toContain('UEFI x86 bootfile is required for UEFI x86 architecture'); + }); + + it('should validate UEFI ARM64 architecture requires UEFI ARM64 bootfile', () => { + const profile: PXEProfile = { + name: 'Test', + tftpServer: '192.168.1.10', + architecture: 'uefi-arm64', + }; + + const errors = validatePXEProfile(profile); + expect(errors).toContain('UEFI ARM64 bootfile is required for UEFI ARM64 architecture'); + }); + + it('should validate UEFI ARM32 architecture requires UEFI ARM32 bootfile', () => { + const profile: PXEProfile = { + name: 'Test', + tftpServer: '192.168.1.10', + architecture: 'uefi-arm32', + }; + + const errors = validatePXEProfile(profile); + expect(errors).toContain('UEFI ARM32 bootfile is required for UEFI ARM32 architecture'); + }); + }); + + describe('validateNetworkSettings', () => { + it('should accept undefined network settings', () => { + const errors = validateNetworkSettings(undefined); + expect(errors).toEqual([]); + }); + + it('should accept valid network settings', () => { + const network = { + subnet: '192.168.1.0', + netmask: '255.255.255.0', + rangeStart: '192.168.1.100', + rangeEnd: '192.168.1.200', + gateway: '192.168.1.1', + dns: '8.8.8.8', + }; + + const errors = validateNetworkSettings(network); + expect(errors).toEqual([]); + }); + + it('should validate subnet format', () => { + const network = { + subnet: 'invalid', + }; + + const errors = validateNetworkSettings(network); + expect(errors).toContain('Invalid subnet format'); + }); + + it('should validate netmask format', () => { + const network = { + netmask: 'invalid', + }; + + const errors = validateNetworkSettings(network); + expect(errors).toContain('Invalid netmask format'); + }); + + it('should validate range start format', () => { + const network = { + rangeStart: 'invalid', + }; + + const errors = validateNetworkSettings(network); + expect(errors).toContain('Invalid range start format'); + }); + + it('should validate range end format', () => { + const network = { + rangeEnd: 'invalid', + }; + + const errors = validateNetworkSettings(network); + expect(errors).toContain('Invalid range end format'); + }); + + it('should validate gateway format', () => { + const network = { + gateway: 'invalid', + }; + + const errors = validateNetworkSettings(network); + expect(errors).toContain('Invalid gateway format'); + }); + + it('should validate DNS server format', () => { + const network = { + dns: 'invalid', + }; + + const errors = validateNetworkSettings(network); + expect(errors).toContain('Invalid DNS server format'); + }); + }); + + describe('generatePXEProfile', () => { + it('should generate ISC dhcpd configuration for BIOS only', () => { + const profile: PXEProfile = { + name: 'BIOS Only', + tftpServer: 'pxe.example.com', + architecture: 'bios', + biosBootfile: 'pxelinux.0', + }; + + const result = generatePXEProfile(profile); + + expect(result.profile).toEqual(profile); + expect(result.examples.iscDhcpd).toBeDefined(); + expect(result.examples.iscDhcpd).toContain('next-server pxe.example.com'); + expect(result.examples.iscDhcpd).toContain('filename "pxelinux.0"'); + }); + + it('should generate ISC dhcpd configuration for UEFI only', () => { + const profile: PXEProfile = { + name: 'UEFI Only', + tftpServer: 'pxe.example.com', + architecture: 'uefi-x64', + uefiX64Bootfile: 'bootx64.efi', + }; + + const result = generatePXEProfile(profile); + + expect(result.profile).toEqual(profile); + expect(result.examples.iscDhcpd).toBeDefined(); + expect(result.examples.iscDhcpd).toContain('next-server pxe.example.com'); + expect(result.examples.iscDhcpd).toContain('filename "bootx64.efi"'); + }); + + it('should generate ISC dhcpd configuration with auto-detection', () => { + const profile: PXEProfile = { + name: 'Auto-detect', + tftpServer: 'pxe.example.com', + architecture: 'auto', + biosBootfile: 'pxelinux.0', + uefiX64Bootfile: 'bootx64.efi', + }; + + const result = generatePXEProfile(profile); + + expect(result.profile).toEqual(profile); + expect(result.examples.iscDhcpd).toBeDefined(); + expect(result.examples.iscDhcpd).toContain('option arch code 93'); + expect(result.examples.iscDhcpd).toContain('if option arch = 00 {'); + expect(result.examples.iscDhcpd).toContain('filename "pxelinux.0"'); + expect(result.examples.iscDhcpd).toContain('} elsif option arch = 07 {'); + expect(result.examples.iscDhcpd).toContain('filename "bootx64.efi"'); + }); + + it('should generate Kea DHCPv4 configuration for BIOS only', () => { + const profile: PXEProfile = { + name: 'BIOS Only', + tftpServer: 'pxe.example.com', + architecture: 'bios', + biosBootfile: 'pxelinux.0', + }; + + const result = generatePXEProfile(profile); + + expect(result.examples.keaDhcp4).toBeDefined(); + expect(result.examples.keaDhcp4).toContain('"next-server": "pxe.example.com"'); + expect(result.examples.keaDhcp4).toContain('"boot-file-name": "pxelinux.0"'); + }); + + it('should generate Kea DHCPv4 configuration with auto-detection', () => { + const profile: PXEProfile = { + name: 'Auto-detect', + tftpServer: 'pxe.example.com', + architecture: 'auto', + biosBootfile: 'pxelinux.0', + uefiX64Bootfile: 'bootx64.efi', + }; + + const result = generatePXEProfile(profile); + + expect(result.examples.keaDhcp4).toBeDefined(); + expect(result.examples.keaDhcp4).toContain('"client-classes"'); + expect(result.examples.keaDhcp4).toContain('"name": "BIOS"'); + expect(result.examples.keaDhcp4).toContain('"test": "option[93].hex == 0x0000"'); + expect(result.examples.keaDhcp4).toContain('"boot-file-name": "pxelinux.0"'); + expect(result.examples.keaDhcp4).toContain('"name": "UEFI_X64"'); + expect(result.examples.keaDhcp4).toContain('"test": "option[93].hex == 0x0007"'); + expect(result.examples.keaDhcp4).toContain('"boot-file-name": "bootx64.efi"'); + }); + + it('should use custom network settings', () => { + const profile: PXEProfile = { + name: 'Custom Network', + tftpServer: 'pxe.example.com', + architecture: 'bios', + biosBootfile: 'pxelinux.0', + network: { + subnet: '10.0.0.0', + netmask: '255.255.255.0', + rangeStart: '10.0.0.50', + rangeEnd: '10.0.0.100', + gateway: '10.0.0.1', + dns: '1.1.1.1', + }, + }; + + const result = generatePXEProfile(profile); + + expect(result.examples.iscDhcpd).toContain('subnet 10.0.0.0 netmask 255.255.255.0'); + expect(result.examples.iscDhcpd).toContain('range 10.0.0.50 10.0.0.100'); + expect(result.examples.iscDhcpd).toContain('option routers 10.0.0.1'); + expect(result.examples.iscDhcpd).toContain('option domain-name-servers 1.1.1.1'); + + expect(result.examples.keaDhcp4).toContain('"subnet": "10.0.0.0/24"'); + expect(result.examples.keaDhcp4).toContain('"pool": "10.0.0.50 - 10.0.0.100"'); + expect(result.examples.keaDhcp4).toContain('"data": "10.0.0.1"'); + expect(result.examples.keaDhcp4).toContain('"data": "1.1.1.1"'); + }); + + it('should support all architecture types in auto mode', () => { + const profile: PXEProfile = { + name: 'Full Multi-Architecture', + tftpServer: 'pxe.example.com', + architecture: 'auto', + biosBootfile: 'pxelinux.0', + uefiX64Bootfile: 'bootx64.efi', + uefiX86Bootfile: 'bootia32.efi', + uefiArm64Bootfile: 'bootaa64.efi', + uefiArm32Bootfile: 'bootarm.efi', + }; + + const result = generatePXEProfile(profile); + + // ISC dhcpd should have all architectures + expect(result.examples.iscDhcpd).toContain('filename "pxelinux.0"'); + expect(result.examples.iscDhcpd).toContain('filename "bootx64.efi"'); + expect(result.examples.iscDhcpd).toContain('filename "bootia32.efi"'); + expect(result.examples.iscDhcpd).toContain('filename "bootaa64.efi"'); + expect(result.examples.iscDhcpd).toContain('filename "bootarm.efi"'); + + // Kea should have all client classes + expect(result.examples.keaDhcp4).toContain('"name": "BIOS"'); + expect(result.examples.keaDhcp4).toContain('"name": "UEFI_X64"'); + expect(result.examples.keaDhcp4).toContain('"name": "UEFI_X86"'); + expect(result.examples.keaDhcp4).toContain('"name": "UEFI_ARM64"'); + expect(result.examples.keaDhcp4).toContain('"name": "UEFI_ARM32"'); + }); + + it('should throw error for invalid profile', () => { + const profile: PXEProfile = { + name: '', + tftpServer: '192.168.1.10', + architecture: 'bios', + }; + + expect(() => generatePXEProfile(profile)).toThrow(); + }); + + it('should throw error for invalid network settings', () => { + const profile: PXEProfile = { + name: 'Test', + tftpServer: '192.168.1.10', + architecture: 'bios', + biosBootfile: 'pxelinux.0', + network: { + subnet: 'invalid', + }, + }; + + expect(() => generatePXEProfile(profile)).toThrow(); + }); + }); + + describe('getDefaultPXEProfile', () => { + it('should return a valid default profile', () => { + const profile = getDefaultPXEProfile(); + + expect(profile.name).toBe('Standard PXE Profile'); + expect(profile.architecture).toBe('auto'); + expect(profile.tftpServer).toBe('pxe.example.com'); + expect(profile.biosBootfile).toBe('pxelinux.0'); + expect(profile.uefiX64Bootfile).toBe('bootx64.efi'); + + const errors = validatePXEProfile(profile); + expect(errors).toEqual([]); + }); + }); + + describe('PXE_PRESETS', () => { + it('should have valid presets', () => { + expect(PXE_PRESETS.length).toBeGreaterThan(0); + + for (const preset of PXE_PRESETS) { + expect(preset.name).toBeTruthy(); + expect(preset.tftpServer).toBeTruthy(); + expect(preset.architecture).toBeTruthy(); + + const errors = validatePXEProfile(preset); + expect(errors).toEqual([]); + } + }); + + it('should include Standard PXE (BIOS only)', () => { + const preset = PXE_PRESETS.find((p) => p.name === 'Standard PXE (BIOS only)'); + expect(preset).toBeDefined(); + expect(preset?.architecture).toBe('bios'); + expect(preset?.biosBootfile).toBe('pxelinux.0'); + }); + + it('should include UEFI x64 only', () => { + const preset = PXE_PRESETS.find((p) => p.name === 'UEFI x64 only'); + expect(preset).toBeDefined(); + expect(preset?.architecture).toBe('uefi-x64'); + expect(preset?.uefiX64Bootfile).toBe('bootx64.efi'); + }); + + it('should include Auto-detect preset', () => { + const preset = PXE_PRESETS.find((p) => p.name === 'Auto-detect (BIOS + UEFI x64)'); + expect(preset).toBeDefined(); + expect(preset?.architecture).toBe('auto'); + expect(preset?.biosBootfile).toBe('pxelinux.0'); + expect(preset?.uefiX64Bootfile).toBe('bootx64.efi'); + }); + + it('should include Full Multi-Architecture preset', () => { + const preset = PXE_PRESETS.find((p) => p.name === 'Full Multi-Architecture'); + expect(preset).toBeDefined(); + expect(preset?.architecture).toBe('auto'); + expect(preset?.biosBootfile).toBe('pxelinux.0'); + expect(preset?.uefiX64Bootfile).toBe('bootx64.efi'); + expect(preset?.uefiX86Bootfile).toBe('bootia32.efi'); + expect(preset?.uefiArm64Bootfile).toBe('bootaa64.efi'); + expect(preset?.uefiArm32Bootfile).toBe('bootarm.efi'); + }); + + it('should include iPXE Chain Loading preset', () => { + const preset = PXE_PRESETS.find((p) => p.name === 'iPXE Chain Loading'); + expect(preset).toBeDefined(); + expect(preset?.biosBootfile).toBe('undionly.kpxe'); + expect(preset?.uefiX64Bootfile).toBe('ipxe-x64.efi'); + }); + + it('should include Windows Deployment preset', () => { + const preset = PXE_PRESETS.find((p) => p.name === 'Windows Deployment (WDS)'); + expect(preset).toBeDefined(); + expect(preset?.biosBootfile).toContain('wdsnbp.com'); + expect(preset?.uefiX64Bootfile).toContain('wdsmgfw.efi'); + }); + }); + + describe('ARCH_CODES', () => { + it('should have correct architecture codes', () => { + expect(ARCH_CODES.BIOS).toBe(0x0000); + expect(ARCH_CODES.UEFI_X64).toBe(0x0007); + expect(ARCH_CODES.UEFI_X86).toBe(0x0006); + expect(ARCH_CODES.UEFI_ARM64).toBe(0x000b); + expect(ARCH_CODES.UEFI_ARM32).toBe(0x000a); + }); + }); +});