Search in sources :

Example 21 with MXSession

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

the class RoomNameTestHelper method createScenario.

/**
 * Create a base scenario for all room name tests
 *
 * @param nbOfUsers       nb of user to create and to invite in the room (min to 1)
 * @param roomName        initial room name, or null for no room name
 * @param withLazyLoading true to enable lazy loading for alice account
 * @return initialized data
 */
public RoomNameScenarioData createScenario(final int nbOfUsers, @Nullable final String roomName, final boolean withLazyLoading) throws Exception {
    final SessionTestParams createSessionParams = new SessionTestParams(true);
    List<MXSession> createdSessions = new ArrayList<>(nbOfUsers);
    // Create all the accounts
    for (int i = 0; i < nbOfUsers; i++) {
        MXSession session = mTestHelper.createAccount("User_" + i, createSessionParams);
        createdSessions.add(session);
    }
    Assert.assertEquals(nbOfUsers, createdSessions.size());
    // First user create a Room
    final MXSession firstSession = createdSessions.get(0);
    final Map<String, String> results = new HashMap<>();
    CountDownLatch latch = new CountDownLatch(1);
    firstSession.createRoom(new TestApiCallback<String>(latch) {

        @Override
        public void onSuccess(String info) {
            results.put("roomId", info);
            super.onSuccess(info);
        }
    });
    mTestHelper.await(latch);
    final String roomId = results.get("roomId");
    final Room room = firstSession.getDataHandler().getRoom(roomId);
    // Update room name
    if (roomName != null) {
        latch = new CountDownLatch(1);
        room.updateName(roomName, new TestApiCallback<Void>(latch));
        mTestHelper.await(latch);
    }
    // update join rules
    latch = new CountDownLatch(1);
    room.updateJoinRules(RoomState.JOIN_RULE_PUBLIC, new TestApiCallback<Void>(latch));
    mTestHelper.await(latch);
    // all other users join the room
    for (int i = 1; i < nbOfUsers; i++) {
        latch = new CountDownLatch(1);
        createdSessions.get(i).joinRoom(roomId, new TestApiCallback<String>(latch));
        mTestHelper.await(latch);
    }
    // invite dave
    latch = new CountDownLatch(1);
    room.invite(firstSession, "@dave:localhost:8480", new TestApiCallback<Void>(latch));
    mTestHelper.await(latch);
    final SessionTestParams logSessionParams = new SessionTestParams(true, false, withLazyLoading);
    List<MXSession> loggedSessions = new ArrayList<>(nbOfUsers);
    // open new sessions, using the same user ids
    for (MXSession session : createdSessions) {
        MXSession loggedSession = mTestHelper.logIntoAccount(session.getMyUserId(), logSessionParams);
        loggedSessions.add(loggedSession);
    }
    // Clear created sessions (must be done after getting the user ids)
    mTestHelper.clearAllSessions(createdSessions);
    return new RoomNameScenarioData(loggedSessions, roomId);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) MXSession(org.matrix.androidsdk.MXSession) SessionTestParams(org.matrix.androidsdk.common.SessionTestParams) Room(org.matrix.androidsdk.data.Room)

Example 22 with MXSession

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

the class CommonTestHelper method logIntoAccount.

/**
 * Logs into an existing account
 *
 * @param userId     the userId to log in
 * @param password   the password to log in
 * @param testParams test params about the session
 * @return the session associated with the existing account
 */
private MXSession logIntoAccount(@NonNull final String userId, @NonNull final String password, @NonNull final SessionTestParams testParams) throws InterruptedException {
    final Context context = InstrumentationRegistry.getContext();
    final MXSession session = logAccountAndSync(context, userId, password, testParams);
    Assert.assertNotNull(session);
    return session;
}
Also used : Context(android.content.Context) MXSession(org.matrix.androidsdk.MXSession)

Example 23 with MXSession

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

the class CommonTestHelper method createAccount.

// PRIVATE METHODS *****************************************************************************
/**
 * Creates a unique account
 *
 * @param userNamePrefix the user name prefix
 * @param password       the password
 * @param testParams     test params about the session
 * @return the session associated with the newly created account
 */
private MXSession createAccount(@NonNull final String userNamePrefix, @NonNull final String password, @NonNull final SessionTestParams testParams) throws InterruptedException {
    final Context context = InstrumentationRegistry.getContext();
    final MXSession session = createAccountAndSync(context, userNamePrefix + "_" + System.currentTimeMillis() + UUID.randomUUID(), password, testParams);
    Assert.assertNotNull(session);
    return session;
}
Also used : Context(android.content.Context) MXSession(org.matrix.androidsdk.MXSession)

