BIT-621: Add URI matching for autofill (#842)

This commit is contained in:
Lucas Kivi
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent 0e5e6b4444
commit 0c6ea8d18d
19 changed files with 26674 additions and 95 deletions
@@ -8,11 +8,13 @@ import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.data.autofill.model.AutofillCipher
import com.x8bit.bitwarden.data.autofill.provider.AutofillCipherProvider
import com.x8bit.bitwarden.data.autofill.provider.AutofillCipherProviderImpl
import com.x8bit.bitwarden.data.platform.manager.ciphermatching.CipherMatchingManager
import com.x8bit.bitwarden.data.platform.repository.model.DataState
import com.x8bit.bitwarden.data.platform.util.subtitle
import com.x8bit.bitwarden.data.platform.util.takeIfUriMatches
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
import com.x8bit.bitwarden.data.vault.repository.model.VaultState
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
@@ -37,6 +39,7 @@ class AutofillCipherProviderTest {
}
private val cardCipherView: CipherView = mockk {
every { card } returns cardView
every { deletedDate } returns null
every { name } returns CARD_NAME
every { type } returns CipherType.CARD
}
@@ -45,6 +48,7 @@ class AutofillCipherProviderTest {
every { username } returns LOGIN_USERNAME
}
private val loginCipherView: CipherView = mockk {
every { deletedDate } returns null
every { login } returns loginView
every { name } returns LOGIN_NAME
every { type } returns CipherType.LOGIN
@@ -52,6 +56,7 @@ class AutofillCipherProviderTest {
private val authRepository: AuthRepository = mockk {
every { activeUserId } returns ACTIVE_USER_ID
}
private val cipherMatchingManager: CipherMatchingManager = mockk()
private val mutableVaultStateFlow = MutableStateFlow(
VaultState(
unlockingVaultUserIds = emptySet(),
@@ -73,17 +78,16 @@ class AutofillCipherProviderTest {
@BeforeEach
fun setup() {
mockkStatic(CipherView::takeIfUriMatches)
mockkStatic(CipherView::subtitle)
autofillCipherProvider = AutofillCipherProviderImpl(
authRepository = authRepository,
cipherMatchingManager = cipherMatchingManager,
vaultRepository = vaultRepository,
)
}
@AfterEach
fun teardown() {
unmockkStatic(CipherView::takeIfUriMatches)
unmockkStatic(CipherView::subtitle)
}
@@ -130,11 +134,17 @@ class AutofillCipherProviderTest {
assertFalse(result.await())
}
@Suppress("MaxLineLength")
@Test
fun `getCardAutofillCiphers when unlocked should return non-null card ciphers`() =
fun `getCardAutofillCiphers when unlocked should return non-null and non-deleted card ciphers`() =
runTest {
val deletedCardCipherView: CipherView = mockk {
every { deletedDate } returns mockk()
every { type } returns CipherType.CARD
}
val cipherViews = listOf(
cardCipherView,
deletedCardCipherView,
loginCipherView,
)
mutableCiphersStateFlow.value = DataState.Loaded(
@@ -168,18 +178,28 @@ class AutofillCipherProviderTest {
assertEquals(emptyList<AutofillCipher.Card>(), actual)
}
@Suppress("MaxLineLength")
@Test
fun `getLoginAutofillCiphers when unlocked should return matching login ciphers`() =
fun `getLoginAutofillCiphers when unlocked should return matched, non-deleted, login ciphers`() =
runTest {
val unmatchedLoginCipherView: CipherView = mockk {
every { takeIfUriMatches(URI) } returns null
val deletedLoginCipherView: CipherView = mockk {
every { deletedDate } returns mockk()
every { type } returns CipherType.LOGIN
}
val cipherViews = listOf(
cardCipherView,
loginCipherView,
unmatchedLoginCipherView,
deletedLoginCipherView,
)
val filteredCipherViews = listOf(
loginCipherView,
)
coEvery {
cipherMatchingManager.filterCiphersForMatches(
ciphers = filteredCipherViews,
matchUri = URI,
)
} returns filteredCipherViews
mutableCiphersStateFlow.value = DataState.Loaded(
data = cipherViews,
)
@@ -191,14 +211,20 @@ class AutofillCipherProviderTest {
LOGIN_AUTOFILL_CIPHER,
)
every { loginCipherView.subtitle } returns LOGIN_SUBTITLE
every { loginCipherView.takeIfUriMatches(URI) } returns loginCipherView
// Test & Verify
// Test
val actual = autofillCipherProvider.getLoginAutofillCiphers(
uri = URI,
)
// Verify
assertEquals(expected, actual)
coVerify {
cipherMatchingManager.filterCiphersForMatches(
ciphers = filteredCipherViews,
matchUri = URI,
)
}
}
@Test
@@ -0,0 +1,385 @@
package com.x8bit.bitwarden.data.platform.manager.ciphermatching
import android.content.Context
import com.bitwarden.core.CipherView
import com.bitwarden.core.LoginUriView
import com.bitwarden.core.LoginView
import com.bitwarden.core.UriMatchType
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
import com.x8bit.bitwarden.data.platform.repository.model.DataState
import com.x8bit.bitwarden.data.platform.util.getDomainOrNull
import com.x8bit.bitwarden.data.platform.util.getHostWithPortOrNull
import com.x8bit.bitwarden.data.platform.util.getWebHostFromAndroidUriOrNull
import com.x8bit.bitwarden.data.platform.util.isAndroidApp
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
import com.x8bit.bitwarden.data.vault.repository.model.DomainsData
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
class CipherMatchingManagerTest {
private lateinit var cipherMatchingManager: CipherMatchingManager
// Setup dependencies
private val context: Context = mockk()
private val settingsRepository: SettingsRepository = mockk {
every { defaultUriMatchType } returns DEFAULT_URI_MATCH_TYPE
}
private val vaultRepository: VaultRepository = mockk {
every { domainsStateFlow } returns MutableStateFlow(DataState.Loaded(DOMAINS_DATA))
}
// Setup test ciphers
private val defaultMatchLoginUriViewOne: LoginUriView = mockk {
every { match } returns null
every { uri } returns DEFAULT_LOGIN_VIEW_URI_ONE
}
private val defaultMatchLoginUriViewTwo: LoginUriView = mockk {
every { match } returns null
every { uri } returns DEFAULT_LOGIN_VIEW_URI_TWO
}
private val defaultMatchLoginUriViewThree: LoginUriView = mockk {
every { match } returns null
every { uri } returns DEFAULT_LOGIN_VIEW_URI_THREE
}
private val defaultMatchLoginUriViewFour: LoginUriView = mockk {
every { match } returns null
every { uri } returns DEFAULT_LOGIN_VIEW_URI_FOUR
}
private val defaultMatchLoginUriViewFive: LoginUriView = mockk {
every { match } returns null
every { uri } returns DEFAULT_LOGIN_VIEW_URI_FIVE
}
private val defaultMatchLoginView: LoginView = mockk {
every { uris } returns listOf(
defaultMatchLoginUriViewOne,
defaultMatchLoginUriViewTwo,
defaultMatchLoginUriViewThree,
defaultMatchLoginUriViewFour,
defaultMatchLoginUriViewFive,
)
}
private val defaultMatchCipher: CipherView = mockk {
every { login } returns defaultMatchLoginView
}
private val exactMatchLoginUriViewOne: LoginUriView = mockk {
every { match } returns UriMatchType.EXACT
every { uri } returns "google.com"
}
private val exactMatchLoginUriViewTwo: LoginUriView = mockk {
every { match } returns UriMatchType.EXACT
every { uri } returns "notExactMatch.com"
}
private val exactMatchLoginView: LoginView = mockk {
every { uris } returns listOf(
exactMatchLoginUriViewOne,
exactMatchLoginUriViewTwo,
)
}
private val exactMatchCipher: CipherView = mockk {
every { login } returns exactMatchLoginView
}
private val hostMatchLoginUriViewMatching: LoginUriView = mockk {
every { match } returns UriMatchType.HOST
every { uri } returns HOST_LOGIN_VIEW_URI_MATCHING
}
private val hostMatchLoginUriViewNotMatching: LoginUriView = mockk {
every { match } returns UriMatchType.HOST
every { uri } returns HOST_LOGIN_VIEW_URI_NOT_MATCHING
}
private val hostMatchLoginView: LoginView = mockk {
every { uris } returns listOf(
hostMatchLoginUriViewMatching,
hostMatchLoginUriViewNotMatching,
)
}
private val hostMatchCipher: CipherView = mockk {
every { login } returns hostMatchLoginView
}
private val neverMatchLoginUriView: LoginUriView = mockk {
every { match } returns UriMatchType.NEVER
every { uri } returns "google.com"
}
private val neverMatchLoginView: LoginView = mockk {
every { uris } returns listOf(neverMatchLoginUriView)
}
private val neverMatchCipher: CipherView = mockk {
every { login } returns neverMatchLoginView
}
private val regexMatchLoginUriViewMatching: LoginUriView = mockk {
every { match } returns UriMatchType.REGULAR_EXPRESSION
every { uri } returns ".*"
}
private val regexMatchLoginUriViewNotMatching: LoginUriView = mockk {
every { match } returns UriMatchType.REGULAR_EXPRESSION
every { uri } returns "$^"
}
private val regexMatchLoginView: LoginView = mockk {
every { uris } returns listOf(
regexMatchLoginUriViewMatching,
regexMatchLoginUriViewNotMatching,
)
}
private val regexMatchCipher: CipherView = mockk {
every { login } returns regexMatchLoginView
}
private val startsWithMatchLoginUriViewMatching: LoginUriView = mockk {
every { match } returns UriMatchType.STARTS_WITH
every { uri } returns "g"
}
private val startsWithMatchLoginUriViewNotMatching: LoginUriView = mockk {
every { match } returns UriMatchType.REGULAR_EXPRESSION
every { uri } returns "!!!!!!"
}
private val startsWithMatchLoginView: LoginView = mockk {
every { uris } returns listOf(
startsWithMatchLoginUriViewMatching,
startsWithMatchLoginUriViewNotMatching,
)
}
private val startsWithMatchCipher: CipherView = mockk {
every { login } returns startsWithMatchLoginView
}
private val ciphers: List<CipherView> = listOf(
defaultMatchCipher,
exactMatchCipher,
hostMatchCipher,
neverMatchCipher,
regexMatchCipher,
startsWithMatchCipher,
)
@BeforeEach
fun setup() {
mockkStatic(
String::isAndroidApp,
String::getDomainOrNull,
String::getWebHostFromAndroidUriOrNull,
)
cipherMatchingManager = CipherMatchingManagerImpl(
context = context,
settingsRepository = settingsRepository,
vaultRepository = vaultRepository,
)
}
@AfterEach
fun teardown() {
unmockkStatic(
String::isAndroidApp,
String::getDomainOrNull,
String::getWebHostFromAndroidUriOrNull,
)
}
@Suppress("MaxLineLength")
@Test
fun `filterCiphersForMatches should perform cipher matching when is android app and matching URI`() =
runTest {
// Setup
val uri = "google.com"
val expected = listOf(
defaultMatchCipher,
exactMatchCipher,
hostMatchCipher,
regexMatchCipher,
startsWithMatchCipher,
)
setupMocksForMatchingCiphers(
isAndroidApp = true,
uri = uri,
)
// Test
val actual = cipherMatchingManager.filterCiphersForMatches(
ciphers = ciphers,
matchUri = uri,
)
// Verify
assertEquals(expected, actual)
}
@Suppress("MaxLineLength")
@Test
fun `filterCiphersForMatches should perform cipher matching when is android app and difficult to match URI`() =
runTest {
// Setup
val uri = "difficultToMatch.com"
// The default cipher only has a fuzzy match
// and therefore is at the end of the list.
val expected = listOf(
hostMatchCipher,
regexMatchCipher,
defaultMatchCipher,
)
setupMocksForMatchingCiphers(
isAndroidApp = true,
uri = uri,
)
// Test
val actual = cipherMatchingManager.filterCiphersForMatches(
ciphers = ciphers,
matchUri = uri,
)
// Verify
assertEquals(expected, actual)
}
@Suppress("MaxLineLength")
@Test
fun `filterCiphersForMatches should perform cipher matching when not android app and matching URI`() =
runTest {
// Setup
val uri = "google.com"
val expected = listOf(
defaultMatchCipher,
exactMatchCipher,
hostMatchCipher,
regexMatchCipher,
startsWithMatchCipher,
)
setupMocksForMatchingCiphers(
isAndroidApp = false,
uri = uri,
)
// Test
val actual = cipherMatchingManager.filterCiphersForMatches(
ciphers = ciphers,
matchUri = uri,
)
// Verify
assertEquals(expected, actual)
}
@Suppress("MaxLineLength")
@Test
fun `filterCiphersForMatches should perform cipher matching when not android app and difficult to match URI`() =
runTest {
// Setup
val uri = "difficultToMatch.com"
val expected = listOf(
hostMatchCipher,
regexMatchCipher,
)
setupMocksForMatchingCiphers(
isAndroidApp = false,
uri = uri,
)
// Test
val actual = cipherMatchingManager.filterCiphersForMatches(
ciphers = ciphers,
matchUri = uri,
)
// Verify
assertEquals(expected, actual)
}
@Test
fun `filterCiphersForMatches should skip ciphers without login details`() =
runTest {
// Setup
val uri = "noMatches.com"
val ciphers = listOf<CipherView>(
mockk {
every { login } returns null
},
)
with(uri) {
every { isAndroidApp() } returns false
every { getDomainOrNull(context = context) } returns this
every { getWebHostFromAndroidUriOrNull() } returns null
}
// Test
val actual = cipherMatchingManager.filterCiphersForMatches(
ciphers = ciphers,
matchUri = uri,
)
// Verify
assertEquals(emptyList<CipherView>(), actual)
}
/**
* Setup mocks for matching the massive list of [ciphers].
*/
private fun setupMocksForMatchingCiphers(
isAndroidApp: Boolean,
uri: String,
) {
with(uri) {
every { isAndroidApp() } returns isAndroidApp
every { getDomainOrNull(context = context) } returns this.takeIf { isAndroidApp }
every { getHostWithPortOrNull() } returns HOST_WITH_PORT
every {
getWebHostFromAndroidUriOrNull()
} returns ANDROID_APP_WEB_URL.takeIf { isAndroidApp }
}
every {
DEFAULT_LOGIN_VIEW_URI_ONE.getDomainOrNull(context = context)
} returns DEFAULT_LOGIN_VIEW_URI_ONE
every {
DEFAULT_LOGIN_VIEW_URI_TWO.getDomainOrNull(context = context)
} returns null
every {
DEFAULT_LOGIN_VIEW_URI_THREE.getDomainOrNull(context = context)
} returns uri
every {
DEFAULT_LOGIN_VIEW_URI_FOUR.getDomainOrNull(context = context)
} returns "bitwarden.com"
every {
DEFAULT_LOGIN_VIEW_URI_FIVE.getDomainOrNull(context = context)
} returns null
every { HOST_LOGIN_VIEW_URI_MATCHING.getHostWithPortOrNull() } returns HOST_WITH_PORT
every { HOST_LOGIN_VIEW_URI_NOT_MATCHING.getHostWithPortOrNull() } returns null
}
}
private const val ANDROID_APP_WEB_URL = "ANDROID_APP_WEB_URL"
private val DEFAULT_URI_MATCH_TYPE =
com.x8bit.bitwarden.data.platform.repository.model.UriMatchType.DOMAIN
private val EQUIVALENT_DOMAINS = listOf(
"google.com",
"google.co.uk",
)
private val GLOBAL_EQUIVALENT_DOMAINS_DATA = listOf(
"bitwarden.com",
"bitwarden.co.uk",
ANDROID_APP_WEB_URL,
)
private val GLOBAL_EQUIVALENT_DOMAINS = DomainsData.GlobalEquivalentDomain(
isExcluded = false,
domains = GLOBAL_EQUIVALENT_DOMAINS_DATA,
type = 0,
)
private val DOMAINS_DATA = DomainsData(
equivalentDomains = listOf(EQUIVALENT_DOMAINS),
globalEquivalentDomains = listOf(GLOBAL_EQUIVALENT_DOMAINS),
)
// Setup state for default ciphers
private const val DEFAULT_LOGIN_VIEW_URI_ONE: String = "google.com"
private const val DEFAULT_LOGIN_VIEW_URI_TWO: String = ANDROID_APP_WEB_URL
private const val DEFAULT_LOGIN_VIEW_URI_THREE: String = "DEFAULT_LOGIN_VIEW_URI_THREE"
private const val DEFAULT_LOGIN_VIEW_URI_FOUR: String = "DEFAULT_LOGIN_VIEW_URI_FOUR"
private const val DEFAULT_LOGIN_VIEW_URI_FIVE: String = "DEFAULT_LOGIN_VIEW_URI_FIVE"
// Setup state for host ciphers
private const val HOST_LOGIN_VIEW_URI_MATCHING: String = "DEFAULT_LOGIN_VIEW_URI_MATCHING"
private const val HOST_LOGIN_VIEW_URI_NOT_MATCHING: String = "DEFAULT_LOGIN_VIEW_URI_NOT_MATCHING"
private const val HOST_WITH_PORT: String = "HOST_WITH_PORT"
@@ -4,10 +4,7 @@ import com.bitwarden.core.CardView
import com.bitwarden.core.CipherType
import com.bitwarden.core.CipherView
import com.bitwarden.core.IdentityView
import com.bitwarden.core.LoginUriView
import com.bitwarden.core.LoginView
import com.x8bit.bitwarden.data.platform.util.subtitle
import com.x8bit.bitwarden.data.platform.util.takeIfUriMatches
import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.Assertions.assertEquals
@@ -332,52 +329,4 @@ class CipherViewExtensionsTest {
// Verify
assertNull(actual)
}
@Test
fun `takeIfUriMatches should return the CipherView if the URI matches`() {
// Setup
val testUri = "com.x8bit.bitwarden"
val loginUriView: LoginUriView = mockk {
every { uri } returns testUri
}
val loginView: LoginView = mockk {
every { uris } returns listOf(loginUriView)
}
val expected: CipherView = mockk {
every { login } returns loginView
every { type } returns CipherType.IDENTITY
}
// Test
val actual = expected.takeIfUriMatches(
uri = testUri,
)
// Verify
assertEquals(expected, actual)
}
@Test
fun `takeIfUriMatches should return null if the URI doesn't match`() {
// Setup
val testUri = "com.x8bit.bitwarden"
val loginUriView: LoginUriView = mockk {
every { uri } returns "com.google"
}
val loginView: LoginView = mockk {
every { uris } returns listOf(loginUriView)
}
val cipherView: CipherView = mockk {
every { login } returns loginView
every { type } returns CipherType.IDENTITY
}
// Test
val actual = cipherView.takeIfUriMatches(
uri = testUri,
)
// Verify
assertNull(actual)
}
}
@@ -0,0 +1,141 @@
package com.x8bit.bitwarden.data.util
import android.content.Context
import com.x8bit.bitwarden.data.platform.util.findLastSubstringIndicesOrNull
import com.x8bit.bitwarden.data.platform.util.getDomainOrNull
import com.x8bit.bitwarden.data.platform.util.getWebHostFromAndroidUriOrNull
import com.x8bit.bitwarden.data.platform.util.isAndroidApp
import com.x8bit.bitwarden.data.platform.util.parseDomainOrNull
import com.x8bit.bitwarden.data.platform.util.toUriOrNull
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import io.mockk.verify
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import java.net.URI
class StringExtensionsTest {
@AfterEach
fun teardown() {
unmockkStatic(URI::parseDomainOrNull)
}
@Test
fun `toUriOrNull should return null when uri is malformed`() {
assertNull("not a uri".toUriOrNull())
}
@Test
fun `toUriOrNull should return URI when uri is valid`() {
assertNotNull("www.google.com".toUriOrNull())
}
@Test
fun `isAndroidApp should return true when string starts with android app protocol`() {
assertTrue("androidapp://com.x8bit.bitwarden".isAndroidApp())
}
@Test
fun `isAndroidApp should return false when doesn't start with android app protocol`() {
assertFalse("com.x8bit.bitwarden".isAndroidApp())
}
@Test
fun `getWebHostFromAndroidUriOrNull should return null when not android app`() {
assertNull("com.x8bit.bitwarden".getWebHostFromAndroidUriOrNull())
}
@Test
fun `getWebHostFromAndroidUriOrNull should return null when no dot`() {
assertNull("androidapp://comx8bitbitwarden".getWebHostFromAndroidUriOrNull())
}
@Test
fun `getWebHostFromAndroidUriOrNull should return web host when has dot`() {
// Setup
val expected = "x8bit.com"
// Test
val actual = "androidapp://com.x8bit.bitwarden".getWebHostFromAndroidUriOrNull()
// Verify
assertEquals(expected, actual)
}
@Test
fun `getDomainOrNull should invoke parseDomainOrNull when URI is created`() {
// Setup
mockkStatic(URI::parseDomainOrNull)
val context: Context = mockk()
val expected = "google.com"
every {
any<URI>().parseDomainOrNull(context = context)
} returns expected
// Test
val actual = "www.google.com".getDomainOrNull(
context = context,
)
// Verify
assertEquals(expected, actual)
verify(exactly = 1) {
any<URI>().parseDomainOrNull(context = context)
}
}
@Test
fun `getDomainOrNull should not invoke parseDomainOrNull when URI is not created`() {
// Setup
mockkStatic(URI::parseDomainOrNull)
val context: Context = mockk()
// Test
val actual = "not a URI".getDomainOrNull(
context = context,
)
// Verify
assertNull(actual)
verify(exactly = 0) {
any<URI>().parseDomainOrNull(context = context)
}
}
@Test
fun `findLastSubstringIndicesOrNull should return null if substring doesn't appear`() {
// Setup
val substring = "hello"
// Test
val actual = "goodbye".findLastSubstringIndicesOrNull(
substring = substring,
)
// Verify
assertNull(actual)
}
@Test
fun `findLastSubstringIndicesOrNull should return indices of last substring appearance`() {
// Setup
val substring = "hello"
val expected = IntRange(7, 11)
// Test
val actual = "hello, hello".findLastSubstringIndicesOrNull(
substring = substring,
)
// Verify
assertEquals(expected, actual)
}
}
@@ -0,0 +1,342 @@
package com.x8bit.bitwarden.data.util
import android.content.Context
import android.content.res.Resources
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.platform.manager.model.DomainName
import com.x8bit.bitwarden.data.platform.util.parseDomainNameOrNull
import com.x8bit.bitwarden.data.platform.util.parseDomainOrNull
import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
import java.net.URI
class UriExtensionsTest {
private val resources: Resources = mockk {
every { getStringArray(R.array.exception_suffixes) } returns emptyArray()
every { getStringArray(R.array.normal_suffixes) } returns emptyArray()
every { getStringArray(R.array.wild_card_suffixes) } returns emptyArray()
}
private val context: Context = mockk {
every { this@mockk.resources } returns this@UriExtensionsTest.resources
}
@Test
fun `parseDomainOrNull should return null when host is null`() {
// Setup
val uri: URI = mockk {
every { host } returns null
}
// Test
val actual = uri.parseDomainOrNull(
context = context,
)
// Verify
assertNull(actual)
}
@Test
fun `parseDomainOrNull should return host when it is localhost`() {
// Setup
val expected = "localhost"
val uri: URI = mockk {
every { host } returns expected
}
// Test
val actual = uri.parseDomainOrNull(
context = context,
)
// Verify
assertEquals(expected, actual)
}
@Test
fun `parseDomainOrNull should return host when it is ip address`() {
// Setup
val expected = "192.168.1.1"
val uri: URI = mockk {
every { host } returns expected
}
// Test
val actual = uri.parseDomainOrNull(
context = context,
)
// Verify
assertEquals(expected, actual)
}
@Test
fun `parseDomainOrNull should return domain name when it matches exception suffix`() {
// Setup
val uri: URI = mockk()
val expected = listOf(
"example.co.uk",
"example.co.uk",
"example.uk",
null,
)
every {
resources.getStringArray(R.array.exception_suffixes)
} returns arrayOf("co.uk", "uk")
// Test & Verify
listOf(
"example.co.uk",
"sub.example.co.uk",
"sub.example.uk",
"sub.example.uk.usa",
)
.forEachIndexed { index, host ->
// Setup item test
every { uri.host } returns host
// Test
val actual = uri.parseDomainOrNull(
context = context,
)
// Verify
assertEquals(expected[index], actual)
}
}
@Test
fun `parseDomainOrNull should return domain name when it matches normal suffix`() {
// Setup
val uri: URI = mockk()
val expected = listOf(
"example.co.uk",
"example.co.uk",
"example.uk",
null,
)
every { resources.getStringArray(R.array.exception_suffixes) } returns arrayOf(
"co.uk",
"uk",
)
// Test & Verify
listOf(
"example.co.uk",
"sub.example.co.uk",
"sub.example.uk",
"sub.example.uk.usa",
)
.forEachIndexed { index, host ->
// Setup item test
every { uri.host } returns host
// Test
val actual = uri.parseDomainOrNull(
context = context,
)
// Verify
assertEquals(expected[index], actual)
}
}
@Test
fun `parseDomainOrNull should return domain name when it matches wild card suffix`() {
// Setup
val uri: URI = mockk()
val expected = listOf(
"example.foo.compute.amazonaws.com",
"example.foo.compute.amazonaws.com",
"example.foo.amazonaws.com",
null,
)
every { resources.getStringArray(R.array.wild_card_suffixes) } returns arrayOf(
"compute.amazonaws.com",
"amazonaws.com",
)
// Test & Verify
listOf(
"sub.example.foo.compute.amazonaws.com",
"bar.sub.example.foo.compute.amazonaws.com",
"bar.sub.example.foo.amazonaws.com",
"foo.sub.example.foo.amazonaws.com.usa",
)
.forEachIndexed { index, host ->
// Setup item test
every { uri.host } returns host
// Test
val actual = uri.parseDomainOrNull(
context = context,
)
// Verify
assertEquals(expected[index], actual)
}
}
@Test
fun `parseDomainNameOrNull should return null when host is null`() {
// Setup
val uri: URI = mockk {
every { host } returns null
}
// Test
val actual = uri.parseDomainNameOrNull(
context = context,
)
// Verify
assertNull(actual)
}
@Test
fun `parseDomainNameOrNull should return domain name when it matches exception suffix`() {
// Setup
val uri: URI = mockk()
val expected = listOf(
DomainName(
secondLevelDomain = "example",
subDomain = null,
topLevelDomain = "co.uk",
),
DomainName(
secondLevelDomain = "example",
subDomain = "sub",
topLevelDomain = "co.uk",
),
DomainName(
secondLevelDomain = "example",
subDomain = "sub",
topLevelDomain = "uk",
),
null,
)
every { resources.getStringArray(R.array.exception_suffixes) } returns arrayOf(
"co.uk",
"uk",
)
// Test & Verify
listOf(
"example.co.uk",
"sub.example.co.uk",
"sub.example.uk",
"sub.example.uk.usa",
)
.forEachIndexed { index, host ->
// Setup item test
every { uri.host } returns host
// Test
val actual = uri.parseDomainNameOrNull(
context = context,
)
// Verify
assertEquals(expected[index], actual)
}
}
@Test
fun `parseDomainNameOrNull should return domain name when it matches normal suffix`() {
// Setup
val uri: URI = mockk()
val expected = listOf(
DomainName(
secondLevelDomain = "example",
subDomain = null,
topLevelDomain = "co.uk",
),
DomainName(
secondLevelDomain = "example",
subDomain = "sub",
topLevelDomain = "co.uk",
),
DomainName(
secondLevelDomain = "example",
subDomain = "sub",
topLevelDomain = "uk",
),
null,
)
every { resources.getStringArray(R.array.exception_suffixes) } returns arrayOf(
"co.uk",
"uk",
)
// Test & Verify
listOf(
"example.co.uk",
"sub.example.co.uk",
"sub.example.uk",
"sub.example.uk.usa",
)
.forEachIndexed { index, host ->
// Setup item test
every { uri.host } returns host
// Test
val actual = uri.parseDomainNameOrNull(
context = context,
)
// Verify
assertEquals(expected[index], actual)
}
}
@Test
fun `parseDomainNameOrNull should return domain name when it matches wild card suffix`() {
// Setup
val uri: URI = mockk()
val expected = listOf(
DomainName(
secondLevelDomain = "example",
subDomain = "sub",
topLevelDomain = "foo.compute.amazonaws.com",
),
DomainName(
secondLevelDomain = "example",
subDomain = "bar.sub",
topLevelDomain = "foo.compute.amazonaws.com",
),
DomainName(
secondLevelDomain = "example",
subDomain = "bar.sub",
topLevelDomain = "foo.amazonaws.com",
),
null,
)
every { resources.getStringArray(R.array.wild_card_suffixes) } returns arrayOf(
"compute.amazonaws.com",
"amazonaws.com",
)
// Test & Verify
listOf(
"sub.example.foo.compute.amazonaws.com",
"bar.sub.example.foo.compute.amazonaws.com",
"bar.sub.example.foo.amazonaws.com",
"bar.sub.example.foo.amazonaws.com.usa",
)
.forEachIndexed { index, host ->
// Setup item test
every { uri.host } returns host
// Test
val actual = uri.parseDomainNameOrNull(
context = context,
)
// Verify
assertEquals(expected[index], actual)
}
}
}
@@ -24,4 +24,32 @@ class UriMatchTypeExtensionsTest {
)
}
}
@Test
fun `toSdkUriMatchType should return the correct value for each type`() {
assertEquals(
com.bitwarden.core.UriMatchType.DOMAIN,
UriMatchType.DOMAIN.toSdkUriMatchType(),
)
assertEquals(
com.bitwarden.core.UriMatchType.EXACT,
UriMatchType.EXACT.toSdkUriMatchType(),
)
assertEquals(
com.bitwarden.core.UriMatchType.HOST,
UriMatchType.HOST.toSdkUriMatchType(),
)
assertEquals(
com.bitwarden.core.UriMatchType.NEVER,
UriMatchType.NEVER.toSdkUriMatchType(),
)
assertEquals(
com.bitwarden.core.UriMatchType.REGULAR_EXPRESSION,
UriMatchType.REGULAR_EXPRESSION.toSdkUriMatchType(),
)
assertEquals(
com.bitwarden.core.UriMatchType.STARTS_WITH,
UriMatchType.STARTS_WITH.toSdkUriMatchType(),
)
}
}