Skip to content

Commit c91fbb4

Browse files
committed
Fix tests
1 parent ce1d76e commit c91fbb4

File tree

2 files changed

+27
-36
lines changed

2 files changed

+27
-36
lines changed

lib/src/androidTest/java/at/bitfire/cert4android/CustomCertManagerTest.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
package at.bitfire.cert4android
66

77
import androidx.test.platform.app.InstrumentationRegistry
8+
import kotlinx.coroutines.CoroutineScope
9+
import kotlinx.coroutines.Dispatchers
10+
import kotlinx.coroutines.cancel
11+
import org.junit.After
812
import org.junit.Assume.assumeNotNull
913
import org.junit.Before
1014
import org.junit.Test
@@ -19,6 +23,7 @@ class CustomCertManagerTest {
1923

2024
private lateinit var certManager: CustomCertManager
2125
private lateinit var paranoidCertManager: CustomCertManager
26+
private val scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
2227

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

3338
@Before
3439
fun createCertManager() {
35-
certManager = CustomCertManager(context, true, null)
36-
paranoidCertManager = CustomCertManager(context, false, null)
40+
certManager = CustomCertManager(context, true, scope, getUserDecision = { true })
41+
paranoidCertManager = CustomCertManager(context, false, scope, getUserDecision = { false })
3742
}
3843

44+
@After
45+
fun cleanUp() {
46+
scope.cancel()
47+
}
3948

4049
@Test(expected = CertificateException::class)
4150
fun testCheckClientCertificate() {
Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package at.bitfire.cert4android
22

33
import androidx.test.platform.app.InstrumentationRegistry
4-
import io.mockk.every
4+
import io.mockk.coEvery
5+
import io.mockk.coVerify
6+
import io.mockk.mockk
57
import io.mockk.mockkObject
68
import io.mockk.unmockkAll
7-
import io.mockk.verify
89
import kotlinx.coroutines.Dispatchers
910
import kotlinx.coroutines.launch
1011
import kotlinx.coroutines.runBlocking
1112
import org.junit.After
1213
import org.junit.Assert.*
1314
import org.junit.Before
1415
import org.junit.Test
16+
import java.security.cert.X509Certificate
1517
import java.util.Collections
1618
import java.util.concurrent.Semaphore
17-
import kotlin.concurrent.thread
1819

1920
class UserDecisionRegistryTest {
2021

@@ -29,7 +30,6 @@ class UserDecisionRegistryTest {
2930
fun setUp() {
3031
mockkObject(NotificationUtils)
3132
mockkObject(registry)
32-
every { registry.requestDecision(any(), any(), any()) } returns Unit
3333
}
3434

3535
@After
@@ -41,38 +41,32 @@ class UserDecisionRegistryTest {
4141

4242
@Test
4343
fun testCheck_FirstDecision_Negative() {
44-
every { registry.requestDecision(testCert, any(), any()) } answers {
45-
registry.onUserDecision(testCert, false)
46-
}
4744
assertFalse(runBlocking {
48-
registry.check(testCert, true)
45+
registry.check(testCert, this) { false }
4946
})
5047
}
5148

5249
@Test
5350
fun testCheck_FirstDecision_Positive() {
54-
every { registry.requestDecision(testCert, any(), any()) } answers {
55-
registry.onUserDecision(testCert, true)
56-
}
5751
assertTrue(runBlocking {
58-
registry.check(testCert, true)
52+
registry.check(testCert, this) { true }
5953
})
6054
}
6155

6256
@Test
6357
fun testCheck_MultipleDecisionsForSameCert_Negative() {
6458
val canSendFeedback = Semaphore(0)
65-
every { registry.requestDecision(testCert, any(), any()) } answers {
66-
thread {
59+
val getUserDecision: suspend (X509Certificate) -> Boolean = mockk {
60+
coEvery { this@mockk(testCert) } coAnswers {
6761
canSendFeedback.acquire()
68-
registry.onUserDecision(testCert, false)
62+
false
6963
}
7064
}
7165
val results = Collections.synchronizedList(mutableListOf<Boolean>())
7266
runBlocking {
7367
repeat(5) {
7468
launch(Dispatchers.Default) {
75-
results += registry.check(testCert, true)
69+
results += registry.check(testCert, this, getUserDecision)
7670
}
7771
}
7872
canSendFeedback.release()
@@ -82,23 +76,23 @@ class UserDecisionRegistryTest {
8276
}
8377
assertEquals(5, results.size)
8478
assertTrue(results.all { !it })
85-
verify(exactly = 1) { registry.requestDecision(any(), any(), any()) }
79+
coVerify(exactly = 1) { getUserDecision(testCert) }
8680
}
8781

8882
@Test
8983
fun testCheck_MultipleDecisionsForSameCert_Positive() {
9084
val canSendFeedback = Semaphore(0)
91-
every { registry.requestDecision(testCert, any(), any()) } answers {
92-
thread {
85+
val getUserDecision: suspend (X509Certificate) -> Boolean = mockk {
86+
coEvery { this@mockk(testCert) } coAnswers {
9387
canSendFeedback.acquire()
94-
registry.onUserDecision(testCert, true)
88+
true
9589
}
9690
}
9791
val results = Collections.synchronizedList(mutableListOf<Boolean>())
9892
runBlocking {
9993
repeat(5) {
10094
launch(Dispatchers.Default) {
101-
results += registry.check(testCert, true)
95+
results += registry.check(testCert, this, getUserDecision)
10296
}
10397
}
10498
canSendFeedback.release()
@@ -108,19 +102,7 @@ class UserDecisionRegistryTest {
108102
}
109103
assertEquals(5, results.size)
110104
assertTrue(results.all { it })
111-
verify(exactly = 1) { registry.requestDecision(any(), any(), any()) }
112-
}
113-
114-
@Test
115-
fun testCheck_UserDecisionImpossible() {
116-
every { NotificationUtils.notificationsPermitted(any()) } returns false
117-
assertFalse(runBlocking {
118-
// should return instantly
119-
registry.check(testCert, false)
120-
})
121-
verify(inverse = true) {
122-
registry.requestDecision(any(), any(), any())
123-
}
105+
coVerify(exactly = 1) { getUserDecision(testCert) }
124106
}
125107

126108
}

0 commit comments

Comments
 (0)