Skip to content

Commit

Permalink
More thorough IP address regex tests (#1938)
Browse files Browse the repository at this point in the history
* reformat test for easier copy-paste

* add ipv4-mapped ipv6 and a few other cases

* add more test cases
  • Loading branch information
david-crespo authored Feb 5, 2024
1 parent e5a1f80 commit 76877ff
Showing 1 changed file with 62 additions and 24 deletions.
86 changes: 62 additions & 24 deletions libs/util/str.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,34 +85,72 @@ describe('titleCase', () => {
})
})

// Rust playground showing the results of these test cases match the results of std::net::{Ipv4Addr, Ipv6Addr}
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=babc49cd34bf19669137e22b9202d2eb
// Rust playground comparing results with std::net::{Ipv4Addr, Ipv6Addr}
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=493b3345b9f6c0b1c8ee91834e99ef7b

test.each(['123.4.56.7', '1.2.3.4'])('ipv4Regex passes: %s', (s) => {
expect(IPV4_REGEX.test(s)).toBe(true)
})

test.each([
['', false],
['1', false],
['abc', false],
['a.b.c.d', false],
'',
'1',
'abc',
'a.b.c.d',
// some implementations (I think incorrectly) allow leading zeros but nexus does not
['01.102.103.104', false],
['123.4.56.7', true],
['1.2.3.4', true],
])('ipv4Regex %s', (s, result) => {
expect(IPV4_REGEX.test(s)).toBe(result)
'01.102.103.104',
'::ffff:192.0.2.128',
'127.0.0',
'127.0.0.1.',
'127.0.0.1 ',
' 127.0.0.1',
'10002.3.4',
'1.2.3.4.5',
'256.0.0.0',
'260.0.0.0',
])('ipv4Regex fails: %s', (s) => {
expect(IPV4_REGEX.test(s)).toBe(false)
})

test.each([
'2001:db8:3333:4444:5555:6666:7777:8888',
'2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF',
'::',
'2001:db8::',
'::1234:5678',
'2001:db8::1234:5678',
'2001:0db8:85a3:0000:0000:8a2e:0370:7334',
'::ffff:192.0.2.128',
'1:2:3:4:5:6:7:8',
'::ffff:10.0.0.1',
'::ffff:1.2.3.4',
'::ffff:0.0.0.0',
'1:2:3:4:5:6:77:88',
'::ffff:255.255.255.255',
'fe08::7:8',
'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
])('ipv6Regex passes: %s', (s) => {
expect(IPV6_REGEX.test(s)).toBe(true)
})

test.each([
['', false],
['1', false],
['abc', false],
['123.4.56.7', false],
['2001:db8:3333:4444:5555:6666:7777:8888', true],
['2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF', true],
['::', true],
['2001:db8::', true],
['::1234:5678', true],
['2001:db8::1234:5678', true],
['2001:0db8:85a3:0000:0000:8a2e:0370:7334', true],
])('ipv6Regex %s', (s, result) => {
expect(IPV6_REGEX.test(s)).toBe(result)
'',
'1',
'abc',
'123.4.56.7',
'2001:0db8:85a3:0000:0000:8a2e:0370:7334 ',
' 2001:db8::',
'1:2:3:4:5:6:7:8:9',
'1:2:3:4:5:6::7:8',
':1:2:3:4:5:6:7:8',
'1:2:3:4:5:6:7:8:',
'::1:2:3:4:5:6:7:8',
'1:2:3:4:5:6:7:8::',
'1:2:3:4:5:6:7:88888',
'2001:db8:3:4:5::192.0.2.33', // std::new::Ipv6Net allows this one
'fe08::7:8%',
'fe08::7:8i',
'fe08::7:8interface',
])('ipv6Regex fails: %s', (s) => {
expect(IPV6_REGEX.test(s)).toBe(false)
})

0 comments on commit 76877ff

Please sign in to comment.