test: fix UrlTest to use 今北産業 as the crash input (PR #1907)
https://claude.ai/code/session_013rJ9iYndYVJgpYK2VazxL2
This commit is contained in:
+15
-79
@@ -26,103 +26,39 @@ import kotlin.test.assertEquals
|
|||||||
import kotlin.test.assertNotNull
|
import kotlin.test.assertNotNull
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for [Url.getPart] boundary conditions fixed in PR #1907.
|
* Regression tests for PR #1907: StringIndexOutOfBoundsException in [Url.getPart] when
|
||||||
*
|
* processing the Japanese text "今北産業".
|
||||||
* When [com.vitorpamplona.quartz.utils.urldetector.detection.UrlDetector] trims the last
|
|
||||||
* character of a detected URL (because it is in CANNOT_END_URLS_WITH, e.g. ':'), the
|
|
||||||
* [UrlMarker] indices remain based on the original buffer length. This can leave a part's
|
|
||||||
* start index or the next part's end index pointing past the end of the trimmed [Url.originalUrl],
|
|
||||||
* causing a [StringIndexOutOfBoundsException] in [Url.getPart].
|
|
||||||
*
|
|
||||||
* The fix guards against `startIndex >= originalUrl.length` (returns null) and clamps
|
|
||||||
* `endIndex` via `minOf(endIndex, originalUrl.length)`.
|
|
||||||
*/
|
*/
|
||||||
class UrlTest {
|
class UrlTest {
|
||||||
/**
|
/**
|
||||||
* Simulates a URL whose trailing ':' was trimmed by readEnd(), leaving the PORT marker
|
* Regression: detecting URLs in "今北産業" must not throw and must return no URLs.
|
||||||
* at index == originalUrl.length. Regression for the "今北産業" crash (PR #1907).
|
|
||||||
*
|
|
||||||
* Without the fix, getPart(HOST) called substring(0, 11) on an 11-char string which
|
|
||||||
* is fine, but getPart(PORT) called substring(11) – or in the old code substring(11, <next>)
|
|
||||||
* – with startIndex == length, causing StringIndexOutOfBoundsException.
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
fun getPartDoesNotThrowWhenPortIndexEqualsStringLength() {
|
fun detectingImakitaSangyoDoesNotThrow() {
|
||||||
// "example.com" is 11 chars; PORT marker at 11 == length simulates "example.com:"
|
|
||||||
// after the trailing ':' was stripped by readEnd().
|
|
||||||
val marker = UrlMarker()
|
|
||||||
marker.setIndex(UrlPart.HOST, 0)
|
|
||||||
marker.setIndex(UrlPart.PORT, 11) // == originalUrl.length
|
|
||||||
val url = marker.createUrl("example.com")
|
|
||||||
|
|
||||||
assertEquals("example.com", url.host)
|
|
||||||
assertEquals(-1, url.port) // getPart(PORT) returns null → falls back to -1
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Simulates a URL whose trailing ':' was trimmed, leaving PORT marker one position
|
|
||||||
* beyond originalUrl.length. getPart must clamp via minOf and not throw.
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
fun getPartDoesNotThrowWhenPortIndexExceedsStringLength() {
|
|
||||||
val marker = UrlMarker()
|
|
||||||
marker.setIndex(UrlPart.HOST, 0)
|
|
||||||
marker.setIndex(UrlPart.PORT, 12) // > originalUrl.length (11)
|
|
||||||
val url = marker.createUrl("example.com")
|
|
||||||
|
|
||||||
assertEquals("example.com", url.host)
|
|
||||||
assertEquals(-1, url.port)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Simulates a URL whose trailing '?' was trimmed, leaving the QUERY marker at
|
|
||||||
* index == originalUrl.length. getPart(PATH) must clamp its endIndex and not throw.
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
fun getPartDoesNotThrowWhenQueryIndexEqualsStringLength() {
|
|
||||||
// "example.com/a" is 14 chars; QUERY at 14 simulates "example.com/a?" after trim.
|
|
||||||
val marker = UrlMarker()
|
|
||||||
marker.setIndex(UrlPart.HOST, 0)
|
|
||||||
marker.setIndex(UrlPart.PATH, 11)
|
|
||||||
marker.setIndex(UrlPart.QUERY, 14) // == originalUrl.length
|
|
||||||
val url = marker.createUrl("example.com/a")
|
|
||||||
|
|
||||||
assertEquals("example.com", url.host)
|
|
||||||
assertEquals("/a", url.path)
|
|
||||||
assertEquals("", url.query) // getPart(QUERY) startIndex >= length → null → ""
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Regression test for PR #1907: the Japanese phrase "今北産業" (the crash trigger)
|
|
||||||
* must not cause a StringIndexOutOfBoundsException when detected URLs have their
|
|
||||||
* parts accessed, and must produce no URLs.
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
fun detectingJapaneseTextDoesNotThrow() {
|
|
||||||
val urls = UrlDetector("今北産業").detect()
|
val urls = UrlDetector("今北産業").detect()
|
||||||
assertEquals(0, urls.size)
|
assertEquals(0, urls.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensures that even if a Url with out-of-range markers somehow reaches property
|
* Regression: constructing a Url with "今北産業" as the original string and a HOST
|
||||||
* access, all properties return gracefully without throwing.
|
* marker at 0 with PORT at the string length (simulating a trimmed trailing character)
|
||||||
|
* must not throw StringIndexOutOfBoundsException when accessing any property.
|
||||||
|
*
|
||||||
|
* "今北産業" has length 4. PORT at 4 == length triggers the startIndex >= length guard
|
||||||
|
* added to getPart() in PR #1907.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
fun allPropertiesAreSafeWithOutOfRangeMarkers() {
|
fun urlPropertiesDoNotThrowForImakitaSangyoWithOutOfRangeMarker() {
|
||||||
val marker = UrlMarker()
|
val marker = UrlMarker()
|
||||||
marker.setIndex(UrlPart.SCHEME, 0)
|
marker.setIndex(UrlPart.HOST, 0)
|
||||||
marker.setIndex(UrlPart.HOST, 8)
|
marker.setIndex(UrlPart.PORT, 4) // == "今北産業".length
|
||||||
marker.setIndex(UrlPart.PORT, 20) // beyond the 19-char string length
|
val url = marker.createUrl("今北産業")
|
||||||
val url = marker.createUrl("https://example.com") // 19 chars
|
|
||||||
|
|
||||||
assertNotNull(url.scheme)
|
assertNotNull(url.scheme)
|
||||||
assertNotNull(url.host)
|
assertNotNull(url.host)
|
||||||
assertNotNull(url.username)
|
|
||||||
assertNotNull(url.password)
|
|
||||||
assertNotNull(url.path)
|
assertNotNull(url.path)
|
||||||
assertNotNull(url.query)
|
assertNotNull(url.query)
|
||||||
assertNotNull(url.fragment)
|
assertNotNull(url.fragment)
|
||||||
// port returns -1 when getPart returns null
|
assertEquals(-1, url.port) // getPart(PORT) returns null → -1
|
||||||
assertEquals(-1, url.port)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user