Cleanup tests
This commit is contained in:
parent
3ceac70536
commit
f3fb07079e
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Event> {
|
||||
// val sentEvents = ArrayList<Event>(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<TimelineEvent> {
|
||||
val sentEvents = ArrayList<TimelineEvent>(nbOfMessages)
|
||||
val latch = CountDownLatch(nbOfMessages)
|
||||
val onEventSentListener = object : Timeline.Listener {
|
||||
override fun onTimelineFailure(throwable: Throwable) {
|
||||
}
|
||||
|
||||
override fun onTimelineUpdated(snapshot: List<TimelineEvent>) {
|
||||
// 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<Unit>(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<String, Any>()
|
||||
//
|
||||
// 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
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -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<String> = Arrays.asList("0 - Hello I'm Alice!", "4 - Go!")
|
||||
// val messagesFromBob: List<String> = 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<String, Any>()
|
||||
// val aliceSession = mTestHelper.createAccount(TestConstants.USER_ALICE, defaultSessionParams)
|
||||
//
|
||||
// var roomId: String? = null
|
||||
// val lock1 = CountDownLatch(1)
|
||||
//
|
||||
// aliceSession.createRoom(CreateRoomParams().apply { name = "MyRoom" }, object : TestMatrixCallback<String>(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<Void?>(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<String, String>()
|
||||
//
|
||||
// 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<Unit>(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<String, String>()
|
||||
//
|
||||
// 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<Void?>(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<String>(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<String, Any>()
|
||||
//
|
||||
// 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<Void>(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<Void>(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<Void>(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<Void>(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<Void>(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<String, Map<String, String>>().apply {
|
||||
// this["something"] = HashMap<String, String>().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<String> = Arrays.asList("0 - Hello I'm Alice!", "4 - Go!")
|
||||
val messagesFromBob: List<String> = 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<String>(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<Unit>(lock2) {})
|
||||
mTestHelper.await(lock2)
|
||||
|
||||
return CryptoTestData(aliceSession, roomId!!)
|
||||
}
|
||||
|
||||
/**
|
||||
* @return alice and bob sessions
|
||||
*/
|
||||
fun doE2ETestWithAliceAndBobInARoom(): CryptoTestData {
|
||||
val statuses = HashMap<String, String>()
|
||||
|
||||
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<Unit>(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<String, String>()
|
||||
|
||||
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<Unit>(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<Unit>(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<TimelineEvent>) {
|
||||
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<String, Any>()
|
||||
|
||||
// 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<String, Map<String, String>>().apply {
|
||||
this["something"] = HashMap<String, String>().apply {
|
||||
this["ed25519:something"] = "hijklmnop"
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fun createFakeMegolmBackupCreationInfo(): MegolmBackupCreationInfo {
|
||||
return MegolmBackupCreationInfo().apply {
|
||||
algorithm = MXCRYPTO_ALGORITHM_MEGOLM_BACKUP
|
||||
authData = createFakeMegolmBackupAuthData()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue