use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class CryptoTest method test25_testLeftAndJoinedBob.
@Test
public // issue https://github.com/vector-im/riot-web/issues/2305
void test25_testLeftAndJoinedBob() throws Exception {
Log.e(LOG_TAG, "test25_testLeftAndJoinedBob");
Context context = InstrumentationRegistry.getContext();
final String messageFromAlice = "Hello I'm Alice!";
final String message2FromAlice = "I'm still Alice!";
final Map<String, Object> results = new HashMap<>();
MXSession aliceSession = mTestHelper.createAccount(TestConstants.USER_ALICE, mCryptoTestHelper.getDefaultSessionParams());
MXSession bobSession = mTestHelper.createAccount(TestConstants.USER_BOB, mCryptoTestHelper.getDefaultSessionParams());
CountDownLatch lock_1 = new CountDownLatch(2);
aliceSession.enableCrypto(true, new TestApiCallback<Void>(lock_1));
bobSession.enableCrypto(true, new TestApiCallback<Void>(lock_1));
mTestHelper.await(lock_1);
Assert.assertNotNull(aliceSession.getCrypto());
Assert.assertNotNull(bobSession.getCrypto());
aliceSession.getCrypto().setWarnOnUnknownDevices(false);
bobSession.getCrypto().setWarnOnUnknownDevices(false);
CountDownLatch lock0 = new CountDownLatch(1);
aliceSession.createRoom(null, null, RoomDirectoryVisibility.DIRECTORY_VISIBILITY_PUBLIC, null, null, new TestApiCallback<String>(lock0) {
@Override
public void onSuccess(String roomId) {
results.put("roomId", roomId);
super.onSuccess(roomId);
}
});
mTestHelper.await(lock0);
Assert.assertTrue(results.containsKey("roomId"));
String aliceRoomId = (String) results.get("roomId");
Room roomFromAlicePOV = aliceSession.getDataHandler().getRoom(aliceRoomId);
CountDownLatch lock1 = new CountDownLatch(1);
roomFromAlicePOV.enableEncryptionWithAlgorithm(CryptoConstantsKt.MXCRYPTO_ALGORITHM_MEGOLM, new TestApiCallback<Void>(lock1) {
@Override
public void onSuccess(Void info) {
results.put("enableEncryptionWithAlgorithm", "enableEncryptionWithAlgorithm");
super.onSuccess(info);
}
});
mTestHelper.await(lock1);
Assert.assertTrue(results.containsKey("enableEncryptionWithAlgorithm"));
CountDownLatch lock2 = new CountDownLatch(1);
bobSession.joinRoom(aliceRoomId, new TestApiCallback<String>(lock2) {
@Override
public void onSuccess(String info) {
results.put("joinRoom", "joinRoom");
super.onSuccess(info);
}
});
mTestHelper.await(lock2);
Assert.assertTrue(results.containsKey("joinRoom"));
Room roomFromBobPOV = bobSession.getDataHandler().getRoom(aliceRoomId);
final CountDownLatch lock3 = new CountDownLatch(1);
final List<Event> receivedEvents = new ArrayList<>();
EventTimeline.Listener eventTimelineListener = new EventTimeline.Listener() {
public void onEvent(Event event, EventTimeline.Direction direction, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE)) {
receivedEvents.add(event);
lock3.countDown();
}
}
};
roomFromBobPOV.getTimeline().addEventTimelineListener(eventTimelineListener);
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(messageFromAlice, aliceSession, aliceRoomId), new SimpleApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
// Ignore
}
});
mTestHelper.await(lock3);
Assert.assertEquals(1, receivedEvents.size());
Event event = receivedEvents.get(0);
mCryptoTestHelper.checkEncryptedEvent(event, aliceRoomId, messageFromAlice, aliceSession);
CountDownLatch lock4 = new CountDownLatch(1);
roomFromBobPOV.leave(new TestApiCallback<Void>(lock4) {
@Override
public void onSuccess(Void info) {
results.put("leave", "leave");
super.onSuccess(info);
}
});
mTestHelper.await(lock4);
Assert.assertTrue(results.containsKey("leave"));
// Make Bob come back to the room with a new device
Credentials bobCredentials = bobSession.getCredentials();
bobSession.clear(context);
MXSession bobSession2 = mTestHelper.logIntoAccount(bobSession.getMyUserId(), mCryptoTestHelper.getEncryptedSessionParams());
Assert.assertNotNull(bobSession2);
Assert.assertTrue(bobSession2.isCryptoEnabled());
Assert.assertNotEquals(bobSession2.getCrypto().getMyDevice().deviceId, bobCredentials.deviceId);
bobSession2.getCrypto().setWarnOnUnknownDevices(false);
CountDownLatch lock5 = new CountDownLatch(1);
bobSession2.joinRoom(aliceRoomId, new TestApiCallback<String>(lock5) {
@Override
public void onSuccess(String info) {
results.put("joinRoom2", "joinRoom2");
super.onSuccess(info);
}
});
mTestHelper.await(lock5);
Assert.assertTrue(results.containsKey("joinRoom2"));
Room roomFromBobPOV2 = bobSession2.getDataHandler().getRoom(aliceRoomId);
final CountDownLatch lock6 = new CountDownLatch(1);
final List<Event> receivedEvents2 = new ArrayList<>();
EventTimeline.Listener eventTimelineListener2 = new EventTimeline.Listener() {
public void onEvent(Event event, EventTimeline.Direction direction, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE)) {
receivedEvents2.add(event);
lock6.countDown();
}
}
};
roomFromBobPOV2.getTimeline().addEventTimelineListener(eventTimelineListener2);
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(message2FromAlice, aliceSession, aliceRoomId), new SimpleApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
// Ignore
}
});
mTestHelper.await(lock6);
Assert.assertEquals(1, receivedEvents2.size());
event = receivedEvents2.get(0);
mCryptoTestHelper.checkEncryptedEvent(event, aliceRoomId, message2FromAlice, aliceSession);
aliceSession.clear(context);
bobSession.clear(context);
bobSession2.clear(context);
}
use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class CryptoTest method test09_testAliceInAEncryptedRoomAfterInitialSync.
@Test
public void test09_testAliceInAEncryptedRoomAfterInitialSync() throws Exception {
Log.e(LOG_TAG, "test09_testAliceInAEncryptedRoomAfterInitialSync");
Context context = InstrumentationRegistry.getContext();
final Map<String, Object> results = new HashMap<>();
CryptoTestData cryptoTestData = mCryptoTestHelper.doE2ETestWithAliceInARoom();
MXSession aliceSession = cryptoTestData.getFirstSession();
final String aliceRoomId = cryptoTestData.getRoomId();
aliceSession.getCrypto().setWarnOnUnknownDevices(false);
final String message = "Hello myself!";
Credentials aliceCredentials = aliceSession.getCredentials();
aliceSession.clear(context);
HomeServerConnectionConfig hs = mTestHelper.createHomeServerConfig(aliceCredentials);
IMXStore store = new MXFileStore(hs, false, context);
final CountDownLatch lock1 = new CountDownLatch(1);
final MXSession aliceSession2 = new MXSession.Builder(hs, new MXDataHandler(store, aliceCredentials), context).withLegacyCryptoStore(mCryptoTestHelper.getUSE_LEGACY_CRYPTO_STORE()).build();
MXStoreListener listener = new MXStoreListener() {
@Override
public void postProcess(String accountId) {
}
@Override
public void onStoreReady(String accountId) {
results.put("onStoreReady", "onStoreReady");
lock1.countDown();
}
@Override
public void onStoreCorrupted(String accountId, String description) {
lock1.countDown();
}
@Override
public void onStoreOOM(String accountId, String description) {
lock1.countDown();
}
};
aliceSession2.getDataHandler().getStore().addMXStoreListener(listener);
aliceSession2.getDataHandler().getStore().open();
mTestHelper.await(lock1);
Assert.assertTrue(results.containsKey("onStoreReady"));
final CountDownLatch lock1b = new CountDownLatch(2);
MXEventListener eventListener = new MXEventListener() {
@Override
public void onInitialSyncComplete(String toToken) {
results.put("onInitialSyncComplete", "onInitialSyncComplete");
lock1b.countDown();
}
@Override
public void onCryptoSyncComplete() {
results.put("onCryptoSyncComplete", "onCryptoSyncComplete");
lock1b.countDown();
}
};
aliceSession2.getDataHandler().addListener(eventListener);
aliceSession2.startEventStream(null);
mTestHelper.await(lock1b);
Assert.assertTrue(results.containsKey("onInitialSyncComplete"));
Assert.assertTrue(results.containsKey("onCryptoSyncComplete"));
Room roomFromAlicePOV2 = aliceSession2.getDataHandler().getRoom(aliceRoomId);
Assert.assertTrue(roomFromAlicePOV2.isEncrypted());
final CountDownLatch lock2 = new CountDownLatch(1);
if (false) {
// The android client does not echo its own message
MXEventListener aliceEventListener = new MXEventListener() {
@Override
public void onLiveEvent(Event event, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE)) {
mCryptoTestHelper.checkEncryptedEvent(event, aliceRoomId, message, aliceSession2);
lock2.countDown();
}
}
};
roomFromAlicePOV2.addEventListener(aliceEventListener);
}
// the IOS client echoes the message
// the android client does not
roomFromAlicePOV2.sendEvent(mCryptoTestHelper.buildTextEvent(message, aliceSession2, aliceRoomId), new TestApiCallback<Void>(lock2) {
@Override
public void onSuccess(Void info) {
results.put("sendEvent", "sendEvent");
super.onSuccess(info);
}
});
mTestHelper.await(lock2);
Assert.assertTrue(results.containsKey("sendEvent"));
cryptoTestData.clear(context);
aliceSession2.clear(context);
}
use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class CryptoTest method test01_testCryptoNoDeviceId.
@Test
public void test01_testCryptoNoDeviceId() throws Exception {
Log.e(LOG_TAG, "test01_testCryptoNoDeviceId");
Context context = InstrumentationRegistry.getContext();
final Map<String, Object> results = new HashMap<>();
MXSession bobSession = mTestHelper.createAccount(TestConstants.USER_BOB, mCryptoTestHelper.getDefaultSessionParams());
Assert.assertNull(bobSession.getCrypto());
bobSession.getCredentials().deviceId = null;
CountDownLatch lock1 = new CountDownLatch(1);
bobSession.enableCrypto(true, new TestApiCallback<Void>(lock1) {
@Override
public void onSuccess(Void info) {
results.put("enableCrypto", "enableCrypto");
super.onSuccess(info);
}
});
mTestHelper.await(lock1);
Assert.assertTrue(results.containsKey("enableCrypto"));
Assert.assertNotNull(bobSession.getCrypto());
Assert.assertNotNull(bobSession.getCredentials().deviceId);
bobSession.clear(context);
}
use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class CryptoTest method test10_testAliceDecryptOldMessageWithANewDeviceInAEncryptedRoom.
@Test
public void test10_testAliceDecryptOldMessageWithANewDeviceInAEncryptedRoom() throws Exception {
Log.e(LOG_TAG, "test10_testAliceDecryptOldMessageWithANewDeviceInAEncryptedRoom");
Context context = InstrumentationRegistry.getContext();
final Map<String, Object> results = new HashMap<>();
CryptoTestData cryptoTestData = mCryptoTestHelper.doE2ETestWithAliceInARoom();
MXSession aliceSession = cryptoTestData.getFirstSession();
String aliceRoomId = cryptoTestData.getRoomId();
aliceSession.getCrypto().setWarnOnUnknownDevices(false);
String message = "Hello myself!";
Room roomFromAlicePOV = aliceSession.getDataHandler().getRoom(aliceRoomId);
CountDownLatch lock1 = new CountDownLatch(1);
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(message, aliceSession, aliceRoomId), new TestApiCallback<Void>(lock1) {
@Override
public void onSuccess(Void info) {
results.put("sendEvent", "sendEvent");
super.onSuccess(info);
}
});
mTestHelper.await(lock1);
Assert.assertTrue(results.containsKey("sendEvent"));
Credentials aliceCredentials = aliceSession.getCredentials();
Credentials aliceCredentials2 = new Credentials();
// close the session and clear the data (not the crypto store)
aliceSession.clear(context);
aliceCredentials2.userId = aliceCredentials.userId;
aliceCredentials2.homeServer = aliceCredentials.homeServer;
aliceCredentials2.accessToken = aliceCredentials.accessToken;
aliceCredentials2.refreshToken = aliceCredentials.refreshToken;
aliceCredentials2.deviceId = "AliceNewDevice";
HomeServerConnectionConfig hs = mTestHelper.createHomeServerConfig(aliceCredentials2);
IMXStore store = new MXFileStore(hs, false, context);
MXSession aliceSession2 = new MXSession.Builder(hs, new MXDataHandler(store, aliceCredentials2), context).withLegacyCryptoStore(mCryptoTestHelper.getUSE_LEGACY_CRYPTO_STORE()).build();
aliceSession2.enableCryptoWhenStarting();
final CountDownLatch lock1b = new CountDownLatch(1);
MXStoreListener listener = new MXStoreListener() {
@Override
public void postProcess(String accountId) {
}
@Override
public void onStoreReady(String accountId) {
results.put("onStoreReady", "onStoreReady");
lock1b.countDown();
}
@Override
public void onStoreCorrupted(String accountId, String description) {
lock1b.countDown();
}
@Override
public void onStoreOOM(String accountId, String description) {
lock1b.countDown();
}
};
aliceSession2.getDataHandler().getStore().addMXStoreListener(listener);
aliceSession2.getDataHandler().getStore().open();
mTestHelper.await(lock1b);
Assert.assertTrue(results.containsKey("onStoreReady"));
final CountDownLatch lock2 = new CountDownLatch(2);
MXEventListener eventListener = new MXEventListener() {
@Override
public void onInitialSyncComplete(String toToken) {
results.put("onInitialSyncComplete", "onInitialSyncComplete");
lock2.countDown();
}
@Override
public void onCryptoSyncComplete() {
results.put("onCryptoSyncComplete", "onCryptoSyncComplete");
lock2.countDown();
}
};
aliceSession2.getDataHandler().addListener(eventListener);
aliceSession2.startEventStream(null);
mTestHelper.await(lock2);
Assert.assertTrue(results.containsKey("onInitialSyncComplete"));
Assert.assertTrue(results.containsKey("onCryptoSyncComplete"));
Room roomFromAlicePOV2 = aliceSession2.getDataHandler().getRoom(aliceRoomId);
Assert.assertNotNull(roomFromAlicePOV2);
Assert.assertTrue(roomFromAlicePOV2.getState().isEncrypted());
Event event = roomFromAlicePOV2.getDataHandler().getStore().getLatestEvent(aliceRoomId);
Assert.assertNotNull(event);
Assert.assertTrue(event.isEncrypted());
Assert.assertNull(event.getClearEvent());
Assert.assertNotNull(event.getCryptoError());
Assert.assertEquals(MXCryptoError.UNKNOWN_INBOUND_SESSION_ID_ERROR_CODE, event.getCryptoError().errcode);
cryptoTestData.clear(context);
aliceSession2.clear(context);
}
use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class LazyLoadingTestHelper method createScenario.
/**
* Create a base scenario for all lazy loading tests
* Common initial conditions:
* - Bob create a public room named "LazyLoading Test Room"
* - Sam and Alice join the room
* - Dave is invited
* - Alice sends 50 messages
* - Bob sends 1 message
* - Alice sends 50 messages
* - Alice makes an initial /sync with lazy-loading enabled or not
*
* @param withLazyLoading true to enable lazy loading for Alice, Bob and Sam accounts
* @return initialized data
*/
public LazyLoadingScenarioData createScenario(boolean withLazyLoading) throws Exception {
final SessionTestParams createSessionParams = new SessionTestParams(true);
MXSession aliceSession = mTestHelper.createAccount(TestConstants.USER_ALICE, createSessionParams);
MXSession bobSession = mTestHelper.createAccount(TestConstants.USER_BOB, createSessionParams);
MXSession samSession = mTestHelper.createAccount(TestConstants.USER_SAM, createSessionParams);
final String aliceId = aliceSession.getMyUserId();
final String bobId = bobSession.getMyUserId();
final String samId = samSession.getMyUserId();
final Map<String, String> results = new HashMap<>();
CountDownLatch latch = new CountDownLatch(1);
bobSession.createRoom(new TestApiCallback<String>(latch) {
@Override
public void onSuccess(String info) {
results.put("roomId", info);
super.onSuccess(info);
}
});
mTestHelper.await(latch);
final String roomId = results.get("roomId");
final Room bobRoom = bobSession.getDataHandler().getRoom(roomId);
// update name and join rules
latch = new CountDownLatch(1);
bobRoom.updateName("LazyLoading Test Room", new TestApiCallback<Void>(latch));
mTestHelper.await(latch);
latch = new CountDownLatch(1);
bobRoom.updateJoinRules(RoomState.JOIN_RULE_PUBLIC, new TestApiCallback<Void>(latch));
mTestHelper.await(latch);
// sam join
latch = new CountDownLatch(1);
samSession.joinRoom(roomId, new TestApiCallback<String>(latch));
mTestHelper.await(latch);
// alice join
latch = new CountDownLatch(1);
aliceSession.joinRoom(roomId, new TestApiCallback<String>(latch));
mTestHelper.await(latch);
final Room aliceRoom = aliceSession.getDataHandler().getStore().getRoom(roomId);
// invite dave
latch = new CountDownLatch(1);
bobRoom.invite(bobSession, "@dave:localhost:8480", new TestApiCallback<Void>(latch));
mTestHelper.await(latch);
// Send messages
final List<Event> aliceFirstMessages = mTestHelper.sendTextMessage(aliceRoom, "Alice message", 50);
final List<Event> bobMessages = mTestHelper.sendTextMessage(bobRoom, "Bob message", 1);
final List<Event> aliceLastMessages = mTestHelper.sendTextMessage(aliceRoom, "Alice message", 50);
Assert.assertEquals(50, aliceFirstMessages.size());
Assert.assertEquals(1, bobMessages.size());
Assert.assertEquals(50, aliceLastMessages.size());
final String bobMessageId = bobMessages.isEmpty() ? null : bobMessages.get(0).eventId;
// Clear sessions and open new ones
final Context context = InstrumentationRegistry.getContext();
aliceSession.clear(context);
bobSession.clear(context);
samSession.clear(context);
final SessionTestParams logSessionParams = new SessionTestParams(false, false, withLazyLoading);
aliceSession = mTestHelper.logIntoAccount(aliceId, logSessionParams);
bobSession = mTestHelper.logIntoAccount(bobId, logSessionParams);
samSession = mTestHelper.logIntoAccount(samId, logSessionParams);
return new LazyLoadingScenarioData(aliceSession, bobSession, samSession, roomId, bobMessageId);
}
Aggregations