-
Notifications
You must be signed in to change notification settings - Fork 2
Add support for parsing IPv6 addresses and update tests for IPv4/6 #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds support for parsing IPv6 addresses by implementing hex range parsing capabilities. The implementation follows the existing decimal range parsing patterns and integrates seamlessly with the current hostlist parser architecture.
Changes:
- Added hex range parsing support to enable IPv6 address expansion (e.g.,
2001:db8::[0-f]) - Extended host element parser to accept colon characters for IPv6 address literals
- Added comprehensive test coverage for IPv4 and IPv6 address parsing scenarios
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/structures.rs | Added HexRange variants and format_hex_prefix function to support hexadecimal range formatting |
| src/lib.rs | Implemented hex parsing functions (hex_digits, leading_hex, range_hex, disjoint_hex) and integrated them into the range parser; added IPv4/IPv6 integration tests |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| #[test] | ||
| fn test_parse_ip_addresses() { | ||
| assert_debug_snapshot!("IPv4 single", parse("192.168.0.1")); | ||
| assert_debug_snapshot!("IPv6 compressed", parse("2001:db8::1")); | ||
| assert_debug_snapshot!( | ||
| "IPv6 full", | ||
| parse("fe80:1234:5678:9abc:def0:1234:5678:9abc") | ||
| ); | ||
| assert_debug_snapshot!("Multiple IPv6 literals", parse("2001:db8::1, 2001:db8::2")); | ||
| assert_debug_snapshot!("IPv6 expansion", parse("2001:db8::[0-f]")); | ||
| } |
Copilot
AI
Jan 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new hex parsing functions (leading_hex, range_hex, disjoint_hex) lack unit tests similar to their decimal equivalents (test_leading_zeros, test_range_digits, test_disjoint_digits). While integration tests are provided, unit tests would help ensure each parser component works correctly in isolation and make debugging easier if issues arise. Consider adding unit tests that directly test these functions.
| #[test] | ||
| fn test_parse_ip_addresses() { | ||
| assert_debug_snapshot!("IPv4 single", parse("192.168.0.1")); | ||
| assert_debug_snapshot!("IPv6 compressed", parse("2001:db8::1")); | ||
| assert_debug_snapshot!( | ||
| "IPv6 full", | ||
| parse("fe80:1234:5678:9abc:def0:1234:5678:9abc") | ||
| ); | ||
| assert_debug_snapshot!("Multiple IPv6 literals", parse("2001:db8::1, 2001:db8::2")); | ||
| assert_debug_snapshot!("IPv6 expansion", parse("2001:db8::[0-f]")); | ||
| } |
Copilot
AI
Jan 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test suite doesn't include a test case for hex disjoint ranges (e.g., parse("2001:db8::[a,b,c]")). While the "IPv6 expansion" test covers hex ranges with the dash operator, adding a test for comma-separated hex values would provide more complete coverage of the disjoint_hex parser functionality.
| #[test] | ||
| fn test_parse_ip_addresses() { | ||
| assert_debug_snapshot!("IPv4 single", parse("192.168.0.1")); | ||
| assert_debug_snapshot!("IPv6 compressed", parse("2001:db8::1")); | ||
| assert_debug_snapshot!( | ||
| "IPv6 full", | ||
| parse("fe80:1234:5678:9abc:def0:1234:5678:9abc") | ||
| ); | ||
| assert_debug_snapshot!("Multiple IPv6 literals", parse("2001:db8::1, 2001:db8::2")); | ||
| assert_debug_snapshot!("IPv6 expansion", parse("2001:db8::[0-f]")); | ||
| } |
Copilot
AI
Jan 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test suite doesn't include a test case for reversed hex ranges (e.g., parse("2001:db8::[f-0]")). While reversed decimal ranges are tested (line 466), adding a test for reversed hex ranges would provide more complete coverage of the HexRangeReversed functionality.
| pub(crate) fn format_hex_prefix(num: u64, prefix: usize, same_prefix_len: bool) -> String { | ||
| let s = format!("{:x}", num); | ||
| let width = if same_prefix_len { | ||
| prefix + s.len() | ||
| } else { | ||
| prefix + 1 | ||
| }; | ||
|
|
||
| if width <= s.len() { | ||
| s | ||
| } else { | ||
| let mut out = String::with_capacity(width); | ||
| out.extend(std::iter::repeat_n('0', width - s.len())); | ||
| out.push_str(&s); | ||
| out | ||
| } | ||
| } |
Copilot
AI
Jan 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test suite in structures.rs only tests the decimal range variants (Range, Disjoint) but doesn't test the new hex variants (HexRange, HexRangeReversed, HexDisjoint). Consider adding tests similar to test_range_output_range_iter and test_range_output_disjoint_iter for the hex variants to ensure the iterator implementation and formatting work correctly.
palash3
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug:
In ipv6 hostlist 1-10 is treated as decimal instead of hexadecimal.
So only 10 ips are generated instead of 16.
|
The changes allow us to specify bogus addresses |
No thats not by the change, that is already the case. Remember the hostlist operates on FQDNs and supports IPv4 by accident. |
|
Yeah, but I think the drift between IPv4/fqdn and IPv6 is greater. Also before the change you could only add digits in the range so the above example would have been valid no ? |
| ); | ||
| assert_debug_snapshot!("Multiple IPv6 literals", parse("2001:db8::1, 2001:db8::2")); | ||
| assert_debug_snapshot!("IPv6 expansion", parse("2001:db8::[0-f]")); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| assert_debug_snapshot!("IPv6 expansion with base 16", parse("2001:db8::[00-10]")); |
This should also work. But maybe it makes sense to limit that. Its very easy to generate billions of addresses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason the hex functions allowed only strings with alphabetic characters ? Removed this and your test works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are your thoughts on limiting the amount of expansion to 2-3 chars? I'm not sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inappropriate usage of these wild cards is always going to be an issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean we can easily count a range and abort if the amount is greater than a threshold
No description provided.