Search in sources :

Example 6 with Room

use of org.matrix.androidsdk.data.Room 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));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EventTimeline(org.matrix.androidsdk.data.EventTimeline) CountDownLatch(java.util.concurrent.CountDownLatch) Event(org.matrix.androidsdk.rest.model.Event) MatrixError(org.matrix.androidsdk.rest.model.MatrixError) Room(org.matrix.androidsdk.data.Room) RoomState(org.matrix.androidsdk.data.RoomState) Test(org.junit.Test)

Example 7 with Room

use of org.matrix.androidsdk.data.Room in project matrix-android-sdk by matrix-org.

the class CryptoTest method test05_testRoomIsEncrypted.

@Test
public void test05_testRoomIsEncrypted() throws Exception {
    Log.e(LOG_TAG, "test05_testRoomIsEncrypted");
    Context context = InstrumentationRegistry.getContext();
    final HashMap<String, Object> results = new HashMap<>();
    createBobAccount();
    final CountDownLatch lock0 = new CountDownLatch(1);
    mBobSession.enableCrypto(true, new ApiCallback<Void>() {

        @Override
        public void onSuccess(Void info) {
            results.put("enableCrypto", "enableCrypto");
            lock0.countDown();
        }

        @Override
        public void onNetworkError(Exception e) {
            lock0.countDown();
        }

        @Override
        public void onMatrixError(MatrixError e) {
            lock0.countDown();
        }

        @Override
        public void onUnexpectedError(Exception e) {
            lock0.countDown();
        }
    });
    lock0.await(1000, TimeUnit.MILLISECONDS);
    assertTrue(results.containsKey("enableCrypto"));
    mRoomId = null;
    final CountDownLatch lock1 = new CountDownLatch(1);
    mBobSession.createRoom(new ApiCallback<String>() {

        @Override
        public void onSuccess(String info) {
            mRoomId = info;
            lock1.countDown();
        }

        @Override
        public void onNetworkError(Exception e) {
            lock1.countDown();
        }

        @Override
        public void onMatrixError(MatrixError e) {
            lock1.countDown();
        }

        @Override
        public void onUnexpectedError(Exception e) {
            lock1.countDown();
        }
    });
    lock1.await(1000, TimeUnit.MILLISECONDS);
    assertTrue(null != mRoomId);
    Room room = mBobSession.getDataHandler().getRoom(mRoomId);
    assertTrue(!room.isEncrypted());
    final CountDownLatch lock2 = new CountDownLatch(1);
    room.enableEncryptionWithAlgorithm(MXCryptoAlgorithms.MXCRYPTO_ALGORITHM_MEGOLM, new ApiCallback<Void>() {

        @Override
        public void onSuccess(Void info) {
            results.put("enableEncryptionWithAlgorithm", "enableEncryptionWithAlgorithm");
            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("enableEncryptionWithAlgorithm"));
    assertTrue(room.isEncrypted());
    mBobSession.clear(context);
}
Also used : Context(android.content.Context) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) JsonObject(com.google.gson.JsonObject) MatrixError(org.matrix.androidsdk.rest.model.MatrixError) Room(org.matrix.androidsdk.data.Room) Test(org.junit.Test)

Example 8 with Room

use of org.matrix.androidsdk.data.Room in project matrix-android-sdk by matrix-org.

the class CryptoTest method test06_testAliceInACryptedRoom.

@Test
public void test06_testAliceInACryptedRoom() throws Exception {
    Log.e(LOG_TAG, "test06_testAliceInACryptedRoom");
    Context context = InstrumentationRegistry.getContext();
    doE2ETestWithAliceInARoom();
    final String message = "Hello myself!";
    Room roomFromAlicePOV = mAliceSession.getDataHandler().getRoom(mRoomId);
    assertTrue(roomFromAlicePOV.isEncrypted());
    final CountDownLatch lock1 = new CountDownLatch(1);
    // the IOS client echoes the message
    // the android client does not
    roomFromAlicePOV.sendEvent(buildTextEvent(message, mAliceSession), new ApiCallback<Void>() {

        @Override
        public void onSuccess(Void info) {
            lock1.countDown();
        }

        @Override
        public void onNetworkError(Exception e) {
            lock1.countDown();
        }

        @Override
        public void onMatrixError(MatrixError e) {
            lock1.countDown();
        }

        @Override
        public void onUnexpectedError(Exception e) {
            lock1.countDown();
        }
    });
    lock1.await(1000, TimeUnit.MILLISECONDS);
    mAliceSession.clear(context);
}
Also used : Context(android.content.Context) CountDownLatch(java.util.concurrent.CountDownLatch) MatrixError(org.matrix.androidsdk.rest.model.MatrixError) Room(org.matrix.androidsdk.data.Room) Test(org.junit.Test)

Example 9 with Room

use of org.matrix.androidsdk.data.Room 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);
}
Also used : Context(android.content.Context) MXStoreListener(org.matrix.androidsdk.data.store.MXStoreListener) HashMap(java.util.HashMap) IMXStore(org.matrix.androidsdk.data.store.IMXStore) MXFileStore(org.matrix.androidsdk.data.store.MXFileStore) CountDownLatch(java.util.concurrent.CountDownLatch) Uri(android.net.Uri) MXEventListener(org.matrix.androidsdk.listeners.MXEventListener) Event(org.matrix.androidsdk.rest.model.Event) JsonObject(com.google.gson.JsonObject) MatrixError(org.matrix.androidsdk.rest.model.MatrixError) Room(org.matrix.androidsdk.data.Room) Credentials(org.matrix.androidsdk.rest.model.login.Credentials) RoomState(org.matrix.androidsdk.data.RoomState) Test(org.junit.Test)

Example 10 with Room

use of org.matrix.androidsdk.data.Room 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));
}
Also used : HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) MXEventListener(org.matrix.androidsdk.listeners.MXEventListener) Event(org.matrix.androidsdk.rest.model.Event) JsonObject(com.google.gson.JsonObject) MatrixError(org.matrix.androidsdk.rest.model.MatrixError) Room(org.matrix.androidsdk.data.Room) RoomState(org.matrix.androidsdk.data.RoomState) Test(org.junit.Test)

Aggregations

Room (org.matrix.androidsdk.data.Room)60 MatrixError (org.matrix.androidsdk.rest.model.MatrixError)33 Event (org.matrix.androidsdk.rest.model.Event)27 ArrayList (java.util.ArrayList)26 CountDownLatch (java.util.concurrent.CountDownLatch)26 HashMap (java.util.HashMap)25 JsonObject (com.google.gson.JsonObject)24 Test (org.junit.Test)22 RoomState (org.matrix.androidsdk.data.RoomState)22 MXEventListener (org.matrix.androidsdk.listeners.MXEventListener)19 Context (android.content.Context)15 RoomMember (org.matrix.androidsdk.rest.model.RoomMember)12 EventTimeline (org.matrix.androidsdk.data.EventTimeline)8 IMXStore (org.matrix.androidsdk.data.store.IMXStore)8 File (java.io.File)7 MXDeviceInfo (org.matrix.androidsdk.crypto.data.MXDeviceInfo)6 ApiCallback (org.matrix.androidsdk.rest.callback.ApiCallback)6 Credentials (org.matrix.androidsdk.rest.model.login.Credentials)6 List (java.util.List)5 Uri (android.net.Uri)4