From f3fb07079ea9f596350f6eddb283355c185839ae Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 7 Jan 2020 18:46:05 +0100 Subject: [PATCH] Cleanup tests --- .../android/account/AccountCreationTest.kt | 9 + .../matrix/android/common/CommonTestHelper.kt | 145 ++-- .../matrix/android/common/CryptoTestHelper.kt | 658 +++++++++--------- .../android/common/SessionTestParams.kt | 5 +- 4 files changed, 371 insertions(+), 446 deletions(-) diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/account/AccountCreationTest.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/account/AccountCreationTest.kt index ab3ddaf083..c44ac9c47b 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/account/AccountCreationTest.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/account/AccountCreationTest.kt @@ -18,6 +18,7 @@ package im.vector.matrix.android.account import im.vector.matrix.android.InstrumentedTest import im.vector.matrix.android.common.CommonTestHelper +import im.vector.matrix.android.common.CryptoTestHelper import im.vector.matrix.android.common.SessionTestParams import im.vector.matrix.android.common.TestConstants import org.junit.FixMethodOrder @@ -31,6 +32,7 @@ import org.junit.runners.MethodSorters class AccountCreationTest : InstrumentedTest { private val commonTestHelper = CommonTestHelper(context()) + private val cryptoTestHelper = CryptoTestHelper(commonTestHelper) @Test fun createAccountTest() { @@ -51,4 +53,11 @@ class AccountCreationTest : InstrumentedTest { session.close() session2.close() } + + @Test + fun simpleE2eTest() { + val res = cryptoTestHelper.doE2ETestWithAliceInARoom() + + res.close() + } } diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt index 453b41345b..b19d4ee5fb 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt @@ -25,8 +25,12 @@ import im.vector.matrix.android.api.auth.data.HomeServerConnectionConfig import im.vector.matrix.android.api.auth.data.LoginFlowResult import im.vector.matrix.android.api.auth.registration.RegistrationResult import im.vector.matrix.android.api.session.Session -import org.junit.Assert.assertNotNull -import org.junit.Assert.assertTrue +import im.vector.matrix.android.api.session.events.model.EventType +import im.vector.matrix.android.api.session.room.Room +import im.vector.matrix.android.api.session.room.timeline.Timeline +import im.vector.matrix.android.api.session.room.timeline.TimelineEvent +import im.vector.matrix.android.api.session.room.timeline.TimelineSettings +import org.junit.Assert.* import java.util.* import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit @@ -88,50 +92,43 @@ class CommonTestHelper(context: Context) { //session.syncState().removeObserver(observer) } -// /** -// * Sends text messages in a room -// * -// * @param room the room where to send the messages -// * @param message the message to send -// * @param nbOfMessages the number of time the message will be sent -// * @throws Exception -// */ -// @Throws(Exception::class) -// fun sendTextMessage(room: Room, message: String, nbOfMessages: Int): List { -// val sentEvents = ArrayList(nbOfMessages) -// val latch = CountDownLatch(nbOfMessages) -// val onEventSentListener = object : MXEventListener() { -// fun onEventSent(event: Event, prevEventId: String) { -// latch.countDown() -// } -// } -// room.addEventListener(onEventSentListener) -// for (i in 0 until nbOfMessages) { -// room.sendTextMessage(message + " #" + (i + 1), null, Message.FORMAT_MATRIX_HTML, object : RoomMediaMessage.EventCreationListener() { -// fun onEventCreated(roomMediaMessage: RoomMediaMessage) { -// val sentEvent = roomMediaMessage.getEvent() -// sentEvents.add(sentEvent) -// } -// -// fun onEventCreationFailed(roomMediaMessage: RoomMediaMessage, errorMessage: String) { -// -// } -// -// fun onEncryptionFailed(roomMediaMessage: RoomMediaMessage) { -// -// } -// }) -// } -// await(latch) -// room.removeEventListener(onEventSentListener) -// -// // Check that all events has been created -// Assert.assertEquals(nbOfMessages.toLong(), sentEvents.size.toLong()) -// -// return sentEvents -// } -// -// + /** + * Sends text messages in a room + * + * @param room the room where to send the messages + * @param message the message to send + * @param nbOfMessages the number of time the message will be sent + */ + fun sendTextMessage(room: Room, message: String, nbOfMessages: Int): List { + val sentEvents = ArrayList(nbOfMessages) + val latch = CountDownLatch(nbOfMessages) + val onEventSentListener = object : Timeline.Listener { + override fun onTimelineFailure(throwable: Throwable) { + } + + override fun onTimelineUpdated(snapshot: List) { + // TODO Count only new messages? + if (snapshot.count { it.root.type == EventType.MESSAGE } == nbOfMessages) { + sentEvents.addAll(snapshot.filter { it.root.type == EventType.MESSAGE }) + latch.countDown() + } + } + } + val timeline = room.createTimeline(null, TimelineSettings(10)) + timeline.addListener(onEventSentListener) + for (i in 0 until nbOfMessages) { + room.sendTextMessage(message + " #" + (i + 1)) + } + await(latch) + timeline.removeListener(onEventSentListener) + + // Check that all events has been created + assertEquals(nbOfMessages.toLong(), sentEvents.size.toLong()) + + return sentEvents + } + + // PRIVATE METHODS ***************************************************************************** /** @@ -277,60 +274,4 @@ class CommonTestHelper(context: Context) { session.signOut(true, object : TestMatrixCallback(lock) {}) await(lock) } - - -// /** -// * Clone a session. -// * It simulate that the user launches again the application with the same Credentials, contrary to login which will create a new DeviceId -// * -// * @param from the session to clone -// * @return the duplicated session -// */ -// @Throws(InterruptedException::class) -// fun createNewSession(from: Session, sessionTestParams: SessionTestParams): Session { -// val context = InstrumentationRegistry.getContext() -// -// val credentials = from.sessionParams.credentials -// val hs = createHomeServerConfig(credentials) -// val store = MXFileStore(hs, false, context) -// val dataHandler = MXDataHandler(store, credentials) -// dataHandler.setLazyLoadingEnabled(sessionTestParams.withLazyLoading) -// store.setDataHandler(dataHandler) -// val session2 = Session.Builder(hs, dataHandler, context) -// .withLegacyCryptoStore(sessionTestParams.withLegacyCryptoStore) -// .build() -// -// val results = HashMap() -// -// val lock = CountDownLatch(1) -// val listener = object : MXStoreListener() { -// fun postProcess(accountId: String) { -// results["postProcess"] = "postProcess $accountId" -// } -// -// fun onStoreReady(accountId: String) { -// results["onStoreReady"] = "onStoreReady" -// lock.countDown() -// } -// -// fun onStoreCorrupted(accountId: String, description: String) { -// results["onStoreCorrupted"] = description -// lock.countDown() -// } -// -// fun onStoreOOM(accountId: String, description: String) { -// results["onStoreOOM"] = "onStoreOOM" -// lock.countDown() -// } -// } -// -// store.addMXStoreListener(listener) -// store.open() -// -// await(lock) -// -// Assert.assertTrue(results.toString(), results.containsKey("onStoreReady")) -// -// return session2 -// } } diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CryptoTestHelper.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CryptoTestHelper.kt index 804e97857e..4ba421e826 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CryptoTestHelper.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CryptoTestHelper.kt @@ -16,343 +16,321 @@ package im.vector.matrix.android.common -// import android.os.SystemClock -// import android.text.TextUtils -// import im.vector.matrix.android.api.session.Session -// import im.vector.matrix.android.api.session.events.model.Event -// import im.vector.matrix.android.api.session.events.model.EventType -// import im.vector.matrix.android.api.session.events.model.toContent -// import im.vector.matrix.android.api.session.room.model.create.CreateRoomParams -// import im.vector.matrix.android.api.session.room.model.message.MessageTextContent -// import im.vector.matrix.android.api.session.room.model.message.MessageType -// import im.vector.matrix.android.internal.crypto.MXCRYPTO_ALGORITHM_MEGOLM -// import im.vector.matrix.android.internal.crypto.MXCRYPTO_ALGORITHM_MEGOLM_BACKUP -// import im.vector.matrix.android.internal.crypto.keysbackup.model.MegolmBackupAuthData -// import im.vector.matrix.android.internal.crypto.keysbackup.model.MegolmBackupCreationInfo -// import org.junit.Assert.* -// import java.util.* -// import java.util.concurrent.CountDownLatch -// -// class CryptoTestHelper(val mTestHelper: CommonTestHelper) { -// -// val messagesFromAlice: List = Arrays.asList("0 - Hello I'm Alice!", "4 - Go!") -// val messagesFromBob: List = Arrays.asList("1 - Hello I'm Bob!", "2 - Isn't life grand?", "3 - Let's go to the opera.") -// -// // Set this value to false to test the new Realm store and to true to test legacy Filestore -// val USE_LEGACY_CRYPTO_STORE = false -// -// // Lazy loading is on by default now -// private val LAZY_LOADING_ENABLED = true -// -// val defaultSessionParams = SessionTestParams(true, false, LAZY_LOADING_ENABLED, USE_LEGACY_CRYPTO_STORE) -// val encryptedSessionParams = SessionTestParams(true, true, LAZY_LOADING_ENABLED, USE_LEGACY_CRYPTO_STORE) -// -// fun buildTextEvent(text: String, session: Session, roomId: String): Event { -// val message = MessageTextContent( -// MessageType.MSGTYPE_TEXT, -// text -// ) -// -// return Event( -// type = EventType.MESSAGE, -// content = message.toContent(), -// senderId = session.myUserId, -// roomId = roomId) -// } -// -// /** -// * @return alice session -// */ -// fun doE2ETestWithAliceInARoom(): CryptoTestData { -// val results = HashMap() -// val aliceSession = mTestHelper.createAccount(TestConstants.USER_ALICE, defaultSessionParams) -// -// var roomId: String? = null -// val lock1 = CountDownLatch(1) -// -// aliceSession.createRoom(CreateRoomParams().apply { name = "MyRoom" }, object : TestMatrixCallback(lock1) { -// override fun onSuccess(data: String) { -// roomId = data -// super.onSuccess(data) -// } -// }) -// -// mTestHelper.await(lock1) -// assertNotNull(roomId) -// -// val room = aliceSession.getRoom(roomId!!) -// -// val lock2 = CountDownLatch(1) -// room.enableEncryptionWithAlgorithm(MXCRYPTO_ALGORITHM_MEGOLM, object : TestMatrixCallback(lock2) { -// override fun onSuccess(data: Void?) { -// results["enableEncryptionWithAlgorithm"] = "enableEncryptionWithAlgorithm" -// super.onSuccess(data) -// } -// }) -// mTestHelper.await(lock2) -// assertTrue(results.containsKey("enableEncryptionWithAlgorithm")) -// -// return CryptoTestData(aliceSession, roomId!!) -// } -// -// /** -// * @param cryptedBob -// * @return alice and bob sessions -// */ -// fun doE2ETestWithAliceAndBobInARoom(cryptedBob: Boolean = true): CryptoTestData { -// val statuses = HashMap() -// -// val cryptoTestData = doE2ETestWithAliceInARoom() -// val aliceSession = cryptoTestData.firstSession -// val aliceRoomId = cryptoTestData.roomId -// -// val room = aliceSession.getRoom(aliceRoomId)!! -// -// val bobSession = mTestHelper.createAccount(TestConstants.USER_BOB, defaultSessionParams) -// -// val lock1 = CountDownLatch(2) -// -// val bobEventListener = object : MXEventListener() { -// override fun onNewRoom(roomId: String) { -// if (TextUtils.equals(roomId, aliceRoomId)) { -// if (!statuses.containsKey("onNewRoom")) { -// statuses["onNewRoom"] = "onNewRoom" -// lock1.countDown() -// } -// } -// } -// } -// -// bobSession.dataHandler.addListener(bobEventListener) -// -// room.invite(bobSession.myUserId, callback = object : TestMatrixCallback(lock1) { -// override fun onSuccess(data: Unit) { -// statuses["invite"] = "invite" -// super.onSuccess(data) -// } -// }) -// -// mTestHelper.await(lock1) -// -// assertTrue(statuses.containsKey("invite") && statuses.containsKey("onNewRoom")) -// -// bobSession.dataHandler.removeListener(bobEventListener) -// -// val lock2 = CountDownLatch(2) -// -// bobSession.joinRoom(aliceRoomId, callback = TestMatrixCallback(lock2)) -// -// room.addEventListener(object : MXEventListener() { -// override fun onLiveEvent(event: Event, roomState: RoomState) { -// if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_STATE_ROOM_MEMBER)) { -// val contentToConsider = event.contentAsJsonObject -// val member = JsonUtils.toRoomMember(contentToConsider) -// -// if (TextUtils.equals(member.membership, RoomMember.MEMBERSHIP_JOIN)) { -// statuses["AliceJoin"] = "AliceJoin" -// lock2.countDown() -// } -// } -// } -// }) -// -// mTestHelper.await(lock2) -// -// // Ensure bob can send messages to the room -// val roomFromBobPOV = bobSession.getRoom(aliceRoomId)!! -// assertNotNull(roomFromBobPOV.state.powerLevels) -// assertTrue(roomFromBobPOV.state.powerLevels.maySendMessage(bobSession.myUserId)) -// -// assertTrue(statuses.toString() + "", statuses.containsKey("AliceJoin")) -// -// bobSession.dataHandler.removeListener(bobEventListener) -// -// return CryptoTestData(aliceSession, aliceRoomId, bobSession) -// } -// -// /** -// * @return Alice, Bob and Sam session -// */ -// fun doE2ETestWithAliceAndBobAndSamInARoom(): CryptoTestData { -// val statuses = HashMap() -// -// val cryptoTestData = doE2ETestWithAliceAndBobInARoom(true) -// val aliceSession = cryptoTestData.firstSession -// val aliceRoomId = cryptoTestData.roomId -// -// val room = aliceSession.getRoom(aliceRoomId)!! -// -// val samSession = mTestHelper.createAccount(TestConstants.USER_SAM, defaultSessionParams) -// -// val lock1 = CountDownLatch(2) -// -// val samEventListener = object : MXEventListener() { -// override fun onNewRoom(roomId: String) { -// if (TextUtils.equals(roomId, aliceRoomId)) { -// if (!statuses.containsKey("onNewRoom")) { -// statuses["onNewRoom"] = "onNewRoom" -// lock1.countDown() -// } -// } -// } -// } -// -// samSession.dataHandler.addListener(samEventListener) -// -// room.invite(aliceSession, samSession.myUserId, object : TestMatrixCallback(lock1) { -// override fun onSuccess(info: Void?) { -// statuses["invite"] = "invite" -// super.onSuccess(info) -// } -// }) -// -// mTestHelper.await(lock1) -// -// assertTrue(statuses.containsKey("invite") && statuses.containsKey("onNewRoom")) -// -// samSession.dataHandler.removeListener(samEventListener) -// -// val lock2 = CountDownLatch(1) -// -// samSession.joinRoom(aliceRoomId, object : TestMatrixCallback(lock2) { -// override fun onSuccess(info: String) { -// statuses["joinRoom"] = "joinRoom" -// super.onSuccess(info) -// } -// }) -// -// mTestHelper.await(lock2) -// assertTrue(statuses.containsKey("joinRoom")) -// -// // wait the initial sync -// SystemClock.sleep(1000) -// -// samSession.dataHandler.removeListener(samEventListener) -// -// return CryptoTestData(aliceSession, aliceRoomId, cryptoTestData.secondSession, samSession) -// } -// -// /** -// * @param cryptedBob -// * @return Alice and Bob sessions -// */ -// fun doE2ETestWithAliceAndBobInARoomWithEncryptedMessages(cryptedBob: Boolean): CryptoTestData { -// val cryptoTestData = doE2ETestWithAliceAndBobInARoom(cryptedBob) -// val aliceSession = cryptoTestData.firstSession -// val aliceRoomId = cryptoTestData.roomId -// val bobSession = cryptoTestData.secondSession!! -// -// bobSession.setWarnOnUnknownDevices(false) -// -// aliceSession.setWarnOnUnknownDevices(false) -// -// val roomFromBobPOV = bobSession.getRoom(aliceRoomId)!! -// val roomFromAlicePOV = aliceSession.getRoom(aliceRoomId)!! -// -// var messagesReceivedByBobCount = 0 -// var lock = CountDownLatch(3) -// -// val bobEventsListener = object : MXEventListener() { -// override fun onLiveEvent(event: Event, roomState: RoomState) { -// if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE) && !TextUtils.equals(event.getSender(), bobSession.myUserId)) { -// messagesReceivedByBobCount++ -// lock.countDown() -// } -// } -// } -// -// roomFromBobPOV.addEventListener(bobEventsListener) -// -// val results = HashMap() -// -// bobSession.dataHandler.addListener(object : MXEventListener() { -// override fun onToDeviceEvent(event: Event) { -// results["onToDeviceEvent"] = event -// lock.countDown() -// } -// }) -// -// // Alice sends a message -// roomFromAlicePOV.sendEvent(buildTextEvent(messagesFromAlice[0], aliceSession, aliceRoomId), TestMatrixCallback(lock, true)) -// mTestHelper.await(lock) -// assertTrue(results.containsKey("onToDeviceEvent")) -// assertEquals(1, messagesReceivedByBobCount) -// -// // Bob send a message -// lock = CountDownLatch(1) -// roomFromBobPOV.sendEvent(buildTextEvent(messagesFromBob[0], bobSession, aliceRoomId), TestMatrixCallback(lock, true)) -// // android does not echo the messages sent from itself -// messagesReceivedByBobCount++ -// mTestHelper.await(lock) -// assertEquals(2, messagesReceivedByBobCount) -// -// // Bob send a message -// lock = CountDownLatch(1) -// roomFromBobPOV.sendEvent(buildTextEvent(messagesFromBob[1], bobSession, aliceRoomId), TestMatrixCallback(lock, true)) -// // android does not echo the messages sent from itself -// messagesReceivedByBobCount++ -// mTestHelper.await(lock) -// assertEquals(3, messagesReceivedByBobCount) -// -// // Bob send a message -// lock = CountDownLatch(1) -// roomFromBobPOV.sendEvent(buildTextEvent(messagesFromBob[2], bobSession, aliceRoomId), TestMatrixCallback(lock, true)) -// // android does not echo the messages sent from itself -// messagesReceivedByBobCount++ -// mTestHelper.await(lock) -// assertEquals(4, messagesReceivedByBobCount) -// -// // Alice sends a message -// lock = CountDownLatch(2) -// roomFromAlicePOV.sendEvent(buildTextEvent(messagesFromAlice[1], aliceSession, aliceRoomId), TestMatrixCallback(lock, true)) -// mTestHelper.await(lock) -// assertEquals(5, messagesReceivedByBobCount) -// -// return cryptoTestData -// } -// -// fun checkEncryptedEvent(event: CryptoEvent, roomId: String, clearMessage: String, senderSession: Session) { -// assertEquals(EventType.ENCRYPTED, event.wireType) -// assertNotNull(event.wireContent) -// -// val eventWireContent = event.wireContent.asJsonObject -// assertNotNull(eventWireContent) -// -// assertNull(eventWireContent.get("body")) -// assertEquals(MXCRYPTO_ALGORITHM_MEGOLM, eventWireContent.get("algorithm").asString) -// -// assertNotNull(eventWireContent.get("ciphertext")) -// assertNotNull(eventWireContent.get("session_id")) -// assertNotNull(eventWireContent.get("sender_key")) -// -// assertEquals(senderSession.sessionParams.credentials.deviceId, eventWireContent.get("device_id").asString) -// -// assertNotNull(event.getEventId()) -// assertEquals(roomId, event.getRoomId()) -// assertEquals(EventType.MESSAGE, event.getType()) -// assertTrue(event.getAge() < 10000) -// -// val eventContent = event.contentAsJsonObject -// assertNotNull(eventContent) -// assertEquals(clearMessage, eventContent.get("body").asString) -// assertEquals(senderSession.myUserId, event.getSender()) -// } -// -// fun createFakeMegolmBackupAuthData(): MegolmBackupAuthData { -// return MegolmBackupAuthData( -// publicKey = "abcdefg", -// signatures = HashMap>().apply { -// this["something"] = HashMap().apply { -// this["ed25519:something"] = "hijklmnop" -// } -// } -// ) -// } -// -// fun createFakeMegolmBackupCreationInfo(): MegolmBackupCreationInfo { -// return MegolmBackupCreationInfo().apply { -// algorithm = MXCRYPTO_ALGORITHM_MEGOLM_BACKUP -// authData = createFakeMegolmBackupAuthData() -// } -// } -// } +import android.os.SystemClock +import android.text.TextUtils +import im.vector.matrix.android.api.session.Session +import im.vector.matrix.android.api.session.events.model.Event +import im.vector.matrix.android.api.session.events.model.EventType +import im.vector.matrix.android.api.session.events.model.toContent +import im.vector.matrix.android.api.session.room.model.create.CreateRoomParams +import im.vector.matrix.android.api.session.room.timeline.Timeline +import im.vector.matrix.android.api.session.room.timeline.TimelineEvent +import im.vector.matrix.android.api.session.room.timeline.TimelineSettings +import im.vector.matrix.android.internal.crypto.MXCRYPTO_ALGORITHM_MEGOLM +import im.vector.matrix.android.internal.crypto.MXCRYPTO_ALGORITHM_MEGOLM_BACKUP +import im.vector.matrix.android.internal.crypto.keysbackup.model.MegolmBackupAuthData +import im.vector.matrix.android.internal.crypto.keysbackup.model.MegolmBackupCreationInfo +import org.junit.Assert.* +import java.util.* +import java.util.concurrent.CountDownLatch + +class CryptoTestHelper(val mTestHelper: CommonTestHelper) { + + val messagesFromAlice: List = Arrays.asList("0 - Hello I'm Alice!", "4 - Go!") + val messagesFromBob: List = Arrays.asList("1 - Hello I'm Bob!", "2 - Isn't life grand?", "3 - Let's go to the opera.") + + val defaultSessionParams = SessionTestParams(true) + + /** + * @return alice session + */ + fun doE2ETestWithAliceInARoom(): CryptoTestData { + val aliceSession = mTestHelper.createAccount(TestConstants.USER_ALICE, defaultSessionParams) + + var roomId: String? = null + val lock1 = CountDownLatch(1) + + aliceSession.createRoom(CreateRoomParams().apply { name = "MyRoom" }, object : TestMatrixCallback(lock1) { + override fun onSuccess(data: String) { + roomId = data + super.onSuccess(data) + } + }) + + mTestHelper.await(lock1) + assertNotNull(roomId) + + val room = aliceSession.getRoom(roomId!!)!! + + val lock2 = CountDownLatch(1) + room.enableEncryptionWithAlgorithm(MXCRYPTO_ALGORITHM_MEGOLM, object : TestMatrixCallback(lock2) {}) + mTestHelper.await(lock2) + + return CryptoTestData(aliceSession, roomId!!) + } + + /** + * @return alice and bob sessions + */ + fun doE2ETestWithAliceAndBobInARoom(): CryptoTestData { + val statuses = HashMap() + + val cryptoTestData = doE2ETestWithAliceInARoom() + val aliceSession = cryptoTestData.firstSession + val aliceRoomId = cryptoTestData.roomId + + val room = aliceSession.getRoom(aliceRoomId)!! + + val bobSession = mTestHelper.createAccount(TestConstants.USER_BOB, defaultSessionParams) + + val lock1 = CountDownLatch(2) + +// val bobEventListener = object : MXEventListener() { +// override fun onNewRoom(roomId: String) { +// if (TextUtils.equals(roomId, aliceRoomId)) { +// if (!statuses.containsKey("onNewRoom")) { +// statuses["onNewRoom"] = "onNewRoom" +// lock1.countDown() +// } +// } +// } +// } +// +// bobSession.dataHandler.addListener(bobEventListener) + + room.invite(bobSession.myUserId, callback = object : TestMatrixCallback(lock1) { + override fun onSuccess(data: Unit) { + statuses["invite"] = "invite" + super.onSuccess(data) + } + }) + + mTestHelper.await(lock1) + + assertTrue(statuses.containsKey("invite") && statuses.containsKey("onNewRoom")) + +// bobSession.dataHandler.removeListener(bobEventListener) + + val lock2 = CountDownLatch(2) + + bobSession.joinRoom(aliceRoomId, callback = TestMatrixCallback(lock2)) + +// room.addEventListener(object : MXEventListener() { +// override fun onLiveEvent(event: Event, roomState: RoomState) { +// if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_STATE_ROOM_MEMBER)) { +// val contentToConsider = event.contentAsJsonObject +// val member = JsonUtils.toRoomMember(contentToConsider) +// +// if (TextUtils.equals(member.membership, RoomMember.MEMBERSHIP_JOIN)) { +// statuses["AliceJoin"] = "AliceJoin" +// lock2.countDown() +// } +// } +// } +// }) + + mTestHelper.await(lock2) + + // Ensure bob can send messages to the room +// val roomFromBobPOV = bobSession.getRoom(aliceRoomId)!! +// assertNotNull(roomFromBobPOV.powerLevels) +// assertTrue(roomFromBobPOV.powerLevels.maySendMessage(bobSession.myUserId)) + + assertTrue(statuses.toString() + "", statuses.containsKey("AliceJoin")) + +// bobSession.dataHandler.removeListener(bobEventListener) + + return CryptoTestData(aliceSession, aliceRoomId, bobSession) + } + + /** + * @return Alice, Bob and Sam session + */ + fun doE2ETestWithAliceAndBobAndSamInARoom(): CryptoTestData { + val statuses = HashMap() + + val cryptoTestData = doE2ETestWithAliceAndBobInARoom() + val aliceSession = cryptoTestData.firstSession + val aliceRoomId = cryptoTestData.roomId + + val room = aliceSession.getRoom(aliceRoomId)!! + + val samSession = mTestHelper.createAccount(TestConstants.USER_SAM, defaultSessionParams) + + val lock1 = CountDownLatch(2) + +// val samEventListener = object : MXEventListener() { +// override fun onNewRoom(roomId: String) { +// if (TextUtils.equals(roomId, aliceRoomId)) { +// if (!statuses.containsKey("onNewRoom")) { +// statuses["onNewRoom"] = "onNewRoom" +// lock1.countDown() +// } +// } +// } +// } +// +// samSession.dataHandler.addListener(samEventListener) + + room.invite(samSession.myUserId, null, object : TestMatrixCallback(lock1) { + override fun onSuccess(data: Unit) { + statuses["invite"] = "invite" + super.onSuccess(data) + } + }) + + mTestHelper.await(lock1) + + assertTrue(statuses.containsKey("invite") && statuses.containsKey("onNewRoom")) + +// samSession.dataHandler.removeListener(samEventListener) + + val lock2 = CountDownLatch(1) + + samSession.joinRoom(aliceRoomId, null, object : TestMatrixCallback(lock2) { + override fun onSuccess(data: Unit) { + statuses["joinRoom"] = "joinRoom" + super.onSuccess(data) + } + }) + + mTestHelper.await(lock2) + assertTrue(statuses.containsKey("joinRoom")) + + // wait the initial sync + SystemClock.sleep(1000) + +// samSession.dataHandler.removeListener(samEventListener) + + return CryptoTestData(aliceSession, aliceRoomId, cryptoTestData.secondSession, samSession) + } + + /** + * @return Alice and Bob sessions + */ + fun doE2ETestWithAliceAndBobInARoomWithEncryptedMessages(): CryptoTestData { + val cryptoTestData = doE2ETestWithAliceAndBobInARoom() + val aliceSession = cryptoTestData.firstSession + val aliceRoomId = cryptoTestData.roomId + val bobSession = cryptoTestData.secondSession!! + + bobSession.setWarnOnUnknownDevices(false) + + aliceSession.setWarnOnUnknownDevices(false) + + val roomFromBobPOV = bobSession.getRoom(aliceRoomId)!! + val roomFromAlicePOV = aliceSession.getRoom(aliceRoomId)!! + + var lock = CountDownLatch(1) + + val bobEventsListener = object : Timeline.Listener { + override fun onTimelineFailure(throwable: Throwable) { + } + + override fun onTimelineUpdated(snapshot: List) { + val size = snapshot.filter { it.root.senderId != bobSession.myUserId && it.root.getClearType() == EventType.MESSAGE } + .size + + if (size == 3) { + lock.countDown() + } + } + } + + val bobTimeline = roomFromBobPOV.createTimeline(null, TimelineSettings(10)) + bobTimeline.addListener(bobEventsListener) + + val results = HashMap() + + // bobSession.dataHandler.addListener(object : MXEventListener() { + // override fun onToDeviceEvent(event: Event) { + // results["onToDeviceEvent"] = event + // lock.countDown() + // } + // }) + + // Alice sends a message + roomFromAlicePOV.sendTextMessage(messagesFromAlice[0]) + assertTrue(results.containsKey("onToDeviceEvent")) +// assertEquals(1, messagesReceivedByBobCount) + + // Bob send a message + lock = CountDownLatch(1) + roomFromBobPOV.sendTextMessage(messagesFromBob[0]) + // android does not echo the messages sent from itself +// messagesReceivedByBobCount++ + mTestHelper.await(lock) +// assertEquals(2, messagesReceivedByBobCount) + + // Bob send a message + lock = CountDownLatch(1) + roomFromBobPOV.sendTextMessage(messagesFromBob[1]) + // android does not echo the messages sent from itself +// messagesReceivedByBobCount++ + mTestHelper.await(lock) +// assertEquals(3, messagesReceivedByBobCount) + + // Bob send a message + lock = CountDownLatch(1) + roomFromBobPOV.sendTextMessage(messagesFromBob[2]) + // android does not echo the messages sent from itself +// messagesReceivedByBobCount++ + mTestHelper.await(lock) +// assertEquals(4, messagesReceivedByBobCount) + + // Alice sends a message + lock = CountDownLatch(2) + roomFromAlicePOV.sendTextMessage(messagesFromAlice[1]) + mTestHelper.await(lock) +// assertEquals(5, messagesReceivedByBobCount) + + bobTimeline.removeListener(bobEventsListener) + + return cryptoTestData + } + + fun checkEncryptedEvent(event: Event, roomId: String, clearMessage: String, senderSession: Session) { + assertEquals(EventType.ENCRYPTED, event.type) + assertNotNull(event.content) + + val eventWireContent = event.content.toContent() + assertNotNull(eventWireContent) + + assertNull(eventWireContent.get("body")) + assertEquals(MXCRYPTO_ALGORITHM_MEGOLM, eventWireContent.get("algorithm")) + + assertNotNull(eventWireContent.get("ciphertext")) + assertNotNull(eventWireContent.get("session_id")) + assertNotNull(eventWireContent.get("sender_key")) + + assertEquals(senderSession.sessionParams.credentials.deviceId, eventWireContent.get("device_id")) + + assertNotNull(event.eventId) + assertEquals(roomId, event.roomId) + assertEquals(EventType.MESSAGE, event.getClearType()) + // TODO assertTrue(event.getAge() < 10000) + + val eventContent = event.toContent() + assertNotNull(eventContent) + assertEquals(clearMessage, eventContent.get("body")) + assertEquals(senderSession.myUserId, event.senderId) + } + + fun createFakeMegolmBackupAuthData(): MegolmBackupAuthData { + return MegolmBackupAuthData( + publicKey = "abcdefg", + signatures = HashMap>().apply { + this["something"] = HashMap().apply { + this["ed25519:something"] = "hijklmnop" + } + } + ) + } + + fun createFakeMegolmBackupCreationInfo(): MegolmBackupCreationInfo { + return MegolmBackupCreationInfo().apply { + algorithm = MXCRYPTO_ALGORITHM_MEGOLM_BACKUP + authData = createFakeMegolmBackupAuthData() + } + } +} diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/SessionTestParams.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/SessionTestParams.kt index db3b2c9e3f..7d1d23e951 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/SessionTestParams.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/SessionTestParams.kt @@ -16,7 +16,4 @@ package im.vector.matrix.android.common -data class SessionTestParams @JvmOverloads constructor(val withInitialSync: Boolean = false, - val withCryptoEnabled: Boolean = false, - val withLazyLoading: Boolean = true, - val withLegacyCryptoStore: Boolean = false) +data class SessionTestParams @JvmOverloads constructor(val withInitialSync: Boolean = false)