diff --git a/src/lib/components/furniture/SettingsPanel.svelte b/src/lib/components/furniture/SettingsPanel.svelte index 6d2d7f8..8527168 100644 --- a/src/lib/components/furniture/SettingsPanel.svelte +++ b/src/lib/components/furniture/SettingsPanel.svelte @@ -566,6 +566,10 @@ grid-column: span 2; } + .theme-option { + flex-direction: column; + } + @media (max-width: 768px) { &.theme-section, &.accessibility-section, @@ -622,7 +626,7 @@ .theme-option { display: flex; - flex-direction: column; + flex-direction: row; align-items: center; gap: var(--spacing-xs); padding: var(--spacing-sm); diff --git a/src/lib/components/tools/DHCPOption119Builder.svelte b/src/lib/components/tools/DHCPOption119Builder.svelte new file mode 100644 index 0000000..7fdd311 --- /dev/null +++ b/src/lib/components/tools/DHCPOption119Builder.svelte @@ -0,0 +1,894 @@ + + + + {#if mode === 'encode'} + ex.label} + getDescription={(ex) => ex.description} + selectedIndex={selectedExampleIndex} + /> + {:else} + ex.label} + getDescription={(ex) => ex.description} + selectedIndex={selectedExampleIndex} + /> + {/if} + + {#if mode === 'encode'} +
+
+

Domain List

+
+
+ {#each config.domains as _, i (`domain-${i}`)} +
+
+

+ Domain {i + 1} +

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

Validation Errors

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

Encoded Option 119

+ +
+
+

Hex-Encoded (Compact)

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

Wire Format (Spaced)

+ +
+
{result.wireFormat}
+
+ +
+
Total Length: {result.totalLength} bytes
+
Domains: {result.domainList.length}
+
+
+ {/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} + {:else} +
+
+

Decode Option 119 Hex

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

Validation Errors

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

Decoded Domain Search List

+ +
+
Total Length: {decodeResult.totalLength} bytes
+
Domains Found: {decodeResult.domains.length}
+
+ +
+

Domain List

+ {#each decodeResult.domains as domain, i (i)} +
+ + {domain} +
+ {/each} +
+
+ {/if} + {/if} +
+ + diff --git a/src/lib/constants/nav.ts b/src/lib/constants/nav.ts index 849a0ed..12e6bd1 100644 --- a/src/lib/constants/nav.ts +++ b/src/lib/constants/nav.ts @@ -917,6 +917,36 @@ export const SUB_NAV: Record = { 'infrastructure', ], }, + { + href: makePath('/dhcp/options/option119-domain-search'), + label: 'Option 119 - Domain Search', + description: + 'Encode and decode Domain Search List (RFC 3397/6731) to/from RFC 1035 wire format with domain compression', + icon: 'globe', + keywords: [ + 'dhcp', + 'option', + '119', + 'domain', + 'search', + 'list', + 'rfc', + '3397', + '6731', + '1035', + 'wire', + 'format', + 'compression', + 'encoder', + 'decoder', + 'isc', + 'kea', + 'dns', + 'suffix', + 'client', + 'configuration', + ], + }, ], '/dns': [ { diff --git a/src/lib/utils/dhcp-option119.ts b/src/lib/utils/dhcp-option119.ts new file mode 100644 index 0000000..9ba7420 --- /dev/null +++ b/src/lib/utils/dhcp-option119.ts @@ -0,0 +1,345 @@ +/** + * DHCP Option 119 - Domain Search List (RFC 3397 / RFC 6731) + * Utilities for encoding/decoding domain names to/from RFC 1035 wire format + */ + +export interface DomainSearchConfig { + domains: string[]; + network?: { + subnet?: string; + netmask?: string; + rangeStart?: string; + rangeEnd?: string; + }; +} + +export interface DomainSearchResult { + hexEncoded: string; + wireFormat: string; + domainList: string[]; + totalLength: number; + examples: { + iscDhcpd?: string; + keaDhcp4?: string; + }; +} + +export interface ParsedDomainSearch { + domains: string[]; + totalLength: number; + rawHex: string; +} + +/** + * Encode domain name to RFC 1035 wire format (without compression) + * Example: "example.com" -> 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00 + * Note: This function is kept for reference but not currently used. + * The encodeDomainsWithCompression function is used instead for better efficiency. + */ +function _encodeDomainName(domain: string): number[] { + const labels = domain.split('.'); + const bytes: number[] = []; + + for (const label of labels) { + if (label.length === 0) continue; + if (label.length > 63) { + throw new Error(`Label "${label}" exceeds maximum length of 63 characters`); + } + + // Length byte + bytes.push(label.length); + + // Label characters + for (let i = 0; i < label.length; i++) { + bytes.push(label.charCodeAt(i)); + } + } + + // Terminating null byte + bytes.push(0); + + return bytes; +} + +/** + * Encode multiple domains with compression for common suffixes + */ +function encodeDomainsWithCompression(domains: string[]): number[] { + const bytes: number[] = []; + const domainOffsets: Map = new Map(); + + for (const domain of domains) { + const labels = domain.split('.'); + let currentOffset = bytes.length; + + for (let i = 0; i < labels.length; i++) { + const suffix = labels.slice(i).join('.'); + const existingOffset = domainOffsets.get(suffix); + + if (existingOffset !== undefined && existingOffset < 16384) { + // Use compression pointer (0xC0 | high bits, low bits) + const pointer = 0xc000 | existingOffset; + bytes.push((pointer >> 8) & 0xff); + bytes.push(pointer & 0xff); + break; + } else { + // Store this suffix offset for future compression + domainOffsets.set(suffix, currentOffset); + + const label = labels[i]; + if (label.length > 63) { + throw new Error(`Label "${label}" exceeds maximum length of 63 characters`); + } + + // Length byte + bytes.push(label.length); + currentOffset++; + + // Label characters + for (let j = 0; j < label.length; j++) { + bytes.push(label.charCodeAt(j)); + currentOffset++; + } + + // If this was the last label, add terminating null + if (i === labels.length - 1) { + bytes.push(0); + } + } + } + } + + return bytes; +} + +/** + * Parse RFC 1035 wire format to domain names + */ +function parseWireFormat(bytes: number[]): string[] { + const domains: string[] = []; + let offset = 0; + + while (offset < bytes.length) { + const domain = parseDomainAtOffset(bytes, offset); + if (domain.name === '') break; + + domains.push(domain.name); + offset = domain.nextOffset; + } + + return domains; +} + +interface ParsedDomain { + name: string; + nextOffset: number; +} + +function parseDomainAtOffset(bytes: number[], startOffset: number): ParsedDomain { + const labels: string[] = []; + let offset = startOffset; + const visited = new Set(); + + while (offset < bytes.length) { + if (visited.has(offset)) { + throw new Error('Compression pointer loop detected'); + } + visited.add(offset); + + const length = bytes[offset]; + + // Check for compression pointer (top 2 bits set: 11xxxxxx) + if ((length & 0xc0) === 0xc0) { + if (offset + 1 >= bytes.length) { + throw new Error('Invalid compression pointer'); + } + const pointer = ((length & 0x3f) << 8) | bytes[offset + 1]; + const compressed = parseDomainAtOffset(bytes, pointer); + labels.push(compressed.name); + return { + name: labels.join('.'), + nextOffset: offset + 2, + }; + } + + // Terminating null byte + if (length === 0) { + return { + name: labels.join('.'), + nextOffset: offset + 1, + }; + } + + // Regular label + if (offset + 1 + length > bytes.length) { + throw new Error('Label extends beyond data'); + } + + let label = ''; + for (let i = 1; i <= length; i++) { + label += String.fromCharCode(bytes[offset + i]); + } + labels.push(label); + offset += length + 1; + } + + return { + name: labels.join('.'), + nextOffset: offset, + }; +} + +/** + * Build Option 119 hex string from domain list + */ +export function buildOption119(config: DomainSearchConfig): DomainSearchResult { + // Validate domains + for (const domain of config.domains) { + if (!domain.trim()) { + throw new Error('Empty domain name not allowed'); + } + if (!/^[a-zA-Z0-9.-]+$/.test(domain)) { + throw new Error(`Invalid domain name: ${domain}`); + } + // Check for leading/trailing dots or consecutive dots + if (domain.startsWith('.') || domain.endsWith('.') || domain.includes('..')) { + throw new Error(`Invalid domain name format: ${domain}`); + } + } + + const bytes = encodeDomainsWithCompression(config.domains); + const hexEncoded = bytes.map((b) => b.toString(16).padStart(2, '0')).join(''); + const wireFormat = bytes.map((b) => b.toString(16).padStart(2, '0')).join(' '); + + const examples = generateExamples(config, hexEncoded); + + return { + hexEncoded, + wireFormat, + domainList: config.domains, + totalLength: bytes.length, + examples, + }; +} + +/** + * Parse Option 119 hex string + */ +export function parseOption119(hexString: string): ParsedDomainSearch { + 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)); + } + + try { + const domains = parseWireFormat(bytes); + + return { + domains, + totalLength: bytes.length, + rawHex: cleanHex, + }; + } catch (error) { + if (error instanceof Error) { + throw new Error(`Invalid wire format: ${error.message}`); + } + throw new Error('Invalid wire format data'); + } +} + +/** + * Generate configuration examples for different DHCP servers + */ +function generateExamples(config: DomainSearchConfig, hexEncoded: string): DomainSearchResult['examples'] { + const examples: DomainSearchResult['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('# Domain Search List (Option 119)'); + iscLines.push('# RFC 3397 - domain search list for DHCP clients'); + iscLines.push(''); + iscLines.push(`subnet ${subnet} netmask ${netmask} {`); + iscLines.push(` range ${rangeStart} ${rangeEnd};`); + iscLines.push(' '); + iscLines.push(' # Specify domain search list'); + iscLines.push(` option domain-search "${config.domains.join('", "')}";`); + iscLines.push(' '); + iscLines.push(' # Alternative: Hex format'); + iscLines.push(` # option domain-search ${hexEncoded.match(/.{1,2}/g)?.join(':')};`); + iscLines.push('}'); + + examples.iscDhcpd = iscLines.join('\n'); + + // Kea DHCPv4 example + const keaLines: string[] = []; + keaLines.push('// Domain Search List (Option 119)'); + keaLines.push('// RFC 3397 - domain search list configuration'); + 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(' ],'); + keaLines.push(' "option-data": ['); + keaLines.push(' {'); + keaLines.push(' "name": "domain-search",'); + keaLines.push(' "code": 119,'); + keaLines.push(` "data": "${config.domains.join(', ')}"`); + keaLines.push(' }'); + keaLines.push(' ]'); + keaLines.push(' }'); + keaLines.push(' ]'); + keaLines.push(' }'); + keaLines.push('}'); + + examples.keaDhcp4 = keaLines.join('\n'); + + return examples; +} + +/** + * Get default config + */ +export function getDefaultOption119Config(): DomainSearchConfig { + return { + domains: ['example.com', 'corp.example.com'], + }; +} + +/** + * Common domain search examples + */ +export const DOMAIN_SEARCH_EXAMPLES = [ + { label: 'Corporate', domains: ['corp.example.com', 'example.com'] }, + { label: 'Multi-site', domains: ['site1.example.com', 'site2.example.com', 'example.com'] }, + { label: 'Development', domains: ['dev.example.com', 'staging.example.com', 'example.com'] }, +]; diff --git a/src/routes/dhcp/+page.svelte b/src/routes/dhcp/+page.svelte new file mode 100644 index 0000000..4d6913b --- /dev/null +++ b/src/routes/dhcp/+page.svelte @@ -0,0 +1,41 @@ + + +
+ + + +
+ + diff --git a/src/routes/dhcp/options/option119-domain-search/+page.svelte b/src/routes/dhcp/options/option119-domain-search/+page.svelte new file mode 100644 index 0000000..09b3f7b --- /dev/null +++ b/src/routes/dhcp/options/option119-domain-search/+page.svelte @@ -0,0 +1,5 @@ + + + diff --git a/src/styles/base.scss b/src/styles/base.scss index e42d07a..767cb3c 100644 --- a/src/styles/base.scss +++ b/src/styles/base.scss @@ -184,6 +184,9 @@ pre { display: flex; align-items: center; justify-content: center; + .icon { + display: flex; + } } .logo-icon svg { diff --git a/tests/unit/utils/dhcp-option119.test.ts b/tests/unit/utils/dhcp-option119.test.ts new file mode 100644 index 0000000..35133ce --- /dev/null +++ b/tests/unit/utils/dhcp-option119.test.ts @@ -0,0 +1,483 @@ +import { describe, it, expect } from 'vitest'; +import { + buildOption119, + parseOption119, + getDefaultOption119Config, + DOMAIN_SEARCH_EXAMPLES, + type DomainSearchConfig, +} from '../../../src/lib/utils/dhcp-option119'; + +describe('dhcp-option119.ts', () => { + describe('buildOption119', () => { + describe('basic encoding', () => { + it('encodes a single domain without compression', () => { + const config: DomainSearchConfig = { + domains: ['example.com'], + }; + + const result = buildOption119(config); + + // "example.com" -> 07 65 78 61 6d 70 6c 65 03 63 6f 6d 00 + expect(result.hexEncoded).toBe('076578616d706c6503636f6d00'); + expect(result.domainList).toEqual(['example.com']); + expect(result.totalLength).toBe(13); + }); + + it('encodes multiple domains with compression', () => { + const config: DomainSearchConfig = { + domains: ['corp.example.com', 'example.com'], + }; + + const result = buildOption119(config); + + // First domain: "corp.example.com" fully encoded + // Second domain: uses compression pointer to "example.com" + expect(result.hexEncoded).toBe('04636f7270076578616d706c6503636f6d00c005'); + expect(result.domainList).toEqual(['corp.example.com', 'example.com']); + }); + + it('encodes complex multi-site domains', () => { + const config: DomainSearchConfig = { + domains: ['site1.example.com', 'site2.example.com', 'example.com'], + }; + + const result = buildOption119(config); + + // All three domains with appropriate compression + expect(result.hexEncoded).toBe('057369746531076578616d706c6503636f6d00057369746532c006c006'); + expect(result.domainList).toEqual(['site1.example.com', 'site2.example.com', 'example.com']); + }); + + it('provides wire format with spaces', () => { + const config: DomainSearchConfig = { + domains: ['example.com'], + }; + + const result = buildOption119(config); + + expect(result.wireFormat).toBe('07 65 78 61 6d 70 6c 65 03 63 6f 6d 00'); + }); + }); + + describe('validation', () => { + it('rejects empty domain names', () => { + const config: DomainSearchConfig = { + domains: [''], + }; + + expect(() => buildOption119(config)).toThrow('Empty domain name not allowed'); + }); + + it('rejects domains with invalid characters', () => { + const config: DomainSearchConfig = { + domains: ['example@test.com'], + }; + + expect(() => buildOption119(config)).toThrow('Invalid domain name'); + }); + + it('rejects domains with leading dots', () => { + const config: DomainSearchConfig = { + domains: ['.example.com'], + }; + + expect(() => buildOption119(config)).toThrow('Invalid domain name format'); + }); + + it('rejects domains with trailing dots', () => { + const config: DomainSearchConfig = { + domains: ['example.com.'], + }; + + expect(() => buildOption119(config)).toThrow('Invalid domain name format'); + }); + + it('rejects domains with consecutive dots', () => { + const config: DomainSearchConfig = { + domains: ['example..com'], + }; + + expect(() => buildOption119(config)).toThrow('Invalid domain name format'); + }); + + it('rejects labels longer than 63 characters', () => { + const longLabel = 'a'.repeat(64); + const config: DomainSearchConfig = { + domains: [`${longLabel}.example.com`], + }; + + expect(() => buildOption119(config)).toThrow('exceeds maximum length of 63 characters'); + }); + + it('accepts labels with exactly 63 characters', () => { + const maxLabel = 'a'.repeat(63); + const config: DomainSearchConfig = { + domains: [`${maxLabel}.com`], + }; + + const result = buildOption119(config); + expect(result.domainList).toEqual([`${maxLabel}.com`]); + }); + + it('accepts valid domain names with hyphens', () => { + const config: DomainSearchConfig = { + domains: ['my-domain.example-site.com'], + }; + + const result = buildOption119(config); + expect(result.domainList).toEqual(['my-domain.example-site.com']); + }); + + it('accepts valid domain names with numbers', () => { + const config: DomainSearchConfig = { + domains: ['site123.example.com'], + }; + + const result = buildOption119(config); + expect(result.domainList).toEqual(['site123.example.com']); + }); + }); + + describe('configuration examples', () => { + it('generates ISC dhcpd configuration', () => { + const config: DomainSearchConfig = { + domains: ['example.com', 'corp.example.com'], + }; + + const result = buildOption119(config); + + expect(result.examples.iscDhcpd).toBeDefined(); + expect(result.examples.iscDhcpd).toContain('option domain-search'); + expect(result.examples.iscDhcpd).toContain('example.com'); + expect(result.examples.iscDhcpd).toContain('corp.example.com'); + }); + + it('generates Kea DHCP configuration', () => { + const config: DomainSearchConfig = { + domains: ['example.com', 'corp.example.com'], + }; + + const result = buildOption119(config); + + expect(result.examples.keaDhcp4).toBeDefined(); + expect(result.examples.keaDhcp4).toContain('"name": "domain-search"'); + expect(result.examples.keaDhcp4).toContain('"code": 119'); + expect(result.examples.keaDhcp4).toContain('example.com'); + }); + + it('uses default network settings when not provided', () => { + const config: DomainSearchConfig = { + domains: ['example.com'], + }; + + const result = buildOption119(config); + + expect(result.examples.iscDhcpd).toContain('192.168.1.0'); + expect(result.examples.iscDhcpd).toContain('255.255.255.0'); + expect(result.examples.iscDhcpd).toContain('192.168.1.100'); + expect(result.examples.iscDhcpd).toContain('192.168.1.200'); + }); + + it('uses custom network settings when provided', () => { + const config: DomainSearchConfig = { + domains: ['example.com'], + network: { + subnet: '10.0.0.0', + netmask: '255.255.255.0', + rangeStart: '10.0.0.50', + rangeEnd: '10.0.0.150', + }, + }; + + const result = buildOption119(config); + + expect(result.examples.iscDhcpd).toContain('10.0.0.0'); + expect(result.examples.iscDhcpd).toContain('255.255.255.0'); + expect(result.examples.iscDhcpd).toContain('10.0.0.50'); + expect(result.examples.iscDhcpd).toContain('10.0.0.150'); + }); + + it('calculates correct CIDR notation in Kea config', () => { + const config: DomainSearchConfig = { + domains: ['example.com'], + network: { + subnet: '10.0.0.0', + netmask: '255.255.255.0', + }, + }; + + const result = buildOption119(config); + + expect(result.examples.keaDhcp4).toContain('"subnet": "10.0.0.0/24"'); + }); + }); + + describe('compression pointer generation', () => { + it('generates correct compression pointers for shared suffixes', () => { + const config: DomainSearchConfig = { + domains: ['corp.example.com', 'example.com'], + }; + + const result = buildOption119(config); + + // The hex should end with c005 (compression pointer to offset 5) + expect(result.hexEncoded).toContain('c005'); + }); + + it('reuses compression pointers for multiple matches', () => { + const config: DomainSearchConfig = { + domains: ['site1.example.com', 'site2.example.com', 'example.com'], + }; + + const result = buildOption119(config); + + // Should have multiple compression pointers + expect(result.hexEncoded).toContain('c006'); + }); + + it('handles no compression for unrelated domains', () => { + const config: DomainSearchConfig = { + domains: ['example.com', 'another.org'], + }; + + const result = buildOption119(config); + + // Both domains encoded fully, no compression pointers + expect(result.hexEncoded).toBe('076578616d706c6503636f6d0007616e6f74686572036f726700'); + }); + }); + }); + + describe('parseOption119', () => { + describe('basic decoding', () => { + it('decodes a single domain without compression', () => { + const hexInput = '076578616d706c6503636f6d00'; + + const result = parseOption119(hexInput); + + expect(result.domains).toEqual(['example.com']); + expect(result.totalLength).toBe(13); + expect(result.rawHex).toBe(hexInput); + }); + + it('decodes multiple domains with compression', () => { + const hexInput = '04636f7270076578616d706c6503636f6d00c005'; + + const result = parseOption119(hexInput); + + expect(result.domains).toEqual(['corp.example.com', 'example.com']); + }); + + it('decodes complex multi-site domains', () => { + const hexInput = '057369746531076578616d706c6503636f6d00057369746532c006c006'; + + const result = parseOption119(hexInput); + + expect(result.domains).toEqual(['site1.example.com', 'site2.example.com', 'example.com']); + }); + + it('handles hex input with spaces', () => { + const hexInput = '07 65 78 61 6d 70 6c 65 03 63 6f 6d 00'; + + const result = parseOption119(hexInput); + + expect(result.domains).toEqual(['example.com']); + }); + + it('handles hex input with colons', () => { + const hexInput = '07:65:78:61:6d:70:6c:65:03:63:6f:6d:00'; + + const result = parseOption119(hexInput); + + expect(result.domains).toEqual(['example.com']); + }); + + it('handles mixed case hex input', () => { + const hexInput = '076578616D706C6503636F6D00'; + + const result = parseOption119(hexInput); + + expect(result.domains).toEqual(['example.com']); + }); + }); + + describe('error handling', () => { + it('rejects empty hex string', () => { + expect(() => parseOption119('')).toThrow('Empty hex string'); + }); + + it('rejects hex string with odd number of characters', () => { + expect(() => parseOption119('076578616d706c6503636f6d0')).toThrow('odd number of characters'); + }); + + it('rejects invalid compression pointer', () => { + // Compression pointer at the end without second byte + const hexInput = '076578616d706c6503636f6d00c0'; + + expect(() => parseOption119(hexInput)).toThrow('Invalid wire format'); + }); + + it('detects compression pointer loops', () => { + // Create a loop: pointer points back to itself or creates a cycle + // This is a malformed case + const hexInput = 'c000'; // Points to offset 0 (itself) + + expect(() => parseOption119(hexInput)).toThrow('Invalid wire format'); + }); + + it('detects label extending beyond data', () => { + // Label length says 10 bytes but only 5 remain + const hexInput = '0a6578616d706c'; // Length 10, but only 7 bytes follow + + expect(() => parseOption119(hexInput)).toThrow('Invalid wire format'); + }); + }); + + describe('round-trip encoding/decoding', () => { + it('maintains data integrity for single domain', () => { + const config: DomainSearchConfig = { + domains: ['example.com'], + }; + + const encoded = buildOption119(config); + const decoded = parseOption119(encoded.hexEncoded); + + expect(decoded.domains).toEqual(config.domains); + }); + + it('maintains data integrity for multiple domains', () => { + const config: DomainSearchConfig = { + domains: ['corp.example.com', 'example.com', 'dev.example.com'], + }; + + const encoded = buildOption119(config); + const decoded = parseOption119(encoded.hexEncoded); + + expect(decoded.domains).toEqual(config.domains); + }); + + it('maintains data integrity for complex scenarios', () => { + const config: DomainSearchConfig = { + domains: ['site1.example.com', 'site2.example.com', 'example.com', 'another.org'], + }; + + const encoded = buildOption119(config); + const decoded = parseOption119(encoded.hexEncoded); + + expect(decoded.domains).toEqual(config.domains); + }); + + it('handles domains with hyphens and numbers', () => { + const config: DomainSearchConfig = { + domains: ['my-domain-123.test-site.com', 'test-site.com'], + }; + + const encoded = buildOption119(config); + const decoded = parseOption119(encoded.hexEncoded); + + expect(decoded.domains).toEqual(config.domains); + }); + }); + }); + + describe('getDefaultOption119Config', () => { + it('returns default configuration with sample domains', () => { + const config = getDefaultOption119Config(); + + expect(config.domains).toBeDefined(); + expect(config.domains.length).toBeGreaterThan(0); + expect(config.domains).toContain('example.com'); + }); + + it('returns valid configuration that can be encoded', () => { + const config = getDefaultOption119Config(); + + expect(() => buildOption119(config)).not.toThrow(); + }); + }); + + describe('DOMAIN_SEARCH_EXAMPLES', () => { + it('provides multiple example configurations', () => { + expect(DOMAIN_SEARCH_EXAMPLES.length).toBeGreaterThan(0); + }); + + it('each example has required properties', () => { + for (const example of DOMAIN_SEARCH_EXAMPLES) { + expect(example.label).toBeDefined(); + expect(example.domains).toBeDefined(); + expect(example.domains.length).toBeGreaterThan(0); + } + }); + + it('all examples can be successfully encoded', () => { + for (const example of DOMAIN_SEARCH_EXAMPLES) { + const config: DomainSearchConfig = { + domains: example.domains, + }; + + expect(() => buildOption119(config)).not.toThrow(); + } + }); + + it('all examples maintain round-trip integrity', () => { + for (const example of DOMAIN_SEARCH_EXAMPLES) { + const config: DomainSearchConfig = { + domains: example.domains, + }; + + const encoded = buildOption119(config); + const decoded = parseOption119(encoded.hexEncoded); + + expect(decoded.domains).toEqual(example.domains); + } + }); + }); + + describe('edge cases', () => { + it('handles single-character labels', () => { + const config: DomainSearchConfig = { + domains: ['a.b.c'], + }; + + const result = buildOption119(config); + const decoded = parseOption119(result.hexEncoded); + + expect(decoded.domains).toEqual(['a.b.c']); + }); + + it('handles domains with many labels', () => { + const config: DomainSearchConfig = { + domains: ['sub1.sub2.sub3.sub4.example.com'], + }; + + const result = buildOption119(config); + const decoded = parseOption119(result.hexEncoded); + + expect(decoded.domains).toEqual(['sub1.sub2.sub3.sub4.example.com']); + }); + + it('handles mixed alphanumeric domains', () => { + const config: DomainSearchConfig = { + domains: ['srv01.dc02.region3.example.com'], + }; + + const result = buildOption119(config); + const decoded = parseOption119(result.hexEncoded); + + expect(decoded.domains).toEqual(['srv01.dc02.region3.example.com']); + }); + + it('handles compression with partial suffix matches', () => { + const config: DomainSearchConfig = { + domains: ['www.example.com', 'mail.example.com', 'ftp.example.com', 'example.com'], + }; + + const result = buildOption119(config); + const decoded = parseOption119(result.hexEncoded); + + expect(decoded.domains).toEqual(config.domains); + // Verify compression is actually happening (should be shorter than uncompressed) + expect(result.totalLength).toBeLessThan(60); // Would be ~60 bytes uncompressed + }); + }); +});