diff --git a/src/lib/components/tools/DHCPOption150Builder.svelte b/src/lib/components/tools/DHCPOption150Builder.svelte new file mode 100644 index 0000000..4d868fa --- /dev/null +++ b/src/lib/components/tools/DHCPOption150Builder.svelte @@ -0,0 +1,1153 @@ + + + + {#if mode === 'encode'} + ex.label} + getDescription={(ex) => ex.description} + selectedIndex={selectedExampleIndex} + /> + {:else} + ex.label} + getDescription={(ex) => ex.description} + selectedIndex={selectedExampleIndex} + /> + {/if} + + {#if mode === 'encode'} +
+
+

Option 150: Cisco TFTP Server List

+

Multiple IPv4 addresses for redundant TFTP servers (Cisco IP phones)

+
+
+ {#if config.option150Servers && config.option150Servers.length > 0} + {#each config.option150Servers as _, i (`opt150-${i}`)} +
+
+ + +
+ +
+ {/each} + {/if} + + +
+
+ +
+
+

Option 66: TFTP Server Name (Standard)

+

Single hostname or IP address for standard PXE boot

+
+
+
+ + +
+
+
+ +
+
+

Option 67: Bootfile Name

+

Filename to boot from TFTP server (e.g., pxelinux.0 for BIOS, bootx64.efi for UEFI)

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

Validation Errors

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

Option 150: TFTP Server List

+ +
+
+

Hex-Encoded (Compact)

+ +
+
{result.option150.hexEncoded}
+
+ +
+
+

Wire Format (Spaced)

+ +
+
{result.option150.wireFormat}
+
+ +
+
Total Length: {result.option150.totalLength} bytes
+
Servers: {result.option150.servers.length}
+
+
+ {/if} + + {#if result.option66} +
+

Option 66: TFTP Server Name

+ +
+
+

Value

+ +
+
{result.option66.value}
+
+ +
+
+

Hex-Encoded

+ +
+
{result.option66.hexEncoded}
+
+ +
+
Total Length: {result.option66.totalLength} bytes
+
+
+ {/if} + + {#if result.option67} +
+

Option 67: Bootfile Name

+ +
+
+

Value

+ +
+
{result.option67.value}
+
+ +
+
+

Hex-Encoded

+ +
+
{result.option67.hexEncoded}
+
+ +
+
Total Length: {result.option67.totalLength} bytes
+
+
+ {/if} + {/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} +
+

Configuration Examples

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

ISC dhcpd Configuration

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

Kea DHCPv4 Configuration

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

Cisco IOS Configuration

+ +
+
{result.examples.ciscoIos}
+
+ {/if} +
+ {/if} + {:else} +
+
+

Decode TFTP Option

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

Validation Errors

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

Decoded Option 150: TFTP Server List

+ +
+
Total Length: {decodeResult150.totalLength} bytes
+
Servers Found: {decodeResult150.servers.length}
+
+ +
+

TFTP Servers

+ {#each decodeResult150.servers as server, i (i)} +
+ + Server {i + 1}: + {server} +
+ {/each} +
+
+ {/if} + + {#if decodeResult66 && validationErrors.length === 0} +
+

Decoded Option 66: TFTP Server Name

+ +
+
Total Length: {decodeResult66.totalLength} bytes
+
+ +
+

TFTP Server

+
+ + {decodeResult66.value} +
+
+
+ {/if} + + {#if decodeResult67 && validationErrors.length === 0} +
+

Decoded Option 67: Bootfile Name

+ +
+
Total Length: {decodeResult67.totalLength} bytes
+
+ +
+

Bootfile

+
+ + {decodeResult67.value} +
+
+
+ {/if} + {/if} +
+ + diff --git a/src/lib/constants/nav.ts b/src/lib/constants/nav.ts index e0396d9..4f70842 100644 --- a/src/lib/constants/nav.ts +++ b/src/lib/constants/nav.ts @@ -978,6 +978,41 @@ export const SUB_NAV: Record = { 'destination', ], }, + { + href: makePath('/dhcp/options/option150-tftp'), + label: 'Options 150/66/67 - TFTP Server', + description: + 'Configure TFTP servers for PXE boot and Cisco IP phones with Option 150 (TFTP list), Option 66 (server name), and Option 67 (bootfile)', + icon: 'server', + keywords: [ + 'dhcp', + 'option', + '150', + '66', + '67', + 'tftp', + 'server', + 'pxe', + 'boot', + 'cisco', + 'ip', + 'phones', + 'bootfile', + 'next-server', + 'filename', + 'network', + 'boot', + 'pxelinux', + 'bootx64.efi', + 'uefi', + 'bios', + 'isc', + 'kea', + 'encoder', + 'decoder', + 'configuration', + ], + }, ], '/dns': [ { diff --git a/src/lib/utils/dhcp-option150.ts b/src/lib/utils/dhcp-option150.ts new file mode 100644 index 0000000..3643b6d --- /dev/null +++ b/src/lib/utils/dhcp-option150.ts @@ -0,0 +1,504 @@ +/** + * DHCP TFTP Server Options + * - Option 150: Cisco TFTP Server List (multiple IPv4 addresses) + * - Option 66: TFTP Server Name (single hostname/IP as string) + * - Option 67: Bootfile Name (filename as string) + * Utilities for encoding/decoding TFTP server configurations for PXE boot + */ + +export interface TFTPConfig { + option150Servers?: string[]; // Array of IPv4 addresses for Option 150 + option66Server?: string; // Single hostname/IP for Option 66 + option67Bootfile?: string; // Bootfile name for Option 67 + network?: { + subnet?: string; + netmask?: string; + rangeStart?: string; + rangeEnd?: string; + }; +} + +export interface TFTPResult { + option150?: { + hexEncoded: string; + wireFormat: string; + servers: string[]; + totalLength: number; + }; + option66?: { + value: string; + hexEncoded: string; + wireFormat: string; + totalLength: number; + }; + option67?: { + value: string; + hexEncoded: string; + wireFormat: string; + totalLength: number; + }; + examples: { + iscDhcpd?: string; + keaDhcp4?: string; + ciscoIos?: string; + }; +} + +export interface ParsedOption150 { + servers: string[]; + totalLength: number; + rawHex: string; +} + +export interface ParsedStringOption { + value: string; + totalLength: number; + rawHex: string; +} + +/** + * Validate IPv4 address format + */ +function isValidIPv4(ip: string): boolean { + const parts = ip.split('.'); + if (parts.length !== 4) return false; + + for (const part of parts) { + const num = parseInt(part, 10); + if (isNaN(num) || num < 0 || num > 255) return false; + if (part !== num.toString()) return false; // No leading zeros + } + + return true; +} + +/** + * Convert IPv4 address to byte array + */ +function ipToBytes(ip: string): number[] { + return ip.split('.').map((octet) => parseInt(octet, 10)); +} + +/** + * Convert byte array to IPv4 address + */ +function bytesToIp(bytes: number[]): string { + return bytes.join('.'); +} + +/** + * Convert string to byte array (ASCII encoding) + */ +function stringToBytes(str: string): number[] { + const bytes: number[] = []; + for (let i = 0; i < str.length; i++) { + bytes.push(str.charCodeAt(i)); + } + return bytes; +} + +/** + * Convert byte array to string (ASCII decoding) + */ +function bytesToString(bytes: number[]): string { + return String.fromCharCode(...bytes); +} + +/** + * Build Option 150 (TFTP Server List) from IPv4 addresses + */ +function buildOption150(servers: string[]): TFTPResult['option150'] { + if (servers.length === 0) { + throw new Error('At least one TFTP server is required for Option 150'); + } + + // Validate all servers + for (const server of servers) { + if (!server.trim()) { + throw new Error('Server address cannot be empty'); + } + if (!isValidIPv4(server)) { + throw new Error(`Invalid IPv4 address: ${server}`); + } + } + + // Encode all servers (each is 4 bytes) + const bytes: number[] = []; + for (const server of servers) { + bytes.push(...ipToBytes(server)); + } + + const hexEncoded = bytes.map((b) => b.toString(16).padStart(2, '0')).join(''); + const wireFormat = bytes.map((b) => b.toString(16).padStart(2, '0')).join(' '); + + return { + hexEncoded, + wireFormat, + servers, + totalLength: bytes.length, + }; +} + +/** + * Parse Option 150 hex string + */ +export function parseOption150(hexString: string): ParsedOption150 { + const cleanHex = hexString.replace(/[^0-9a-fA-F]/g, ''); + + if (cleanHex.length === 0) { + throw new Error('Empty hex string'); + } + + if (cleanHex.length % 2 !== 0) { + throw new Error('Invalid hex string: odd number of characters'); + } + + // Option 150 should contain multiples of 4 bytes (IPv4 addresses) + if (cleanHex.length % 8 !== 0) { + throw new Error('Invalid Option 150 data: length must be multiple of 4 bytes (8 hex chars)'); + } + + const bytes: number[] = []; + for (let i = 0; i < cleanHex.length; i += 2) { + bytes.push(parseInt(cleanHex.substring(i, i + 2), 16)); + } + + const servers: string[] = []; + for (let i = 0; i < bytes.length; i += 4) { + const ipBytes = bytes.slice(i, i + 4); + servers.push(bytesToIp(ipBytes)); + } + + return { + servers, + totalLength: bytes.length, + rawHex: cleanHex, + }; +} + +/** + * Build Option 66 (TFTP Server Name) from hostname/IP string + */ +function buildOption66(server: string): TFTPResult['option66'] { + if (!server.trim()) { + throw new Error('TFTP server name cannot be empty'); + } + + const bytes = stringToBytes(server); + const hexEncoded = bytes.map((b) => b.toString(16).padStart(2, '0')).join(''); + const wireFormat = bytes.map((b) => b.toString(16).padStart(2, '0')).join(' '); + + return { + value: server, + hexEncoded, + wireFormat, + totalLength: bytes.length, + }; +} + +/** + * Parse Option 66 hex string + */ +export function parseOption66(hexString: string): ParsedStringOption { + const cleanHex = hexString.replace(/[^0-9a-fA-F]/g, ''); + + if (cleanHex.length === 0) { + throw new Error('Empty hex string'); + } + + if (cleanHex.length % 2 !== 0) { + throw new Error('Invalid hex string: odd number of characters'); + } + + const bytes: number[] = []; + for (let i = 0; i < cleanHex.length; i += 2) { + bytes.push(parseInt(cleanHex.substring(i, i + 2), 16)); + } + + const value = bytesToString(bytes); + + return { + value, + totalLength: bytes.length, + rawHex: cleanHex, + }; +} + +/** + * Build Option 67 (Bootfile Name) from filename string + */ +function buildOption67(filename: string): TFTPResult['option67'] { + if (!filename.trim()) { + throw new Error('Bootfile name cannot be empty'); + } + + const bytes = stringToBytes(filename); + const hexEncoded = bytes.map((b) => b.toString(16).padStart(2, '0')).join(''); + const wireFormat = bytes.map((b) => b.toString(16).padStart(2, '0')).join(' '); + + return { + value: filename, + hexEncoded, + wireFormat, + totalLength: bytes.length, + }; +} + +/** + * Parse Option 67 hex string + */ +export function parseOption67(hexString: string): ParsedStringOption { + const cleanHex = hexString.replace(/[^0-9a-fA-F]/g, ''); + + if (cleanHex.length === 0) { + throw new Error('Empty hex string'); + } + + if (cleanHex.length % 2 !== 0) { + throw new Error('Invalid hex string: odd number of characters'); + } + + const bytes: number[] = []; + for (let i = 0; i < cleanHex.length; i += 2) { + bytes.push(parseInt(cleanHex.substring(i, i + 2), 16)); + } + + const value = bytesToString(bytes); + + return { + value, + totalLength: bytes.length, + rawHex: cleanHex, + }; +} + +/** + * Build TFTP options from config + */ +export function buildTFTPOptions(config: TFTPConfig): TFTPResult { + const result: TFTPResult = { + examples: {}, + }; + + // Build Option 150 if servers provided + if (config.option150Servers && config.option150Servers.length > 0) { + result.option150 = buildOption150(config.option150Servers); + } + + // Build Option 66 if server provided + if (config.option66Server && config.option66Server.trim()) { + result.option66 = buildOption66(config.option66Server); + } + + // Build Option 67 if bootfile provided + if (config.option67Bootfile && config.option67Bootfile.trim()) { + result.option67 = buildOption67(config.option67Bootfile); + } + + // Require at least one option + if (!result.option150 && !result.option66 && !result.option67) { + throw new Error('At least one TFTP option must be configured'); + } + + // Generate examples + result.examples = generateExamples(config, result); + + return result; +} + +/** + * Generate configuration examples for different systems + */ +function generateExamples(config: TFTPConfig, result: TFTPResult): TFTPResult['examples'] { + const examples: TFTPResult['examples'] = {}; + + // Use network settings or defaults + const subnet = config.network?.subnet?.trim() || '192.168.1.0'; + const netmask = config.network?.netmask?.trim() || '255.255.255.0'; + const rangeStart = config.network?.rangeStart?.trim() || '192.168.1.100'; + const rangeEnd = config.network?.rangeEnd?.trim() || '192.168.1.200'; + + // 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}`; + + // ISC dhcpd example + const iscLines: string[] = []; + iscLines.push('# TFTP Server Options for PXE Boot'); + iscLines.push('# Configure TFTP servers and boot files for network boot'); + iscLines.push(''); + iscLines.push(`subnet ${subnet} netmask ${netmask} {`); + iscLines.push(` range ${rangeStart} ${rangeEnd};`); + iscLines.push(' '); + + if (result.option150) { + iscLines.push(' # Option 150: Cisco TFTP Server List (multiple servers)'); + const serverList = result.option150.servers.map((s) => ipToBytes(s).join(', ')).join(', '); + iscLines.push(` option tftp-server-address ${serverList};`); + iscLines.push(' '); + } + + if (result.option66) { + iscLines.push(' # Option 66: TFTP Server Name (standard)'); + iscLines.push(` next-server ${result.option66.value};`); + iscLines.push(' '); + } + + if (result.option67) { + iscLines.push(' # Option 67: Bootfile Name'); + iscLines.push(` filename "${result.option67.value}";`); + iscLines.push(' '); + } + + iscLines.push(' # PXE-specific options'); + iscLines.push(' # Vendor class identifier for PXE clients'); + iscLines.push(' # option vendor-class-identifier "PXEClient";'); + iscLines.push('}'); + + examples.iscDhcpd = iscLines.join('\n'); + + // Kea DHCPv4 example + const keaLines: string[] = []; + keaLines.push('// TFTP Server Options for PXE Boot'); + keaLines.push('// Kea DHCPv4 configuration for network boot'); + keaLines.push('{'); + keaLines.push(' "Dhcp4": {'); + keaLines.push(' "subnet4": ['); + keaLines.push(' {'); + keaLines.push(` "subnet": "${cidr}",`); + keaLines.push(' "pools": ['); + keaLines.push(' {'); + keaLines.push(` "pool": "${rangeStart} - ${rangeEnd}"`); + keaLines.push(' }'); + keaLines.push(' ],'); + + if (result.option66) { + keaLines.push(` "next-server": "${result.option66.value}",`); + } + + if (result.option67) { + keaLines.push(` "boot-file-name": "${result.option67.value}",`); + } + + keaLines.push(' "option-data": ['); + + const optionDataItems: string[] = []; + + if (result.option150) { + const option150Block = [ + ' {', + ' "name": "tftp-servers",', + ' "code": 150,', + ` "data": "${result.option150.hexEncoded}"`, + ' }', + ]; + optionDataItems.push(option150Block.join('\n')); + } + + if (result.option66) { + const option66Block = [ + ' {', + ' "name": "tftp-server-name",', + ' "code": 66,', + ` "data": "${result.option66.value}"`, + ' }', + ]; + optionDataItems.push(option66Block.join('\n')); + } + + if (result.option67) { + const option67Block = [ + ' {', + ' "name": "boot-file-name",', + ' "code": 67,', + ` "data": "${result.option67.value}"`, + ' }', + ]; + optionDataItems.push(option67Block.join('\n')); + } + + if (optionDataItems.length > 0) { + keaLines.push(optionDataItems.join(',\n')); + } + + keaLines.push(' ]'); + keaLines.push(' }'); + keaLines.push(' ]'); + keaLines.push(' }'); + keaLines.push('}'); + + examples.keaDhcp4 = keaLines.join('\n'); + + // Cisco IOS example + if (result.option150) { + const ciscoLines: string[] = []; + ciscoLines.push('! Cisco IOS DHCP Configuration'); + ciscoLines.push('! Option 150 TFTP Server configuration'); + ciscoLines.push('!'); + ciscoLines.push(`ip dhcp pool PXE_POOL`); + ciscoLines.push(` network ${subnet} ${netmask}`); + ciscoLines.push(` default-router ${rangeStart.split('.').slice(0, 3).join('.')}.1`); + ciscoLines.push(' '); + + for (const server of result.option150.servers) { + ciscoLines.push(` option 150 ip ${server}`); + } + + if (result.option67) { + ciscoLines.push(` bootfile ${result.option67.value}`); + } + + ciscoLines.push('!'); + ciscoLines.push('! Note: Cisco IOS uses "option 150 ip" for TFTP servers'); + ciscoLines.push('! Multiple servers provide redundancy for IP phones/PXE'); + + examples.ciscoIos = ciscoLines.join('\n'); + } + + return examples; +} + +/** + * Get default TFTP config + */ +export function getDefaultTFTPConfig(): TFTPConfig { + return { + option150Servers: ['192.168.1.10', '192.168.1.11'], + option66Server: 'tftp.example.com', + option67Bootfile: 'pxelinux.0', + }; +} + +/** + * Common TFTP examples + */ +export const TFTP_EXAMPLES = [ + { + label: 'Cisco IP Phones', + option150Servers: ['192.168.1.10', '192.168.1.11'], + description: 'Redundant TFTP servers for Cisco IP phone configuration', + }, + { + label: 'PXE Boot (Standard)', + option66Server: 'pxe.example.com', + option67Bootfile: 'pxelinux.0', + description: 'Standard PXE boot with single TFTP server', + }, + { + label: 'PXE Boot (UEFI)', + option66Server: '192.168.1.10', + option67Bootfile: 'bootx64.efi', + description: 'UEFI PXE boot configuration', + }, + { + label: 'Combined (Option 150 + 67)', + option150Servers: ['192.168.1.10', '192.168.1.11'], + option67Bootfile: 'SEP{MAC}.cnf.xml', + description: 'Cisco phones with redundant TFTP and config template', + }, +]; diff --git a/src/routes/dhcp/options/option150-tftp/+page.svelte b/src/routes/dhcp/options/option150-tftp/+page.svelte new file mode 100644 index 0000000..aa23d71 --- /dev/null +++ b/src/routes/dhcp/options/option150-tftp/+page.svelte @@ -0,0 +1,5 @@ + + + diff --git a/tests/unit/utils/dhcp-option150.test.ts b/tests/unit/utils/dhcp-option150.test.ts new file mode 100644 index 0000000..06d201f --- /dev/null +++ b/tests/unit/utils/dhcp-option150.test.ts @@ -0,0 +1,631 @@ +import { describe, it, expect } from 'vitest'; +import { + buildTFTPOptions, + parseOption150, + parseOption66, + parseOption67, + getDefaultTFTPConfig, + TFTP_EXAMPLES, + type TFTPConfig, +} from '../../../src/lib/utils/dhcp-option150'; + +describe('dhcp-option150.ts', () => { + describe('buildTFTPOptions', () => { + describe('Option 150 encoding', () => { + it('encodes single TFTP server', () => { + const config: TFTPConfig = { + option150Servers: ['192.168.1.10'], + }; + + const result = buildTFTPOptions(config); + + expect(result.option150).toBeDefined(); + expect(result.option150!.hexEncoded).toBe('c0a8010a'); + expect(result.option150!.servers).toEqual(['192.168.1.10']); + expect(result.option150!.totalLength).toBe(4); + }); + + it('encodes multiple TFTP servers', () => { + const config: TFTPConfig = { + option150Servers: ['192.168.1.10', '192.168.1.11'], + }; + + const result = buildTFTPOptions(config); + + expect(result.option150).toBeDefined(); + expect(result.option150!.hexEncoded).toBe('c0a8010ac0a8010b'); + expect(result.option150!.servers.length).toBe(2); + expect(result.option150!.totalLength).toBe(8); + }); + + it('provides wire format with spaces', () => { + const config: TFTPConfig = { + option150Servers: ['192.168.1.10'], + }; + + const result = buildTFTPOptions(config); + + expect(result.option150!.wireFormat).toBe('c0 a8 01 0a'); + }); + }); + + describe('Option 66 encoding', () => { + it('encodes hostname as Option 66', () => { + const config: TFTPConfig = { + option66Server: 'pxe.example.com', + }; + + const result = buildTFTPOptions(config); + + expect(result.option66).toBeDefined(); + expect(result.option66!.value).toBe('pxe.example.com'); + expect(result.option66!.hexEncoded).toBe('7078652e6578616d706c652e636f6d'); + expect(result.option66!.totalLength).toBe(15); + }); + + it('encodes IP address as Option 66', () => { + const config: TFTPConfig = { + option66Server: '192.168.1.10', + }; + + const result = buildTFTPOptions(config); + + expect(result.option66).toBeDefined(); + expect(result.option66!.value).toBe('192.168.1.10'); + }); + }); + + describe('Option 67 encoding', () => { + it('encodes bootfile name as Option 67', () => { + const config: TFTPConfig = { + option67Bootfile: 'pxelinux.0', + }; + + const result = buildTFTPOptions(config); + + expect(result.option67).toBeDefined(); + expect(result.option67!.value).toBe('pxelinux.0'); + expect(result.option67!.hexEncoded).toBe('7078656c696e75782e30'); + }); + + it('encodes UEFI bootfile', () => { + const config: TFTPConfig = { + option67Bootfile: 'bootx64.efi', + }; + + const result = buildTFTPOptions(config); + + expect(result.option67).toBeDefined(); + expect(result.option67!.value).toBe('bootx64.efi'); + }); + }); + + describe('combined options', () => { + it('encodes all three options together', () => { + const config: TFTPConfig = { + option150Servers: ['192.168.1.10', '192.168.1.11'], + option66Server: 'pxe.example.com', + option67Bootfile: 'pxelinux.0', + }; + + const result = buildTFTPOptions(config); + + expect(result.option150).toBeDefined(); + expect(result.option66).toBeDefined(); + expect(result.option67).toBeDefined(); + }); + + it('encodes Option 150 + 67 only', () => { + const config: TFTPConfig = { + option150Servers: ['192.168.1.10'], + option67Bootfile: 'SEP{MAC}.cnf.xml', + }; + + const result = buildTFTPOptions(config); + + expect(result.option150).toBeDefined(); + expect(result.option66).toBeUndefined(); + expect(result.option67).toBeDefined(); + }); + + it('encodes Option 66 + 67 only', () => { + const config: TFTPConfig = { + option66Server: 'tftp.example.com', + option67Bootfile: 'pxelinux.0', + }; + + const result = buildTFTPOptions(config); + + expect(result.option150).toBeUndefined(); + expect(result.option66).toBeDefined(); + expect(result.option67).toBeDefined(); + }); + }); + + describe('validation', () => { + it('rejects empty Option 150 server', () => { + const config: TFTPConfig = { + option150Servers: [''], + }; + + expect(() => buildTFTPOptions(config)).toThrow('cannot be empty'); + }); + + it('rejects invalid IPv4 address in Option 150', () => { + const config: TFTPConfig = { + option150Servers: ['256.0.0.0'], + }; + + expect(() => buildTFTPOptions(config)).toThrow('Invalid IPv4 address'); + }); + + it('rejects empty Option 66 server', () => { + const config: TFTPConfig = { + option66Server: '', + }; + + expect(() => buildTFTPOptions(config)).toThrow('At least one TFTP option must be configured'); + }); + + it('rejects empty Option 67 bootfile', () => { + const config: TFTPConfig = { + option67Bootfile: '', + }; + + expect(() => buildTFTPOptions(config)).toThrow('At least one TFTP option must be configured'); + }); + + it('requires at least one option configured', () => { + const config: TFTPConfig = {}; + + expect(() => buildTFTPOptions(config)).toThrow('At least one TFTP option must be configured'); + }); + + it('accepts valid Option 150 configuration', () => { + const config: TFTPConfig = { + option150Servers: ['192.168.1.10', '192.168.1.11', '10.0.0.1'], + }; + + expect(() => buildTFTPOptions(config)).not.toThrow(); + }); + }); + + describe('configuration examples', () => { + it('generates ISC dhcpd configuration', () => { + const config: TFTPConfig = { + option150Servers: ['192.168.1.10'], + option67Bootfile: 'pxelinux.0', + }; + + const result = buildTFTPOptions(config); + + expect(result.examples.iscDhcpd).toBeDefined(); + expect(result.examples.iscDhcpd).toContain('tftp-server-address'); + expect(result.examples.iscDhcpd).toContain('filename'); + }); + + it('generates Kea DHCPv4 configuration', () => { + const config: TFTPConfig = { + option66Server: 'tftp.example.com', + option67Bootfile: 'pxelinux.0', + }; + + const result = buildTFTPOptions(config); + + expect(result.examples.keaDhcp4).toBeDefined(); + expect(result.examples.keaDhcp4).toContain('next-server'); + expect(result.examples.keaDhcp4).toContain('boot-file-name'); + }); + + it('generates Cisco IOS configuration for Option 150', () => { + const config: TFTPConfig = { + option150Servers: ['192.168.1.10', '192.168.1.11'], + }; + + const result = buildTFTPOptions(config); + + expect(result.examples.ciscoIos).toBeDefined(); + expect(result.examples.ciscoIos).toContain('option 150 ip'); + expect(result.examples.ciscoIos).toContain('192.168.1.10'); + expect(result.examples.ciscoIos).toContain('192.168.1.11'); + }); + + it('uses default network settings when not provided', () => { + const config: TFTPConfig = { + option150Servers: ['192.168.1.10'], + }; + + const result = buildTFTPOptions(config); + + expect(result.examples.iscDhcpd).toContain('192.168.1.0'); + expect(result.examples.iscDhcpd).toContain('255.255.255.0'); + }); + + it('uses custom network settings when provided', () => { + const config: TFTPConfig = { + option150Servers: ['10.0.0.1'], + network: { + subnet: '10.0.0.0', + netmask: '255.255.255.0', + rangeStart: '10.0.0.50', + rangeEnd: '10.0.0.150', + }, + }; + + const result = buildTFTPOptions(config); + + expect(result.examples.iscDhcpd).toContain('10.0.0.0'); + expect(result.examples.iscDhcpd).toContain('10.0.0.50'); + }); + }); + }); + + describe('parseOption150', () => { + describe('basic decoding', () => { + it('decodes single TFTP server', () => { + const hexInput = 'c0a8010a'; + + const result = parseOption150(hexInput); + + expect(result.servers).toEqual(['192.168.1.10']); + expect(result.totalLength).toBe(4); + }); + + it('decodes multiple TFTP servers', () => { + const hexInput = 'c0a8010ac0a8010b'; + + const result = parseOption150(hexInput); + + expect(result.servers).toEqual(['192.168.1.10', '192.168.1.11']); + expect(result.totalLength).toBe(8); + }); + + it('handles hex input with spaces', () => { + const hexInput = 'c0 a8 01 0a'; + + const result = parseOption150(hexInput); + + expect(result.servers).toEqual(['192.168.1.10']); + }); + + it('handles hex input with colons', () => { + const hexInput = 'c0:a8:01:0a'; + + const result = parseOption150(hexInput); + + expect(result.servers).toEqual(['192.168.1.10']); + }); + + it('handles mixed case hex input', () => { + const hexInput = 'C0A8010A'; + + const result = parseOption150(hexInput); + + expect(result.servers).toEqual(['192.168.1.10']); + }); + }); + + describe('error handling', () => { + it('rejects empty hex string', () => { + expect(() => parseOption150('')).toThrow('Empty hex string'); + }); + + it('rejects hex string with odd number of characters', () => { + expect(() => parseOption150('c0a801')).toThrow('multiple of 4 bytes'); + }); + + it('rejects data not multiple of 4 bytes', () => { + // 6 bytes instead of 4 or 8 + expect(() => parseOption150('c0a8010ac0a8')).toThrow('multiple of 4 bytes'); + }); + }); + + describe('round-trip encoding/decoding', () => { + it('maintains data integrity for single server', () => { + const config: TFTPConfig = { + option150Servers: ['192.168.1.10'], + }; + + const encoded = buildTFTPOptions(config); + const decoded = parseOption150(encoded.option150!.hexEncoded); + + expect(decoded.servers).toEqual(config.option150Servers); + }); + + it('maintains data integrity for multiple servers', () => { + const config: TFTPConfig = { + option150Servers: ['192.168.1.10', '192.168.1.11', '10.0.0.1'], + }; + + const encoded = buildTFTPOptions(config); + const decoded = parseOption150(encoded.option150!.hexEncoded); + + expect(decoded.servers).toEqual(config.option150Servers); + }); + }); + }); + + describe('parseOption66', () => { + describe('basic decoding', () => { + it('decodes hostname', () => { + const hexInput = '7078652e6578616d706c652e636f6d'; + + const result = parseOption66(hexInput); + + expect(result.value).toBe('pxe.example.com'); + expect(result.totalLength).toBe(15); + }); + + it('decodes IP address', () => { + const hexInput = '3139322e3136382e312e3130'; + + const result = parseOption66(hexInput); + + expect(result.value).toBe('192.168.1.10'); + }); + + it('handles hex input with spaces', () => { + const hexInput = '70 78 65 2e 65 78 61 6d 70 6c 65 2e 63 6f 6d'; + + const result = parseOption66(hexInput); + + expect(result.value).toBe('pxe.example.com'); + }); + }); + + describe('error handling', () => { + it('rejects empty hex string', () => { + expect(() => parseOption66('')).toThrow('Empty hex string'); + }); + + it('rejects hex string with odd number of characters', () => { + expect(() => parseOption66('70786')).toThrow('odd number of characters'); + }); + }); + + describe('round-trip encoding/decoding', () => { + it('maintains data integrity for hostname', () => { + const config: TFTPConfig = { + option66Server: 'tftp.example.com', + }; + + const encoded = buildTFTPOptions(config); + const decoded = parseOption66(encoded.option66!.hexEncoded); + + expect(decoded.value).toBe(config.option66Server); + }); + }); + }); + + describe('parseOption67', () => { + describe('basic decoding', () => { + it('decodes bootfile name', () => { + const hexInput = '7078656c696e75782e30'; + + const result = parseOption67(hexInput); + + expect(result.value).toBe('pxelinux.0'); + }); + + it('decodes UEFI bootfile', () => { + const hexInput = '626f6f747836342e656669'; + + const result = parseOption67(hexInput); + + expect(result.value).toBe('bootx64.efi'); + }); + }); + + describe('error handling', () => { + it('rejects empty hex string', () => { + expect(() => parseOption67('')).toThrow('Empty hex string'); + }); + + it('rejects hex string with odd number of characters', () => { + expect(() => parseOption67('70786')).toThrow('odd number of characters'); + }); + }); + + describe('round-trip encoding/decoding', () => { + it('maintains data integrity for bootfile', () => { + const config: TFTPConfig = { + option67Bootfile: 'pxelinux.0', + }; + + const encoded = buildTFTPOptions(config); + const decoded = parseOption67(encoded.option67!.hexEncoded); + + expect(decoded.value).toBe(config.option67Bootfile); + }); + }); + }); + + describe('getDefaultTFTPConfig', () => { + it('returns default configuration', () => { + const config = getDefaultTFTPConfig(); + + expect(config.option150Servers).toBeDefined(); + expect(config.option66Server).toBeDefined(); + expect(config.option67Bootfile).toBeDefined(); + }); + + it('returns valid configuration that can be encoded', () => { + const config = getDefaultTFTPConfig(); + + expect(() => buildTFTPOptions(config)).not.toThrow(); + }); + }); + + describe('TFTP_EXAMPLES', () => { + it('provides multiple example configurations', () => { + expect(TFTP_EXAMPLES.length).toBeGreaterThan(0); + }); + + it('each example has required properties', () => { + for (const example of TFTP_EXAMPLES) { + expect(example.label).toBeDefined(); + expect(example.description).toBeDefined(); + } + }); + + it('all examples can be successfully encoded', () => { + for (const example of TFTP_EXAMPLES) { + const config: TFTPConfig = { + option150Servers: example.option150Servers, + option66Server: example.option66Server, + option67Bootfile: example.option67Bootfile, + }; + + expect(() => buildTFTPOptions(config)).not.toThrow(); + } + }); + }); + + describe('UI component examples validation', () => { + describe('encode examples', () => { + it('Cisco IP Phones example produces valid output', () => { + const config: TFTPConfig = { + option150Servers: ['192.168.1.10', '192.168.1.11'], + }; + + const result = buildTFTPOptions(config); + expect(result.option150!.hexEncoded).toBe('c0a8010ac0a8010b'); + }); + + it('PXE Boot (Standard) example produces valid output', () => { + const config: TFTPConfig = { + option66Server: 'pxe.example.com', + option67Bootfile: 'pxelinux.0', + }; + + const result = buildTFTPOptions(config); + expect(result.option66).toBeDefined(); + expect(result.option67).toBeDefined(); + }); + + it('PXE Boot (UEFI) example produces valid output', () => { + const config: TFTPConfig = { + option66Server: '192.168.1.10', + option67Bootfile: 'bootx64.efi', + }; + + const result = buildTFTPOptions(config); + expect(result.option66).toBeDefined(); + expect(result.option67).toBeDefined(); + }); + + it('Combined (Option 150 + 67) example produces valid output', () => { + const config: TFTPConfig = { + option150Servers: ['192.168.1.10', '192.168.1.11'], + option67Bootfile: 'SEP{MAC}.cnf.xml', + }; + + const result = buildTFTPOptions(config); + expect(result.option150).toBeDefined(); + expect(result.option67).toBeDefined(); + }); + }); + + describe('decode examples', () => { + it('Option 150: Dual TFTP decode example is valid', () => { + const hexInput = 'c0a8010ac0a8010b'; + + const result = parseOption150(hexInput); + + expect(result.servers).toEqual(['192.168.1.10', '192.168.1.11']); + }); + + it('Option 66: Hostname decode example is valid', () => { + const hexInput = '7078652e6578616d706c652e636f6d'; + + const result = parseOption66(hexInput); + + expect(result.value).toBe('pxe.example.com'); + }); + + it('Option 67: PXE Boot decode example is valid', () => { + const hexInput = '7078656c696e75782e30'; + + const result = parseOption67(hexInput); + + expect(result.value).toBe('pxelinux.0'); + }); + + it('Option 67: UEFI Boot decode example is valid', () => { + const hexInput = '626f6f747836342e656669'; + + const result = parseOption67(hexInput); + + expect(result.value).toBe('bootx64.efi'); + }); + + it('all decode examples match their descriptions', () => { + // Option 150: Dual TFTP + const opt150 = parseOption150('c0a8010ac0a8010b'); + expect(opt150.servers).toEqual(['192.168.1.10', '192.168.1.11']); + + // Option 66: Hostname + const opt66 = parseOption66('7078652e6578616d706c652e636f6d'); + expect(opt66.value).toBe('pxe.example.com'); + + // Option 67: PXE Boot + const opt67Pxe = parseOption67('7078656c696e75782e30'); + expect(opt67Pxe.value).toBe('pxelinux.0'); + + // Option 67: UEFI Boot + const opt67Uefi = parseOption67('626f6f747836342e656669'); + expect(opt67Uefi.value).toBe('bootx64.efi'); + }); + }); + }); + + describe('edge cases', () => { + it('handles maximum number of Option 150 servers', () => { + const servers = Array.from({ length: 10 }, (_, i) => `192.168.1.${i + 10}`); + const config: TFTPConfig = { + option150Servers: servers, + }; + + const result = buildTFTPOptions(config); + const decoded = parseOption150(result.option150!.hexEncoded); + + expect(decoded.servers).toEqual(servers); + expect(decoded.servers.length).toBe(10); + }); + + it('handles very long hostname in Option 66', () => { + const hostname = 'very-long-hostname-for-tftp-server.subdomain.example.com'; + const config: TFTPConfig = { + option66Server: hostname, + }; + + const result = buildTFTPOptions(config); + const decoded = parseOption66(result.option66!.hexEncoded); + + expect(decoded.value).toBe(hostname); + }); + + it('handles special characters in bootfile name', () => { + const bootfile = 'boot/{MAC}-config.cfg'; + const config: TFTPConfig = { + option67Bootfile: bootfile, + }; + + const result = buildTFTPOptions(config); + const decoded = parseOption67(result.option67!.hexEncoded); + + expect(decoded.value).toBe(bootfile); + }); + + it('handles different IP ranges for Option 150', () => { + const config: TFTPConfig = { + option150Servers: ['10.0.0.1', '172.16.0.1', '192.168.1.1'], + }; + + const result = buildTFTPOptions(config); + const decoded = parseOption150(result.option150!.hexEncoded); + + expect(decoded.servers).toEqual(config.option150Servers); + }); + }); +});