From eb0faa3484b62f6419e5708531fff2a1d451d804 Mon Sep 17 00:00:00 2001 From: ganfra Date: Thu, 2 Jun 2022 15:38:28 +0200 Subject: [PATCH] Fix some E2E tests --- .../android/sdk/common/CryptoTestHelper.kt | 75 +++++++++++++- .../sdk/internal/crypto/E2eeSanityTests.kt | 98 ++----------------- .../internal/crypto/DefaultCryptoService.kt | 4 +- 3 files changed, 82 insertions(+), 95 deletions(-) diff --git a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/common/CryptoTestHelper.kt b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/common/CryptoTestHelper.kt index ef190f5a2e..dce0a58278 100644 --- a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/common/CryptoTestHelper.kt +++ b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/common/CryptoTestHelper.kt @@ -50,7 +50,9 @@ import org.matrix.android.sdk.api.session.events.model.EventType import org.matrix.android.sdk.api.session.events.model.toContent import org.matrix.android.sdk.api.session.events.model.toModel import org.matrix.android.sdk.api.session.getRoom +import org.matrix.android.sdk.api.session.getRoomSummary import org.matrix.android.sdk.api.session.room.Room +import org.matrix.android.sdk.api.session.room.failure.JoinRoomFailure import org.matrix.android.sdk.api.session.room.model.Membership import org.matrix.android.sdk.api.session.room.model.RoomSummary import org.matrix.android.sdk.api.session.room.model.create.CreateRoomParams @@ -149,6 +151,76 @@ class CryptoTestHelper(private val testHelper: CommonTestHelper) { return CryptoTestData(aliceRoomId, listOf(aliceSession, bobSession)) } + fun inviteNewUsersAndWaitForThemToJoin(session: Session, roomId: String, usernames: List): List { + val newSessions = usernames.map { username -> + testHelper.createAccount(username, SessionTestParams(true)).also { + it.cryptoService().enableKeyGossiping(false) + } + } + + val room = session.getRoom(roomId)!! + + Log.v("#E2E TEST", "All accounts created") + // we want to invite them in the room + newSessions.forEach { newSession -> + testHelper.runBlockingTest { + Log.v("#E2E TEST", "${session.myUserId} invites ${newSession.myUserId}") + room.membershipService().invite(newSession.myUserId) + } + } + + // All user should accept invite + newSessions.forEach { newSession -> + waitForAndAcceptInviteInRoom(newSession, roomId) + Log.v("#E2E TEST", "${newSession.myUserId} joined room $roomId") + } + ensureMembersHaveJoined(session, newSessions, roomId) + return newSessions + } + + private fun ensureMembersHaveJoined(session: Session, invitedUserSessions: List, roomId: String) { + testHelper.waitWithLatch { latch -> + testHelper.retryPeriodicallyWithLatch(latch) { + invitedUserSessions.map { invitedUserSession -> + session.roomService().getRoomMember(invitedUserSession.myUserId, roomId)?.membership + }.all { + it == Membership.JOIN + } + } + } + } + + private fun waitForAndAcceptInviteInRoom(session: Session, roomId: String) { + testHelper.waitWithLatch { latch -> + testHelper.retryPeriodicallyWithLatch(latch) { + val roomSummary = session.getRoomSummary(roomId) + (roomSummary != null && roomSummary.membership == Membership.INVITE).also { + if (it) { + Log.v("#E2E TEST", "${session.myUserId} can see the invite from ${roomSummary?.inviterId}") + } + } + } + } + + // not sure why it's taking so long :/ + testHelper.runBlockingTest(90_000) { + Log.v("#E2E TEST", "${session.myUserId} tries to join room $roomId") + try { + session.roomService().joinRoom(roomId) + } catch (ex: JoinRoomFailure.JoinedWithTimeout) { + // it's ok we will wait after + } + } + + Log.v("#E2E TEST", "${session.myUserId} waiting for join echo ...") + testHelper.waitWithLatch { + testHelper.retryPeriodicallyWithLatch(it) { + val roomSummary = session.getRoomSummary(roomId) + roomSummary != null && roomSummary.membership == Membership.JOIN + } + } + } + /** * @return Alice, Bob and Sam session */ @@ -320,7 +392,8 @@ class CryptoTestHelper(private val testHelper: CommonTestHelper) { aliceVerificationService.beginKeyVerification( VerificationMethod.SAS, roomId, - bob.myUserId,) + bob.myUserId, + ) } // we should reach SHOW SAS on both 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 fb98ebe182..74420d01b7 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 @@ -43,11 +43,8 @@ import org.matrix.android.sdk.api.session.events.model.content.EncryptedEventCon import org.matrix.android.sdk.api.session.events.model.content.WithHeldCode import org.matrix.android.sdk.api.session.events.model.toModel import org.matrix.android.sdk.api.session.getRoom -import org.matrix.android.sdk.api.session.getRoomSummary import org.matrix.android.sdk.api.session.room.Room -import org.matrix.android.sdk.api.session.room.failure.JoinRoomFailure import org.matrix.android.sdk.api.session.room.getTimelineEvent -import org.matrix.android.sdk.api.session.room.model.Membership import org.matrix.android.sdk.api.session.room.model.message.MessageContent import org.matrix.android.sdk.api.session.room.send.SendState import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings @@ -88,36 +85,14 @@ class E2eeSanityTests : InstrumentedTest { // add some more users and invite them val otherAccounts = listOf("benoit", "valere", "ganfra") // , "adam", "manu") - .map { - testHelper.createAccount(it, SessionTestParams(true)).also { - it.cryptoService().enableKeyGossiping(false) - } + .let { + cryptoTestHelper.inviteNewUsersAndWaitForThemToJoin(aliceSession, e2eRoomID, it) } - Log.v("#E2E TEST", "All accounts created") - // we want to invite them in the room - otherAccounts.forEach { - testHelper.runBlockingTest { - Log.v("#E2E TEST", "Alice invites ${it.myUserId}") - aliceRoomPOV.membershipService().invite(it.myUserId) - } - } - - // All user should accept invite - otherAccounts.forEach { otherSession -> - waitForAndAcceptInviteInRoom(testHelper, otherSession, e2eRoomID) - Log.v("#E2E TEST", "${otherSession.myUserId} joined room $e2eRoomID") - } - - // check that alice see them as joined (not really necessary?) - ensureMembersHaveJoined(testHelper, aliceSession, otherAccounts, e2eRoomID) - - Log.v("#E2E TEST", "All users have joined the room") Log.v("#E2E TEST", "Alice is sending the message") val text = "This is my message" val sentEventId: String? = sendMessageInRoom(testHelper, aliceRoomPOV, text) - // val sentEvent = testHelper.sendTextMessage(aliceRoomPOV, "Hello all", 1).first() Assert.assertTrue("Message should be sent", sentEventId != null) // All should be able to decrypt @@ -131,31 +106,13 @@ class E2eeSanityTests : InstrumentedTest { } } } - + Log.v("#E2E TEST", "Everybody received the encrypted message and could decrypt") // Add a new user to the room, and check that he can't decrypt val newAccount = listOf("adam") // , "adam", "manu") - .map { - testHelper.createAccount(it, SessionTestParams(true)) + .let { + cryptoTestHelper.inviteNewUsersAndWaitForThemToJoin(aliceSession, e2eRoomID, it) } - newAccount.forEach { - testHelper.runBlockingTest { - Log.v("#E2E TEST", "Alice invites ${it.myUserId}") - aliceRoomPOV.membershipService().invite(it.myUserId) - } - } - - newAccount.forEach { - waitForAndAcceptInviteInRoom(testHelper, it, e2eRoomID) - } - - ensureMembersHaveJoined(testHelper, aliceSession, newAccount, e2eRoomID) - - // wait a bit - testHelper.runBlockingTest { - delay(3_000) - } - // check that messages are encrypted (uisi) newAccount.forEach { otherSession -> testHelper.waitWithLatch { latch -> @@ -165,7 +122,7 @@ class E2eeSanityTests : InstrumentedTest { } timelineEvent != null && timelineEvent.root.getClearType() == EventType.ENCRYPTED && - timelineEvent.root.mCryptoError == MXCryptoError.ErrorType.UNKNOWN_INBOUND_SESSION_ID + timelineEvent.root.mCryptoError == MXCryptoError.ErrorType.UNABLE_TO_DECRYPT } } } @@ -747,49 +704,6 @@ class E2eeSanityTests : InstrumentedTest { testHelper.signOutAndClose(aliceNewSession) } - private fun ensureMembersHaveJoined(testHelper: CommonTestHelper, aliceSession: Session, otherAccounts: List, e2eRoomID: String) { - testHelper.waitWithLatch { latch -> - testHelper.retryPeriodicallyWithLatch(latch) { - otherAccounts.map { - aliceSession.roomService().getRoomMember(it.myUserId, e2eRoomID)?.membership - }.all { - it == Membership.JOIN - } - } - } - } - - private fun waitForAndAcceptInviteInRoom(testHelper: CommonTestHelper, otherSession: Session, e2eRoomID: String) { - testHelper.waitWithLatch { latch -> - testHelper.retryPeriodicallyWithLatch(latch) { - val roomSummary = otherSession.getRoomSummary(e2eRoomID) - (roomSummary != null && roomSummary.membership == Membership.INVITE).also { - if (it) { - Log.v("#E2E TEST", "${otherSession.myUserId} can see the invite from alice") - } - } - } - } - - // not sure why it's taking so long :/ - testHelper.runBlockingTest(90_000) { - Log.v("#E2E TEST", "${otherSession.myUserId} tries to join room $e2eRoomID") - try { - otherSession.roomService().joinRoom(e2eRoomID) - } catch (ex: JoinRoomFailure.JoinedWithTimeout) { - // it's ok we will wait after - } - } - - Log.v("#E2E TEST", "${otherSession.myUserId} waiting for join echo ...") - testHelper.waitWithLatch { - testHelper.retryPeriodicallyWithLatch(it) { - val roomSummary = otherSession.getRoomSummary(e2eRoomID) - roomSummary != null && roomSummary.membership == Membership.JOIN - } - } - } - private fun ensureIsDecrypted(testHelper: CommonTestHelper, sentEventIds: List, session: Session, e2eRoomID: String) { testHelper.waitWithLatch { latch -> sentEventIds.forEach { sentEventId -> diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt index 833843812e..8a207628fc 100755 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt @@ -625,11 +625,11 @@ internal class DefaultCryptoService @Inject constructor( } override fun enableKeyGossiping(enable: Boolean) { - TODO("Not yet implemented") + cryptoStore.enableKeyGossiping(enable) } override fun isKeyGossipingEnabled(): Boolean { - TODO("Not yet implemented") + return cryptoStore.isKeyGossipingEnabled() } /**