Start fixing crypto tests compilation

This commit is contained in:
ganfra 2022-04-15 20:05:06 +02:00
parent 9cb43ce4c8
commit 48793f531c
8 changed files with 108 additions and 50 deletions

View File

@ -378,7 +378,7 @@ class CommonTestHelper(context: Context) {
assertTrue(latch.await(timeout ?: TestConstants.timeOutMillis, TimeUnit.MILLISECONDS))
}
suspend fun retryPeriodicallyWithLatch(latch: CountDownLatch, condition: (() -> Boolean)) {
suspend fun retryPeriodicallyWithLatch(latch: CountDownLatch, condition: suspend (() -> Boolean)) {
while (true) {
delay(1000)
if (condition()) {

View File

@ -19,6 +19,7 @@ package org.matrix.android.sdk.common
import android.os.SystemClock
import android.util.Log
import androidx.lifecycle.Observer
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
@ -275,7 +276,7 @@ class CryptoTestHelper(private val testHelper: CommonTestHelper) {
}
fun initializeCrossSigning(session: Session) {
testHelper.doSync<Unit> {
testHelper.runBlockingTest {
session.cryptoService().crossSigningService()
.initializeCrossSigning(
object : UserInteractiveAuthInterceptor {
@ -288,7 +289,7 @@ class CryptoTestHelper(private val testHelper: CommonTestHelper) {
)
)
}
}, it)
})
}
}
@ -300,12 +301,14 @@ class CryptoTestHelper(private val testHelper: CommonTestHelper) {
val aliceVerificationService = alice.cryptoService().verificationService()
val bobVerificationService = bob.cryptoService().verificationService()
aliceVerificationService.beginKeyVerificationInDMs(
VerificationMethod.SAS,
requestID,
roomId,
bob.myUserId,
bob.sessionParams.credentials.deviceId!!)
runBlocking {
aliceVerificationService.beginKeyVerificationInDMs(
VerificationMethod.SAS,
requestID,
roomId,
bob.myUserId,
bob.sessionParams.credentials.deviceId!!)
}
// we should reach SHOW SAS on both
var alicePovTx: OutgoingSasVerificationTransaction? = null
@ -346,8 +349,10 @@ class CryptoTestHelper(private val testHelper: CommonTestHelper) {
assertEquals("SAS code do not match", alicePovTx!!.getDecimalCodeRepresentation(), bobPovTx!!.getDecimalCodeRepresentation())
bobPovTx!!.userHasVerifiedShortCode()
alicePovTx!!.userHasVerifiedShortCode()
runBlocking {
bobPovTx!!.userHasVerifiedShortCode()
alicePovTx!!.userHasVerifiedShortCode()
}
testHelper.waitWithLatch {
testHelper.retryPeriodicallyWithLatch(it) {

View File

@ -60,8 +60,8 @@ class PreShareKeysTest : InstrumentedTest {
Log.d("#Test", "Room Key Received from alice $preShareCount")
// Force presharing of new outbound key
testHelper.doSync<Unit> {
aliceSession.cryptoService().prepareToEncrypt(e2eRoomID, it)
testHelper.runBlockingTest {
aliceSession.cryptoService().prepareToEncrypt(e2eRoomID)
}
testHelper.waitWithLatch { latch ->

View File

@ -144,9 +144,12 @@ class UnwedgingTest : InstrumentedTest {
// - Store the olm session between A&B devices
// Let us pickle our session with bob here so we can later unpickle it
// and wedge our session.
val sessionIdsForBob = aliceCryptoStore.getDeviceSessionIds(bobSession.cryptoService().getMyDevice().identityKey()!!)
var myDevice = testHelper.runBlockingTest {
bobSession.cryptoService().getMyDevice()
}
val sessionIdsForBob = aliceCryptoStore.getDeviceSessionIds(myDevice.identityKey()!!)
sessionIdsForBob!!.size shouldBe 1
val olmSession = aliceCryptoStore.getDeviceSession(sessionIdsForBob.first(), bobSession.cryptoService().getMyDevice().identityKey()!!)!!
val olmSession = aliceCryptoStore.getDeviceSession(sessionIdsForBob.first(),myDevice.identityKey()!!)!!
val oldSession = serializeForRealm(olmSession.olmSession)
@ -174,7 +177,10 @@ class UnwedgingTest : InstrumentedTest {
// Let us wedge the session now. Set crypto state like after the first message
Timber.i("## CRYPTO | testUnwedging: wedge the session now. Set crypto state like after the first message")
aliceCryptoStore.storeSession(OlmSessionWrapper(deserializeFromRealm<OlmSession>(oldSession)!!), bobSession.cryptoService().getMyDevice().identityKey()!!)
myDevice = testHelper.runBlockingTest {
bobSession.cryptoService().getMyDevice()
}
aliceCryptoStore.storeSession(OlmSessionWrapper(deserializeFromRealm<OlmSession>(oldSession)!!), myDevice.identityKey()!!)
Thread.sleep(6_000)
// Force new session, and key share
@ -207,7 +213,7 @@ class UnwedgingTest : InstrumentedTest {
bobTimeline.removeListener(bobHasThreeDecryptedEventsListener)
// It's a trick to force key request on fail to decrypt
testHelper.doSync<Unit> {
testHelper.runBlockingTest {
bobSession.cryptoService().crossSigningService()
.initializeCrossSigning(
object : UserInteractiveAuthInterceptor {
@ -220,7 +226,7 @@ class UnwedgingTest : InstrumentedTest {
)
)
}
}, it)
})
}
// Wait until we received back the key

View File

@ -23,6 +23,8 @@ import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertNotNull
import junit.framework.TestCase.assertTrue
import junit.framework.TestCase.fail
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import org.junit.Assert
import org.junit.FixMethodOrder
import org.junit.Ignore
@ -93,7 +95,9 @@ class KeyShareTests : InstrumentedTest {
assert(receivedEvent!!.isEncrypted())
try {
aliceSession2.cryptoService().decryptEvent(receivedEvent.root, "foo")
commonTestHelper.runBlockingTest {
aliceSession2.cryptoService().decryptEvent(receivedEvent.root, "foo")
}
fail("should fail")
} catch (failure: Throwable) {
}
@ -106,7 +110,7 @@ class KeyShareTests : InstrumentedTest {
var outGoingRequestId: String? = null
commonTestHelper.waitWithLatch { latch ->
commonTestHelper.waitWithLatch { latch ->
commonTestHelper.retryPeriodicallyWithLatch(latch) {
aliceSession2.cryptoService().getOutgoingRoomKeyRequests()
.filter { req ->
@ -148,7 +152,9 @@ class KeyShareTests : InstrumentedTest {
}
try {
aliceSession2.cryptoService().decryptEvent(receivedEvent.root, "foo")
commonTestHelper.runBlockingTest {
aliceSession2.cryptoService().decryptEvent(receivedEvent.root, "foo")
}
fail("should fail")
} catch (failure: Throwable) {
}
@ -185,7 +191,9 @@ class KeyShareTests : InstrumentedTest {
}
try {
aliceSession2.cryptoService().decryptEvent(receivedEvent.root, "foo")
commonTestHelper.runBlockingTest {
aliceSession2.cryptoService().decryptEvent(receivedEvent.root, "foo")
}
} catch (failure: Throwable) {
fail("should have been able to decrypt")
}
@ -199,7 +207,7 @@ class KeyShareTests : InstrumentedTest {
fun test_ShareSSSSSecret() {
val aliceSession1 = commonTestHelper.createAccount(TestConstants.USER_ALICE, SessionTestParams(true))
commonTestHelper.doSync<Unit> {
commonTestHelper.runBlockingTest {
aliceSession1.cryptoService().crossSigningService()
.initializeCrossSigning(
object : UserInteractiveAuthInterceptor {
@ -211,7 +219,7 @@ class KeyShareTests : InstrumentedTest {
)
)
}
}, it)
})
}
// Also bootstrap keybackup on first session
@ -249,8 +257,10 @@ class KeyShareTests : InstrumentedTest {
}
if (tx.state == VerificationTxState.ShortCodeReady) {
session1ShortCode = tx.getDecimalCodeRepresentation()
Thread.sleep(500)
tx.userHasVerifiedShortCode()
commonTestHelper.runBlockingTest {
delay(500)
tx.userHasVerifiedShortCode()
}
}
}
}
@ -262,16 +272,20 @@ class KeyShareTests : InstrumentedTest {
if (tx is SasVerificationTransaction) {
if (tx.state == VerificationTxState.ShortCodeReady) {
session2ShortCode = tx.getDecimalCodeRepresentation()
Thread.sleep(500)
tx.userHasVerifiedShortCode()
commonTestHelper.runBlockingTest {
delay(500)
tx.userHasVerifiedShortCode()
}
}
}
}
})
val txId = "m.testVerif12"
aliceVerificationService2.beginKeyVerification(VerificationMethod.SAS, aliceSession1.myUserId, aliceSession1.sessionParams.deviceId
?: "", txId)
commonTestHelper.runBlockingTest {
aliceVerificationService2.beginKeyVerification(VerificationMethod.SAS, aliceSession1.myUserId, aliceSession1.sessionParams.deviceId
?: "", txId)
}
commonTestHelper.waitWithLatch { latch ->
commonTestHelper.retryPeriodicallyWithLatch(latch) {
@ -312,7 +326,7 @@ class KeyShareTests : InstrumentedTest {
fun test_ImproperKeyShareBug() {
val aliceSession = commonTestHelper.createAccount(TestConstants.USER_ALICE, SessionTestParams(true))
commonTestHelper.doSync<Unit> {
commonTestHelper.runBlockingTest {
aliceSession.cryptoService().crossSigningService()
.initializeCrossSigning(
object : UserInteractiveAuthInterceptor {
@ -325,7 +339,7 @@ class KeyShareTests : InstrumentedTest {
)
)
}
}, it)
})
}
// Create an encrypted room and send a couple of messages
@ -346,7 +360,7 @@ class KeyShareTests : InstrumentedTest {
// Create bob session
val bobSession = commonTestHelper.createAccount(TestConstants.USER_BOB, SessionTestParams(true))
commonTestHelper.doSync<Unit> {
commonTestHelper.runBlockingTest {
bobSession.cryptoService().crossSigningService()
.initializeCrossSigning(
object : UserInteractiveAuthInterceptor {
@ -359,7 +373,7 @@ class KeyShareTests : InstrumentedTest {
)
)
}
}, it)
})
}
// Let alice invite bob
@ -380,7 +394,10 @@ class KeyShareTests : InstrumentedTest {
val roomRoomBobPov = aliceSession.getRoom(roomId)
val beforeJoin = roomRoomBobPov!!.getTimelineEvent(secondEventId)
var dRes = tryOrNull { bobSession.cryptoService().decryptEvent(beforeJoin!!.root, "") }
var dRes =
commonTestHelper.runBlockingTest {
tryOrNull { bobSession.cryptoService().decryptEvent(beforeJoin!!.root, "") }
}
assert(dRes == null)
@ -391,7 +408,9 @@ class KeyShareTests : InstrumentedTest {
Thread.sleep(3_000)
// With the bug the first session would have improperly reshare that key :/
dRes = tryOrNull { bobSession.cryptoService().decryptEvent(beforeJoin.root, "") }
dRes = commonTestHelper.runBlockingTest {
tryOrNull { bobSession.cryptoService().decryptEvent(beforeJoin.root, "") }
}
Log.d("#TEST", "KS: sgould not decrypt that ${beforeJoin.root.getClearContent().toModel<MessageContent>()?.body}")
assert(dRes?.clearEvent == null)
}

View File

@ -19,6 +19,7 @@ package org.matrix.android.sdk.internal.crypto.gossiping
import android.util.Log
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import kotlinx.coroutines.runBlocking
import org.junit.Assert
import org.junit.FixMethodOrder
import org.junit.Ignore
@ -92,7 +93,9 @@ class WithHeldTests : InstrumentedTest {
// Bob should not be able to decrypt because the keys is withheld
try {
// .. might need to wait a bit for stability?
bobUnverifiedSession.cryptoService().decryptEvent(eventBobPOV.root, "")
runBlocking {
bobUnverifiedSession.cryptoService().decryptEvent(eventBobPOV.root, "")
}
Assert.fail("This session should not be able to decrypt")
} catch (failure: Throwable) {
val type = (failure as MXCryptoError.Base).errorType
@ -117,7 +120,9 @@ class WithHeldTests : InstrumentedTest {
// Previous message should still be undecryptable (partially withheld session)
try {
// .. might need to wait a bit for stability?
bobUnverifiedSession.cryptoService().decryptEvent(eventBobPOV.root, "")
runBlocking {
bobUnverifiedSession.cryptoService().decryptEvent(eventBobPOV.root, "")
}
Assert.fail("This session should not be able to decrypt")
} catch (failure: Throwable) {
val type = (failure as MXCryptoError.Base).errorType
@ -164,7 +169,9 @@ class WithHeldTests : InstrumentedTest {
val eventBobPOV = bobSession.getRoom(testData.roomId)?.getTimelineEvent(eventId)
try {
// .. might need to wait a bit for stability?
bobSession.cryptoService().decryptEvent(eventBobPOV!!.root, "")
runBlocking {
bobSession.cryptoService().decryptEvent(eventBobPOV!!.root, "")
}
Assert.fail("This session should not be able to decrypt")
} catch (failure: Throwable) {
val type = (failure as MXCryptoError.Base).errorType
@ -222,7 +229,7 @@ class WithHeldTests : InstrumentedTest {
cryptoTestHelper.initializeCrossSigning(bobSecondSession)
// Trust bob second device from Alice POV
mTestHelper.runBlockingTest {
testHelper.runBlockingTest {
aliceSession.cryptoService().crossSigningService().trustDevice(bobSecondSession.sessionParams.deviceId!!)
bobSecondSession.cryptoService().crossSigningService().trustDevice(aliceSession.sessionParams.deviceId!!)
}

View File

@ -19,6 +19,7 @@ package org.matrix.android.sdk.internal.crypto.keysbackup
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
@ -164,17 +165,22 @@ class KeysBackupTest : InstrumentedTest {
val latch = CountDownLatch(1)
assertEquals(2, cryptoTestData.firstSession.cryptoService().inboundGroupSessionsCount(false))
assertEquals(0, cryptoTestData.firstSession.cryptoService().inboundGroupSessionsCount(true))
runBlocking {
assertEquals(2, cryptoTestData.firstSession.cryptoService().inboundGroupSessionsCount(false))
assertEquals(0, cryptoTestData.firstSession.cryptoService().inboundGroupSessionsCount(true))
}
val stateObserver = StateObserver(keysBackup, latch, 5)
keysBackupTestHelper.prepareAndCreateKeysBackupData(keysBackup)
testHelper.await(latch)
val nbOfKeys = cryptoTestData.firstSession.cryptoService().inboundGroupSessionsCount(false)
val backedUpKeys = cryptoTestData.firstSession.cryptoService().inboundGroupSessionsCount(true)
val nbOfKeys = runBlocking {
cryptoTestData.firstSession.cryptoService().inboundGroupSessionsCount(false)
}
val backedUpKeys = runBlocking {
cryptoTestData.firstSession.cryptoService().inboundGroupSessionsCount(true)
}
assertEquals(2, nbOfKeys)
assertEquals("All keys must have been marked as backed up", nbOfKeys, backedUpKeys)
@ -833,9 +839,12 @@ class KeysBackupTest : InstrumentedTest {
assertEquals(1, keysBackupVersionTrust.signatures.size)
val signature = keysBackupVersionTrust.signatures[0]
val device = runBlocking {
cryptoTestData.firstSession.cryptoService().getMyDevice()
}
assertTrue(signature.valid)
assertNotNull(signature.device)
assertEquals(cryptoTestData.firstSession.cryptoService().getMyDevice().deviceId, signature.deviceId)
assertEquals(device.deviceId, signature.deviceId)
assertEquals(signature.device!!.deviceId, cryptoTestData.firstSession.sessionParams.deviceId)
stateObserver.stopAndCheckStates(null)

View File

@ -80,7 +80,10 @@ class KeysBackupTestHelper(
}
}
Assert.assertEquals(2, cryptoTestData.firstSession.cryptoService().keysBackupService().getTotalNumbersOfBackedUpKeys())
val totalNumbersOfBackedUpKeys = testHelper.runBlockingTest {
cryptoTestData.firstSession.cryptoService().keysBackupService().getTotalNumbersOfBackedUpKeys()
}
Assert.assertEquals(2, totalNumbersOfBackedUpKeys)
val aliceUserId = cryptoTestData.firstSession.myUserId
@ -88,15 +91,21 @@ class KeysBackupTestHelper(
val aliceSession2 = testHelper.logIntoAccount(aliceUserId, KeysBackupTestConstants.defaultSessionParamsWithInitialSync)
// Test check: aliceSession2 has no keys at login
Assert.assertEquals(0, aliceSession2.cryptoService().inboundGroupSessionsCount(false))
val inboundGroupSessionCount = testHelper.runBlockingTest {
aliceSession2.cryptoService().inboundGroupSessionsCount(false)
}
Assert.assertEquals(0, inboundGroupSessionCount)
// Wait for backup state to be NotTrusted
waitForKeysBackupToBeInState(aliceSession2, KeysBackupState.NotTrusted)
stateObserver.stopAndCheckStates(null)
val totalNumbersOfBackedUpKeysFromNewSession = testHelper.runBlockingTest {
aliceSession2.cryptoService().keysBackupService().getTotalNumbersOfBackedUpKeys()
}
return KeysBackupScenarioData(cryptoTestData,
aliceSession2.cryptoService().keysBackupService().getTotalNumbersOfBackedUpKeys(),
totalNumbersOfBackedUpKeysFromNewSession,
prepareKeysBackupDataResult,
aliceSession2)
}
@ -182,7 +191,10 @@ class KeysBackupTestHelper(
Assert.assertEquals(total, imported)
// - The new device must have the same count of megolm keys
Assert.assertEquals(testData.aliceKeys, testData.aliceSession2.cryptoService().inboundGroupSessionsCount(false))
val inboundGroupSessionCount = testHelper.runBlockingTest {
testData.aliceSession2.cryptoService().inboundGroupSessionsCount(false)
}
Assert.assertEquals(testData.aliceKeys, inboundGroupSessionCount)
// - Alice must have the same keys on both devices
//