From 9c8efbeac021ea57f1ce5e65de403cc3084bcc9c Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Wed, 17 Sep 2025 22:59:09 +0100 Subject: [PATCH] Tests for contexts, routes, and mask conversions --- tests/unit/contexts/cidr.test.ts | 356 ++++++++++++++++ tests/unit/routes/cidr/mask-converter.test.ts | 388 ++++++++++++++++++ tests/unit/utils/mask-conversions.test.ts | 354 ++++++++++++++++ 3 files changed, 1098 insertions(+) create mode 100644 tests/unit/contexts/cidr.test.ts create mode 100644 tests/unit/routes/cidr/mask-converter.test.ts create mode 100644 tests/unit/utils/mask-conversions.test.ts diff --git a/tests/unit/contexts/cidr.test.ts b/tests/unit/contexts/cidr.test.ts new file mode 100644 index 0000000..9eb6bbd --- /dev/null +++ b/tests/unit/contexts/cidr.test.ts @@ -0,0 +1,356 @@ +import { describe, it, expect, beforeEach } from 'vitest'; +import { writable, get } from 'svelte/store'; +import { cidrToMask, maskToCidr } from '$lib/utils/ip-calculations'; +import { validateSubnetMask } from '$lib/utils/ip-validation'; +import { COMMON_SUBNETS } from '$lib/constants/networks'; + +describe('CIDR Context Logic', () => { + let cidr: any; + let mask: any; + let handleMaskChange: any; + let getSubnetInfo: any; + + beforeEach(() => { + // Recreate the context logic for testing + cidr = writable(24); + mask = writable('255.255.255.0'); + + // Keep mask in sync when CIDR changes + cidr.subscribe(($cidr: number) => { + const m = cidrToMask($cidr).octets.join('.'); + if (get(mask) !== m) mask.set(m); + }); + + // Mask changes -> attempt to update CIDR (only when valid) + handleMaskChange = (value: string) => { + mask.set(value); + if (validateSubnetMask(value).valid) { + const newCidr = maskToCidr(value); + if (get(cidr) !== newCidr) cidr.set(newCidr); + } + }; + + getSubnetInfo = (c: number) => { + const subnet = COMMON_SUBNETS.find((s) => s.cidr === c); + return subnet || { + cidr: c, + mask: cidrToMask(c).octets.join('.'), + hosts: Math.pow(2, 32 - c) - 2 + }; + }; + }); + + describe('CIDR to Mask synchronization', () => { + it('should update mask when CIDR changes', () => { + cidr.set(16); + expect(get(mask)).toBe('255.255.0.0'); + + cidr.set(8); + expect(get(mask)).toBe('255.0.0.0'); + + cidr.set(30); + expect(get(mask)).toBe('255.255.255.252'); + }); + + it('should handle edge CIDR values', () => { + cidr.set(0); + expect(get(mask)).toBe('0.0.0.0'); + + cidr.set(32); + expect(get(mask)).toBe('255.255.255.255'); + + cidr.set(1); + expect(get(mask)).toBe('128.0.0.0'); + + cidr.set(31); + expect(get(mask)).toBe('255.255.255.254'); + }); + + it('should handle all common subnet values', () => { + COMMON_SUBNETS.forEach(subnet => { + cidr.set(subnet.cidr); + expect(get(mask)).toBe(subnet.mask); + }); + }); + }); + + describe('Mask to CIDR synchronization', () => { + it('should update CIDR when valid mask changes', () => { + handleMaskChange('255.255.0.0'); + expect(get(cidr)).toBe(16); + + handleMaskChange('255.0.0.0'); + expect(get(cidr)).toBe(8); + + handleMaskChange('255.255.255.252'); + expect(get(cidr)).toBe(30); + }); + + it('should handle edge mask values', () => { + handleMaskChange('0.0.0.0'); + expect(get(cidr)).toBe(0); + + handleMaskChange('255.255.255.255'); + expect(get(cidr)).toBe(32); + + handleMaskChange('128.0.0.0'); + expect(get(cidr)).toBe(1); + + handleMaskChange('255.255.255.254'); + expect(get(cidr)).toBe(31); + }); + + it('should not update CIDR for invalid masks', () => { + const originalCidr = get(cidr); + + // Invalid IP format + handleMaskChange('256.255.255.0'); + expect(get(cidr)).toBe(originalCidr); + + // Non-contiguous mask + handleMaskChange('255.255.0.255'); + expect(get(cidr)).toBe(originalCidr); + + // Invalid format + handleMaskChange('invalid.mask'); + expect(get(cidr)).toBe(originalCidr); + + // Empty string + handleMaskChange(''); + expect(get(cidr)).toBe(originalCidr); + }); + + it('should update mask store even for invalid values', () => { + handleMaskChange('invalid.mask'); + expect(get(mask)).toBe('invalid.mask'); + + handleMaskChange('256.255.255.0'); + expect(get(mask)).toBe('256.255.255.0'); + }); + }); + + describe('getSubnetInfo function', () => { + it('should return common subnet info for known CIDR values', () => { + COMMON_SUBNETS.forEach(expectedSubnet => { + const result = getSubnetInfo(expectedSubnet.cidr); + expect(result).toEqual(expectedSubnet); + }); + }); + + it('should calculate info for uncommon CIDR values', () => { + const testCases = [ + { cidr: 12, expectedHosts: Math.pow(2, 32 - 12) - 2 }, + { cidr: 18, expectedHosts: Math.pow(2, 32 - 18) - 2 }, + { cidr: 22, expectedHosts: Math.pow(2, 32 - 22) - 2 }, + { cidr: 31, expectedHosts: Math.pow(2, 32 - 31) - 2 } + ]; + + testCases.forEach(({ cidr, expectedHosts }) => { + const result = getSubnetInfo(cidr); + expect(result.cidr).toBe(cidr); + expect(result.mask).toBe(cidrToMask(cidr).octets.join('.')); + expect(result.hosts).toBe(expectedHosts); + }); + }); + + it('should handle edge cases', () => { + const result0 = getSubnetInfo(0); + expect(result0.cidr).toBe(0); + expect(result0.mask).toBe('0.0.0.0'); + expect(result0.hosts).toBe(Math.pow(2, 32) - 2); + + const result32 = getSubnetInfo(32); + expect(result32.cidr).toBe(32); + expect(result32.mask).toBe('255.255.255.255'); + expect(result32.hosts).toBe(Math.pow(2, 0) - 2); // -1 effectively + }); + + it('should return consistent results', () => { + for (let i = 0; i < 10; i++) { + const result1 = getSubnetInfo(24); + const result2 = getSubnetInfo(24); + expect(result1).toEqual(result2); + } + }); + }); + + describe('Bidirectional synchronization', () => { + it('should maintain consistency during rapid changes', () => { + const testSequence = [8, 16, 24, 25, 26, 27, 28, 29, 30]; + + testSequence.forEach(targetCidr => { + // Change via CIDR + cidr.set(targetCidr); + const maskAfterCidrChange = get(mask); + + // Change back via mask + handleMaskChange(maskAfterCidrChange); + const cidrAfterMaskChange = get(cidr); + + expect(cidrAfterMaskChange).toBe(targetCidr); + expect(get(mask)).toBe(maskAfterCidrChange); + }); + }); + + it('should handle complex change sequences', () => { + // Start with known state + cidr.set(24); + expect(get(mask)).toBe('255.255.255.0'); + + // Change mask to different value + handleMaskChange('255.255.255.128'); + expect(get(cidr)).toBe(25); + + // Change CIDR to another value + cidr.set(26); + expect(get(mask)).toBe('255.255.255.192'); + + // Change mask back + handleMaskChange('255.255.255.0'); + expect(get(cidr)).toBe(24); + }); + + it('should recover from invalid mask input', () => { + cidr.set(24); + const originalMask = get(mask); + + // Enter invalid mask + handleMaskChange('invalid.mask'); + expect(get(mask)).toBe('invalid.mask'); + expect(get(cidr)).toBe(24); // Should remain unchanged + + // Enter valid mask + handleMaskChange('255.255.0.0'); + expect(get(cidr)).toBe(16); + expect(get(mask)).toBe('255.255.0.0'); + + // Return to original via CIDR + cidr.set(24); + expect(get(mask)).toBe(originalMask); + }); + }); + + describe('Real-world usage patterns', () => { + it('should handle user typing a mask gradually', () => { + const typingSequence = [ + '2', + '25', + '255', + '255.', + '255.2', + '255.25', + '255.255', + '255.255.', + '255.255.2', + '255.255.25', + '255.255.255', + '255.255.255.', + '255.255.255.0' + ]; + + let finalCidr = get(cidr); + + typingSequence.forEach((partialMask, index) => { + handleMaskChange(partialMask); + expect(get(mask)).toBe(partialMask); + + // Only the final complete mask should update CIDR + if (partialMask === '255.255.255.0') { + expect(get(cidr)).toBe(24); + finalCidr = 24; + } else { + // CIDR should remain unchanged for incomplete input + expect(get(cidr)).toBe(finalCidr); + } + }); + }); + + it('should handle common subnet selection workflow', () => { + // Simulate user clicking through common subnets + const commonCidrs = [24, 25, 26, 27, 28, 29, 30]; + + commonCidrs.forEach(selectedCidr => { + cidr.set(selectedCidr); + const expectedMask = COMMON_SUBNETS.find(s => s.cidr === selectedCidr)?.mask || + cidrToMask(selectedCidr).dotted; + expect(get(mask)).toBe(expectedMask); + + const subnetInfo = getSubnetInfo(selectedCidr); + expect(subnetInfo.cidr).toBe(selectedCidr); + expect(subnetInfo.mask).toBe(get(mask)); + }); + }); + + it('should handle slider input simulation', () => { + // Simulate dragging slider through values + for (let sliderValue = 0; sliderValue <= 32; sliderValue++) { + cidr.set(sliderValue); + const currentMask = get(mask); + + // Verify the mask is valid + const validation = validateSubnetMask(currentMask); + expect(validation.valid).toBe(true); + + // Verify round-trip conversion + const convertedBack = maskToCidr(currentMask); + expect(convertedBack).toBe(sliderValue); + } + }); + }); + + describe('State consistency and integrity', () => { + it('should maintain state integrity under stress', () => { + const operations = [ + () => cidr.set(Math.floor(Math.random() * 33)), + () => handleMaskChange('255.255.255.0'), + () => handleMaskChange('255.255.0.0'), + () => handleMaskChange('255.0.0.0'), + () => cidr.set(24), + () => cidr.set(16), + () => cidr.set(8) + ]; + + for (let i = 0; i < 100; i++) { + const operation = operations[Math.floor(Math.random() * operations.length)]; + operation(); + + // Verify state is always consistent + const currentCidr = get(cidr); + const currentMask = get(mask); + + expect(currentCidr).toBeGreaterThanOrEqual(0); + expect(currentCidr).toBeLessThanOrEqual(32); + + if (validateSubnetMask(currentMask).valid) { + const expectedCidr = maskToCidr(currentMask); + expect(currentCidr).toBe(expectedCidr); + } + + const expectedMask = cidrToMask(currentCidr).octets.join('.'); + if (validateSubnetMask(currentMask).valid) { + expect(currentMask).toBe(expectedMask); + } + } + }); + + it('should handle concurrent updates gracefully', () => { + // Simulate rapid concurrent updates + const updates = []; + for (let i = 0; i < 50; i++) { + updates.push(() => cidr.set(i % 33)); + updates.push(() => handleMaskChange(cidrToMask(i % 33).octets.join('.'))); + } + + // Execute all updates + updates.forEach(update => update()); + + // Verify final state is consistent + const finalCidr = get(cidr); + const finalMask = get(mask); + const expectedMask = cidrToMask(finalCidr).octets.join('.'); + + expect(finalMask).toBe(expectedMask); + expect(maskToCidr(finalMask)).toBe(finalCidr); + }); + }); +}); \ No newline at end of file diff --git a/tests/unit/routes/cidr/mask-converter.test.ts b/tests/unit/routes/cidr/mask-converter.test.ts new file mode 100644 index 0000000..6b9f732 --- /dev/null +++ b/tests/unit/routes/cidr/mask-converter.test.ts @@ -0,0 +1,388 @@ +import { describe, it, expect, beforeEach, vi } from 'vitest'; +import { render, screen, fireEvent } from '@testing-library/svelte'; +import { writable, get } from 'svelte/store'; +import { cidrToMask, maskToCidr } from '$lib/utils/ip-calculations'; +import { validateSubnetMask } from '$lib/utils/ip-validation'; +import { COMMON_SUBNETS } from '$lib/constants/networks'; + +// Mock the navigation functions +vi.mock('$app/navigation', () => ({ + goto: vi.fn() +})); + +vi.mock('$app/stores', () => ({ + page: writable({ + url: { + pathname: '/cidr/mask-converter/cidr-to-subnet-mask' + } + }) +})); + +describe('CIDR Mask Converter Integration', () => { + describe('Core Conversion Logic', () => { + it('should convert CIDR to subnet mask correctly', () => { + const testCases = [ + { cidr: 8, expected: '255.0.0.0' }, + { cidr: 16, expected: '255.255.0.0' }, + { cidr: 24, expected: '255.255.255.0' }, + { cidr: 25, expected: '255.255.255.128' }, + { cidr: 26, expected: '255.255.255.192' }, + { cidr: 27, expected: '255.255.255.224' }, + { cidr: 28, expected: '255.255.255.240' }, + { cidr: 29, expected: '255.255.255.248' }, + { cidr: 30, expected: '255.255.255.252' } + ]; + + testCases.forEach(({ cidr, expected }) => { + const result = cidrToMask(cidr); + expect(result.octets.join('.')).toBe(expected); + }); + }); + + it('should convert subnet mask to CIDR correctly', () => { + const testCases = [ + { mask: '255.0.0.0', expected: 8 }, + { mask: '255.255.0.0', expected: 16 }, + { mask: '255.255.255.0', expected: 24 }, + { mask: '255.255.255.128', expected: 25 }, + { mask: '255.255.255.192', expected: 26 }, + { mask: '255.255.255.224', expected: 27 }, + { mask: '255.255.255.240', expected: 28 }, + { mask: '255.255.255.248', expected: 29 }, + { mask: '255.255.255.252', expected: 30 } + ]; + + testCases.forEach(({ mask, expected }) => { + const result = maskToCidr(mask); + expect(result).toBe(expected); + }); + }); + }); + + describe('Context Integration', () => { + let cidr: any; + let mask: any; + let handleMaskChange: any; + let getSubnetInfo: any; + + beforeEach(() => { + // Setup the context logic + cidr = writable(24); + mask = writable('255.255.255.0'); + + cidr.subscribe(($cidr: number) => { + const m = cidrToMask($cidr).octets.join('.'); + if (get(mask) !== m) mask.set(m); + }); + + handleMaskChange = (value: string) => { + mask.set(value); + if (validateSubnetMask(value).valid) { + const newCidr = maskToCidr(value); + if (get(cidr) !== newCidr) cidr.set(newCidr); + } + }; + + getSubnetInfo = (c: number) => { + const subnet = COMMON_SUBNETS.find((s) => s.cidr === c); + return subnet || { + cidr: c, + mask: cidrToMask(c).octets.join('.'), + hosts: Math.pow(2, 32 - c) - 2 + }; + }; + }); + + it('should synchronize CIDR and mask bidirectionally', () => { + // Test CIDR to mask + cidr.set(16); + expect(get(mask)).toBe('255.255.0.0'); + + // Test mask to CIDR + handleMaskChange('255.255.255.128'); + expect(get(cidr)).toBe(25); + + // Test round trip + cidr.set(30); + expect(get(mask)).toBe('255.255.255.252'); + handleMaskChange(get(mask)); + expect(get(cidr)).toBe(30); + }); + + it('should handle invalid mask input gracefully', () => { + const originalCidr = get(cidr); + + // Test invalid masks + handleMaskChange('invalid.mask'); + expect(get(mask)).toBe('invalid.mask'); + expect(get(cidr)).toBe(originalCidr); // Should not change + + handleMaskChange('256.255.255.0'); + expect(get(mask)).toBe('256.255.255.0'); + expect(get(cidr)).toBe(originalCidr); // Should not change + + // Test non-contiguous mask + handleMaskChange('255.255.0.255'); + expect(get(mask)).toBe('255.255.0.255'); + expect(get(cidr)).toBe(originalCidr); // Should not change + }); + + it('should provide correct subnet information', () => { + // Test common subnets + COMMON_SUBNETS.forEach(subnet => { + const info = getSubnetInfo(subnet.cidr); + expect(info).toEqual(subnet); + }); + + // Test calculated subnet + const info = getSubnetInfo(22); + expect(info.cidr).toBe(22); + expect(info.mask).toBe('255.255.252.0'); + expect(info.hosts).toBe(1022); // 2^10 - 2 + }); + }); + + describe('Component State Management', () => { + it('should handle slider value changes correctly', () => { + const cidr = writable(24); + const mask = writable('255.255.255.0'); + + // Set up synchronization like in the actual component + cidr.subscribe(($cidr: number) => { + const m = cidrToMask($cidr).octets.join('.'); + if (get(mask) !== m) mask.set(m); + }); + + // Simulate slider changes + const testValues = [8, 16, 20, 24, 26, 28, 30]; + + testValues.forEach(value => { + cidr.set(value); + const expectedMask = cidrToMask(value).octets.join('.'); + + // Check that mask gets updated + expect(get(mask)).toBe(expectedMask); + }); + }); + + it('should validate mask input correctly', () => { + const validMasks = [ + '255.255.255.0', + '255.255.0.0', + '255.0.0.0', + '255.255.255.128', + '255.255.255.192' + ]; + + const invalidMasks = [ + 'invalid', + '256.255.255.0', + '255.255.0.255', + '255.255.255.127' + ]; + + validMasks.forEach(mask => { + const result = validateSubnetMask(mask); + expect(result.valid).toBe(true); + }); + + invalidMasks.forEach(mask => { + const result = validateSubnetMask(mask); + expect(result.valid).toBe(false); + }); + }); + }); + + describe('Common Subnet Selection', () => { + it('should handle common subnet button clicks', () => { + const cidr = writable(24); + const mask = writable('255.255.255.0'); + + // Sync mask when CIDR changes + cidr.subscribe(($cidr: number) => { + const m = cidrToMask($cidr).octets.join('.'); + if (get(mask) !== m) mask.set(m); + }); + + // Test clicking different common subnets + COMMON_SUBNETS.forEach(subnet => { + cidr.set(subnet.cidr); + expect(get(mask)).toBe(subnet.mask); + expect(get(cidr)).toBe(subnet.cidr); + }); + }); + + it('should show active state for current subnet', () => { + const currentCidr = 24; + const activeSubnet = COMMON_SUBNETS.find(s => s.cidr === currentCidr); + + expect(activeSubnet).toBeDefined(); + expect(activeSubnet?.cidr).toBe(24); + expect(activeSubnet?.mask).toBe('255.255.255.0'); + }); + }); + + describe('Performance and Edge Cases', () => { + it('should handle rapid value changes efficiently', () => { + const cidr = writable(24); + const mask = writable('255.255.255.0'); + + cidr.subscribe(($cidr: number) => { + const m = cidrToMask($cidr).octets.join('.'); + if (get(mask) !== m) mask.set(m); + }); + + const startTime = performance.now(); + + // Rapid changes + for (let i = 0; i < 1000; i++) { + const randomCidr = Math.floor(Math.random() * 33); + cidr.set(randomCidr); + expect(get(mask)).toBe(cidrToMask(randomCidr).octets.join('.')); + } + + const endTime = performance.now(); + expect(endTime - startTime).toBeLessThan(100); // Should be fast + }); + + it('should handle edge CIDR values correctly', () => { + const testCases = [ + { cidr: 0, mask: '0.0.0.0' }, + { cidr: 1, mask: '128.0.0.0' }, + { cidr: 31, mask: '255.255.255.254' }, + { cidr: 32, mask: '255.255.255.255' } + ]; + + testCases.forEach(({ cidr, mask }) => { + const result = cidrToMask(cidr); + expect(result.octets.join('.')).toBe(mask); + + const backToCidr = maskToCidr(mask); + expect(backToCidr).toBe(cidr); + }); + }); + }); + + describe('Host Calculation Accuracy', () => { + it('should calculate host counts correctly', () => { + const testCases = [ + { cidr: 24, expectedHosts: 254 }, + { cidr: 25, expectedHosts: 126 }, + { cidr: 26, expectedHosts: 62 }, + { cidr: 27, expectedHosts: 30 }, + { cidr: 28, expectedHosts: 14 }, + { cidr: 29, expectedHosts: 6 }, + { cidr: 30, expectedHosts: 2 } + ]; + + testCases.forEach(({ cidr, expectedHosts }) => { + const calculatedHosts = Math.pow(2, 32 - cidr) - 2; + expect(calculatedHosts).toBe(expectedHosts); + }); + }); + + it('should match COMMON_SUBNETS host counts', () => { + COMMON_SUBNETS.forEach(subnet => { + const calculatedHosts = Math.pow(2, 32 - subnet.cidr) - 2; + expect(calculatedHosts).toBe(subnet.hosts); + }); + }); + }); + + describe('Binary Representation Accuracy', () => { + it('should produce correct binary patterns for subnet masks', () => { + const testCases = [ + { + cidr: 24, + mask: '255.255.255.0', + binary: '11111111111111111111111100000000' + }, + { + cidr: 25, + mask: '255.255.255.128', + binary: '11111111111111111111111110000000' + }, + { + cidr: 26, + mask: '255.255.255.192', + binary: '11111111111111111111111111000000' + } + ]; + + testCases.forEach(({ cidr, mask, binary }) => { + const result = cidrToMask(cidr); + expect(result.octets.join('.')).toBe(mask); + + const actualBinary = result.octets + .map(octet => octet.toString(2).padStart(8, '0')) + .join(''); + expect(actualBinary).toBe(binary); + }); + }); + }); + + describe('Real-world Scenarios', () => { + it('should handle common network design scenarios', () => { + const scenarios = [ + { + name: 'Small office network', + cidr: 24, + mask: '255.255.255.0', + hosts: 254, + description: 'Typical small business network' + }, + { + name: 'Point-to-point link', + cidr: 30, + mask: '255.255.255.252', + hosts: 2, + description: 'Router-to-router connection' + }, + { + name: 'Large corporate segment', + cidr: 16, + mask: '255.255.0.0', + hosts: 65534, + description: 'Large department network' + }, + { + name: 'VLAN segment', + cidr: 26, + mask: '255.255.255.192', + hosts: 62, + description: 'Small department VLAN' + } + ]; + + scenarios.forEach(scenario => { + const maskResult = cidrToMask(scenario.cidr); + expect(maskResult.octets.join('.')).toBe(scenario.mask); + + const cidrResult = maskToCidr(scenario.mask); + expect(cidrResult).toBe(scenario.cidr); + + const hostCount = Math.pow(2, 32 - scenario.cidr) - 2; + expect(hostCount).toBe(scenario.hosts); + }); + }); + + it('should handle subnet planning workflow', () => { + // Simulate a network administrator planning subnets + const requirements = [ + { department: 'Engineering', requiredHosts: 100, selectedCidr: 25 }, // 126 hosts + { department: 'Sales', requiredHosts: 50, selectedCidr: 26 }, // 62 hosts + { department: 'Admin', requiredHosts: 20, selectedCidr: 27 }, // 30 hosts + { department: 'Guest', requiredHosts: 10, selectedCidr: 28 } // 14 hosts + ]; + + requirements.forEach(req => { + const availableHosts = Math.pow(2, 32 - req.selectedCidr) - 2; + expect(availableHosts).toBeGreaterThanOrEqual(req.requiredHosts); + + const mask = cidrToMask(req.selectedCidr).octets.join('.'); + const validation = validateSubnetMask(mask); + expect(validation.valid).toBe(true); + }); + }); + }); +}); \ No newline at end of file diff --git a/tests/unit/utils/mask-conversions.test.ts b/tests/unit/utils/mask-conversions.test.ts new file mode 100644 index 0000000..59c9d71 --- /dev/null +++ b/tests/unit/utils/mask-conversions.test.ts @@ -0,0 +1,354 @@ +import { describe, it, expect } from 'vitest'; +import { cidrToMask, maskToCidr } from '$lib/utils/ip-calculations'; +import { validateSubnetMask } from '$lib/utils/ip-validation'; + +describe('Mask Conversion Utilities', () => { + describe('cidrToMask', () => { + it('should convert common CIDR values to correct subnet masks', () => { + expect(cidrToMask(8).octets.join('.')).toBe('255.0.0.0'); + expect(cidrToMask(16).octets.join('.')).toBe('255.255.0.0'); + expect(cidrToMask(24).octets.join('.')).toBe('255.255.255.0'); + expect(cidrToMask(25).octets.join('.')).toBe('255.255.255.128'); + expect(cidrToMask(26).octets.join('.')).toBe('255.255.255.192'); + expect(cidrToMask(27).octets.join('.')).toBe('255.255.255.224'); + expect(cidrToMask(28).octets.join('.')).toBe('255.255.255.240'); + expect(cidrToMask(29).octets.join('.')).toBe('255.255.255.248'); + expect(cidrToMask(30).octets.join('.')).toBe('255.255.255.252'); + expect(cidrToMask(31).octets.join('.')).toBe('255.255.255.254'); + expect(cidrToMask(32).octets.join('.')).toBe('255.255.255.255'); + }); + + it('should handle edge cases', () => { + expect(cidrToMask(0).octets.join('.')).toBe('0.0.0.0'); + expect(cidrToMask(1).octets.join('.')).toBe('128.0.0.0'); + expect(cidrToMask(7).octets.join('.')).toBe('254.0.0.0'); + expect(cidrToMask(9).octets.join('.')).toBe('255.128.0.0'); + expect(cidrToMask(15).octets.join('.')).toBe('255.254.0.0'); + expect(cidrToMask(17).octets.join('.')).toBe('255.255.128.0'); + expect(cidrToMask(23).octets.join('.')).toBe('255.255.254.0'); + }); + + it('should return correct octets array', () => { + const mask24 = cidrToMask(24); + expect(mask24.octets).toEqual([255, 255, 255, 0]); + + const mask26 = cidrToMask(26); + expect(mask26.octets).toEqual([255, 255, 255, 192]); + + const mask30 = cidrToMask(30); + expect(mask30.octets).toEqual([255, 255, 255, 252]); + }); + + it('should handle all valid CIDR values (0-32)', () => { + for (let cidr = 0; cidr <= 32; cidr++) { + const result = cidrToMask(cidr); + expect(result).toBeDefined(); + expect(result.octets.join('.')).toMatch(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/); + expect(result.octets).toHaveLength(4); + expect(result.octets.every(octet => octet >= 0 && octet <= 255)).toBe(true); + } + }); + + it('should produce valid subnet masks', () => { + for (let cidr = 0; cidr <= 32; cidr++) { + const mask = cidrToMask(cidr); + const validation = validateSubnetMask(mask.octets.join('.')); + expect(validation.valid).toBe(true); + } + }); + }); + + describe('maskToCidr', () => { + it('should convert common subnet masks to correct CIDR values', () => { + expect(maskToCidr('255.0.0.0')).toBe(8); + expect(maskToCidr('255.255.0.0')).toBe(16); + expect(maskToCidr('255.255.255.0')).toBe(24); + expect(maskToCidr('255.255.255.128')).toBe(25); + expect(maskToCidr('255.255.255.192')).toBe(26); + expect(maskToCidr('255.255.255.224')).toBe(27); + expect(maskToCidr('255.255.255.240')).toBe(28); + expect(maskToCidr('255.255.255.248')).toBe(29); + expect(maskToCidr('255.255.255.252')).toBe(30); + expect(maskToCidr('255.255.255.254')).toBe(31); + expect(maskToCidr('255.255.255.255')).toBe(32); + }); + + it('should handle edge cases', () => { + expect(maskToCidr('0.0.0.0')).toBe(0); + expect(maskToCidr('128.0.0.0')).toBe(1); + expect(maskToCidr('254.0.0.0')).toBe(7); + expect(maskToCidr('255.128.0.0')).toBe(9); + expect(maskToCidr('255.254.0.0')).toBe(15); + expect(maskToCidr('255.255.128.0')).toBe(17); + expect(maskToCidr('255.255.254.0')).toBe(23); + }); + + it('should handle less common valid subnet masks', () => { + expect(maskToCidr('192.0.0.0')).toBe(2); + expect(maskToCidr('224.0.0.0')).toBe(3); + expect(maskToCidr('240.0.0.0')).toBe(4); + expect(maskToCidr('248.0.0.0')).toBe(5); + expect(maskToCidr('252.0.0.0')).toBe(6); + expect(maskToCidr('255.192.0.0')).toBe(10); + expect(maskToCidr('255.224.0.0')).toBe(11); + expect(maskToCidr('255.240.0.0')).toBe(12); + expect(maskToCidr('255.248.0.0')).toBe(13); + expect(maskToCidr('255.252.0.0')).toBe(14); + }); + + it('should return values in the valid CIDR range', () => { + const validMasks = [ + '0.0.0.0', '128.0.0.0', '192.0.0.0', '224.0.0.0', '240.0.0.0', + '248.0.0.0', '252.0.0.0', '254.0.0.0', '255.0.0.0', '255.128.0.0', + '255.192.0.0', '255.224.0.0', '255.240.0.0', '255.248.0.0', + '255.252.0.0', '255.254.0.0', '255.255.0.0', '255.255.128.0', + '255.255.192.0', '255.255.224.0', '255.255.240.0', '255.255.248.0', + '255.255.252.0', '255.255.254.0', '255.255.255.0', '255.255.255.128', + '255.255.255.192', '255.255.255.224', '255.255.255.240', + '255.255.255.248', '255.255.255.252', '255.255.255.254', '255.255.255.255' + ]; + + validMasks.forEach(mask => { + const cidr = maskToCidr(mask); + expect(cidr).toBeGreaterThanOrEqual(0); + expect(cidr).toBeLessThanOrEqual(32); + }); + }); + }); + + describe('validateSubnetMask', () => { + it('should validate correct subnet masks', () => { + const validMasks = [ + '255.255.255.0', + '255.255.255.128', + '255.255.255.192', + '255.255.255.224', + '255.255.255.240', + '255.255.255.248', + '255.255.255.252', + '255.255.255.254', + '255.255.255.255', + '255.255.0.0', + '255.0.0.0', + '0.0.0.0' + ]; + + validMasks.forEach(mask => { + const result = validateSubnetMask(mask); + expect(result.valid).toBe(true); + expect(result.error).toBeUndefined(); + }); + }); + + it('should reject invalid IP formats', () => { + const invalidFormats = [ + '256.255.255.0', + '255.256.255.0', + '255.255.256.0', + '255.255.255.256', + '255.255.255', + '255.255.255.0.0', + 'invalid.mask.format', + '255.255.255.-1', + '', + '...' + ]; + + invalidFormats.forEach(mask => { + const result = validateSubnetMask(mask); + expect(result.valid).toBe(false); + expect(result.error).toBeDefined(); + }); + }); + + it('should reject non-contiguous subnet masks', () => { + const nonContiguousMasks = [ + '255.255.0.255', // Gap in the middle + '255.254.255.0', // Gap in second octet + '255.0.255.0', // Gap in third octet + '128.255.255.0', // Gap in first octet + '255.255.255.128', // Actually valid, but let's test edge case + '255.255.255.254', // Valid + '255.255.255.127', // Invalid - not contiguous + '255.255.255.191', // Invalid - not contiguous + '255.255.255.63', // Invalid - not contiguous + '255.255.128.128', // Invalid - gap + '254.254.0.0', // Invalid - non-contiguous + '129.0.0.0' // Invalid - non-contiguous + ]; + + // Filter to only test the actually non-contiguous ones + const actuallyInvalid = [ + '255.255.0.255', + '255.254.255.0', + '255.0.255.0', + '128.255.255.0', + '255.255.255.127', + '255.255.255.191', + '255.255.255.63', + '255.255.128.128', + '254.254.0.0', + '129.0.0.0' + ]; + + actuallyInvalid.forEach(mask => { + const result = validateSubnetMask(mask); + expect(result.valid).toBe(false); + expect(result.error).toContain('contiguous'); + }); + }); + + it('should validate all generated CIDR-to-mask conversions', () => { + for (let cidr = 0; cidr <= 32; cidr++) { + const mask = cidrToMask(cidr); + const validation = validateSubnetMask(mask.octets.join('.')); + expect(validation.valid).toBe(true); + } + }); + }); + + describe('Round-trip conversions', () => { + it('should maintain accuracy in CIDR → Mask → CIDR conversions', () => { + for (let originalCidr = 0; originalCidr <= 32; originalCidr++) { + const mask = cidrToMask(originalCidr); + const convertedCidr = maskToCidr(mask.octets.join('.')); + expect(convertedCidr).toBe(originalCidr); + } + }); + + it('should maintain accuracy in Mask → CIDR → Mask conversions', () => { + const testMasks = [ + '0.0.0.0', + '128.0.0.0', + '255.0.0.0', + '255.255.0.0', + '255.255.255.0', + '255.255.255.128', + '255.255.255.192', + '255.255.255.224', + '255.255.255.240', + '255.255.255.248', + '255.255.255.252', + '255.255.255.254', + '255.255.255.255' + ]; + + testMasks.forEach(originalMask => { + const cidr = maskToCidr(originalMask); + const convertedMask = cidrToMask(cidr); + expect(convertedMask.octets.join('.')).toBe(originalMask); + }); + }); + }); + + describe('Binary representation accuracy', () => { + it('should produce masks with correct binary patterns', () => { + const testCases = [ + { cidr: 8, expectedBinary: '11111111000000000000000000000000' }, + { cidr: 16, expectedBinary: '11111111111111110000000000000000' }, + { cidr: 24, expectedBinary: '11111111111111111111111100000000' }, + { cidr: 25, expectedBinary: '11111111111111111111111110000000' }, + { cidr: 26, expectedBinary: '11111111111111111111111111000000' }, + { cidr: 30, expectedBinary: '11111111111111111111111111111100' } + ]; + + testCases.forEach(({ cidr, expectedBinary }) => { + const mask = cidrToMask(cidr); + const actualBinary = mask.octets + .map(octet => octet.toString(2).padStart(8, '0')) + .join(''); + expect(actualBinary).toBe(expectedBinary); + }); + }); + + it('should parse binary correctly in maskToCidr', () => { + const testMasks = [ + { mask: '255.0.0.0', expectedOnes: 8 }, + { mask: '255.255.0.0', expectedOnes: 16 }, + { mask: '255.255.255.0', expectedOnes: 24 }, + { mask: '255.255.255.128', expectedOnes: 25 }, + { mask: '255.255.255.192', expectedOnes: 26 }, + { mask: '255.255.255.252', expectedOnes: 30 } + ]; + + testMasks.forEach(({ mask, expectedOnes }) => { + const cidr = maskToCidr(mask); + expect(cidr).toBe(expectedOnes); + }); + }); + }); + + describe('Performance and stress tests', () => { + it('should handle rapid conversions efficiently', () => { + const startTime = performance.now(); + + for (let i = 0; i < 1000; i++) { + for (let cidr = 0; cidr <= 32; cidr++) { + const mask = cidrToMask(cidr); + const backToCidr = maskToCidr(mask.octets.join('.')); + expect(backToCidr).toBe(cidr); + } + } + + const endTime = performance.now(); + const duration = endTime - startTime; + + // Should complete 33,000 conversions in reasonable time (< 1 second) + expect(duration).toBeLessThan(1000); + }); + + it('should maintain consistency across multiple rapid calls', () => { + const results = new Set(); + + for (let i = 0; i < 100; i++) { + const mask24 = cidrToMask(24); + results.add(mask24.octets.join('.')); + } + + expect(results.size).toBe(1); + expect(results.has('255.255.255.0')).toBe(true); + }); + }); + + describe('Edge cases and error conditions', () => { + it('should handle boundary CIDR values correctly', () => { + // Test exact boundaries + expect(cidrToMask(0).octets.join('.')).toBe('0.0.0.0'); + expect(cidrToMask(32).octets.join('.')).toBe('255.255.255.255'); + + // Test near boundaries + expect(cidrToMask(1).octets.join('.')).toBe('128.0.0.0'); + expect(cidrToMask(31).octets.join('.')).toBe('255.255.255.254'); + }); + + it('should handle boundary subnet masks correctly', () => { + expect(maskToCidr('0.0.0.0')).toBe(0); + expect(maskToCidr('255.255.255.255')).toBe(32); + expect(maskToCidr('128.0.0.0')).toBe(1); + expect(maskToCidr('255.255.255.254')).toBe(31); + }); + + it('should validate complex subnet mask patterns', () => { + // Test various valid patterns + const complexValid = [ + '255.255.255.0', // Standard /24 + '255.255.254.0', // /23 + '255.255.252.0', // /22 + '255.255.248.0', // /21 + '255.255.240.0', // /20 + '255.255.224.0', // /19 + '255.255.192.0', // /18 + '255.255.128.0', // /17 + ]; + + complexValid.forEach(mask => { + const validation = validateSubnetMask(mask); + expect(validation.valid).toBe(true); + + const cidr = maskToCidr(mask); + const backToMask = cidrToMask(cidr); + expect(backToMask.octets.join('.')).toBe(mask); + }); + }); + }); +}); \ No newline at end of file