Search in sources :

Example 6 with MXStoreListener

use of org.matrix.androidsdk.data.store.MXStoreListener 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 HashMap<String, Object> results = new HashMap<>();
    createBobAccount();
    mBobSession.getCredentials().deviceId = "BobDevice";
    assertTrue(null == mBobSession.getCrypto());
    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"));
    assertTrue(null != mBobSession.getCrypto());
    SystemClock.sleep(1000);
    final String deviceCurve25519Key = mBobSession.getCrypto().getOlmDevice().getDeviceCurve25519Key();
    final String deviceEd25519Key = mBobSession.getCrypto().getOlmDevice().getDeviceEd25519Key();
    final List<MXDeviceInfo> myUserDevices = mBobSession.getCrypto().getUserDevices(mBobSession.getMyUserId());
    assertTrue(null != myUserDevices);
    assertTrue(1 == myUserDevices.size());
    final Credentials bobCredentials = mBobSession.getCredentials();
    Uri uri = Uri.parse(CryptoTestHelper.TESTS_HOME_SERVER_URL);
    HomeServerConnectionConfig hs = new HomeServerConnectionConfig(uri);
    hs.setCredentials(bobCredentials);
    IMXStore store = new MXFileStore(hs, context);
    MXSession bobSession2 = new MXSession(hs, new MXDataHandler(store, bobCredentials), context);
    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();
    lock1.await(1000, TimeUnit.MILLISECONDS);
    assertTrue(results.containsKey("onStoreReady"));
    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);
    lock2.await(1000, TimeUnit.MILLISECONDS);
    assertTrue(results.containsKey("onInitialSyncComplete"));
    assertTrue(results.containsKey("onCryptoSyncComplete"));
    MXCrypto crypto = bobSession2.getCrypto();
    assertNotNull(crypto);
    assertTrue(TextUtils.equals(deviceCurve25519Key, crypto.getOlmDevice().getDeviceCurve25519Key()));
    assertTrue(TextUtils.equals(deviceEd25519Key, crypto.getOlmDevice().getDeviceEd25519Key()));
    List<MXDeviceInfo> myUserDevices2 = bobSession2.getCrypto().getUserDevices(bobSession2.getMyUserId());
    assertTrue(1 == myUserDevices2.size());
    assertTrue(TextUtils.equals(myUserDevices2.get(0).deviceId, myUserDevices.get(0).deviceId));
    mBobSession.clear(context);
    bobSession2.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) MXDeviceInfo(org.matrix.androidsdk.crypto.data.MXDeviceInfo) CountDownLatch(java.util.concurrent.CountDownLatch) Uri(android.net.Uri) MXEventListener(org.matrix.androidsdk.listeners.MXEventListener) JsonObject(com.google.gson.JsonObject) MatrixError(org.matrix.androidsdk.rest.model.MatrixError) Credentials(org.matrix.androidsdk.rest.model.login.Credentials) MXCrypto(org.matrix.androidsdk.crypto.MXCrypto) Test(org.junit.Test)

Aggregations

Context (android.content.Context)6 Uri (android.net.Uri)6 JsonObject (com.google.gson.JsonObject)6 HashMap (java.util.HashMap)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Test (org.junit.Test)6 IMXStore (org.matrix.androidsdk.data.store.IMXStore)6 MXFileStore (org.matrix.androidsdk.data.store.MXFileStore)6 MXStoreListener (org.matrix.androidsdk.data.store.MXStoreListener)6 MXEventListener (org.matrix.androidsdk.listeners.MXEventListener)6 MatrixError (org.matrix.androidsdk.rest.model.MatrixError)6 Credentials (org.matrix.androidsdk.rest.model.login.Credentials)6 MXDeviceInfo (org.matrix.androidsdk.crypto.data.MXDeviceInfo)3 Room (org.matrix.androidsdk.data.Room)3 Event (org.matrix.androidsdk.rest.model.Event)3 MXUsersDevicesMap (org.matrix.androidsdk.crypto.data.MXUsersDevicesMap)2 MXCrypto (org.matrix.androidsdk.crypto.MXCrypto)1 MXOlmSessionResult (org.matrix.androidsdk.crypto.data.MXOlmSessionResult)1 RoomState (org.matrix.androidsdk.data.RoomState)1