use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class CryptoTest method test26_testBlackListUnverifiedDevices.
@Test
public // Check that the message can be decrypted by the Bob's device and the Sam's device
void test26_testBlackListUnverifiedDevices() throws Exception {
Log.e(LOG_TAG, "test26_testBlackListUnverifiedDevices");
Context context = InstrumentationRegistry.getContext();
final Map<String, Object> results = new HashMap<>();
CryptoTestData cryptoTestData = mCryptoTestHelper.doE2ETestWithAliceAndBobAndSamInARoom();
final MXSession aliceSession = cryptoTestData.getFirstSession();
final String aliceRoomId = cryptoTestData.getRoomId();
final MXSession bobSession = cryptoTestData.getSecondSession();
final MXSession samSession = cryptoTestData.getThirdSession();
final String messageFromAlice = "Hello I'm Alice!";
Room roomFromBobPOV = bobSession.getDataHandler().getRoom(aliceRoomId);
Room roomFromAlicePOV = aliceSession.getDataHandler().getRoom(aliceRoomId);
Room roomFromSamPOV = samSession.getDataHandler().getRoom(aliceRoomId);
Assert.assertTrue(roomFromBobPOV.isEncrypted());
Assert.assertTrue(roomFromAlicePOV.isEncrypted());
Assert.assertTrue(roomFromSamPOV.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;
// only one bob device
List<String> deviceInfos = unknownDevices.getUserDeviceIds(bobSession.getMyUserId());
Assert.assertEquals(1, deviceInfos.size());
Assert.assertTrue(deviceInfos.contains(bobSession.getCrypto().getMyDevice().deviceId));
// only one Sam device
deviceInfos = unknownDevices.getUserDeviceIds(samSession.getMyUserId());
Assert.assertEquals(1, deviceInfos.size());
Assert.assertTrue(deviceInfos.contains(samSession.getCrypto().getMyDevice().deviceId));
CountDownLatch lock2 = new CountDownLatch(1);
aliceSession.getCrypto().setDevicesKnown(Arrays.asList(bobSession.getCrypto().getMyDevice(), samSession.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(5);
MXEventListener eventListenerBob1 = 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("onLiveEventBob1", "onLiveEvent");
lock3.countDown();
}
}
};
MXEventListener eventListenerSam1 = 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("onLiveEventSam1", "onLiveEvent");
lock3.countDown();
}
}
};
bobSession.getDataHandler().addListener(new MXEventListener() {
@Override
public void onToDeviceEvent(Event event) {
results.put("onToDeviceEventBob", event);
lock3.countDown();
}
});
samSession.getDataHandler().addListener(new MXEventListener() {
@Override
public void onToDeviceEvent(Event event) {
results.put("onToDeviceEventSam", event);
lock3.countDown();
}
});
roomFromBobPOV.addEventListener(eventListenerBob1);
roomFromSamPOV.addEventListener(eventListenerSam1);
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(messageFromAlice, aliceSession, aliceRoomId), new TestApiCallback<Void>(lock3) {
@Override
public void onSuccess(Void info) {
lock3.countDown();
}
});
mTestHelper.await(lock3);
Assert.assertTrue(results.containsKey("onToDeviceEventBob"));
Assert.assertTrue(results.containsKey("onToDeviceEventSam"));
Assert.assertTrue(results.containsKey("onLiveEventBob1"));
Assert.assertTrue(results.containsKey("onLiveEventSam1"));
roomFromBobPOV.removeEventListener(eventListenerBob1);
roomFromSamPOV.removeEventListener(eventListenerSam1);
// play with the device black listing
final List<CountDownLatch> activeLock = new ArrayList<>();
final List<String> activeMessage = new ArrayList<>();
MXEventListener eventListenerBob2 = new MXEventListener() {
@Override
public void onLiveEvent(Event event, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE)) {
mCryptoTestHelper.checkEncryptedEvent(event, aliceRoomId, activeMessage.get(0), aliceSession);
results.put("eventListenerBob2", "onLiveEvent");
activeLock.get(0).countDown();
} else if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE_ENCRYPTED)) {
results.put("eventListenerEncyptedBob2", "onLiveEvent");
activeLock.get(0).countDown();
}
}
};
MXEventListener eventListenerSam2 = new MXEventListener() {
@Override
public void onLiveEvent(Event event, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE)) {
mCryptoTestHelper.checkEncryptedEvent(event, aliceRoomId, activeMessage.get(0), aliceSession);
results.put("eventListenerSam2", "onLiveEvent");
activeLock.get(0).countDown();
} else if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE_ENCRYPTED)) {
results.put("eventListenerEncyptedSam2", "onLiveEvent");
activeLock.get(0).countDown();
}
}
};
roomFromBobPOV.addEventListener(eventListenerBob2);
roomFromSamPOV.addEventListener(eventListenerSam2);
Assert.assertFalse(aliceSession.getCrypto().getCryptoStore().getGlobalBlacklistUnverifiedDevices());
CountDownLatch lock4 = new CountDownLatch(1);
aliceSession.getCrypto().setGlobalBlacklistUnverifiedDevices(true, new TestApiCallback<Void>(lock4) {
@Override
public void onSuccess(Void info) {
results.put("setGlobalBlacklistUnverifiedDevices True", "setGlobalBlacklistUnverifiedDevices");
super.onSuccess(info);
}
});
mTestHelper.await(lock4);
Assert.assertTrue(results.containsKey("setGlobalBlacklistUnverifiedDevices True"));
Assert.assertTrue(aliceSession.getCrypto().getCryptoStore().getGlobalBlacklistUnverifiedDevices());
// ensure that there is no received message
results.clear();
CountDownLatch lock5 = new CountDownLatch(3);
activeLock.clear();
activeLock.add(lock5);
activeMessage.clear();
activeMessage.add("message 1");
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(activeMessage.get(0), aliceSession, aliceRoomId), new TestApiCallback<Void>(lock5));
mTestHelper.await(lock5);
Assert.assertFalse(results.containsKey("eventListenerBob2"));
Assert.assertFalse(results.containsKey("eventListenerSam2"));
Assert.assertTrue(results.containsKey("eventListenerEncyptedBob2"));
Assert.assertTrue(results.containsKey("eventListenerEncyptedSam2"));
CountDownLatch lock6 = new CountDownLatch(1);
aliceSession.getCrypto().setGlobalBlacklistUnverifiedDevices(false, new TestApiCallback<Void>(lock6) {
@Override
public void onSuccess(Void info) {
results.put("setGlobalBlacklistUnverifiedDevices false", "setGlobalBlacklistUnverifiedDevices");
super.onSuccess(info);
}
});
mTestHelper.await(lock6);
Assert.assertTrue(results.containsKey("setGlobalBlacklistUnverifiedDevices false"));
Assert.assertFalse(aliceSession.getCrypto().getCryptoStore().getGlobalBlacklistUnverifiedDevices());
// ensure that the messages are received
results.clear();
CountDownLatch lock7 = new CountDownLatch(3);
activeLock.clear();
activeLock.add(lock7);
activeMessage.clear();
activeMessage.add("message 2");
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(activeMessage.get(0), aliceSession, aliceRoomId), new TestApiCallback<Void>(lock7));
mTestHelper.await(lock7);
Assert.assertTrue(results.containsKey("eventListenerBob2"));
Assert.assertTrue(results.containsKey("eventListenerSam2"));
Assert.assertFalse(results.containsKey("eventListenerEncyptedBob2"));
Assert.assertFalse(results.containsKey("eventListenerEncyptedSam2"));
// verify the bob device
CountDownLatch lock8 = new CountDownLatch(1);
aliceSession.getCrypto().setDeviceVerification(MXDeviceInfo.DEVICE_VERIFICATION_VERIFIED, bobSession.getCrypto().getMyDevice().deviceId, bobSession.getMyUserId(), new TestApiCallback<Void>(lock8) {
@Override
public void onSuccess(Void info) {
results.put("setDeviceVerificationBob", "setDeviceVerificationBob");
super.onSuccess(info);
}
});
mTestHelper.await(lock8);
Assert.assertTrue(results.containsKey("setDeviceVerificationBob"));
CountDownLatch lock9 = new CountDownLatch(1);
aliceSession.getCrypto().setRoomBlacklistUnverifiedDevices(roomFromAlicePOV.getRoomId(), new TestApiCallback<Void>(lock9) {
@Override
public void onSuccess(Void info) {
results.put("setRoomBlacklistUnverifiedDevices", "setRoomBlacklistUnverifiedDevices");
super.onSuccess(info);
}
});
mTestHelper.await(lock9);
Assert.assertTrue(results.containsKey("setRoomBlacklistUnverifiedDevices"));
// ensure that the messages are received
results.clear();
CountDownLatch lock10 = new CountDownLatch(3);
activeLock.clear();
activeLock.add(lock10);
activeMessage.clear();
activeMessage.add("message 3");
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(activeMessage.get(0), aliceSession, aliceRoomId), new TestApiCallback<Void>(lock10));
mTestHelper.await(lock10);
Assert.assertTrue(results.containsKey("eventListenerBob2"));
Assert.assertFalse(results.containsKey("eventListenerSam2"));
Assert.assertFalse(results.containsKey("eventListenerEncyptedBob2"));
Assert.assertTrue(results.containsKey("eventListenerEncyptedSam2"));
CountDownLatch lock11 = new CountDownLatch(1);
aliceSession.getCrypto().setRoomUnBlacklistUnverifiedDevices(roomFromAlicePOV.getRoomId(), new TestApiCallback<Void>(lock11) {
@Override
public void onSuccess(Void info) {
results.put("setRoomUnBlacklistUnverifiedDevices", "setRoomUnBlacklistUnverifiedDevices");
super.onSuccess(info);
}
});
mTestHelper.await(lock11);
Assert.assertTrue(results.containsKey("setRoomUnBlacklistUnverifiedDevices"));
// ensure that the messages are received
results.clear();
CountDownLatch lock12 = new CountDownLatch(3);
activeLock.clear();
activeLock.add(lock12);
activeMessage.clear();
activeMessage.add("message 3");
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(activeMessage.get(0), aliceSession, aliceRoomId), new TestApiCallback<Void>(lock12));
mTestHelper.await(lock12);
Assert.assertTrue(results.containsKey("eventListenerBob2"));
Assert.assertTrue(results.containsKey("eventListenerSam2"));
Assert.assertFalse(results.containsKey("eventListenerEncyptedBob2"));
Assert.assertFalse(results.containsKey("eventListenerEncyptedSam2"));
cryptoTestData.clear(context);
}
use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class CryptoTest method test27_testEnableEncryptionAfterNonEncryptedMessages.
@Test
public // -> Bob must be able to decrypt this message
void test27_testEnableEncryptionAfterNonEncryptedMessages() throws Exception {
Log.e(LOG_TAG, "test27_testEnableEncryptionAfterNonEncryptedMessages");
Context context = InstrumentationRegistry.getContext();
final Map<String, Object> results = new HashMap<>();
final String messageFromAlice = "Hello I'm Alice!";
final String message2FromAlice = "I'm still Alice!";
MXSession aliceSession = mTestHelper.createAccount(TestConstants.USER_ALICE, mCryptoTestHelper.getDefaultSessionParams());
MXSession bobSession = mTestHelper.createAccount(TestConstants.USER_BOB, mCryptoTestHelper.getDefaultSessionParams());
CountDownLatch lock00b = new CountDownLatch(2);
aliceSession.enableCrypto(true, new TestApiCallback<Void>(lock00b) {
@Override
public void onSuccess(Void info) {
results.put("enableCrypto1", "enableCrypto1");
super.onSuccess(info);
}
});
bobSession.enableCrypto(true, new TestApiCallback<Void>(lock00b) {
@Override
public void onSuccess(Void info) {
results.put("enableCrypto2", "enableCrypto2");
super.onSuccess(info);
}
});
mTestHelper.await(lock00b);
Assert.assertTrue(results.containsKey("enableCrypto2"));
Assert.assertTrue(results.containsKey("enableCrypto1"));
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");
CountDownLatch lock1 = new CountDownLatch(1);
bobSession.joinRoom(aliceRoomId, new TestApiCallback<String>(lock1) {
@Override
public void onSuccess(String info) {
results.put("joinRoom", "joinRoom");
super.onSuccess(info);
}
});
mTestHelper.await(lock1);
Assert.assertTrue(results.containsKey("joinRoom"));
Room roomFromAlicePOV = aliceSession.getDataHandler().getRoom(aliceRoomId);
CountDownLatch lock2 = new CountDownLatch(1);
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(messageFromAlice, aliceSession, aliceRoomId), new TestApiCallback<Void>(lock2) {
@Override
public void onSuccess(Void info) {
results.put("sendEvent1", "sendEvent1");
super.onSuccess(info);
}
});
mTestHelper.await(lock2);
Assert.assertTrue(results.containsKey("sendEvent1"));
// 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 lock3 = new CountDownLatch(1);
roomFromAlicePOV.enableEncryptionWithAlgorithm(CryptoConstantsKt.MXCRYPTO_ALGORITHM_MEGOLM, new TestApiCallback<Void>(lock3) {
@Override
public void onSuccess(Void info) {
results.put("enableEncryptionWithAlgorithm", "enableEncryptionWithAlgorithm");
super.onSuccess(info);
}
});
mTestHelper.await(lock3);
Assert.assertTrue(results.containsKey("enableEncryptionWithAlgorithm"));
Room roomFromBobPOV2 = bobSession2.getDataHandler().getRoom(aliceRoomId);
final CountDownLatch lock4 = 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);
lock4.countDown();
} else if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE_ENCRYPTED)) {
lock4.countDown();
}
}
};
roomFromBobPOV2.getTimeline().addEventTimelineListener(eventTimelineListener2);
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(message2FromAlice, aliceSession, aliceRoomId), new SimpleApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
// Ignore
}
});
mTestHelper.await(lock4);
Assert.assertEquals(1, receivedEvents2.size());
Event 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 test12_testAliceAndBobInAEncryptedRoomBackPaginationFromHomeServer.
@Test
public void test12_testAliceAndBobInAEncryptedRoomBackPaginationFromHomeServer() throws Exception {
Log.e(LOG_TAG, "test12_testAliceAndBobInAEncryptedRoomBackPaginationFromHomeServer");
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();
String eventId = bobSession.getDataHandler().getStore().getLatestEvent(aliceRoomId).eventId;
EventTimeline timeline = EventTimelineFactory.pastTimeline(bobSession.getDataHandler(), aliceRoomId, eventId);
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();
}
}
};
timeline.addEventTimelineListener(eventTimelineListener);
timeline.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(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);
}
use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class CryptoTest method test28_testLeftBobAndAliceWithNewDevice.
// Test for https://github.com/vector-im/riot-web/issues/4983
// - Alice and Bob share an e2e room; Bob tracks Alice's devices
// - Bob leaves the room, so stops getting updates
// - Alice adds a new device
// - Alice and Bob start sharing a room again
// - Bob has an out of date list of Alice's devices
@Test
public void test28_testLeftBobAndAliceWithNewDevice() throws Exception {
Log.e(LOG_TAG, "test28_testLeftBobAndAliceWithNewDevice");
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();
// - Bob leaves the room, so stops getting updates
CountDownLatch lock1 = new CountDownLatch(1);
final Room bobLeftRoom = bobSession.getDataHandler().getRoom(aliceRoomId);
bobLeftRoom.leave(new TestApiCallback<Void>(lock1) {
@Override
public void onSuccess(Void info) {
results.put("lock1", "lock1");
super.onSuccess(info);
}
});
mTestHelper.await(lock1);
Assert.assertTrue(results.containsKey("lock1"));
// - Alice adds a new device
final MXSession aliceSession2 = mTestHelper.logIntoAccount(aliceSession.getMyUserId(), mCryptoTestHelper.getEncryptedSessionParams());
Assert.assertNotNull(aliceSession2);
// - Alice and Bob start sharing a room again
final String[] aliceRoomId2 = { null };
CountDownLatch lock3 = new CountDownLatch(1);
aliceSession2.createRoom(null, null, RoomDirectoryVisibility.DIRECTORY_VISIBILITY_PUBLIC, null, null, new TestApiCallback<String>(lock3) {
@Override
public void onSuccess(String info) {
aliceRoomId2[0] = info;
super.onSuccess(info);
}
});
mTestHelper.await(lock3);
Assert.assertNotNull(aliceRoomId2[0]);
Room roomFromAlicePOV = aliceSession2.getDataHandler().getRoom(aliceRoomId2[0]);
CountDownLatch lock4 = new CountDownLatch(1);
roomFromAlicePOV.enableEncryptionWithAlgorithm(CryptoConstantsKt.MXCRYPTO_ALGORITHM_MEGOLM, new TestApiCallback<Void>(lock4) {
@Override
public void onSuccess(Void info) {
results.put("lock4", "lock4");
super.onSuccess(info);
}
});
mTestHelper.await(lock4);
Assert.assertTrue(results.containsKey("lock4"));
CountDownLatch lock5 = new CountDownLatch(1);
bobSession.joinRoom(aliceRoomId2[0], new TestApiCallback<String>(lock5) {
@Override
public void onSuccess(String info) {
results.put("lock5", "lock5");
super.onSuccess(info);
}
});
mTestHelper.await(lock5);
Assert.assertTrue(results.containsKey("lock5"));
// - Bob has an out of date list of Alice's devices
Room roomFromBobPOV = bobSession.getDataHandler().getRoom(aliceRoomId2[0]);
final String messageFromBob = "Hello Alice with new device!";
final CountDownLatch lock6 = new CountDownLatch(2);
MXEventListener eventListener = new MXEventListener() {
@Override
public void onLiveEvent(Event event, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE)) {
mCryptoTestHelper.checkEncryptedEvent(event, aliceRoomId2[0], messageFromBob, bobSession);
results.put("lock6", "lock6");
lock6.countDown();
}
}
};
roomFromAlicePOV.addEventListener(eventListener);
roomFromBobPOV.sendEvent(mCryptoTestHelper.buildTextEvent(messageFromBob, bobSession, aliceRoomId2[0]), new TestApiCallback<Void>(lock6));
mTestHelper.await(lock6);
Assert.assertTrue(results.containsKey("lock6"));
cryptoTestData.clear(context);
aliceSession2.clear(context);
}
use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class CryptoTest method test24_testExportImport.
@Test
public void test24_testExportImport() throws Exception {
Log.e(LOG_TAG, "test24_testExportImport");
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!";
String password = "hello";
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();
CountDownLatch lock1a = new CountDownLatch(1);
aliceSession.getCrypto().exportRoomKeys(password, new TestApiCallback<byte[]>(lock1a) {
@Override
public void onSuccess(byte[] info) {
results.put("exportRoomKeys", info);
super.onSuccess(info);
}
});
mTestHelper.await(lock1a);
Assert.assertTrue(results.containsKey("exportRoomKeys"));
// close the session and clear the data
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);
// import the e2e keys
// test with a wrong password
CountDownLatch lock3 = new CountDownLatch(1);
aliceSession2.getCrypto().importRoomKeys((byte[]) results.get("exportRoomKeys"), "wrong password", null, new TestApiCallback<ImportRoomKeysResult>(lock3, false) {
@Override
public void onUnexpectedError(Exception e) {
results.put("importRoomKeys_failed", "importRoomKeys_failed");
super.onUnexpectedError(e);
}
});
mTestHelper.await(lock3);
Assert.assertTrue(results.containsKey("importRoomKeys_failed"));
// check that the message cannot be decrypted
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);
CountDownLatch lock4 = new CountDownLatch(1);
aliceSession2.getCrypto().importRoomKeys((byte[]) results.get("exportRoomKeys"), password, null, new TestApiCallback<ImportRoomKeysResult>(lock4) {
@Override
public void onSuccess(ImportRoomKeysResult info) {
Assert.assertEquals(1, info.getTotalNumberOfKeys());
Assert.assertEquals(1, info.getSuccessfullyNumberOfImportedKeys());
results.put("importRoomKeys", "importRoomKeys");
super.onSuccess(info);
}
});
mTestHelper.await(lock4);
Assert.assertTrue(results.containsKey("importRoomKeys"));
// check that the message CAN be decrypted
event = roomFromAlicePOV2.getDataHandler().getStore().getLatestEvent(aliceRoomId);
Assert.assertNotNull(event);
Assert.assertTrue(event.isEncrypted());
Assert.assertNotNull(event.getClearEvent());
Assert.assertNull(event.getCryptoError());
mCryptoTestHelper.checkEncryptedEvent(event, aliceRoomId, message, aliceSession);
cryptoTestData.clear(context);
aliceSession2.clear(context);
}
Aggregations