From 86f6f85009502431234d3e7dfb091bd6c49d8a7a Mon Sep 17 00:00:00 2001 From: klaus Date: Thu, 29 Oct 2026 15:25:37 +0900 Subject: [PATCH] =?UTF-8?q?feat(event):=20=EB=94=A5=EB=A7=81=ED=81=AC=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=20=EA=B3=84=EC=95=BD=EC=9D=84=20=EA=B2=80?= =?UTF-8?q?=EC=A6=9D=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../v2/event/EventDeepLinkContract.kt | 54 +++++++++++ .../v2/event/EventDeepLinkContractTest.kt | 90 +++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 app/src/main/java/kr/co/vividnext/sodalive/v2/event/EventDeepLinkContract.kt create mode 100644 app/src/test/java/kr/co/vividnext/sodalive/v2/event/EventDeepLinkContractTest.kt diff --git a/app/src/main/java/kr/co/vividnext/sodalive/v2/event/EventDeepLinkContract.kt b/app/src/main/java/kr/co/vividnext/sodalive/v2/event/EventDeepLinkContract.kt new file mode 100644 index 00000000..0db35d6d --- /dev/null +++ b/app/src/main/java/kr/co/vividnext/sodalive/v2/event/EventDeepLinkContract.kt @@ -0,0 +1,54 @@ +package kr.co.vividnext.sodalive.v2.event + +import android.net.Uri + +sealed interface EventDeepLinkContract { + data class Direct(val eventId: Long) : EventDeepLinkContract + data object SdkOwned : EventDeepLinkContract + data object InvalidEvent : EventDeepLinkContract + data object Unrelated : EventDeepLinkContract + + companion object { + private val routingKeys = setOf( + "deep_link_value", "deep_link_sub5", "room_id", "channel_id", "message_id", "audition_id", + "content_id", "postId", "community_creator_id", "community_post_id" + ) + + fun classify(uri: Uri, appScheme: String): EventDeepLinkContract { + val values = if (uri.isHierarchical) uri.getQueryParameters("deep_link_value") else emptyList() + val isEvent = uri.host == "event-detail" || "event-detail" in uri.pathSegments || + "event-detail" in values || (!uri.isHierarchical && uri.schemeSpecificPart.startsWith("event-detail")) + if (!isEvent) return Unrelated + if (!uri.isHierarchical || uri.encodedAuthority != uri.host) return InvalidEvent + if (routingKeys.any { uri.getQueryParameters(it).size > 1 }) return InvalidEvent + + if (uri.scheme == appScheme && uri.host == "event-detail") { + val rawId = uri.encodedPath?.removePrefix("/") + val id = positiveId(rawId) ?: return InvalidEvent + if (values.isNotEmpty() && values.single() != "event-detail") return InvalidEvent + val queryId = uri.getQueryParameter("deep_link_sub5") + if (queryId != null && positiveId(queryId) != id) return InvalidEvent + if (routingKeys.minus(setOf("deep_link_value", "deep_link_sub5")).any { it in uri.queryParameterNames }) { + return InvalidEvent + } + return Direct(id) + } + + return if (uri.scheme == "https" && uri.host == "$appScheme.onelink.me" && + parseUdl(values.singleOrNull(), uri.getQueryParameter("deep_link_sub5")) != null + ) { + SdkOwned + } else { + InvalidEvent + } + } + + fun parseUdl(value: String?, sub5: String?): Long? = + if (value == "event-detail") positiveId(sub5) else null + + fun canonicalUri(eventId: Long, appScheme: String): Uri = Uri.parse("$appScheme://event-detail/$eventId") + + private fun positiveId(raw: String?): Long? = raw?.takeIf { it.isNotEmpty() && it.all { digit -> digit in '0'..'9' } } + ?.toLongOrNull()?.takeIf { it > 0 } + } +} diff --git a/app/src/test/java/kr/co/vividnext/sodalive/v2/event/EventDeepLinkContractTest.kt b/app/src/test/java/kr/co/vividnext/sodalive/v2/event/EventDeepLinkContractTest.kt new file mode 100644 index 00000000..59006c22 --- /dev/null +++ b/app/src/test/java/kr/co/vividnext/sodalive/v2/event/EventDeepLinkContractTest.kt @@ -0,0 +1,90 @@ +package kr.co.vividnext.sodalive.v2.event + +import android.app.Application +import android.net.Uri +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], application = Application::class) +class EventDeepLinkContractTest { + @Test + fun `직접 링크는 환경별 양수 Long 전체 범위를 보존한다`() { + listOf("voiceon", "voiceon-test").forEach { scheme -> + listOf(1L, 2147483648L, Long.MAX_VALUE).forEach { id -> + assertEquals( + EventDeepLinkContract.Direct(id), + EventDeepLinkContract.classify(Uri.parse("$scheme://event-detail/$id"), scheme) + ) + assertEquals("$scheme://event-detail/$id", EventDeepLinkContract.canonicalUri(id, scheme).toString()) + } + } + } + + @Test + fun `잘못된 ID와 권한 경로 및 라우팅 query 충돌은 이벤트로 거부한다`() { + listOf( + "voiceon-test://event-detail", "voiceon-test://event-detail/0", "voiceon-test://event-detail/-1", + "voiceon-test://event-detail/+1", "voiceon-test://event-detail/%201", "voiceon-test://event-detail/1", + "voiceon-test://event-detail/9223372036854775808", "voiceon-test://event-detail/1/2", + "voiceon-test://event-detail//1", "voiceon-test://event-detail/1/", + "voiceon-test://user@event-detail/1", "voiceon-test://event-detail:80/1", + "voiceon-test://event-detail:invalid/1", "voiceon-test://event-detail:/1", + "voiceon://event-detail/1", "https://foreign.example/event-detail/1", + "voiceon-test://event-detail/1?deep_link_value=live&deep_link_sub5=2", + "voiceon-test://event-detail/1?deep_link_sub5=1&deep_link_sub5=2", + "voiceon-test://event-detail/1?room_id=2", "voiceon-test:event-detail/1" + ).forEach { raw -> + assertEquals(raw, EventDeepLinkContract.InvalidEvent, EventDeepLinkContract.classify(Uri.parse(raw), "voiceon-test")) + } + } + + @Test + fun `현재 환경 HTTPS OneLink만 SDK 소유이며 잘못된 이벤트는 다른 도메인으로 바뀌지 않는다`() { + assertEquals( + EventDeepLinkContract.SdkOwned, + EventDeepLinkContract.classify( + Uri.parse("https://voiceon-test.onelink.me/abc?deep_link_value=event-detail&deep_link_sub5=9223372036854775807"), + "voiceon-test" + ) + ) + listOf( + "https://foreign.example/abc", + "https://voiceon.onelink.me/abc", + "http://voiceon-test.onelink.me/abc", + "https://user@voiceon-test.onelink.me/abc", + "https://voiceon-test.onelink.me:443/abc" + ).forEach { prefix -> + assertEquals( + EventDeepLinkContract.InvalidEvent, + EventDeepLinkContract.classify(Uri.parse("$prefix?deep_link_value=event-detail&deep_link_sub5=1"), "voiceon-test") + ) + } + assertEquals( + EventDeepLinkContract.InvalidEvent, + EventDeepLinkContract.classify( + Uri.parse("https://voiceon-test.onelink.me/a?deep_link_value=live&deep_link_value=event-detail&deep_link_sub5=1"), + "voiceon-test" + ) + ) + assertEquals( + EventDeepLinkContract.Unrelated, + EventDeepLinkContract.classify(Uri.parse("sodalive://chat/2"), "voiceon-test") + ) + } + + @Test + fun `UDL은 정확한 value와 ASCII 양수 문자열만 수용한다`() { + assertEquals(Long.MAX_VALUE, EventDeepLinkContract.parseUdl("event-detail", "9223372036854775807")) + listOf(null, "", "0", "-1", "+1", " 1", "1 ", "1", "9223372036854775808").forEach { id -> + assertNull(EventDeepLinkContract.parseUdl("event-detail", id)) + } + listOf(null, "live", "EVENT-DETAIL").forEach { value -> + assertNull(EventDeepLinkContract.parseUdl(value, "1")) + } + } +}