ipaddr.test.ts
This file
Path
src/utils/ipaddr.test.ts
149 lines · 5.6 KiB · TypeScript
Last changed by
fix(utils/ipaddr): avoid truncation on embedded IPv4 addresses in expandIPv6 (#5247)Nanasibefore adoptionc91ec9b
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 1–29why, one step back1import {2 convertIPv4BinaryToString,3 convertIPv4ToBinary,4 convertIPv6BinaryToString,5 convertIPv6ToBinary,6 distinctRemoteAddr,7 expandIPv6,8 INVALID_IP_ADDRESS_ERROR_CODE,9} from './ipaddr'1011const expectInvalidIPAddressError = (fn: () => unknown) => {12 try {13 fn()14 } catch (error) {15 expect(error).toBeInstanceOf(TypeError)16 expect(error).toHaveProperty('code', INVALID_IP_ADDRESS_ERROR_CODE)17 return18 }19 throw new Error('Expected invalid IP address error')20}2122describe('expandIPv6', () => {23 it('Should result be valid', () => {24 expect(expandIPv6('1::1')).toBe('0001:0000:0000:0000:0000:0000:0000:0001')25 expect(expandIPv6('::1')).toBe('0000:0000:0000:0000:0000:0000:0000:0001')26 expect(expandIPv6('2001:2::')).toBe('2001:0002:0000:0000:0000:0000:0000:0000')27 expect(expandIPv6('2001:2::')).toBe('2001:0002:0000:0000:0000:0000:0000:0000')28 expect(expandIPv6('2001:0:0:db8::1')).toBe('2001:0000:0000:0db8:0000:0000:0000:0001')29 expect(expandIPv6('::ffff:127.0.0.1')).toBe('0000:0000:0000:0000:0000:ffff:7f00:0001')fix(utils/ipaddr): avoid truncation on embedded IPv4 addresses in expandIPv6 (#5247)Nanasibefore adoptionc91ec9b
lines 30–32why, one step back30 expect(expandIPv6('::ffff:0.0.0.1')).toBe('0000:0000:0000:0000:0000:ffff:0000:0001')31 expect(expandIPv6('2001:db8:1:2:3:4:0.0.0.1')).toBe('2001:0db8:0001:0002:0003:0004:0000:0001')32 expect(expandIPv6('::0.0.0.0')).toBe('0000:0000:0000:0000:0000:0000:0000:0000')Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 33–149why, one step back33 })3435 it('Should expand the unspecified address "::" to eight zero groups', () => {36 expect(expandIPv6('::')).toBe('0000:0000:0000:0000:0000:0000:0000:0000')37 })38})39describe('distinctRemoteAddr', () => {40 it('Should result be valid', () => {41 expect(distinctRemoteAddr('1::1')).toBe('IPv6')42 expect(distinctRemoteAddr('::1')).toBe('IPv6')43 expect(distinctRemoteAddr('::ffff:127.0.0.1')).toBe('IPv6')4445 expect(distinctRemoteAddr('192.168.2.0')).toBe('IPv4')46 expect(distinctRemoteAddr('192.168.2.0')).toBe('IPv4')4748 expect(distinctRemoteAddr('example.com')).toBeUndefined()49 })5051 it('Should reject invalid IPv4 addresses with octets > 255', () => {52 expect(distinctRemoteAddr('1.2.3.256')).toBeUndefined()53 expect(distinctRemoteAddr('1.2.3.999')).toBeUndefined()54 expect(distinctRemoteAddr('1.2.2.355')).toBeUndefined()55 expect(distinctRemoteAddr('256.0.0.1')).toBeUndefined()56 expect(distinctRemoteAddr('999.999.999.999')).toBeUndefined()57 })5859 it('Should accept valid IPv4 edge cases', () => {60 expect(distinctRemoteAddr('0.0.0.0')).toBe('IPv4')61 expect(distinctRemoteAddr('255.255.255.255')).toBe('IPv4')62 expect(distinctRemoteAddr('1.2.3.4')).toBe('IPv4')63 })64})6566describe('convertIPv4ToBinary', () => {67 it('Should result is valid', () => {68 expect(convertIPv4ToBinary('0.0.0.0')).toBe(0n)69 expect(convertIPv4ToBinary('0.0.0.1')).toBe(1n)7071 expect(convertIPv4ToBinary('0.0.1.0')).toBe(1n << 8n)72 })7374 test.each(['1.2.3.256', '1.2.3', '1.2.3.4.5', '1..3.4', '01.2.3.4', 'a.b.c.d'])(75 'Should throw for invalid IPv4: %s',76 (input) => {77 expectInvalidIPAddressError(() => convertIPv4ToBinary(input))78 }79 )80})8182describe('convertIPv4ToString', () => {83 // add tons of test cases here84 test.each`85 input | expected86 ${'0.0.0.0'} | ${'0.0.0.0'}87 ${'0.0.0.1'} | ${'0.0.0.1'}88 ${'0.0.1.0'} | ${'0.0.1.0'}89 `('convertIPv4ToString($input) === $expected', ({ input, expected }) => {90 expect(convertIPv4BinaryToString(convertIPv4ToBinary(input))).toBe(expected)91 })92})9394describe('convertIPv6ToBinary', () => {95 it('Should result is valid', () => {96 expect(convertIPv6ToBinary('::0')).toBe(0n)97 expect(convertIPv6ToBinary('::1')).toBe(1n)9899 expect(convertIPv6ToBinary('::f')).toBe(15n)100 expect(convertIPv6ToBinary('1234::5678')).toBe(24196103360772296748952112894165669496n)101 expect(convertIPv6ToBinary('::ffff:127.0.0.1')).toBe(281472812449793n)102 expect(convertIPv6ToBinary('fe80::1%eth0')).toBe(convertIPv6ToBinary('fe80::1'))103 })104105 test.each([106 '1::2::3',107 '1:2:3:4:5:6:7:8:9',108 '1:2:3:4:5:6:7',109 '12345::',110 'gggg::',111 '::ffff:127.0.0.256',112 '1234:::5678',113 '2001:db8::1%eth0',114 '::ffff:127.0.0.1%eth0',115 '1:2:3%eth0',116 'gggg%eth0',117 'fe80::1%',118 ])('Should throw for invalid IPv6: %s', (input) => {119 expectInvalidIPAddressError(() => convertIPv6ToBinary(input))120 })121})122123describe('convertIPv6ToString', () => {124 // add tons of test cases here125 test.each`126 input | expected127 ${'::1'} | ${'::1'}128 ${'1::'} | ${'1::'}129 ${'1234::5678'} | ${'1234::5678'}130 ${'2001:2::'} | ${'2001:2::'}131 ${'2001::db8:0:0:0:0:1'} | ${'2001:0:db8::1'}132 ${'1234:5678:9abc:def0:1234:5678:9abc:def0'} | ${'1234:5678:9abc:def0:1234:5678:9abc:def0'}133 ${'::ffff:127.0.0.1'} | ${'::ffff:127.0.0.1'}134 ${'fe80::1%eth0'} | ${'fe80::1'}135 ${'1:0:2:3:4:5:6:7'} | ${'1:0:2:3:4:5:6:7'}136 ${'0:1:2:3:4:5:6:7'} | ${'0:1:2:3:4:5:6:7'}137 ${'1:2:3:4:5:6:7:0'} | ${'1:2:3:4:5:6:7:0'}138 ${'1:0:0:2:3:4:5:6'} | ${'1::2:3:4:5:6'}139 `('convertIPv6ToString($input) === $expected', ({ input, expected }) => {140 expect(convertIPv6BinaryToString(convertIPv6ToBinary(input))).toBe(expected)141 })142143 it('Should compress the unspecified address (all zero groups) to "::"', () => {144 expect(convertIPv6BinaryToString(0n)).toBe('::')145 expect(convertIPv6BinaryToString(convertIPv6ToBinary('::'))).toBe('::')146 // The output must round-trip back to the same binary value.147 expect(convertIPv6ToBinary(convertIPv6BinaryToString(0n))).toBe(0n)148 })149})