Example 24 with MXSession

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

the class CommonTestHelper method logAccountAndSync.

/**
 * Start an account login
 *
 * @param context           the context
 * @param userName          the account username
 * @param password          the password
 * @param sessionTestParams session test params
 */
private MXSession logAccountAndSync(final Context context, final String userName, final String password, final SessionTestParams sessionTestParams) throws InterruptedException {
    final HomeServerConnectionConfig hs = createHomeServerConfig(null);
    LoginRestClient loginRestClient = new LoginRestClient(hs);
    final Map<String, Object> params = new HashMap<>();
    CountDownLatch lock = new CountDownLatch(1);
    // get the registration session id
    loginRestClient.loginWithUser(userName, password, new TestApiCallback<Credentials>(lock) {

        @Override
        public void onSuccess(Credentials credentials) {
            params.put("credentials", credentials);
            super.onSuccess(credentials);
        }
    });
    await(lock);
    final Credentials credentials = (Credentials) params.get("credentials");
    Assert.assertNotNull(credentials);
    hs.setCredentials(credentials);
    final IMXStore store = new MXFileStore(hs, false, context);
    MXDataHandler mxDataHandler = new MXDataHandler(store, credentials);
    mxDataHandler.setLazyLoadingEnabled(sessionTestParams.getWithLazyLoading());
    final MXSession mxSession = new MXSession.Builder(hs, mxDataHandler, context).withLegacyCryptoStore(sessionTestParams.getWithLegacyCryptoStore()).build();
    if (sessionTestParams.getWithCryptoEnabled()) {
        mxSession.enableCryptoWhenStarting();
    }
    if (sessionTestParams.getWithInitialSync()) {
        syncSession(mxSession, sessionTestParams.getWithCryptoEnabled());
    }
    return mxSession;
}
Also used : HashMap(java.util.HashMap) IMXStore(org.matrix.androidsdk.data.store.IMXStore) MXFileStore(org.matrix.androidsdk.data.store.MXFileStore) CountDownLatch(java.util.concurrent.CountDownLatch) HomeServerConnectionConfig(org.matrix.androidsdk.HomeServerConnectionConfig) MXSession(org.matrix.androidsdk.MXSession) MXDataHandler(org.matrix.androidsdk.MXDataHandler) LoginRestClient(org.matrix.androidsdk.rest.client.LoginRestClient) Credentials(org.matrix.androidsdk.rest.model.login.Credentials)

Example 25 with MXSession

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

the class CryptoRestTest method test03_testClaimOneTimeKeysForUsersDevices.

