use of org.matrix.androidsdk.data.store.MXFileStore in project matrix-android-sdk by matrix-org.
the class CryptoTest method test11_testAliceAndBobInACryptedRoomBackPaginationFromMemoryStore.
@Test
public void test11_testAliceAndBobInACryptedRoomBackPaginationFromMemoryStore() throws Exception {
Log.e(LOG_TAG, "test11_testAliceAndBobInACryptedRoomBackPaginationFromMemoryStore");
Context context = InstrumentationRegistry.getContext();
final HashMap<String, Object> results = new HashMap();
doE2ETestWithAliceAndBobInARoomWithCryptedMessages(true);
Credentials bobCredentials = mBobSession.getCredentials();
mBobSession.clear(context);
Uri uri = Uri.parse(CryptoTestHelper.TESTS_HOME_SERVER_URL);
HomeServerConnectionConfig hs = new HomeServerConnectionConfig(uri);
hs.setCredentials(bobCredentials);
IMXStore store = new MXFileStore(hs, context);
final CountDownLatch lock1 = new CountDownLatch(2);
MXSession bobSession2 = new MXSession(hs, new MXDataHandler(store, bobCredentials), context);
MXEventListener eventListener = new MXEventListener() {
@Override
public void onInitialSyncComplete(String toToken) {
results.put("onInitialSyncComplete", "onInitialSyncComplete");
lock1.countDown();
}
@Override
public void onCryptoSyncComplete() {
results.put("onCryptoSyncComplete", "onCryptoSyncComplete");
lock1.countDown();
}
};
bobSession2.getDataHandler().addListener(eventListener);
bobSession2.getDataHandler().getStore().open();
bobSession2.startEventStream(null);
lock1.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("onInitialSyncComplete"));
assertTrue(results.containsKey("onCryptoSyncComplete"));
assertTrue(null != bobSession2.getCrypto());
Room roomFromBobPOV = bobSession2.getDataHandler().getRoom(mRoomId);
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();
}
}
};
roomFromBobPOV.getLiveTimeLine().addEventTimelineListener(eventTimelineListener);
roomFromBobPOV.getLiveTimeLine().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(receivedEvents.size() + " instead of 5", 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);
bobSession2.clear(context);
mAliceSession.clear(context);
}
use of org.matrix.androidsdk.data.store.MXFileStore in project matrix-android-sdk by matrix-org.
the class CryptoTest method test04_testEnsureOlmSessionsForUsers.
@Test
public void test04_testEnsureOlmSessionsForUsers() throws Exception {
Log.e(LOG_TAG, "test04_testEnsureOlmSessionsForUsers");
Context context = InstrumentationRegistry.getContext();
createAliceAccount();
final HashMap<String, Object> results = new HashMap<>();
mAliceSession.getCredentials().deviceId = "AliceDevice";
final CountDownLatch lock0 = new CountDownLatch(1);
mAliceSession.enableCrypto(true, new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
results.put("enableCryptoAlice", "enableCryptoAlice");
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("enableCryptoAlice"));
createBobAccount();
final CountDownLatch lock2 = new CountDownLatch(1);
mBobSession.enableCrypto(true, new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
results.put("enableCryptoBob", "enableCryptoAlice");
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("enableCryptoBob"));
final CountDownLatch lock3 = new CountDownLatch(1);
mBobSession.getCrypto().getDeviceList().downloadKeys(Arrays.asList(mBobSession.getMyUserId(), mAliceSession.getMyUserId()), false, new ApiCallback<MXUsersDevicesMap<MXDeviceInfo>>() {
@Override
public void onSuccess(MXUsersDevicesMap<MXDeviceInfo> map) {
results.put("downloadKeys", map);
lock3.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock3.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock3.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock3.countDown();
}
});
lock3.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("downloadKeys"));
final CountDownLatch lock4 = new CountDownLatch(1);
mBobSession.getCrypto().ensureOlmSessionsForUsers(Arrays.asList(mBobSession.getMyUserId(), mAliceSession.getMyUserId()), new ApiCallback<MXUsersDevicesMap<MXOlmSessionResult>>() {
@Override
public void onSuccess(MXUsersDevicesMap<MXOlmSessionResult> info) {
results.put("ensureOlmSessionsForUsers", info);
lock4.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock4.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock4.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock4.countDown();
}
});
lock4.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("ensureOlmSessionsForUsers"));
MXUsersDevicesMap<MXOlmSessionResult> result = (MXUsersDevicesMap<MXOlmSessionResult>) results.get("ensureOlmSessionsForUsers");
assertTrue(result.getUserIds().size() == 1);
MXOlmSessionResult sessionWithAliceDevice = result.getObject("AliceDevice", mAliceSession.getMyUserId());
assertTrue(null != sessionWithAliceDevice);
assertTrue(null != sessionWithAliceDevice.mSessionId);
assertTrue(TextUtils.equals(sessionWithAliceDevice.mDevice.deviceId, "AliceDevice"));
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 lock5 = new CountDownLatch(1);
MXStoreListener listener = new MXStoreListener() {
@Override
public void postProcess(String accountId) {
}
@Override
public void onStoreReady(String accountId) {
results.put("onStoreReady", "onStoreReady");
lock5.countDown();
}
@Override
public void onStoreCorrupted(String accountId, String description) {
lock5.countDown();
}
@Override
public void onStoreOOM(String accountId, String description) {
lock5.countDown();
}
};
bobSession2.getDataHandler().getStore().addMXStoreListener(listener);
bobSession2.getDataHandler().getStore().open();
bobSession2.getDataHandler().addListener(new MXEventListener() {
@Override
public void onStoreReady() {
lock5.countDown();
}
});
lock5.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("onStoreReady"));
final CountDownLatch lock5b = new CountDownLatch(2);
MXEventListener eventListener = new MXEventListener() {
@Override
public void onInitialSyncComplete(String toToken) {
results.put("onInitialSyncComplete", "onInitialSyncComplete");
lock5b.countDown();
}
@Override
public void onCryptoSyncComplete() {
results.put("onCryptoSyncComplete", "onCryptoSyncComplete");
lock5b.countDown();
}
};
bobSession2.getDataHandler().addListener(eventListener);
bobSession2.startEventStream(null);
lock5b.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("onInitialSyncComplete"));
assertTrue(results.containsKey("onCryptoSyncComplete"));
final CountDownLatch lock6 = new CountDownLatch(1);
bobSession2.getCrypto().ensureOlmSessionsForUsers(Arrays.asList(bobSession2.getMyUserId(), mAliceSession.getMyUserId()), new ApiCallback<MXUsersDevicesMap<MXOlmSessionResult>>() {
@Override
public void onSuccess(MXUsersDevicesMap<MXOlmSessionResult> info) {
results.put("ensureOlmSessionsForUsers2", info);
lock6.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock6.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock6.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock6.countDown();
}
});
lock6.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("ensureOlmSessionsForUsers2"));
MXUsersDevicesMap<MXOlmSessionResult> result2 = (MXUsersDevicesMap<MXOlmSessionResult>) results.get("ensureOlmSessionsForUsers2");
MXOlmSessionResult sessionWithAliceDevice2 = result2.getObject("AliceDevice", mAliceSession.getMyUserId());
assertTrue(null != sessionWithAliceDevice2);
assertTrue(null != sessionWithAliceDevice2.mSessionId);
assertTrue(TextUtils.equals(sessionWithAliceDevice2.mDevice.deviceId, "AliceDevice"));
mBobSession.clear(context);
mAliceSession.clear(context);
bobSession2.clear(context);
}
use of org.matrix.androidsdk.data.store.MXFileStore 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);
}
use of org.matrix.androidsdk.data.store.MXFileStore in project matrix-android-sdk by matrix-org.
the class CryptoTestHelper method logAccountAndSync.
/**
* Start an account login
* @param context the context
* @param userName the account username
* @param password the password
* @throws Exception an exception if the account cannot be synced
*/
public static MXSession logAccountAndSync(Context context, String userName, String password) throws Exception {
Uri uri = Uri.parse(TESTS_HOME_SERVER_URL);
HomeServerConnectionConfig hs = new HomeServerConnectionConfig(uri);
LoginRestClient loginRestClient = new LoginRestClient(hs);
final HashMap<String, Object> params = new HashMap<>();
mLock = new CountDownLatch(1);
// get the registration session id
loginRestClient.loginWithUser(userName, password, new ApiCallback<Credentials>() {
@Override
public void onSuccess(Credentials credentials) {
params.put("credentials", credentials);
mLock.countDown();
}
@Override
public void onNetworkError(Exception e) {
mLock.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
mLock.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
mLock.countDown();
}
});
mLock.await(10000, TimeUnit.MILLISECONDS);
Credentials credentials = (Credentials) params.get("credentials");
assertTrue(null != credentials);
hs.setCredentials(credentials);
IMXStore store = new MXFileStore(hs, context);
MXSession mxSession = new MXSession(hs, new MXDataHandler(store, credentials), context);
mxSession.enableCryptoWhenStarting();
mLock = new CountDownLatch(2);
mxSession.getDataHandler().addListener(new MXEventListener() {
@Override
public void onInitialSyncComplete(String toToken) {
params.put("isInit", true);
mLock.countDown();
}
@Override
public void onCryptoSyncComplete() {
params.put("onCryptoSyncComplete", true);
mLock.countDown();
}
});
mxSession.getDataHandler().getStore().open();
mxSession.startEventStream(null);
mLock.await(10000, TimeUnit.MILLISECONDS);
assertTrue(params.containsKey("isInit"));
assertTrue(params.containsKey("onCryptoSyncComplete"));
return mxSession;
}
use of org.matrix.androidsdk.data.store.MXFileStore in project matrix-android-sdk by matrix-org.
the class TestsHelper method createAccountAndSync.
/**
* Create an account and a dedicated session
* @param context the context
* @param userName the account username
* @param password the password
* @param startSession true to perform an initial sync
* @param callback the callback
* @throws Exception an exception if the account cannot be created
*/
public static void createAccountAndSync(Context context, String userName, String password, boolean startSession, ApiCallback<MXSession> callback) throws Exception {
RestClient.mUseMXExececutor = true;
Uri uri = Uri.parse(TESTS_HOME_SERVER_URL);
HomeServerConnectionConfig hs = new HomeServerConnectionConfig(uri);
LoginRestClient loginRestClient = new LoginRestClient(hs);
final HashMap<String, Object> params = new HashMap<>();
RegistrationParams registrationParams = new RegistrationParams();
mLock = new CountDownLatch(1);
// get the registration session id
loginRestClient.register(registrationParams, new ApiCallback<Credentials>() {
@Override
public void onSuccess(Credentials credentials) {
mLock.countDown();
}
@Override
public void onNetworkError(Exception e) {
mLock.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
// detect if a parameter is expected
RegistrationFlowResponse registrationFlowResponse = null;
// when a response is not completed the server returns an error message
if ((null != e.mStatus) && (e.mStatus == 401)) {
try {
registrationFlowResponse = JsonUtils.toRegistrationFlowResponse(e.mErrorBodyAsString);
} catch (Exception castExcept) {
}
}
// check if the server response can be casted
if (null != registrationFlowResponse) {
params.put("session", registrationFlowResponse.session);
}
mLock.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
mLock.countDown();
}
});
mLock.await(1000, TimeUnit.MILLISECONDS);
String session = (String) params.get("session");
if (null == session) {
callback.onUnexpectedError(null);
}
registrationParams.username = userName;
registrationParams.password = password;
HashMap<String, Object> authParams = new HashMap<>();
authParams.put("session", session);
authParams.put("type", LoginRestClient.LOGIN_FLOW_TYPE_DUMMY);
registrationParams.auth = authParams;
mLock = new CountDownLatch(1);
loginRestClient.register(registrationParams, new ApiCallback<Credentials>() {
@Override
public void onSuccess(Credentials credentials) {
params.put("credentials", credentials);
mLock.countDown();
}
@Override
public void onNetworkError(Exception e) {
mLock.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
mLock.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
mLock.countDown();
}
});
mLock.await(1000, TimeUnit.MILLISECONDS);
Credentials credentials = (Credentials) params.get("credentials");
if (null == credentials) {
callback.onMatrixError(null);
return;
}
hs.setCredentials(credentials);
IMXStore store = new MXFileStore(hs, context);
MXSession mxSession = new MXSession(hs, new MXDataHandler(store, credentials), context);
if (!startSession) {
callback.onSuccess(mxSession);
return;
}
mxSession.getDataHandler().getStore().open();
mxSession.startEventStream(null);
mLock = new CountDownLatch(1);
mxSession.getDataHandler().addListener(new MXEventListener() {
@Override
public void onInitialSyncComplete(String toToken) {
params.put("isInit", true);
mLock.countDown();
}
});
mLock.await(10000, TimeUnit.MILLISECONDS);
if (params.containsKey("isInit")) {
callback.onSuccess(mxSession);
} else {
callback.onMatrixError(null);
}
}
Aggregations