use of org.matrix.androidsdk.rest.model.Event in project matrix-android-sdk by matrix-org.
the class CryptoTest method test20_testAliceAndBlockedBob.
@Test
public void test20_testAliceAndBlockedBob() throws Exception {
Log.e(LOG_TAG, "test20_testAliceAndBlockedBob");
final HashMap<String, String> results = new HashMap<>();
doE2ETestWithAliceAndBobInARoom(true);
mBobSession.getCrypto().setWarnOnUnknownDevices(false);
mAliceSession.getCrypto().setWarnOnUnknownDevices(false);
final Room roomFromBobPOV = mBobSession.getDataHandler().getRoom(mRoomId);
final Room roomFromAlicePOV = mAliceSession.getDataHandler().getRoom(mRoomId);
assertTrue(roomFromBobPOV.isEncrypted());
assertTrue(roomFromAlicePOV.isEncrypted());
final CountDownLatch lock1 = new CountDownLatch(1);
final ArrayList<Event> receivedEvents = new ArrayList<>();
EventTimeline.EventTimelineListener eventTimelineListener = new EventTimeline.EventTimelineListener() {
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.getLiveTimeLine().addEventTimelineListener(eventTimelineListener);
String aliceMessage1 = "Hello I'm Alice!";
roomFromAlicePOV.sendEvent(buildTextEvent(aliceMessage1, mAliceSession), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
}
@Override
public void onNetworkError(Exception e) {
}
@Override
public void onMatrixError(MatrixError e) {
}
@Override
public void onUnexpectedError(Exception e) {
}
});
lock1.await(1000, TimeUnit.MILLISECONDS);
assertTrue(1 == receivedEvents.size());
Event event = receivedEvents.get(0);
assertTrue(checkEncryptedEvent(event, mRoomId, aliceMessage1, mAliceSession));
// block the bob's device
final CountDownLatch lock1b = new CountDownLatch(1);
mAliceSession.getCrypto().setDeviceVerification(MXDeviceInfo.DEVICE_VERIFICATION_BLOCKED, mBobSession.getCredentials().deviceId, mBobSession.getMyUserId(), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
results.put("setDeviceVerification20", "setDeviceVerification20");
lock1b.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock1b.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock1b.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock1b.countDown();
}
});
lock1b.await();
assertTrue(results.containsKey("setDeviceVerification20"));
// /
final CountDownLatch lock2 = new CountDownLatch(1);
final ArrayList<Event> receivedEvents2 = new ArrayList<>();
EventTimeline.EventTimelineListener eventTimelineListener2 = new EventTimeline.EventTimelineListener() {
public void onEvent(Event event, EventTimeline.Direction direction, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE_ENCRYPTED)) {
receivedEvents2.add(event);
lock2.countDown();
}
}
};
roomFromBobPOV.getLiveTimeLine().addEventTimelineListener(eventTimelineListener2);
String aliceMessage2 = "Hello I'm still Alice!";
roomFromAlicePOV.sendEvent(buildTextEvent(aliceMessage2, mAliceSession), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
}
@Override
public void onNetworkError(Exception e) {
}
@Override
public void onMatrixError(MatrixError e) {
}
@Override
public void onUnexpectedError(Exception e) {
}
});
lock2.await(1000, TimeUnit.MILLISECONDS);
assertTrue(1 == receivedEvents2.size());
event = receivedEvents2.get(0);
assertTrue(null == event.getClearEvent());
assertTrue(null != event.getCryptoError());
assertTrue(TextUtils.equals(event.getCryptoError().errcode, MXCryptoError.UNKNOWN_INBOUND_SESSION_ID_ERROR_CODE));
// unblock the bob's device
final CountDownLatch lock2b = new CountDownLatch(1);
mAliceSession.getCrypto().setDeviceVerification(MXDeviceInfo.DEVICE_VERIFICATION_UNVERIFIED, mBobSession.getCredentials().deviceId, mBobSession.getMyUserId(), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
results.put("setDeviceVerification40", "setDeviceVerification40");
lock2b.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock2b.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock2b.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock2b.countDown();
}
});
lock2b.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("setDeviceVerification40"));
// /
final CountDownLatch lock3 = new CountDownLatch(1);
final ArrayList<Event> receivedEvents3 = new ArrayList<>();
EventTimeline.EventTimelineListener eventTimelineListener3 = new EventTimeline.EventTimelineListener() {
public void onEvent(Event event, EventTimeline.Direction direction, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE)) {
receivedEvents3.add(event);
lock3.countDown();
}
}
};
roomFromBobPOV.getLiveTimeLine().addEventTimelineListener(eventTimelineListener3);
String aliceMessage3 = "Hello I'm still Alice and you can read this!";
roomFromAlicePOV.sendEvent(buildTextEvent(aliceMessage3, mAliceSession), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
}
@Override
public void onNetworkError(Exception e) {
}
@Override
public void onMatrixError(MatrixError e) {
}
@Override
public void onUnexpectedError(Exception e) {
}
});
lock3.await(1000, TimeUnit.MILLISECONDS);
assertTrue(1 == receivedEvents3.size());
event = receivedEvents3.get(0);
assertTrue(checkEncryptedEvent(event, mRoomId, aliceMessage3, mAliceSession));
}
use of org.matrix.androidsdk.rest.model.Event in project matrix-android-sdk by matrix-org.
the class CryptoTest method test12_testAliceAndBobInACryptedRoomBackPaginationFromHomeServer.
@Test
public void test12_testAliceAndBobInACryptedRoomBackPaginationFromHomeServer() throws Exception {
Log.e(LOG_TAG, "test12_testAliceAndBobInACryptedRoomBackPaginationFromHomeServer");
Context context = InstrumentationRegistry.getContext();
final HashMap<String, Object> results = new HashMap();
doE2ETestWithAliceAndBobInARoomWithCryptedMessages(true);
String eventId = mBobSession.getDataHandler().getStore().getLatestEvent(mRoomId).eventId;
EventTimeline timeline = new EventTimeline(mBobSession.getDataHandler(), mRoomId, eventId);
final CountDownLatch lock2 = new CountDownLatch(6);
final ArrayList<Event> receivedEvents = new ArrayList<>();
EventTimeline.EventTimelineListener eventTimelineListener = new EventTimeline.EventTimelineListener() {
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 ApiCallback<Integer>() {
@Override
public void onSuccess(Integer info) {
results.put("backPaginate", "backPaginate");
lock2.countDown();
}
@Override
public void onNetworkError(Exception e) {
}
@Override
public void onMatrixError(MatrixError e) {
}
@Override
public void onUnexpectedError(Exception e) {
}
});
lock2.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("backPaginate"));
assertTrue(5 == receivedEvents.size());
checkEncryptedEvent(receivedEvents.get(0), mRoomId, messagesFromAlice.get(1), mAliceSession);
checkEncryptedEvent(receivedEvents.get(1), mRoomId, messagesFromBob.get(2), mBobSession);
checkEncryptedEvent(receivedEvents.get(2), mRoomId, messagesFromBob.get(1), mBobSession);
checkEncryptedEvent(receivedEvents.get(3), mRoomId, messagesFromBob.get(0), mBobSession);
checkEncryptedEvent(receivedEvents.get(4), mRoomId, messagesFromAlice.get(0), mAliceSession);
mBobSession.clear(context);
mAliceSession.clear(context);
}
use of org.matrix.androidsdk.rest.model.Event in project matrix-android-sdk by matrix-org.
the class CryptoTest method test09_testAliceInACryptedRoomAfterInitialSync.
@Test
public void test09_testAliceInACryptedRoomAfterInitialSync() throws Exception {
Log.e(LOG_TAG, "test09_testAliceInACryptedRoomAfterInitialSync");
Context context = InstrumentationRegistry.getContext();
final HashMap<String, Object> results = new HashMap<>();
doE2ETestWithAliceInARoom();
mAliceSession.getCrypto().setWarnOnUnknownDevices(false);
final String message = "Hello myself!";
Credentials aliceCredentials = mAliceSession.getCredentials();
mAliceSession.clear(context);
Uri uri = Uri.parse(CryptoTestHelper.TESTS_HOME_SERVER_URL);
HomeServerConnectionConfig hs = new HomeServerConnectionConfig(uri);
hs.setCredentials(aliceCredentials);
IMXStore store = new MXFileStore(hs, context);
final CountDownLatch lock1 = new CountDownLatch(1);
final MXSession aliceSession2 = new MXSession(hs, new MXDataHandler(store, aliceCredentials), context);
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();
lock1.await(1000, TimeUnit.MILLISECONDS);
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);
lock1b.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("onInitialSyncComplete"));
assertTrue(results.containsKey("onCryptoSyncComplete"));
Room roomFromAlicePOV2 = aliceSession2.getDataHandler().getRoom(mRoomId);
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)) {
try {
if (checkEncryptedEvent(event, mRoomId, message, aliceSession2)) {
lock2.countDown();
}
} catch (Exception e) {
}
}
}
};
roomFromAlicePOV2.addEventListener(aliceEventListener);
}
// the IOS client echoes the message
// the android client does not
roomFromAlicePOV2.sendEvent(buildTextEvent(message, aliceSession2), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
results.put("sendEvent", "sendEvent");
lock2.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock2.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock2.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock2.countDown();
}
});
lock2.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("sendEvent"));
aliceSession2.clear(context);
}
use of org.matrix.androidsdk.rest.model.Event in project matrix-android-sdk by matrix-org.
the class CryptoTest method test15_testReplayAttack.
@Test
public void test15_testReplayAttack() throws Exception {
Log.e(LOG_TAG, "test15_testReplayAttack");
final HashMap<String, Object> results = new HashMap<>();
doE2ETestWithAliceAndBobInARoom(true);
mBobSession.getCrypto().setWarnOnUnknownDevices(false);
mAliceSession.getCrypto().setWarnOnUnknownDevices(false);
String messageFromAlice = "Hello I'm Alice!";
final Room roomFromBobPOV = mBobSession.getDataHandler().getRoom(mRoomId);
final Room roomFromAlicePOV = mAliceSession.getDataHandler().getRoom(mRoomId);
assertTrue(roomFromBobPOV.isEncrypted());
assertTrue(roomFromAlicePOV.isEncrypted());
final CountDownLatch lock1 = new CountDownLatch(2);
MXEventListener bobEventListener = new MXEventListener() {
@Override
public void onLiveEvent(Event event, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE) && !TextUtils.equals(event.getSender(), mBobSession.getMyUserId())) {
results.put("bobEcho", event);
event.setClearData(null);
mBobSession.getDataHandler().decryptEvent(event, roomFromBobPOV.getLiveTimeLine().getTimelineId());
results.put("decrypted", event);
lock1.countDown();
}
}
};
roomFromBobPOV.addEventListener(bobEventListener);
mBobSession.getDataHandler().addListener(new MXEventListener() {
@Override
public void onToDeviceEvent(Event event) {
results.put("onToDeviceEvent", event);
lock1.countDown();
}
});
roomFromAlicePOV.sendEvent(buildTextEvent(messageFromAlice, mAliceSession), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
}
@Override
public void onNetworkError(Exception e) {
}
@Override
public void onMatrixError(MatrixError e) {
}
@Override
public void onUnexpectedError(Exception e) {
}
});
lock1.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("onToDeviceEvent"));
assertTrue(results.containsKey("bobEcho"));
assertTrue(results.containsKey("decrypted"));
Event decryptedEvent = (Event) results.get("decrypted");
assertTrue(null == decryptedEvent.getClearEvent());
assertTrue(TextUtils.equals(decryptedEvent.getCryptoError().errcode, MXCryptoError.DUPLICATED_MESSAGE_INDEX_ERROR_CODE));
// Decrypting it with no replay attack mitigation must still work
mBobSession.getDataHandler().decryptEvent(decryptedEvent, null);
assertTrue(checkEncryptedEvent(decryptedEvent, mRoomId, messageFromAlice, mAliceSession));
}
use of org.matrix.androidsdk.rest.model.Event in project matrix-android-sdk by matrix-org.
the class CryptoTest method test17_testLateRoomKey.
@Test
public void test17_testLateRoomKey() throws Exception {
Log.e(LOG_TAG, "test17_testLateRoomKey");
final HashMap<String, Object> results = new HashMap<>();
doE2ETestWithAliceAndBobInARoom(true);
mBobSession.getCrypto().setWarnOnUnknownDevices(false);
mAliceSession.getCrypto().setWarnOnUnknownDevices(false);
String messageFromAlice = "Hello I'm Alice!";
final Room roomFromBobPOV = mBobSession.getDataHandler().getRoom(mRoomId);
final Room roomFromAlicePOV = mAliceSession.getDataHandler().getRoom(mRoomId);
assertTrue(roomFromBobPOV.isEncrypted());
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();
}
}
};
mBobSession.getDataHandler().addListener(bobEventListener);
final ArrayList<Event> receivedEvents = new ArrayList<>();
EventTimeline.EventTimelineListener eventTimelineListener = new EventTimeline.EventTimelineListener() {
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.getLiveTimeLine().addEventTimelineListener(eventTimelineListener);
roomFromAlicePOV.sendEvent(buildTextEvent(messageFromAlice, mAliceSession), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
}
@Override
public void onNetworkError(Exception e) {
}
@Override
public void onMatrixError(MatrixError e) {
}
@Override
public void onUnexpectedError(Exception e) {
}
});
lock1.await(2000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("onToDeviceEvent"));
assertTrue(1 == receivedEvents.size());
Event event = receivedEvents.get(0);
assertTrue(checkEncryptedEvent(event, mRoomId, messageFromAlice, mAliceSession));
// Reinject a modified version of the received room_key event from Alice.
// From Bob pov, that mimics Alice resharing her keys but with an advanced outbound group session.
Event toDeviceEvent = (Event) results.get("onToDeviceEvent");
String sessionId = toDeviceEvent.getContentAsJsonObject().get("session_id").getAsString();
String senderKey = toDeviceEvent.senderKey();
// remove the session
mBobSession.getCrypto().getOlmDevice().removeInboundGroupSession(sessionId, senderKey);
event.setClearData(null);
// check that the message cannot be decrypted
assertTrue(!mBobSession.getDataHandler().decryptEvent(event, null));
// check the error code
assertTrue(TextUtils.equals(event.getCryptoError().errcode, MXCryptoError.UNKNOWN_INBOUND_SESSION_ID_ERROR_CODE));
receivedEvents.clear();
final CountDownLatch lock2 = new CountDownLatch(1);
roomFromBobPOV.addEventListener(new MXEventListener() {
@Override
public void onEventDecrypted(Event event) {
results.put("onEventDecrypted", "onEventDecrypted");
receivedEvents.add(event);
lock2.countDown();
}
});
event.setClearData(null);
// reinject the session key
mBobSession.getDataHandler().onToDeviceEvent(toDeviceEvent);
// the message should be decrypted later
lock2.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("onEventDecrypted"));
assertTrue(1 == receivedEvents.size());
assertTrue(checkEncryptedEvent(receivedEvents.get(0), mRoomId, messageFromAlice, mAliceSession));
assertTrue(null == receivedEvents.get(0).getCryptoError());
}
Aggregations