Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sunkup committed Feb 25, 2025
1 parent ce1d76e commit c91fbb4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
package at.bitfire.cert4android

import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import org.junit.After
import org.junit.Assume.assumeNotNull
import org.junit.Before
import org.junit.Test
Expand All @@ -19,6 +23,7 @@ class CustomCertManagerTest {

private lateinit var certManager: CustomCertManager
private lateinit var paranoidCertManager: CustomCertManager
private val scope: CoroutineScope = CoroutineScope(Dispatchers.Default)

private var siteCerts: List<X509Certificate>? =
try {
Expand All @@ -32,10 +37,14 @@ class CustomCertManagerTest {

@Before
fun createCertManager() {
certManager = CustomCertManager(context, true, null)
paranoidCertManager = CustomCertManager(context, false, null)
certManager = CustomCertManager(context, true, scope, getUserDecision = { true })
paranoidCertManager = CustomCertManager(context, false, scope, getUserDecision = { false })
}

@After
fun cleanUp() {
scope.cancel()
}

@Test(expected = CertificateException::class)
fun testCheckClientCertificate() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package at.bitfire.cert4android

import androidx.test.platform.app.InstrumentationRegistry
import io.mockk.every
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.unmockkAll
import io.mockk.verify
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import java.security.cert.X509Certificate
import java.util.Collections
import java.util.concurrent.Semaphore
import kotlin.concurrent.thread

class UserDecisionRegistryTest {

Expand All @@ -29,7 +30,6 @@ class UserDecisionRegistryTest {
fun setUp() {
mockkObject(NotificationUtils)
mockkObject(registry)
every { registry.requestDecision(any(), any(), any()) } returns Unit
}

@After
Expand All @@ -41,38 +41,32 @@ class UserDecisionRegistryTest {

@Test
fun testCheck_FirstDecision_Negative() {
every { registry.requestDecision(testCert, any(), any()) } answers {
registry.onUserDecision(testCert, false)
}
assertFalse(runBlocking {
registry.check(testCert, true)
registry.check(testCert, this) { false }
})
}

@Test
fun testCheck_FirstDecision_Positive() {
every { registry.requestDecision(testCert, any(), any()) } answers {
registry.onUserDecision(testCert, true)
}
assertTrue(runBlocking {
registry.check(testCert, true)
registry.check(testCert, this) { true }
})
}

@Test
fun testCheck_MultipleDecisionsForSameCert_Negative() {
val canSendFeedback = Semaphore(0)
every { registry.requestDecision(testCert, any(), any()) } answers {
thread {
val getUserDecision: suspend (X509Certificate) -> Boolean = mockk {
coEvery { this@mockk(testCert) } coAnswers {
canSendFeedback.acquire()
registry.onUserDecision(testCert, false)
false
}
}
val results = Collections.synchronizedList(mutableListOf<Boolean>())
runBlocking {
repeat(5) {
launch(Dispatchers.Default) {
results += registry.check(testCert, true)
results += registry.check(testCert, this, getUserDecision)
}
}
canSendFeedback.release()
Expand All @@ -82,23 +76,23 @@ class UserDecisionRegistryTest {
}
assertEquals(5, results.size)
assertTrue(results.all { !it })
verify(exactly = 1) { registry.requestDecision(any(), any(), any()) }
coVerify(exactly = 1) { getUserDecision(testCert) }
}

@Test
fun testCheck_MultipleDecisionsForSameCert_Positive() {
val canSendFeedback = Semaphore(0)
every { registry.requestDecision(testCert, any(), any()) } answers {
thread {
val getUserDecision: suspend (X509Certificate) -> Boolean = mockk {
coEvery { this@mockk(testCert) } coAnswers {
canSendFeedback.acquire()
registry.onUserDecision(testCert, true)
true
}
}
val results = Collections.synchronizedList(mutableListOf<Boolean>())
runBlocking {
repeat(5) {
launch(Dispatchers.Default) {
results += registry.check(testCert, true)
results += registry.check(testCert, this, getUserDecision)
}
}
canSendFeedback.release()
Expand All @@ -108,19 +102,7 @@ class UserDecisionRegistryTest {
}
assertEquals(5, results.size)
assertTrue(results.all { it })
verify(exactly = 1) { registry.requestDecision(any(), any(), any()) }
}

@Test
fun testCheck_UserDecisionImpossible() {
every { NotificationUtils.notificationsPermitted(any()) } returns false
assertFalse(runBlocking {
// should return instantly
registry.check(testCert, false)
})
verify(inverse = true) {
registry.requestDecision(any(), any(), any())
}
coVerify(exactly = 1) { getUserDecision(testCert) }
}

}

0 comments on commit c91fbb4

Please sign in to comment.