use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class CryptoTest method test04_testEnsureOlmSessionsForUsers.
@Test
public void test04_testEnsureOlmSessionsForUsers() throws Exception {
Log.e(LOG_TAG, "test04_testEnsureOlmSessionsForUsers");
Context context = InstrumentationRegistry.getContext();
MXSession aliceSession = mTestHelper.createAccount(TestConstants.USER_ALICE, mCryptoTestHelper.getDefaultSessionParams());
final Map<String, Object> results = new HashMap<>();
aliceSession.getCredentials().deviceId = "AliceDevice";
CountDownLatch lock0 = new CountDownLatch(1);
aliceSession.enableCrypto(true, new TestApiCallback<Void>(lock0) {
@Override
public void onSuccess(Void info) {
results.put("enableCryptoAlice", "enableCryptoAlice");
super.onSuccess(info);
}
});
mTestHelper.await(lock0);
Assert.assertTrue(results.containsKey("enableCryptoAlice"));
MXSession bobSession = mTestHelper.createAccount(TestConstants.USER_BOB, mCryptoTestHelper.getDefaultSessionParams());
CountDownLatch lock2 = new CountDownLatch(1);
bobSession.enableCrypto(true, new TestApiCallback<Void>(lock2) {
@Override
public void onSuccess(Void info) {
results.put("enableCryptoBob", "enableCryptoAlice");
super.onSuccess(info);
}
});
mTestHelper.await(lock2);
Assert.assertTrue(results.containsKey("enableCryptoBob"));
CountDownLatch lock3 = new CountDownLatch(1);
bobSession.getCrypto().getDeviceList().downloadKeys(Arrays.asList(bobSession.getMyUserId(), aliceSession.getMyUserId()), false, new TestApiCallback<MXUsersDevicesMap<MXDeviceInfo>>(lock3) {
@Override
public void onSuccess(MXUsersDevicesMap<MXDeviceInfo> map) {
results.put("downloadKeys", map);
super.onSuccess(map);
}
});
mTestHelper.await(lock3);
Assert.assertTrue(results.containsKey("downloadKeys"));
CountDownLatch lock4 = new CountDownLatch(1);
bobSession.getCrypto().ensureOlmSessionsForUsers(Arrays.asList(bobSession.getMyUserId(), aliceSession.getMyUserId()), new TestApiCallback<MXUsersDevicesMap<MXOlmSessionResult>>(lock4) {
@Override
public void onSuccess(MXUsersDevicesMap<MXOlmSessionResult> info) {
results.put("ensureOlmSessionsForUsers", info);
super.onSuccess(info);
}
});
mTestHelper.await(lock4);
Assert.assertTrue(results.containsKey("ensureOlmSessionsForUsers"));
MXUsersDevicesMap<MXOlmSessionResult> result = (MXUsersDevicesMap<MXOlmSessionResult>) results.get("ensureOlmSessionsForUsers");
Assert.assertEquals(1, result.getUserIds().size());
MXOlmSessionResult sessionWithAliceDevice = result.getObject("AliceDevice", aliceSession.getMyUserId());
Assert.assertNotNull(sessionWithAliceDevice);
Assert.assertNotNull(sessionWithAliceDevice.mSessionId);
Assert.assertTrue(sessionWithAliceDevice.hasResult);
Assert.assertEquals("AliceDevice", sessionWithAliceDevice.mDevice.deviceId);
MXSession bobSession2 = mTestHelper.createNewSession(bobSession, mCryptoTestHelper.getDefaultSessionParams());
final CountDownLatch lock5 = new CountDownLatch(1);
MXStoreListener listener = new MXStoreListener() {
@Override
public void postProcess(String accountId) {
}
@Override
public void onStoreReady(String accountId) {
results.put("onStoreReady", "onStoreReady");
lock5.countDown();
}
@Override
public void onStoreCorrupted(String accountId, String description) {
lock5.countDown();
}
@Override
public void onStoreOOM(String accountId, String description) {
lock5.countDown();
}
};
bobSession2.getDataHandler().getStore().addMXStoreListener(listener);
bobSession2.getDataHandler().getStore().open();
bobSession2.getDataHandler().addListener(new MXEventListener() {
@Override
public void onStoreReady() {
lock5.countDown();
}
});
mTestHelper.await(lock5);
Assert.assertTrue(results.containsKey("onStoreReady"));
final CountDownLatch lock5b = new CountDownLatch(2);
MXEventListener eventListener = new MXEventListener() {
@Override
public void onInitialSyncComplete(String toToken) {
results.put("onInitialSyncComplete", "onInitialSyncComplete");
lock5b.countDown();
}
@Override
public void onCryptoSyncComplete() {
results.put("onCryptoSyncComplete", "onCryptoSyncComplete");
lock5b.countDown();
}
};
bobSession2.getDataHandler().addListener(eventListener);
bobSession2.startEventStream(null);
mTestHelper.await(lock5b);
Assert.assertTrue(results.containsKey("onInitialSyncComplete"));
Assert.assertTrue(results.containsKey("onCryptoSyncComplete"));
CountDownLatch lock6 = new CountDownLatch(1);
bobSession2.getCrypto().ensureOlmSessionsForUsers(Arrays.asList(bobSession2.getMyUserId(), aliceSession.getMyUserId()), new TestApiCallback<MXUsersDevicesMap<MXOlmSessionResult>>(lock6) {
@Override
public void onSuccess(MXUsersDevicesMap<MXOlmSessionResult> info) {
results.put("ensureOlmSessionsForUsers2", info);
super.onSuccess(info);
}
});
mTestHelper.await(lock6);
Assert.assertTrue(results.containsKey("ensureOlmSessionsForUsers2"));
MXUsersDevicesMap<MXOlmSessionResult> result2 = (MXUsersDevicesMap<MXOlmSessionResult>) results.get("ensureOlmSessionsForUsers2");
MXOlmSessionResult sessionWithAliceDevice2 = result2.getObject("AliceDevice", aliceSession.getMyUserId());
Assert.assertNotNull(sessionWithAliceDevice2);
Assert.assertNotNull(sessionWithAliceDevice2.mSessionId);
Assert.assertTrue(sessionWithAliceDevice2.hasResult);
Assert.assertEquals("AliceDevice", sessionWithAliceDevice2.mDevice.deviceId);
bobSession.clear(context);
aliceSession.clear(context);
bobSession2.clear(context);
}
use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class CryptoTest method test07_testAliceAndBobInAEncryptedRoom.
@Test
public void test07_testAliceAndBobInAEncryptedRoom() throws Exception {
Log.e(LOG_TAG, "test07_testAliceAndBobInAEncryptedRoom");
Context context = InstrumentationRegistry.getContext();
final Map<String, Object> results = new HashMap<>();
CryptoTestData cryptoTestData = mCryptoTestHelper.doE2ETestWithAliceAndBobInARoom(true);
final MXSession aliceSession = cryptoTestData.getFirstSession();
final String aliceRoomId = cryptoTestData.getRoomId();
final MXSession bobSession = cryptoTestData.getSecondSession();
final String messageFromAlice = "Hello I'm Alice!";
Room roomFromBobPOV = bobSession.getDataHandler().getRoom(aliceRoomId);
Room roomFromAlicePOV = aliceSession.getDataHandler().getRoom(aliceRoomId);
Assert.assertTrue(roomFromBobPOV.isEncrypted());
Assert.assertTrue(roomFromAlicePOV.isEncrypted());
CountDownLatch lock1 = new CountDownLatch(1);
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(messageFromAlice, aliceSession, aliceRoomId), new TestApiCallback<Void>(lock1, false) {
@Override
public void onMatrixError(MatrixError e) {
results.put("sendEventError", e);
super.onMatrixError(e);
}
});
mTestHelper.await(lock1);
Assert.assertTrue(results.containsKey("sendEventError"));
MXCryptoError error = (MXCryptoError) results.get("sendEventError");
Assert.assertEquals(MXCryptoError.UNKNOWN_DEVICES_CODE, error.errcode);
MXUsersDevicesMap<MXDeviceInfo> unknownDevices = (MXUsersDevicesMap<MXDeviceInfo>) error.mExceptionData;
List<String> deviceInfos = unknownDevices.getUserDeviceIds(bobSession.getMyUserId());
Assert.assertEquals(1, deviceInfos.size());
Assert.assertEquals(deviceInfos.get(0), bobSession.getCrypto().getMyDevice().deviceId);
CountDownLatch lock2 = new CountDownLatch(1);
aliceSession.getCrypto().setDevicesKnown(Arrays.asList(bobSession.getCrypto().getMyDevice()), new TestApiCallback<Void>(lock2) {
@Override
public void onSuccess(Void info) {
results.put("setDevicesKnown", "setDevicesKnown");
super.onSuccess(info);
}
});
mTestHelper.await(lock2);
Assert.assertTrue(results.containsKey("setDevicesKnown"));
final CountDownLatch lock3 = new CountDownLatch(3);
MXEventListener eventListener = new MXEventListener() {
@Override
public void onLiveEvent(Event event, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE)) {
mCryptoTestHelper.checkEncryptedEvent(event, aliceRoomId, messageFromAlice, aliceSession);
results.put("onLiveEvent", "onLiveEvent");
lock3.countDown();
}
}
};
bobSession.getDataHandler().addListener(new MXEventListener() {
@Override
public void onToDeviceEvent(Event event) {
results.put("onToDeviceEvent", event);
lock3.countDown();
}
});
roomFromBobPOV.addEventListener(eventListener);
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(messageFromAlice, aliceSession, aliceRoomId), new TestApiCallback<Void>(lock3));
mTestHelper.await(lock3);
Assert.assertTrue(results.containsKey("onToDeviceEvent"));
Assert.assertTrue(results.containsKey("onLiveEvent"));
Assert.assertEquals(MXDeviceList.TRACKING_STATUS_UP_TO_DATE, bobSession.getCrypto().getDeviceTrackingStatus(bobSession.getMyUserId()));
Assert.assertEquals(MXDeviceList.TRACKING_STATUS_UP_TO_DATE, bobSession.getCrypto().getDeviceTrackingStatus(aliceSession.getMyUserId()));
Assert.assertEquals(MXDeviceList.TRACKING_STATUS_UP_TO_DATE, aliceSession.getCrypto().getDeviceTrackingStatus(bobSession.getMyUserId()));
Assert.assertEquals(MXDeviceList.TRACKING_STATUS_UP_TO_DATE, aliceSession.getCrypto().getDeviceTrackingStatus(aliceSession.getMyUserId()));
cryptoTestData.clear(context);
}
use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class CryptoTest method test18_testAliceAndBobWithNewDevice.
@Test
public void test18_testAliceAndBobWithNewDevice() throws Exception {
Log.e(LOG_TAG, "test18_testAliceAndBobWithNewDevice");
Context context = InstrumentationRegistry.getContext();
final Map<String, Object> results = new HashMap<>();
CryptoTestData cryptoTestData = mCryptoTestHelper.doE2ETestWithAliceAndBobInARoom(true);
final MXSession aliceSession = cryptoTestData.getFirstSession();
final String aliceRoomId = cryptoTestData.getRoomId();
final MXSession bobSession = cryptoTestData.getSecondSession();
bobSession.getCrypto().setWarnOnUnknownDevices(false);
aliceSession.getCrypto().setWarnOnUnknownDevices(false);
final Room roomFromBobPOV = bobSession.getDataHandler().getRoom(aliceRoomId);
final Room roomFromAlicePOV = aliceSession.getDataHandler().getRoom(aliceRoomId);
String bobDeviceId1 = bobSession.getCredentials().deviceId;
Assert.assertTrue(roomFromBobPOV.isEncrypted());
Assert.assertTrue(roomFromAlicePOV.isEncrypted());
final CountDownLatch lock1 = new CountDownLatch(2);
MXEventListener bobEventListener = new MXEventListener() {
@Override
public void onToDeviceEvent(Event event) {
if (!results.containsKey("onToDeviceEvent")) {
results.put("onToDeviceEvent", event);
lock1.countDown();
}
}
};
bobSession.getDataHandler().addListener(bobEventListener);
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);
lock1.countDown();
}
}
};
roomFromBobPOV.getTimeline().addEventTimelineListener(eventTimelineListener);
String aliceMessage1 = "Hello I'm Alice!";
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(aliceMessage1, aliceSession, aliceRoomId), new SimpleApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
// Ignore
}
});
mTestHelper.await(lock1);
Assert.assertTrue(results.containsKey("onToDeviceEvent"));
Assert.assertEquals(1, receivedEvents.size());
Event event = receivedEvents.get(0);
mCryptoTestHelper.checkEncryptedEvent(event, aliceRoomId, aliceMessage1, aliceSession);
// logout
CountDownLatch lock2 = new CountDownLatch(1);
String bobId = bobSession.getCredentials().userId;
bobSession.logout(context, new TestApiCallback<Void>(lock2) {
@Override
public void onSuccess(Void info) {
results.put("logout", "logout");
super.onSuccess(info);
}
});
mTestHelper.await(lock2);
Assert.assertTrue(results.containsKey("logout"));
final CountDownLatch lock3 = new CountDownLatch(1);
MXEventListener aliceEventListener = new MXEventListener() {
@Override
public void onToDeviceEvent(Event event) {
if (!results.containsKey("onToDeviceEvent2")) {
results.put("onToDeviceEvent2", event);
lock3.countDown();
}
}
};
aliceSession.getDataHandler().addListener(aliceEventListener);
// login with a new device id
MXSession bobSession2 = mTestHelper.logIntoAccount(bobSession.getMyUserId(), mCryptoTestHelper.getEncryptedSessionParams());
String bobDeviceId2 = bobSession2.getCredentials().deviceId;
Assert.assertNotEquals(bobDeviceId2, bobDeviceId1);
// before sending a message, wait that the device event is received.
mTestHelper.await(lock3);
Assert.assertTrue(results.containsKey("onToDeviceEvent2"));
SystemClock.sleep(1000);
final Room roomFromBobPOV2 = bobSession2.getDataHandler().getRoom(aliceRoomId);
Assert.assertNotNull(roomFromBobPOV2);
final List<Event> receivedEvents4 = new ArrayList<>();
final CountDownLatch lock4 = new CountDownLatch(1);
EventTimeline.Listener eventTimelineListener4 = new EventTimeline.Listener() {
public void onEvent(Event event, EventTimeline.Direction direction, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE)) {
receivedEvents4.add(event);
lock4.countDown();
} else {
results.put("event4", event.getType() + "");
lock4.countDown();
}
}
};
roomFromBobPOV2.getTimeline().addEventTimelineListener(eventTimelineListener4);
String aliceMessage2 = "Hello I'm still Alice!";
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(aliceMessage2, aliceSession, aliceRoomId), new SimpleApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
// Ignore
}
});
mTestHelper.await(lock4);
Assert.assertEquals("received event of type " + results.get("event4"), 1, receivedEvents4.size());
event = receivedEvents4.get(0);
mCryptoTestHelper.checkEncryptedEvent(event, aliceRoomId, aliceMessage2, aliceSession);
cryptoTestData.clear(context);
}
use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class CryptoTest method test02_testCryptoPersistenceInStore.
@Test
public void test02_testCryptoPersistenceInStore() throws Exception {
Log.e(LOG_TAG, "test02_testCryptoPersistenceInStore");
Context context = InstrumentationRegistry.getContext();
final Map<String, Object> results = new HashMap<>();
MXSession bobSession = mTestHelper.createAccount(TestConstants.USER_BOB, mCryptoTestHelper.getDefaultSessionParams());
bobSession.getCredentials().deviceId = "BobDevice";
Assert.assertNull(bobSession.getCrypto());
CountDownLatch lock0 = new CountDownLatch(1);
bobSession.enableCrypto(true, new TestApiCallback<Void>(lock0) {
@Override
public void onSuccess(Void info) {
results.put("enableCrypto", "enableCrypto");
super.onSuccess(info);
}
});
mTestHelper.await(lock0);
Assert.assertTrue(results.containsKey("enableCrypto"));
Assert.assertNotNull(bobSession.getCrypto());
SystemClock.sleep(1000);
final String deviceCurve25519Key = bobSession.getCrypto().getOlmDevice().getDeviceCurve25519Key();
final String deviceEd25519Key = bobSession.getCrypto().getOlmDevice().getDeviceEd25519Key();
final List<MXDeviceInfo> myUserDevices = bobSession.getCrypto().getUserDevices(bobSession.getMyUserId());
Assert.assertNotNull(myUserDevices);
Assert.assertEquals(1, myUserDevices.size());
MXSession bobSession2 = mTestHelper.createNewSession(bobSession, mCryptoTestHelper.getDefaultSessionParams());
final CountDownLatch lock1 = new CountDownLatch(1);
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();
}
};
bobSession2.getDataHandler().getStore().addMXStoreListener(listener);
bobSession2.getDataHandler().getStore().open();
mTestHelper.await(lock1);
Assert.assertTrue(results.containsKey("onStoreReady"));
Assert.assertTrue(bobSession2.isCryptoEnabled());
final CountDownLatch lock2 = new CountDownLatch(2);
MXEventListener eventsListener = new MXEventListener() {
@Override
public void onInitialSyncComplete(String toToken) {
results.put("onInitialSyncComplete", "onInitialSyncComplete");
lock2.countDown();
}
@Override
public void onCryptoSyncComplete() {
results.put("onCryptoSyncComplete", "onCryptoSyncComplete");
lock2.countDown();
}
};
bobSession2.getDataHandler().addListener(eventsListener);
bobSession2.startEventStream(null);
mTestHelper.await(lock2);
Assert.assertTrue(results.containsKey("onInitialSyncComplete"));
Assert.assertTrue(results.containsKey("onCryptoSyncComplete"));
MXCrypto crypto = bobSession2.getCrypto();
Assert.assertNotNull(crypto);
Assert.assertEquals(deviceCurve25519Key, crypto.getOlmDevice().getDeviceCurve25519Key());
Assert.assertEquals(deviceEd25519Key, crypto.getOlmDevice().getDeviceEd25519Key());
List<MXDeviceInfo> myUserDevices2 = bobSession2.getCrypto().getUserDevices(bobSession2.getMyUserId());
Assert.assertEquals(1, myUserDevices2.size());
Assert.assertEquals(myUserDevices2.get(0).deviceId, myUserDevices.get(0).deviceId);
bobSession.clear(context);
bobSession2.clear(context);
}
use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class CryptoTest method test11_testAliceAndBobInAEncryptedRoomBackPaginationFromMemoryStore.
@Test
public void test11_testAliceAndBobInAEncryptedRoomBackPaginationFromMemoryStore() throws Exception {
Log.e(LOG_TAG, "test11_testAliceAndBobInAEncryptedRoomBackPaginationFromMemoryStore");
Context context = InstrumentationRegistry.getContext();
final Map<String, Object> results = new HashMap<>();
CryptoTestData cryptoTestData = mCryptoTestHelper.doE2ETestWithAliceAndBobInARoomWithEncryptedMessages(true);
final MXSession aliceSession = cryptoTestData.getFirstSession();
final String aliceRoomId = cryptoTestData.getRoomId();
final MXSession bobSession = cryptoTestData.getSecondSession();
Credentials bobCredentials = bobSession.getCredentials();
bobSession.clear(context);
HomeServerConnectionConfig hs = mTestHelper.createHomeServerConfig(bobCredentials);
IMXStore store = new MXFileStore(hs, false, context);
final CountDownLatch lock1 = new CountDownLatch(2);
MXSession bobSession2 = new MXSession.Builder(hs, new MXDataHandler(store, bobCredentials), context).withLegacyCryptoStore(mCryptoTestHelper.getUSE_LEGACY_CRYPTO_STORE()).build();
MXEventListener eventListener = new MXEventListener() {
@Override
public void onInitialSyncComplete(String toToken) {
results.put("onInitialSyncComplete", "onInitialSyncComplete");
lock1.countDown();
}
@Override
public void onCryptoSyncComplete() {
results.put("onCryptoSyncComplete", "onCryptoSyncComplete");
lock1.countDown();
}
};
bobSession2.getDataHandler().addListener(eventListener);
bobSession2.getDataHandler().getStore().open();
bobSession2.startEventStream(null);
mTestHelper.await(lock1);
Assert.assertTrue(results.containsKey("onInitialSyncComplete"));
Assert.assertTrue(results.containsKey("onCryptoSyncComplete"));
Assert.assertNotNull(bobSession2.getCrypto());
Room roomFromBobPOV = bobSession2.getDataHandler().getRoom(aliceRoomId);
final CountDownLatch lock2 = new CountDownLatch(6);
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);
lock2.countDown();
}
}
};
roomFromBobPOV.getTimeline().addEventTimelineListener(eventTimelineListener);
roomFromBobPOV.getTimeline().backPaginate(new TestApiCallback<Integer>(lock2) {
@Override
public void onSuccess(Integer info) {
results.put("backPaginate", "backPaginate");
super.onSuccess(info);
}
});
mTestHelper.await(lock2);
Assert.assertTrue(results.containsKey("backPaginate"));
Assert.assertEquals(receivedEvents.size() + " instead of 5", 5, receivedEvents.size());
mCryptoTestHelper.checkEncryptedEvent(receivedEvents.get(0), aliceRoomId, mCryptoTestHelper.getMessagesFromAlice().get(1), aliceSession);
mCryptoTestHelper.checkEncryptedEvent(receivedEvents.get(1), aliceRoomId, mCryptoTestHelper.getMessagesFromBob().get(2), bobSession);
mCryptoTestHelper.checkEncryptedEvent(receivedEvents.get(2), aliceRoomId, mCryptoTestHelper.getMessagesFromBob().get(1), bobSession);
mCryptoTestHelper.checkEncryptedEvent(receivedEvents.get(3), aliceRoomId, mCryptoTestHelper.getMessagesFromBob().get(0), bobSession);
mCryptoTestHelper.checkEncryptedEvent(receivedEvents.get(4), aliceRoomId, mCryptoTestHelper.getMessagesFromAlice().get(0), aliceSession);
cryptoTestData.clear(context);
bobSession2.clear(context);
}
Aggregations