From 46a7666f6c18dfcc3ce80f7ee15422adeb94ce3c Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sun, 19 Oct 2025 11:58:06 +0100 Subject: [PATCH] DHCP Option 82 Builder --- .../tools/DHCPOption82Builder.svelte | 864 ++++++++++++++++++ src/lib/constants/nav.ts | 29 + src/lib/utils/dhcp-option82.ts | 353 +++++++ .../options/option82-builder/+page.svelte | 5 + tests/unit/utils/dhcp-option82.test.ts | 452 +++++++++ 5 files changed, 1703 insertions(+) create mode 100644 src/lib/components/tools/DHCPOption82Builder.svelte create mode 100644 src/lib/utils/dhcp-option82.ts create mode 100644 src/routes/dhcp/options/option82-builder/+page.svelte create mode 100644 tests/unit/utils/dhcp-option82.test.ts diff --git a/src/lib/components/tools/DHCPOption82Builder.svelte b/src/lib/components/tools/DHCPOption82Builder.svelte new file mode 100644 index 0000000..217352e --- /dev/null +++ b/src/lib/components/tools/DHCPOption82Builder.svelte @@ -0,0 +1,864 @@ + + + + {#if mode === 'build'} + ex.label} + getDescription={(ex) => ex.description} + selectedIndex={selectedExampleIndex} + /> + {:else} + ex.label} + getDescription={(ex) => ex.description} + selectedIndex={selectedExampleIndex} + /> + {/if} + + {#if mode === 'build'} +
+
+

Configuration

+
+
+ {#each config.suboptions as suboption, i (`sub-${i}-${suboption.type}`)} +
+
+

Suboption {i + 1}

+ {#if config.suboptions.length > 1} + + {/if} +
+ +
+
+ + +
+ +
+ + +
+
+ +
+ + +
+
+ {/each} + + +
+
+ + {#if validationErrors.length > 0} +
+

Validation Errors

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

Generated Option 82

+ +
+
+

Hex-Encoded Value

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

Breakdown

+ {#each result.breakdown as breakdown, i (i)} +
+
+ {breakdown.type} (Code {breakdown.typeCode}) + Length: {breakdown.length} bytes +
+

{breakdown.description}

+
+
Value: {breakdown.value}
+
Hex: {breakdown.hexValue}
+
+
+ {/each} +
+ + {#if result.examples.iscDhcpd} +
+
+

ISC dhcpd Configuration Example

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

Kea DHCPv4 Configuration Example

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

Cisco Relay Agent Example

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

Parse Option 82 Hex

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

Validation Errors

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

Parsed Option 82

+ +
+
Total Length: {parseResult.totalLength} bytes
+
Suboptions Found: {parseResult.suboptions.length}
+
+ +
+

Suboptions

+ {#each parseResult.suboptions as suboption, i (i)} +
+
+ {suboption.type} (Code {suboption.typeCode}) + Length: {suboption.length} bytes +
+

{suboption.description}

+
+
Decoded Value: {suboption.value}
+
Hex Value: {suboption.hexValue}
+
+
+ {/each} +
+
+ {/if} + {/if} +
+ + diff --git a/src/lib/constants/nav.ts b/src/lib/constants/nav.ts index c7f800e..849a0ed 100644 --- a/src/lib/constants/nav.ts +++ b/src/lib/constants/nav.ts @@ -888,6 +888,35 @@ export const SUB_NAV: Record = { 'dhcp6', ], }, + { + href: makePath('/dhcp/options/option82-builder'), + label: 'Option 82 Builder', + description: + 'Construct and parse DHCP Relay Agent Information (Option 82) with Circuit-ID, Remote-ID, and VLAN formats', + icon: 'tools', + keywords: [ + 'dhcp', + 'option', + '82', + 'relay', + 'agent', + 'information', + 'circuit-id', + 'remote-id', + 'vlan', + 'builder', + 'parser', + 'suboption', + 'tlv', + 'acl', + 'policy', + 'cisco', + 'kea', + 'isc', + 'network', + 'infrastructure', + ], + }, ], '/dns': [ { diff --git a/src/lib/utils/dhcp-option82.ts b/src/lib/utils/dhcp-option82.ts new file mode 100644 index 0000000..38613da --- /dev/null +++ b/src/lib/utils/dhcp-option82.ts @@ -0,0 +1,353 @@ +/** + * DHCP Option 82 - Relay Agent Information (RFC 3046) + * Utilities for constructing and parsing Option 82 suboptions + */ + +export type SuboptionType = 'circuit-id' | 'remote-id'; +export type EncodingFormat = 'hex' | 'ascii' | 'vlan-id' | 'hostname-port'; + +export interface Suboption { + type: SuboptionType; + format: EncodingFormat; + value: string; +} + +export interface Option82Config { + suboptions: Suboption[]; +} + +export interface Option82Result { + hexEncoded: string; + asciiDecoded: string; + breakdown: SuboptionBreakdown[]; + examples: { + iscDhcpd?: string; + keaDhcp4?: string; + ciscoRelay?: string; + }; +} + +export interface SuboptionBreakdown { + type: SuboptionType; + typeCode: number; + length: number; + value: string; + hexValue: string; + description: string; +} + +export interface ParsedOption82 { + suboptions: SuboptionBreakdown[]; + totalLength: number; + rawHex: string; +} + +const SUBOPTION_CODES: Record = { + 'circuit-id': 1, + 'remote-id': 2, +}; + +const SUBOPTION_NAMES: Record = { + 1: 'circuit-id', + 2: 'remote-id', +}; + +/** + * Convert string to hex based on format + */ +function encodeValue(value: string, format: EncodingFormat): string { + switch (format) { + case 'hex': + // Already hex, validate and clean + return value.replace(/[^0-9a-fA-F]/g, ''); + + case 'ascii': + // Convert ASCII to hex + return Array.from(value) + .map((c) => c.charCodeAt(0).toString(16).padStart(2, '0')) + .join(''); + + case 'vlan-id': { + // VLAN ID format: 2 bytes for VLAN (0-4095) + const vlanId = parseInt(value, 10); + if (isNaN(vlanId) || vlanId < 0 || vlanId > 4095) { + return '0000'; + } + return vlanId.toString(16).padStart(4, '0'); + } + + case 'hostname-port': { + // Format: hostname:port as ASCII + return Array.from(value) + .map((c) => c.charCodeAt(0).toString(16).padStart(2, '0')) + .join(''); + } + + default: + return ''; + } +} + +/** + * Decode hex value to readable string based on format + */ +function decodeValue(hex: string, format: EncodingFormat): string { + switch (format) { + case 'hex': + return hex; + + case 'ascii': + case 'hostname-port': { + let result = ''; + for (let i = 0; i < hex.length; i += 2) { + const byte = hex.substring(i, i + 2); + result += String.fromCharCode(parseInt(byte, 16)); + } + return result; + } + + case 'vlan-id': { + const vlanId = parseInt(hex, 16); + return vlanId.toString(); + } + + default: + return hex; + } +} + +/** + * Build Option 82 hex string from suboptions + */ +export function buildOption82(config: Option82Config): Option82Result { + const breakdown: SuboptionBreakdown[] = []; + const hexParts: string[] = []; + + for (const suboption of config.suboptions) { + const typeCode = SUBOPTION_CODES[suboption.type]; + const hexValue = encodeValue(suboption.value, suboption.format); + const length = hexValue.length / 2; // Length in bytes + + // TLV format: Type (1 byte) + Length (1 byte) + Value (n bytes) + const typeHex = typeCode.toString(16).padStart(2, '0'); + const lengthHex = length.toString(16).padStart(2, '0'); + + hexParts.push(typeHex + lengthHex + hexValue); + + breakdown.push({ + type: suboption.type, + typeCode, + length, + value: suboption.value, + hexValue, + description: getSuboptionDescription(suboption.type, suboption.format), + }); + } + + const hexEncoded = hexParts.join(''); + const asciiDecoded = breakdown.map((b) => `${b.type}: ${b.value}`).join(', '); + + const examples = generateExamples(config, hexEncoded); + + return { + hexEncoded, + asciiDecoded, + breakdown, + examples, + }; +} + +/** + * Parse Option 82 hex string + */ +export function parseOption82(hexString: string): ParsedOption82 { + const cleanHex = hexString.replace(/[^0-9a-fA-F]/g, ''); + const suboptions: SuboptionBreakdown[] = []; + let offset = 0; + + while (offset < cleanHex.length) { + // Read Type (1 byte) + const typeHex = cleanHex.substring(offset, offset + 2); + const typeCode = parseInt(typeHex, 16); + offset += 2; + + if (offset >= cleanHex.length) break; + + // Read Length (1 byte) + const lengthHex = cleanHex.substring(offset, offset + 2); + const length = parseInt(lengthHex, 16); + offset += 2; + + if (offset + length * 2 > cleanHex.length) break; + + // Read Value (length bytes) + const hexValue = cleanHex.substring(offset, offset + length * 2); + offset += length * 2; + + const type = SUBOPTION_NAMES[typeCode]; + if (!type) continue; + + // Try to decode as ASCII + const asciiValue = decodeValue(hexValue, 'ascii'); + + suboptions.push({ + type, + typeCode, + length, + value: asciiValue, + hexValue, + description: getSuboptionDescription(type, 'ascii'), + }); + } + + return { + suboptions, + totalLength: offset / 2, + rawHex: cleanHex, + }; +} + +/** + * Get description for suboption type + */ +function getSuboptionDescription(type: SuboptionType, _format: EncodingFormat): string { + const descriptions: Record = { + 'circuit-id': 'Identifies the circuit (interface/VLAN) that received the DHCP packet', + 'remote-id': 'Identifies the relay agent itself (hostname, MAC, or ID)', + }; + + return descriptions[type]; +} + +/** + * Generate configuration examples for different DHCP servers + */ +function generateExamples(config: Option82Config, _hexEncoded: string): Option82Result['examples'] { + const examples: Option82Result['examples'] = {}; + + // ISC dhcpd example + const iscLines: string[] = []; + iscLines.push('# Match clients based on Option 82 relay agent information'); + iscLines.push('class "option82-matched" {'); + + for (const suboption of config.suboptions) { + if (suboption.type === 'circuit-id') { + iscLines.push(` match if binary-to-ascii(16, 8, ":", option agent.circuit-id) = "${suboption.value}";`); + } else if (suboption.type === 'remote-id') { + iscLines.push(` match if binary-to-ascii(16, 8, ":", option agent.remote-id) = "${suboption.value}";`); + } + } + + iscLines.push('}'); + iscLines.push(''); + iscLines.push('# Pool restricted to matched clients'); + iscLines.push('subnet 192.168.1.0 netmask 255.255.255.0 {'); + iscLines.push(' pool {'); + iscLines.push(' range 192.168.1.100 192.168.1.200;'); + iscLines.push(' allow members of "option82-matched";'); + iscLines.push(' }'); + iscLines.push('}'); + + examples.iscDhcpd = iscLines.join('\n'); + + // Kea DHCPv4 example + const keaLines: string[] = []; + keaLines.push('// Client class based on Option 82'); + keaLines.push('{'); + keaLines.push(' "client-classes": ['); + keaLines.push(' {'); + keaLines.push(' "name": "option82-matched",'); + + for (const suboption of config.suboptions) { + const subCode = SUBOPTION_CODES[suboption.type]; + keaLines.push(` "test": "relay4[${subCode}].hex == 0x${encodeValue(suboption.value, suboption.format)}",`); + } + + keaLines.push(' "only-if-required": true'); + keaLines.push(' }'); + keaLines.push(' ],'); + keaLines.push(' "subnet4": ['); + keaLines.push(' {'); + keaLines.push(' "subnet": "192.168.1.0/24",'); + keaLines.push(' "pools": ['); + keaLines.push(' {'); + keaLines.push(' "pool": "192.168.1.100 - 192.168.1.200",'); + keaLines.push(' "client-class": "option82-matched"'); + keaLines.push(' }'); + keaLines.push(' ]'); + keaLines.push(' }'); + keaLines.push(' ]'); + keaLines.push('}'); + + examples.keaDhcp4 = keaLines.join('\n'); + + // Cisco relay agent example + const ciscoLines: string[] = []; + ciscoLines.push('! Enable DHCP relay agent information option'); + ciscoLines.push('interface GigabitEthernet0/1'); + ciscoLines.push(' ip address 192.168.1.1 255.255.255.0'); + ciscoLines.push(' ip helper-address 192.168.100.10'); + ciscoLines.push(' !'); + + for (const suboption of config.suboptions) { + if (suboption.type === 'circuit-id' && suboption.format === 'vlan-id') { + ciscoLines.push(` ! Circuit-ID will be automatically set based on VLAN ${suboption.value}`); + } else if (suboption.type === 'remote-id') { + ciscoLines.push(` ip dhcp relay information option-insert`); + } + } + + ciscoLines.push('!'); + ciscoLines.push('! Trust Option 82 from downstream relays'); + ciscoLines.push('ip dhcp relay information trust-all'); + ciscoLines.push('!'); + ciscoLines.push('! Policy to customize Option 82'); + ciscoLines.push('ip dhcp relay information policy-action replace'); + + examples.ciscoRelay = ciscoLines.join('\n'); + + return examples; +} + +/** + * Get default config + */ +export function getDefaultOption82Config(): Option82Config { + return { + suboptions: [ + { + type: 'circuit-id', + format: 'vlan-id', + value: '100', + }, + ], + }; +} + +/** + * Common VLAN formats + */ +export const VLAN_EXAMPLES = [ + { label: 'VLAN 10 (Guests)', value: '10' }, + { label: 'VLAN 20 (Corporate)', value: '20' }, + { label: 'VLAN 100 (Servers)', value: '100' }, + { label: 'VLAN 200 (VoIP)', value: '200' }, +]; + +/** + * Common Circuit-ID examples + */ +export const CIRCUIT_ID_EXAMPLES = [ + { label: 'Hostname:Port', value: 'sw1:Gi0/1', format: 'hostname-port' as EncodingFormat }, + { label: 'VLAN ID', value: '100', format: 'vlan-id' as EncodingFormat }, + { label: 'Custom ASCII', value: 'building-a-floor-3', format: 'ascii' as EncodingFormat }, +]; + +/** + * Common Remote-ID examples + */ +export const REMOTE_ID_EXAMPLES = [ + { label: 'Hostname', value: 'relay-sw1.example.com', format: 'ascii' as EncodingFormat }, + { label: 'MAC Address', value: '001122334455', format: 'hex' as EncodingFormat }, + { label: 'Agent ID', value: 'agent-001', format: 'ascii' as EncodingFormat }, +]; diff --git a/src/routes/dhcp/options/option82-builder/+page.svelte b/src/routes/dhcp/options/option82-builder/+page.svelte new file mode 100644 index 0000000..73e1fa3 --- /dev/null +++ b/src/routes/dhcp/options/option82-builder/+page.svelte @@ -0,0 +1,5 @@ + + + diff --git a/tests/unit/utils/dhcp-option82.test.ts b/tests/unit/utils/dhcp-option82.test.ts new file mode 100644 index 0000000..cb36f74 --- /dev/null +++ b/tests/unit/utils/dhcp-option82.test.ts @@ -0,0 +1,452 @@ +import { describe, it, expect } from 'vitest'; +import { + buildOption82, + parseOption82, + getDefaultOption82Config, + type Option82Config, + type EncodingFormat, +} from '$lib/utils/dhcp-option82'; + +describe('DHCP Option 82', () => { + describe('getDefaultOption82Config', () => { + it('should return default configuration', () => { + const config = getDefaultOption82Config(); + expect(config).toBeDefined(); + expect(config.suboptions).toHaveLength(1); + expect(config.suboptions[0].type).toBe('circuit-id'); + expect(config.suboptions[0].format).toBe('vlan-id'); + expect(config.suboptions[0].value).toBe('100'); + }); + }); + + describe('buildOption82', () => { + it('should build option 82 with ASCII circuit-id', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'circuit-id', + format: 'ascii', + value: 'Gi0/1', + }, + ], + }; + + const result = buildOption82(config); + expect(result.hexEncoded).toBeTruthy(); + expect(result.breakdown).toHaveLength(1); + expect(result.breakdown[0].type).toBe('circuit-id'); + expect(result.breakdown[0].typeCode).toBe(1); + expect(result.breakdown[0].value).toBe('Gi0/1'); + }); + + it('should build option 82 with VLAN ID', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'circuit-id', + format: 'vlan-id', + value: '100', + }, + ], + }; + + const result = buildOption82(config); + expect(result.hexEncoded).toBeTruthy(); + expect(result.breakdown[0].hexValue).toBe('0064'); + expect(result.breakdown[0].length).toBe(2); + }); + + it('should build option 82 with multiple suboptions', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'circuit-id', + format: 'ascii', + value: 'Gi0/1', + }, + { + type: 'remote-id', + format: 'ascii', + value: 'sw1.example', + }, + ], + }; + + const result = buildOption82(config); + expect(result.breakdown).toHaveLength(2); + expect(result.breakdown[0].typeCode).toBe(1); + expect(result.breakdown[1].typeCode).toBe(2); + }); + + it('should handle hex format', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'remote-id', + format: 'hex', + value: '001122334455', + }, + ], + }; + + const result = buildOption82(config); + expect(result.breakdown[0].hexValue).toBe('001122334455'); + }); + + it('should generate ISC dhcpd examples', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'circuit-id', + format: 'ascii', + value: 'Gi0/1', + }, + ], + }; + + const result = buildOption82(config); + expect(result.examples.iscDhcpd).toBeDefined(); + expect(result.examples.iscDhcpd).toContain('match if'); + expect(result.examples.iscDhcpd).toContain('agent.circuit-id'); + }); + + it('should generate Kea DHCPv4 examples', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'circuit-id', + format: 'ascii', + value: 'Gi0/1', + }, + ], + }; + + const result = buildOption82(config); + expect(result.examples.keaDhcp4).toBeDefined(); + expect(result.examples.keaDhcp4).toContain('relay4'); + expect(result.examples.keaDhcp4).toContain('client-classes'); + }); + + it('should generate Cisco relay examples', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'circuit-id', + format: 'vlan-id', + value: '100', + }, + ], + }; + + const result = buildOption82(config); + expect(result.examples.ciscoRelay).toBeDefined(); + expect(result.examples.ciscoRelay).toContain('ip helper-address'); + }); + + it('should handle hostname:port format', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'circuit-id', + format: 'hostname-port', + value: 'sw1:Gi0/1', + }, + ], + }; + + const result = buildOption82(config); + expect(result.breakdown[0].value).toBe('sw1:Gi0/1'); + expect(result.breakdown[0].hexValue.length).toBeGreaterThan(0); + }); + + it('should handle invalid VLAN ID gracefully', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'circuit-id', + format: 'vlan-id', + value: '9999', // Out of range + }, + ], + }; + + const result = buildOption82(config); + expect(result.breakdown[0].hexValue).toBe('0000'); + }); + + it('should clean hex input', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'remote-id', + format: 'hex', + value: '00:11:22:33:44:55', // With colons + }, + ], + }; + + const result = buildOption82(config); + expect(result.breakdown[0].hexValue).toBe('001122334455'); + }); + }); + + describe('parseOption82', () => { + it('should parse circuit-id suboption', () => { + // TLV: Type=01, Length=05, Value="Gi0/1" (4769302f31 in hex) + const hex = '01054769302f31'; + const result = parseOption82(hex); + + expect(result.suboptions).toHaveLength(1); + expect(result.suboptions[0].type).toBe('circuit-id'); + expect(result.suboptions[0].typeCode).toBe(1); + expect(result.suboptions[0].length).toBe(5); + expect(result.suboptions[0].value).toBe('Gi0/1'); + }); + + it('should parse remote-id suboption', () => { + // TLV: Type=02, Length=0b, Value="sw1.example" (7377312e6578616d706c65 in hex, 11 bytes) + const hex = '020b7377312e6578616d706c65'; + const result = parseOption82(hex); + + expect(result.suboptions).toHaveLength(1); + expect(result.suboptions[0].type).toBe('remote-id'); + expect(result.suboptions[0].typeCode).toBe(2); + expect(result.suboptions[0].value).toBe('sw1.example'); + }); + + it('should parse multiple suboptions', () => { + // Circuit-ID + Remote-ID + const hex = '01054769302f31' + '020b7377312e6578616d706c65'; + const result = parseOption82(hex); + + expect(result.suboptions).toHaveLength(2); + expect(result.suboptions[0].type).toBe('circuit-id'); + expect(result.suboptions[1].type).toBe('remote-id'); + }); + + it('should handle hex with spaces', () => { + const hex = '01 05 47 69 30 2f 31'; + const result = parseOption82(hex); + + expect(result.suboptions).toHaveLength(1); + expect(result.suboptions[0].value).toBe('Gi0/1'); + }); + + it('should calculate total length correctly', () => { + const hex = '01054769302f31020b7377312e6578616d706c65'; + const result = parseOption82(hex); + + expect(result.totalLength).toBe(20); // 1+1+5 + 1+1+11 + }); + + it('should handle truncated data', () => { + // Type and length present, but value missing + const hex = '0105'; + const result = parseOption82(hex); + + expect(result.suboptions).toHaveLength(0); + }); + + it('should skip unknown suboption types', () => { + // Type=99 (unknown), Length=04, Value=00000000, then Type=01 (known) + const hex = '990400000000' + '01054769302f31'; + const result = parseOption82(hex); + + // Only circuit-id should be parsed (type 01) + expect(result.suboptions.length).toBeGreaterThan(0); + }); + + it('should handle empty input', () => { + const hex = ''; + const result = parseOption82(hex); + + expect(result.suboptions).toHaveLength(0); + expect(result.totalLength).toBe(0); + }); + + it('should handle VLAN ID suboption', () => { + // Type=01, Length=02, Value=0064 (100 decimal) + const hex = '0102006 4'; + const result = parseOption82(hex); + + expect(result.suboptions).toHaveLength(1); + expect(result.suboptions[0].hexValue).toBe('0064'); + }); + }); + + describe('Round-trip encoding/decoding', () => { + it('should preserve ASCII data through round-trip', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'circuit-id', + format: 'ascii', + value: 'Gi0/1', + }, + ], + }; + + const built = buildOption82(config); + const parsed = parseOption82(built.hexEncoded); + + expect(parsed.suboptions[0].value).toBe('Gi0/1'); + }); + + it('should preserve multiple suboptions through round-trip', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'circuit-id', + format: 'ascii', + value: 'Gi0/1', + }, + { + type: 'remote-id', + format: 'ascii', + value: 'switch-1', + }, + ], + }; + + const built = buildOption82(config); + const parsed = parseOption82(built.hexEncoded); + + expect(parsed.suboptions).toHaveLength(2); + expect(parsed.suboptions[0].value).toBe('Gi0/1'); + expect(parsed.suboptions[1].value).toBe('switch-1'); + }); + }); + + describe('Edge cases', () => { + it('should handle empty suboption value', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'circuit-id', + format: 'ascii', + value: '', + }, + ], + }; + + const result = buildOption82(config); + expect(result.breakdown[0].length).toBe(0); + }); + + it('should handle special characters in ASCII', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'circuit-id', + format: 'ascii', + value: 'Port-1/2.3', + }, + ], + }; + + const result = buildOption82(config); + expect(result.breakdown[0].value).toBe('Port-1/2.3'); + }); + + it('should handle maximum VLAN ID', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'circuit-id', + format: 'vlan-id', + value: '4095', + }, + ], + }; + + const result = buildOption82(config); + expect(result.breakdown[0].hexValue).toBe('0fff'); + }); + + it('should handle minimum VLAN ID', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'circuit-id', + format: 'vlan-id', + value: '0', + }, + ], + }; + + const result = buildOption82(config); + expect(result.breakdown[0].hexValue).toBe('0000'); + }); + + it('should handle long ASCII values', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'remote-id', + format: 'ascii', + value: 'very-long-switch-hostname.example.com', + }, + ], + }; + + const result = buildOption82(config); + expect(result.breakdown[0].value).toBe('very-long-switch-hostname.example.com'); + expect(result.breakdown[0].length).toBeGreaterThan(10); + }); + }); + + describe('Description and metadata', () => { + it('should include descriptions for circuit-id', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'circuit-id', + format: 'ascii', + value: 'Gi0/1', + }, + ], + }; + + const result = buildOption82(config); + expect(result.breakdown[0].description).toBeDefined(); + expect(result.breakdown[0].description).toContain('circuit'); + }); + + it('should include descriptions for remote-id', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'remote-id', + format: 'ascii', + value: 'switch-1', + }, + ], + }; + + const result = buildOption82(config); + expect(result.breakdown[0].description).toBeDefined(); + expect(result.breakdown[0].description).toContain('relay'); + }); + + it('should generate ascii decoded summary', () => { + const config: Option82Config = { + suboptions: [ + { + type: 'circuit-id', + format: 'ascii', + value: 'Gi0/1', + }, + { + type: 'remote-id', + format: 'ascii', + value: 'sw1', + }, + ], + }; + + const result = buildOption82(config); + expect(result.asciiDecoded).toContain('circuit-id: Gi0/1'); + expect(result.asciiDecoded).toContain('remote-id: sw1'); + }); + }); +});