Skip to content

[PM-14846] Improve IP Address and Port Handling in StringExtensions #5118

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

Merged

Conversation

SaintPatrck
Copy link
Contributor

@SaintPatrck SaintPatrck commented May 1, 2025

🎟️ Tracking

PM-14846
PM-20513
Relates to #5084

📔 Objective

Correct parsing of IP addresses with and without scheme and port. URI cannot parse an IP address with a port when it does not also include the scheme (http, https), so autofill matching triggered from the Quick Tile was not working properly.

  • New Function: Added isIpAddress() to check if a string is a valid IPv4 address, optionally with a port and scheme.
  • Regex Update: Added the IP_ADDRESS_WITH_OPTIONAL_PORT regex. It validates IPv4 addresses with or without ports (0-65535 range), with an optional http:// or https:// prefix.
  • URI Parsing: Modified toUriOrNull() to handle IP addresses without schemes by adding a default "https://" scheme before parsing.
  • Tests update: Added new tests for isIpAddress function.

📸 Screenshots

⏰ Reminders before review

  • Contributor guidelines followed
  • All formatters and local linters executed and passed
  • Written new unit and / or integration tests where applicable
  • Protected functional changes with optionality (feature flags)
  • Used internationalization (i18n) for all UI strings
  • CI builds passed
  • Communicated to DevOps any deployment requirements
  • Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team

🦮 Reviewer guidelines

  • 👍 (:+1:) or similar for great changes
  • 📝 (:memo:) or ℹ️ (:information_source:) for notes or general info
  • ❓ (:question:) for questions
  • 🤔 (:thinking:) or 💭 (:thought_balloon:) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion
  • 🎨 (:art:) for suggestions / improvements
  • ❌ (:x:) or ⚠️ (:warning:) for more significant problems or concerns needing attention
  • 🌱 (:seedling:) or ♻️ (:recycle:) for future improvements or indications of technical debt
  • ⛏ (:pick:) for minor or nitpick changes

Copy link
Contributor

github-actions bot commented May 1, 2025

Logo
Checkmarx One – Scan Summary & Detailsb354e1cf-9247-496f-8881-2c8689015cfe

Great job, no security vulnerabilities found in this Pull Request

Copy link

codecov bot commented May 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.86%. Comparing base (23ef5b3) to head (ffcae91).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5118      +/-   ##
==========================================
- Coverage   83.83%   82.86%   -0.97%     
==========================================
  Files         636      966     +330     
  Lines       49358    57256    +7898     
  Branches     6702     7160     +458     
==========================================
+ Hits        41377    47444    +6067     
- Misses       5745     7353    +1608     
- Partials     2236     2459     +223     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

URI("https://$this")
} else {
URI(this)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered replacing URI with android.net.Uri, but that introduces different problems and changes the behavior of this function.

  • Uri.parse() is too lenient. It accepts URI's we consider invalid; e.g., not a uri.
  • Uri does not return the correct port when the URI consists of {IP}:{PORT}, without the scheme.
  • Uri is an Android class, so we cannot use it in unit tests. We would rely on the Roboelectric implementation of Uri, which may or may not accurately reflect the actual implementation from Google.

@SaintPatrck SaintPatrck marked this pull request as ready for review May 1, 2025 16:44
-   **New Function:** Added `isIpAddress()` to check if a string is a valid IPv4 address, optionally with a port and scheme.
-   **Regex Update:** Added the `IP_ADDRESS_WITH_OPTIONAL_PORT` regex. It validates IPv4 addresses with or without ports (0-65535 range), with an optional `http://` or `https://` prefix.
-   **URI Parsing:** Modified `toUriOrNull()` to handle IP addresses without schemes by adding a default "https://" scheme before parsing.
- **Tests update**: Added new tests for `isIpAddress` function.
@SaintPatrck SaintPatrck force-pushed the PM-14846/improve-ip-address-handling-for-autofill branch from 09ddef8 to ffcae91 Compare May 1, 2025 19:35
@SaintPatrck SaintPatrck added this pull request to the merge queue May 1, 2025
Merged via the queue into main with commit ad1566f May 1, 2025
9 checks passed
@SaintPatrck SaintPatrck deleted the PM-14846/improve-ip-address-handling-for-autofill branch May 1, 2025 21:08
assertEquals("192.168.1.1:8080", uriString.getHostWithPortOrNull())
}

@Test
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's merged, though why not use @ParameterizedTest in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First time I've seen ParameterizedTest. Looks interesting. I'll keep it in mind for future use. Thanks for pointing it out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants