From c90ce2b323906cc6d49e85ad5a16706df12565df Mon Sep 17 00:00:00 2001 From: Mojtaba Chenani Date: Fri, 11 Aug 2023 14:35:48 +0200 Subject: [PATCH] fix(message): refactor uri scheme checker --- .../kotlin/com/wire/android/util/UriUtil.kt | 9 ++++++--- .../test/kotlin/com/wire/android/TestUtil.kt | 7 ++++++- .../kotlin/com/wire/android/util/UriUtilTest.kt | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/com/wire/android/util/UriUtil.kt b/app/src/main/kotlin/com/wire/android/util/UriUtil.kt index c2036596c8c..e62e5f5cf8e 100644 --- a/app/src/main/kotlin/com/wire/android/util/UriUtil.kt +++ b/app/src/main/kotlin/com/wire/android/util/UriUtil.kt @@ -17,12 +17,15 @@ */ package com.wire.android.util +import java.net.URI import java.net.URLDecoder fun containsSchema(url: String): Boolean { - val regexPattern = Regex(".+://.+") - - return regexPattern.matches(url) + return try { + URI.create(url).scheme != null + } catch (iae: IllegalArgumentException) { + false // invalid URI + } } fun normalizeLink(url: String): String { diff --git a/app/src/test/kotlin/com/wire/android/TestUtil.kt b/app/src/test/kotlin/com/wire/android/TestUtil.kt index 97768eac750..43d6f5e2e10 100644 --- a/app/src/test/kotlin/com/wire/android/TestUtil.kt +++ b/app/src/test/kotlin/com/wire/android/TestUtil.kt @@ -19,7 +19,12 @@ package com.wire.android import kotlin.random.Random -val charPool: List = ('a'..'z') + ('A'..'Z') + ('0'..'9') +val charPoolWithNumbers: List = ('a'..'z') + ('A'..'Z') + ('0'..'9') +val charPool: List = ('a'..'z') + ('A'..'Z') + +fun Random.stringWithNumbers(length: Int) = (1..length) + .map { Random.nextInt(0, charPoolWithNumbers.size).let { charPoolWithNumbers[it] } } + .joinToString("") fun Random.string(length: Int) = (1..length) .map { Random.nextInt(0, charPool.size).let { charPool[it] } } diff --git a/app/src/test/kotlin/com/wire/android/util/UriUtilTest.kt b/app/src/test/kotlin/com/wire/android/util/UriUtilTest.kt index 9433bb51198..1b7aa51d7c5 100644 --- a/app/src/test/kotlin/com/wire/android/util/UriUtilTest.kt +++ b/app/src/test/kotlin/com/wire/android/util/UriUtilTest.kt @@ -38,6 +38,23 @@ class UriUtilTest { assert(expected == actual) } + @Test + fun givenLink_whenTheLinkStartsWithMailTo_thenReturnsTheSameLink() { + val input = "mailto:alice@wire.com" + val expected = "mailto:alice@wire.com" + val actual = normalizeLink(input) + assert(expected == actual) + } + + @Test + fun givenLink_whenTheLinkIsWireDeepLink_thenReturnsTheSameLink() { + val input = "wire://access/?config=https://nginz-https.elna.wire.link/deeplink.json" + val expected = "wire://access/?config=https://nginz-https.elna.wire.link/deeplink.json" + val actual = normalizeLink(input) + assert(expected == actual) + } + + @Test fun givenLink_whenTheLinkStartsWithRandomSchema_thenReturnsTheSameLink() { val randomString = Random.string(Random.nextInt(5))