@Test
public void test03_testClaimOneTimeKeysForUsersDevices() throws Exception {
    Context context = InstrumentationRegistry.getContext();
    final SessionTestParams testParams = new SessionTestParams(true);
    final MXSession bobSession = mTestHelper.createAccount(TestConstants.USER_BOB, testParams);
    final MXSession aliceSession = mTestHelper.createAccount(TestConstants.USER_ALICE, testParams);
    final Map<String, Object> results = new HashMap<>();
    final Map<String, Object> otks = new HashMap<>();
    {
        Map<String, Object> map = new HashMap<>();
        map.put("key", "ueuHES/Q0P1MZ4J3IUpC8iQTkgQNX66ZpxVLUaTDuB8");
        Map<String, String> signaturesubMap = new HashMap<>();
        signaturesubMap.put("ed25519:deviceId1", "signature1");
        Map<String, Object> signatureMap = new HashMap<>();
        signatureMap.put("@user1", signaturesubMap);
        map.put("signatures", signatureMap);
        otks.put("curve25519:AAAABQ", map);
    }
    {
        Map<String, Object> map = new HashMap<>();
        map.put("key", "PmyaaB68Any+za9CuZXzFsQZW31s/TW6XbAB9akEpQs");
        Map<String, String> signaturesubMap = new HashMap<>();
        signaturesubMap.put("ed25519:deviceId2", "signature2");
        Map<String, Object> signatureMap = new HashMap<>();
        signatureMap.put("@user2", signaturesubMap);
        map.put("signatures", signatureMap);
        otks.put("curve25519:AAAABA", map);
    }
    CountDownLatch lock1 = new CountDownLatch(1);
    bobSession.getCryptoRestClientForTest().uploadKeys(null, otks, "dev1", new TestApiCallback<KeysUploadResponse>(lock1) {

        @Override
        public void onSuccess(KeysUploadResponse keysUploadResponse) {
            results.put("keysUploadResponse", keysUploadResponse);
            super.onSuccess(keysUploadResponse);
        }
    });
    mTestHelper.await(lock1);
    KeysUploadResponse bobKeysUploadResponse = (KeysUploadResponse) results.get("keysUploadResponse");
    Assert.assertNotNull(bobKeysUploadResponse);
    MXUsersDevicesMap<String> usersDevicesKeyTypesMap = new MXUsersDevicesMap<>();
    usersDevicesKeyTypesMap.setObject("curve25519", bobSession.getMyUserId(), "dev1");
    CountDownLatch lock2 = new CountDownLatch(1);
    aliceSession.getCryptoRestClientForTest().claimOneTimeKeysForUsersDevices(usersDevicesKeyTypesMap, new TestApiCallback<MXUsersDevicesMap<MXKey>>(lock2) {

        @Override
        public void onSuccess(MXUsersDevicesMap<MXKey> usersDevicesMap) {
            results.put("usersDevicesMap", usersDevicesMap);
            super.onSuccess(usersDevicesMap);
        }
    });
    mTestHelper.await(lock2);
    MXUsersDevicesMap<MXKey> oneTimeKeys = (MXUsersDevicesMap<MXKey>) results.get("usersDevicesMap");
    Assert.assertNotNull(oneTimeKeys);
    Assert.assertNotNull(oneTimeKeys.getMap());
    Assert.assertEquals(1, oneTimeKeys.getMap().size());
    MXKey bobOtk = oneTimeKeys.getObject("dev1", bobSession.getMyUserId());
    Assert.assertNotNull(bobOtk);
    Assert.assertEquals(MXKey.KEY_CURVE_25519_TYPE, bobOtk.type);
    Assert.assertEquals("AAAABA", bobOtk.keyId);
    Assert.assertEquals("curve25519:AAAABA", bobOtk.getKeyFullId());
    Assert.assertEquals("PmyaaB68Any+za9CuZXzFsQZW31s/TW6XbAB9akEpQs", bobOtk.value);
    Assert.assertNotNull(bobOtk.signatures);
    List<String> keys = new ArrayList<>(bobOtk.signatures.keySet());
    Assert.assertEquals(1, keys.size());
    bobSession.clear(context);
    aliceSession.clear(context);
}
Also used : Context(android.content.Context) KeysUploadResponse(org.matrix.androidsdk.crypto.model.crypto.KeysUploadResponse) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) MXUsersDevicesMap(org.matrix.androidsdk.crypto.data.MXUsersDevicesMap) MXSession(org.matrix.androidsdk.MXSession) MXKey(org.matrix.androidsdk.crypto.data.MXKey) SessionTestParams(org.matrix.androidsdk.common.SessionTestParams) HashMap(java.util.HashMap) MXUsersDevicesMap(org.matrix.androidsdk.crypto.data.MXUsersDevicesMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

MXSession (org.matrix.androidsdk.MXSession)39 CountDownLatch (java.util.concurrent.CountDownLatch)37 Context (android.content.Context)36 HashMap (java.util.HashMap)36 Test (org.junit.Test)32 JsonObject (com.google.gson.JsonObject)27 Room (org.matrix.androidsdk.data.Room)25 MXEventListener (org.matrix.androidsdk.listeners.MXEventListener)25 Event (org.matrix.androidsdk.rest.model.Event)23 CryptoTestData (org.matrix.androidsdk.common.CryptoTestData)22 RoomState (org.matrix.androidsdk.data.RoomState)20 MXStoreListener (org.matrix.androidsdk.data.store.MXStoreListener)16 ArrayList (java.util.ArrayList)14 MXDeviceInfo (org.matrix.androidsdk.crypto.data.MXDeviceInfo)9 EventTimeline (org.matrix.androidsdk.data.timeline.EventTimeline)9 Credentials (org.matrix.androidsdk.rest.model.login.Credentials)9 MXUsersDevicesMap (org.matrix.androidsdk.crypto.data.MXUsersDevicesMap)8 HomeServerConnectionConfig (org.matrix.androidsdk.HomeServerConnectionConfig)7 MXDataHandler (org.matrix.androidsdk.MXDataHandler)7 MXFileStore (org.matrix.androidsdk.data.store.MXFileStore)7