From 1b763fb4c896081ee256708fe1e598d00a110a9f Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 21 Feb 2020 18:52:36 +0100 Subject: [PATCH] Cleanup XSigningTest --- .../crypto/crosssigning/XSigningTest.kt | 103 +++++------------- 1 file changed, 30 insertions(+), 73 deletions(-) diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/crosssigning/XSigningTest.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/crosssigning/XSigningTest.kt index 5681d3e8ec..57027878f9 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/crosssigning/XSigningTest.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/crosssigning/XSigningTest.kt @@ -2,12 +2,10 @@ package im.vector.matrix.android.internal.crypto.crosssigning import androidx.test.ext.junit.runners.AndroidJUnit4 import im.vector.matrix.android.InstrumentedTest -import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.common.CommonTestHelper import im.vector.matrix.android.common.CryptoTestHelper import im.vector.matrix.android.common.SessionTestParams import im.vector.matrix.android.common.TestConstants -import im.vector.matrix.android.common.TestMatrixCallback import im.vector.matrix.android.internal.crypto.model.CryptoDeviceInfo import im.vector.matrix.android.internal.crypto.model.MXUsersDevicesMap import im.vector.matrix.android.internal.crypto.model.rest.UserPasswordAuth @@ -21,7 +19,6 @@ import org.junit.FixMethodOrder import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.MethodSorters -import java.util.concurrent.CountDownLatch @RunWith(AndroidJUnit4::class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @@ -34,14 +31,13 @@ class XSigningTest : InstrumentedTest { fun test_InitializeAndStoreKeys() { val aliceSession = mTestHelper.createAccount(TestConstants.USER_ALICE, SessionTestParams(true)) - val aliceLatch = CountDownLatch(1) - aliceSession.cryptoService().crossSigningService() - .initializeCrossSigning(UserPasswordAuth( - user = aliceSession.myUserId, - password = TestConstants.PASSWORD - ), TestMatrixCallback(aliceLatch)) - - mTestHelper.await(aliceLatch) + mTestHelper.doSync { + aliceSession.cryptoService().crossSigningService() + .initializeCrossSigning(UserPasswordAuth( + user = aliceSession.myUserId, + password = TestConstants.PASSWORD + ), it) + } val myCrossSigningKeys = aliceSession.cryptoService().crossSigningService().getMyCrossSigningKeys() val masterPubKey = myCrossSigningKeys?.masterKey() @@ -78,9 +74,7 @@ class XSigningTest : InstrumentedTest { mTestHelper.doSync { bobSession.cryptoService().crossSigningService().initializeCrossSigning(bobAuthParams, it) } // Check that alice can see bob keys - val downloadLatch = CountDownLatch(1) - aliceSession.cryptoService().downloadKeys(listOf(bobSession.myUserId), true, TestMatrixCallback(downloadLatch)) - mTestHelper.await(downloadLatch) + mTestHelper.doSync> { aliceSession.cryptoService().downloadKeys(listOf(bobSession.myUserId), true, it) } val bobKeysFromAlicePOV = aliceSession.cryptoService().crossSigningService().getUserCrossSigningKeys(bobSession.myUserId) assertNotNull("Alice can see bob Master key", bobKeysFromAlicePOV!!.masterKey()) @@ -112,88 +106,51 @@ class XSigningTest : InstrumentedTest { password = TestConstants.PASSWORD ) - val latch = CountDownLatch(2) - - aliceSession.cryptoService().crossSigningService().initializeCrossSigning(aliceAuthParams, TestMatrixCallback(latch)) - bobSession.cryptoService().crossSigningService().initializeCrossSigning(bobAuthParams, TestMatrixCallback(latch)) - - mTestHelper.await(latch) + mTestHelper.doSync { aliceSession.cryptoService().crossSigningService().initializeCrossSigning(aliceAuthParams, it) } + mTestHelper.doSync { bobSession.cryptoService().crossSigningService().initializeCrossSigning(bobAuthParams, it) } // Check that alice can see bob keys - val downloadLatch = CountDownLatch(1) val bobUserId = bobSession.myUserId - aliceSession.cryptoService().downloadKeys(listOf(bobUserId), true, TestMatrixCallback(downloadLatch)) - mTestHelper.await(downloadLatch) + mTestHelper.doSync> { aliceSession.cryptoService().downloadKeys(listOf(bobUserId), true, it) } val bobKeysFromAlicePOV = aliceSession.cryptoService().crossSigningService().getUserCrossSigningKeys(bobUserId) assertTrue("Bob keys from alice pov should not be trusted", bobKeysFromAlicePOV?.isTrusted() == false) - val trustLatch = CountDownLatch(1) - aliceSession.cryptoService().crossSigningService().trustUser(bobUserId, object : MatrixCallback { - override fun onSuccess(data: Unit) { - trustLatch.countDown() - } - - override fun onFailure(failure: Throwable) { - fail("Failed to trust bob") - } - }) - mTestHelper.await(trustLatch) + mTestHelper.doSync { aliceSession.cryptoService().crossSigningService().trustUser(bobUserId, it) } // Now bobs logs in on a new device and verifies it // We will want to test that in alice POV, this new device would be trusted by cross signing val bobSession2 = mTestHelper.logIntoAccount(bobUserId, SessionTestParams(true)) - val bobSecondDeviceId = bobSession2.sessionParams.credentials.deviceId + val bobSecondDeviceId = bobSession2.sessionParams.credentials.deviceId!! // Check that bob first session sees the new login - val bobKeysLatch = CountDownLatch(1) - bobSession.cryptoService().downloadKeys(listOf(bobUserId), true, object : MatrixCallback> { - override fun onFailure(failure: Throwable) { - fail("Failed to get device") - } + val data = mTestHelper.doSync> { + bobSession.cryptoService().downloadKeys(listOf(bobUserId), true, it) + } - override fun onSuccess(data: MXUsersDevicesMap) { - if (data.getUserDeviceIds(bobUserId)?.contains(bobSecondDeviceId!!) == false) { - fail("Bob should see the new device") - } - bobKeysLatch.countDown() - } - }) - mTestHelper.await(bobKeysLatch) + if (data.getUserDeviceIds(bobUserId)?.contains(bobSecondDeviceId) == false) { + fail("Bob should see the new device") + } val bobSecondDevicePOVFirstDevice = bobSession.cryptoService().getDeviceInfo(bobUserId, bobSecondDeviceId) assertNotNull("Bob Second device should be known and persisted from first", bobSecondDevicePOVFirstDevice) // Manually mark it as trusted from first session - val bobSignLatch = CountDownLatch(1) - bobSession.cryptoService().crossSigningService().signDevice(bobSecondDeviceId!!, object : MatrixCallback { - override fun onSuccess(data: Unit) { - bobSignLatch.countDown() - } - - override fun onFailure(failure: Throwable) { - fail("Failed to trust bob ${failure.localizedMessage}") - } - }) - mTestHelper.await(bobSignLatch) + mTestHelper.doSync { + bobSession.cryptoService().crossSigningService().signDevice(bobSecondDeviceId, it) + } // Now alice should cross trust bob's second device - val aliceKeysLatch = CountDownLatch(1) - aliceSession.cryptoService().downloadKeys(listOf(bobUserId), true, object : MatrixCallback> { - override fun onFailure(failure: Throwable) { - fail("Failed to get device") - } + val data2 = mTestHelper.doSync> { + aliceSession.cryptoService().downloadKeys(listOf(bobUserId), true, it) - override fun onSuccess(data: MXUsersDevicesMap) { - // check that the device is seen - if (data.getUserDeviceIds(bobUserId)?.contains(bobSecondDeviceId) == false) { - fail("Alice should see the new device") - } - aliceKeysLatch.countDown() - } - }) - mTestHelper.await(aliceKeysLatch) + } + + // check that the device is seen + if (data2.getUserDeviceIds(bobUserId)?.contains(bobSecondDeviceId) == false) { + fail("Alice should see the new device") + } val result = aliceSession.cryptoService().crossSigningService().checkDeviceTrust(bobUserId, bobSecondDeviceId, null) assertTrue("Bob second device should be trusted from alice POV", result.isCrossSignedVerified())