diff --git a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/crypto/E2eeSanityTests.kt b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/crypto/E2eeSanityTests.kt index dbff33dd7a..fb98ebe182 100644 --- a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/crypto/E2eeSanityTests.kt +++ b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/crypto/E2eeSanityTests.kt @@ -55,6 +55,7 @@ import org.matrix.android.sdk.common.CommonTestHelper import org.matrix.android.sdk.common.CryptoTestHelper import org.matrix.android.sdk.common.SessionTestParams import org.matrix.android.sdk.common.TestConstants +import org.matrix.android.sdk.internal.crypto.verification.SasVerificationTestHelper import java.util.concurrent.CountDownLatch @RunWith(JUnit4::class) @@ -610,7 +611,6 @@ class E2eeSanityTests : InstrumentedTest { fun testSelfInteractiveVerificationAndGossip() { val testHelper = CommonTestHelper(context()) val cryptoTestHelper = CryptoTestHelper(testHelper) - val aliceSession = testHelper.createAccount("alice", SessionTestParams(true)) cryptoTestHelper.bootstrapSecurity(aliceSession) @@ -618,18 +618,18 @@ class E2eeSanityTests : InstrumentedTest { val aliceNewSession = testHelper.logIntoAccount(aliceSession.myUserId, SessionTestParams(true)) + val transactionId = SasVerificationTestHelper(testHelper, cryptoTestHelper).requestSelfKeyAndWaitForReadyState( + aliceSession, + aliceNewSession, + listOf(VerificationMethod.SAS) + ) + val oldCompleteLatch = CountDownLatch(1) lateinit var oldCode: String aliceSession.cryptoService().verificationService().addListener(object : VerificationService.Listener { override fun verificationRequestUpdated(pr: PendingVerificationRequest) { - val readyInfo = pr.readyInfo ?: return - testHelper.runBlockingTest { - aliceSession.cryptoService().verificationService().beginDeviceVerification( - aliceSession.myUserId, - readyInfo.fromDevice - ) - } + Log.d("##TEST", "existingPov: $pr") } override fun transactionUpdated(tx: VerificationTransaction) { @@ -656,15 +656,8 @@ class E2eeSanityTests : InstrumentedTest { lateinit var newCode: String aliceNewSession.cryptoService().verificationService().addListener(object : VerificationService.Listener { - override fun verificationRequestCreated(pr: PendingVerificationRequest) { - // let's ready - testHelper.runBlockingTest { - aliceNewSession.cryptoService().verificationService().readyPendingVerification( - listOf(VerificationMethod.SAS, VerificationMethod.QR_CODE_SCAN, VerificationMethod.QR_CODE_SHOW), - aliceSession.myUserId, - pr.transactionId!! - ) - } + override fun verificationRequestUpdated(pr: PendingVerificationRequest) { + Log.d("##TEST", "newPov: $pr") } var matchOnce = true @@ -689,11 +682,8 @@ class E2eeSanityTests : InstrumentedTest { } }) - // initiate self verification testHelper.runBlockingTest { - aliceSession.cryptoService().verificationService().requestSelfKeyVerification( - listOf(VerificationMethod.SAS, VerificationMethod.QR_CODE_SCAN, VerificationMethod.QR_CODE_SHOW) - ) + aliceSession.cryptoService().verificationService().beginKeyVerification(VerificationMethod.SAS, aliceNewSession.myUserId, transactionId) } testHelper.await(oldCompleteLatch) testHelper.await(newCompleteLatch) diff --git a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/crypto/verification/SasVerificationTestHelper.kt b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/crypto/verification/SasVerificationTestHelper.kt index 52075c9497..eed502aad0 100644 --- a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/crypto/verification/SasVerificationTestHelper.kt +++ b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/crypto/verification/SasVerificationTestHelper.kt @@ -16,6 +16,7 @@ package org.matrix.android.sdk.internal.crypto.verification +import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.crypto.verification.PendingVerificationRequest import org.matrix.android.sdk.api.session.crypto.verification.VerificationMethod import org.matrix.android.sdk.api.session.crypto.verification.VerificationService @@ -83,4 +84,49 @@ class SasVerificationTestHelper(private val testHelper: CommonTestHelper, privat aliceVerificationService.removeListener(aliceListener) return bobReadyPendingVerificationRequest?.transactionId!! } + + fun requestSelfKeyAndWaitForReadyState(session1: Session, session2: Session, supportedMethods: List): String { + + val session1VerificationService = session1.cryptoService().verificationService() + val session2VerificationService = session2.cryptoService().verificationService() + var bobReadyPendingVerificationRequest: PendingVerificationRequest? = null + + val latch = CountDownLatch(2) + val aliceListener = object : VerificationService.Listener { + override fun verificationRequestUpdated(pr: PendingVerificationRequest) { + if (pr.isReady) { + latch.countDown() + } + } + } + session1VerificationService.addListener(aliceListener) + + val bobListener = object : VerificationService.Listener { + override fun verificationRequestCreated(pr: PendingVerificationRequest) { + testHelper.runBlockingTest { + session2VerificationService.readyPendingVerification( + supportedMethods, + session1.myUserId, + pr.transactionId!! + ) + } + } + + override fun verificationRequestUpdated(pr: PendingVerificationRequest) { + Timber.v("Bob request updated $pr") + if (pr.isReady) { + bobReadyPendingVerificationRequest = pr + latch.countDown() + } + } + } + session2VerificationService.addListener(bobListener) + testHelper.runBlockingTest { + session1VerificationService.requestSelfKeyVerification(supportedMethods) + } + testHelper.await(latch) + session2VerificationService.removeListener(bobListener) + session1VerificationService.removeListener(aliceListener) + return bobReadyPendingVerificationRequest?.transactionId!! + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/QrCodeVerification.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/QrCodeVerification.kt index 63a1047451..a81f4a1bd1 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/QrCodeVerification.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/QrCodeVerification.kt @@ -211,4 +211,8 @@ internal class QrCodeVerification( return } + + override fun toString(): String { + return "QrCodeVerification(qrCodeText=$qrCodeText, state=$state, transactionId='$transactionId', otherUserId='$otherUserId', otherDeviceId=$otherDeviceId, isIncoming=$isIncoming)" + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/SasVerification.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/SasVerification.kt index 89a179ea09..731e85d01e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/SasVerification.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/SasVerification.kt @@ -233,4 +233,8 @@ internal class SasVerification( return } + + override fun toString(): String { + return "SasVerification(otherUserId='$otherUserId', otherDeviceId=$otherDeviceId, isIncoming=$isIncoming, state=$state, transactionId='$transactionId')" + } }