Skip to content

Commit 76877ff

Browse files
authored
More thorough IP address regex tests (#1938)
* reformat test for easier copy-paste * add ipv4-mapped ipv6 and a few other cases * add more test cases
1 parent e5a1f80 commit 76877ff

File tree

1 file changed

+62
-24
lines changed

1 file changed

+62
-24
lines changed

libs/util/str.spec.ts

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -85,34 +85,72 @@ describe('titleCase', () => {
8585
})
8686
})
8787

88-
// Rust playground showing the results of these test cases match the results of std::net::{Ipv4Addr, Ipv6Addr}
89-
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=babc49cd34bf19669137e22b9202d2eb
88+
// Rust playground comparing results with std::net::{Ipv4Addr, Ipv6Addr}
89+
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=493b3345b9f6c0b1c8ee91834e99ef7b
90+
91+
test.each(['123.4.56.7', '1.2.3.4'])('ipv4Regex passes: %s', (s) => {
92+
expect(IPV4_REGEX.test(s)).toBe(true)
93+
})
9094

9195
test.each([
92-
['', false],
93-
['1', false],
94-
['abc', false],
95-
['a.b.c.d', false],
96+
'',
97+
'1',
98+
'abc',
99+
'a.b.c.d',
96100
// some implementations (I think incorrectly) allow leading zeros but nexus does not
97-
['01.102.103.104', false],
98-
['123.4.56.7', true],
99-
['1.2.3.4', true],
100-
])('ipv4Regex %s', (s, result) => {
101-
expect(IPV4_REGEX.test(s)).toBe(result)
101+
'01.102.103.104',
102+
'::ffff:192.0.2.128',
103+
'127.0.0',
104+
'127.0.0.1.',
105+
'127.0.0.1 ',
106+
' 127.0.0.1',
107+
'10002.3.4',
108+
'1.2.3.4.5',
109+
'256.0.0.0',
110+
'260.0.0.0',
111+
])('ipv4Regex fails: %s', (s) => {
112+
expect(IPV4_REGEX.test(s)).toBe(false)
113+
})
114+
115+
test.each([
116+
'2001:db8:3333:4444:5555:6666:7777:8888',
117+
'2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF',
118+
'::',
119+
'2001:db8::',
120+
'::1234:5678',
121+
'2001:db8::1234:5678',
122+
'2001:0db8:85a3:0000:0000:8a2e:0370:7334',
123+
'::ffff:192.0.2.128',
124+
'1:2:3:4:5:6:7:8',
125+
'::ffff:10.0.0.1',
126+
'::ffff:1.2.3.4',
127+
'::ffff:0.0.0.0',
128+
'1:2:3:4:5:6:77:88',
129+
'::ffff:255.255.255.255',
130+
'fe08::7:8',
131+
'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
132+
])('ipv6Regex passes: %s', (s) => {
133+
expect(IPV6_REGEX.test(s)).toBe(true)
102134
})
103135

104136
test.each([
105-
['', false],
106-
['1', false],
107-
['abc', false],
108-
['123.4.56.7', false],
109-
['2001:db8:3333:4444:5555:6666:7777:8888', true],
110-
['2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF', true],
111-
['::', true],
112-
['2001:db8::', true],
113-
['::1234:5678', true],
114-
['2001:db8::1234:5678', true],
115-
['2001:0db8:85a3:0000:0000:8a2e:0370:7334', true],
116-
])('ipv6Regex %s', (s, result) => {
117-
expect(IPV6_REGEX.test(s)).toBe(result)
137+
'',
138+
'1',
139+
'abc',
140+
'123.4.56.7',
141+
'2001:0db8:85a3:0000:0000:8a2e:0370:7334 ',
142+
' 2001:db8::',
143+
'1:2:3:4:5:6:7:8:9',
144+
'1:2:3:4:5:6::7:8',
145+
':1:2:3:4:5:6:7:8',
146+
'1:2:3:4:5:6:7:8:',
147+
'::1:2:3:4:5:6:7:8',
148+
'1:2:3:4:5:6:7:8::',
149+
'1:2:3:4:5:6:7:88888',
150+
'2001:db8:3:4:5::192.0.2.33', // std::new::Ipv6Net allows this one
151+
'fe08::7:8%',
152+
'fe08::7:8i',
153+
'fe08::7:8interface',
154+
])('ipv6Regex fails: %s', (s) => {
155+
expect(IPV6_REGEX.test(s)).toBe(false)
118156
})

0 commit comments

Comments
 (0)