diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/account/AccountCreationTest.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/account/AccountCreationTest.kt index c44ac9c47b..679ea8f3fe 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/account/AccountCreationTest.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/account/AccountCreationTest.kt @@ -38,9 +38,7 @@ class AccountCreationTest : InstrumentedTest { fun createAccountTest() { val session = commonTestHelper.createAccount(TestConstants.USER_ALICE, SessionTestParams(withInitialSync = true)) - commonTestHelper.signout(session) - - session.close() + commonTestHelper.signOutAndClose(session) } @Test @@ -50,14 +48,14 @@ class AccountCreationTest : InstrumentedTest { // Log again to the same account val session2 = commonTestHelper.logIntoAccount(session.myUserId, SessionTestParams(withInitialSync = true)) - session.close() - session2.close() + commonTestHelper.signOutAndClose(session) + commonTestHelper.signOutAndClose(session2) } @Test fun simpleE2eTest() { val res = cryptoTestHelper.doE2ETestWithAliceInARoom() - res.close() + res.cleanUp(commonTestHelper) } } diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt index 2e18133071..44770f3378 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt @@ -283,11 +283,10 @@ class CommonTestHelper(context: Context) { /** * Clear all provided sessions */ - fun Iterable.close() = forEach { it.close() } + fun Iterable.signOutAndClose() = forEach { signOutAndClose(it) } - fun signout(session: Session) { - val lock = CountDownLatch(1) - session.signOut(true, TestMatrixCallback(lock)) - await(lock) + fun signOutAndClose(session: Session) { + doSync { session.signOut(true, it) } + session.close() } } diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CryptoTestData.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CryptoTestData.kt index 8ad9f1ec6f..7eea832160 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CryptoTestData.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CryptoTestData.kt @@ -23,9 +23,9 @@ data class CryptoTestData(val firstSession: Session, val secondSession: Session? = null, val thirdSession: Session? = null) { - fun close() { - firstSession.close() - secondSession?.close() - secondSession?.close() + fun cleanUp(testHelper: CommonTestHelper) { + testHelper.signOutAndClose(firstSession) + secondSession?.let { testHelper.signOutAndClose(it) } + thirdSession?.let { testHelper.signOutAndClose(it) } } } 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 069d32acaf..5681d3e8ec 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 @@ -55,7 +55,7 @@ class XSigningTest : InstrumentedTest { assertTrue("Signing Keys should be trusted", aliceSession.cryptoService().crossSigningService().checkUserTrust(aliceSession.myUserId).isVerified()) - mTestHelper.signout(aliceSession) + mTestHelper.signOutAndClose(aliceSession) } @Test @@ -74,12 +74,8 @@ 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) @@ -96,8 +92,8 @@ class XSigningTest : InstrumentedTest { assertFalse("Bob keys from alice pov should not be trusted", bobKeysFromAlicePOV.isTrusted()) - mTestHelper.signout(aliceSession) - mTestHelper.signout(bobSession) + mTestHelper.signOutAndClose(aliceSession) + mTestHelper.signOutAndClose(bobSession) } @Test @@ -202,8 +198,8 @@ class XSigningTest : InstrumentedTest { val result = aliceSession.cryptoService().crossSigningService().checkDeviceTrust(bobUserId, bobSecondDeviceId, null) assertTrue("Bob second device should be trusted from alice POV", result.isCrossSignedVerified()) - mTestHelper.signout(aliceSession) - mTestHelper.signout(bobSession) - mTestHelper.signout(bobSession2) + mTestHelper.signOutAndClose(aliceSession) + mTestHelper.signOutAndClose(bobSession) + mTestHelper.signOutAndClose(bobSession2) } } diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/keysbackup/KeysBackupTest.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/keysbackup/KeysBackupTest.kt index 236fc24345..b039522e0c 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/keysbackup/KeysBackupTest.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/keysbackup/KeysBackupTest.kt @@ -129,7 +129,7 @@ class KeysBackupTest : InstrumentedTest { assertNotNull(megolmBackupCreationInfo.recoveryKey) stateObserver.stopAndCheckStates(null) - bobSession.close() + mTestHelper.signOutAndClose(bobSession) } /** @@ -160,7 +160,7 @@ class KeysBackupTest : InstrumentedTest { assertTrue(keysBackup.isEnabled) stateObserver.stopAndCheckStates(null) - bobSession.close() + mTestHelper.signOutAndClose(bobSession) } /** @@ -200,7 +200,7 @@ class KeysBackupTest : InstrumentedTest { KeysBackupState.ReadyToBackUp ) ) - cryptoTestData.close() + cryptoTestData.cleanUp(mTestHelper) } /** @@ -239,7 +239,7 @@ class KeysBackupTest : InstrumentedTest { assertEquals("All keys must have been marked as backed up", nbOfKeys, backedUpKeys) stateObserver.stopAndCheckStates(null) - cryptoTestData.close() + cryptoTestData.cleanUp(mTestHelper) } /** @@ -282,7 +282,7 @@ class KeysBackupTest : InstrumentedTest { assertKeysEquals(session.exportKeys(), sessionData) stateObserver.stopAndCheckStates(null) - cryptoTestData.close() + cryptoTestData.cleanUp(mTestHelper) } /** @@ -308,7 +308,7 @@ class KeysBackupTest : InstrumentedTest { checkRestoreSuccess(testData, importRoomKeysResult.totalNumberOfKeys, importRoomKeysResult.successfullyNumberOfImportedKeys) - testData.cryptoTestData.close() + testData.cleanUp(mTestHelper) } /** @@ -359,7 +359,7 @@ class KeysBackupTest : InstrumentedTest { // Request is either sent or unsent assertTrue(unsentRequestAfterRestoration == null && sentRequestAfterRestoration == null) - testData.cryptoTestData.close() + testData.cleanUp(mTestHelper) } /** @@ -418,7 +418,7 @@ class KeysBackupTest : InstrumentedTest { assertEquals(2, keysBackupVersionTrust.signatures.size) stateObserver.stopAndCheckStates(null) - testData.cryptoTestData.close() + testData.cleanUp(mTestHelper) } /** @@ -477,7 +477,7 @@ class KeysBackupTest : InstrumentedTest { assertEquals(2, keysBackupVersionTrust.signatures.size) stateObserver.stopAndCheckStates(null) - testData.cryptoTestData.close() + testData.cleanUp(mTestHelper) } /** @@ -516,7 +516,7 @@ class KeysBackupTest : InstrumentedTest { assertEquals(KeysBackupState.NotTrusted, testData.aliceSession2.cryptoService().keysBackupService().state) stateObserver.stopAndCheckStates(null) - testData.cryptoTestData.close() + testData.cleanUp(mTestHelper) } /** @@ -577,7 +577,7 @@ class KeysBackupTest : InstrumentedTest { assertEquals(2, keysBackupVersionTrust.signatures.size) stateObserver.stopAndCheckStates(null) - testData.cryptoTestData.close() + testData.cleanUp(mTestHelper) } /** @@ -619,7 +619,7 @@ class KeysBackupTest : InstrumentedTest { assertEquals(KeysBackupState.NotTrusted, testData.aliceSession2.cryptoService().keysBackupService().state) stateObserver.stopAndCheckStates(null) - testData.cryptoTestData.close() + testData.cleanUp(mTestHelper) } /** @@ -652,7 +652,7 @@ class KeysBackupTest : InstrumentedTest { // onSuccess may not have been called assertNull(importRoomKeysResult) - testData.cryptoTestData.close() + testData.cleanUp(mTestHelper) } /** @@ -707,7 +707,7 @@ class KeysBackupTest : InstrumentedTest { checkRestoreSuccess(testData, importRoomKeysResult.totalNumberOfKeys, importRoomKeysResult.successfullyNumberOfImportedKeys) - testData.cryptoTestData.close() + testData.cleanUp(mTestHelper) } /** @@ -743,7 +743,7 @@ class KeysBackupTest : InstrumentedTest { // onSuccess may not have been called assertNull(importRoomKeysResult) - testData.cryptoTestData.close() + testData.cleanUp(mTestHelper) } /** @@ -771,7 +771,7 @@ class KeysBackupTest : InstrumentedTest { checkRestoreSuccess(testData, importRoomKeysResult.totalNumberOfKeys, importRoomKeysResult.successfullyNumberOfImportedKeys) - testData.cryptoTestData.close() + testData.cleanUp(mTestHelper) } /** @@ -804,7 +804,7 @@ class KeysBackupTest : InstrumentedTest { // onSuccess may not have been called assertNull(importRoomKeysResult) - testData.cryptoTestData.close() + testData.cleanUp(mTestHelper) } /** @@ -844,7 +844,7 @@ class KeysBackupTest : InstrumentedTest { assertEquals(signature.device!!.deviceId, cryptoTestData.firstSession.sessionParams.credentials.deviceId) stateObserver.stopAndCheckStates(null) - cryptoTestData.close() + cryptoTestData.cleanUp(mTestHelper) } /** @@ -873,7 +873,7 @@ class KeysBackupTest : InstrumentedTest { // - Log Alice on a new device val aliceSession2 = mTestHelper.logIntoAccount(cryptoTestData.firstSession.myUserId, defaultSessionParamsWithInitialSync) - cryptoTestData.close() + cryptoTestData.cleanUp(mTestHelper) val keysBackup2 = aliceSession2.cryptoService().keysBackupService() @@ -903,7 +903,7 @@ class KeysBackupTest : InstrumentedTest { stateObserver.stopAndCheckStates(null) stateObserver2.stopAndCheckStates(null) - aliceSession2.close() + mTestHelper.signOutAndClose(aliceSession2) } /** @@ -970,7 +970,7 @@ class KeysBackupTest : InstrumentedTest { assertFalse(keysBackup.isEnabled) stateObserver.stopAndCheckStates(null) - cryptoTestData.close() + cryptoTestData.cleanUp(mTestHelper) } /** @@ -1073,8 +1073,8 @@ class KeysBackupTest : InstrumentedTest { stateObserver.stopAndCheckStates(null) stateObserver2.stopAndCheckStates(null) - aliceSession2.close() - cryptoTestData.close() + mTestHelper.signOutAndClose(aliceSession2) + cryptoTestData.cleanUp(mTestHelper) } /** @@ -1103,7 +1103,7 @@ class KeysBackupTest : InstrumentedTest { assertFalse(keysBackup.isEnabled) stateObserver.stopAndCheckStates(null) - cryptoTestData.close() + cryptoTestData.cleanUp(mTestHelper) } /* ========================================================================================== @@ -1186,7 +1186,12 @@ class KeysBackupTest : InstrumentedTest { private data class KeysBackupScenarioData(val cryptoTestData: CryptoTestData, val aliceKeys: List, val prepareKeysBackupDataResult: PrepareKeysBackupDataResult, - val aliceSession2: Session) + val aliceSession2: Session) { + fun cleanUp(testHelper: CommonTestHelper) { + cryptoTestData.cleanUp(testHelper) + testHelper.signOutAndClose(aliceSession2) + } + } /** * Common initial condition diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/ssss/QuadSTests.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/ssss/QuadSTests.kt index 7802096338..8dd2109df3 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/ssss/QuadSTests.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/ssss/QuadSTests.kt @@ -128,7 +128,7 @@ class QuadSTests : InstrumentedTest { assertNotNull(defaultKeyAccountData?.content) assertEquals("Unexpected default key ${defaultKeyAccountData?.content}", TEST_KEY_ID, defaultKeyAccountData?.content?.get("key")) - mTestHelper.signout(aliceSession) + mTestHelper.signOutAndClose(aliceSession) } @Test @@ -172,7 +172,7 @@ class QuadSTests : InstrumentedTest { } assertEquals("Secret mismatch", clearSecret, decryptedSecret) - mTestHelper.signout(aliceSession) + mTestHelper.signOutAndClose(aliceSession) } @Test @@ -192,7 +192,7 @@ class QuadSTests : InstrumentedTest { quadS.setDefaultKey(TEST_KEY_ID, it) } - mTestHelper.signout(aliceSession) + mTestHelper.signOutAndClose(aliceSession) } @Test @@ -239,7 +239,7 @@ class QuadSTests : InstrumentedTest { ) } - mTestHelper.signout(aliceSession) + mTestHelper.signOutAndClose(aliceSession) } @Test @@ -298,7 +298,7 @@ class QuadSTests : InstrumentedTest { ) } - mTestHelper.signout(aliceSession) + mTestHelper.signOutAndClose(aliceSession) } private fun assertAccountData(session: Session, type: String): UserAccountDataEvent { diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/verification/SASTest.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/verification/SASTest.kt index 7fad87b82d..85fc7e1b36 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/verification/SASTest.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/verification/SASTest.kt @@ -132,7 +132,7 @@ class SASTest : InstrumentedTest { assertNull(bobVerificationService.getExistingTransaction(aliceSession.myUserId, txID)) assertNull(aliceVerificationService.getExistingTransaction(bobSession.myUserId, txID)) - cryptoTestData.close() + cryptoTestData.cleanUp(mTestHelper) } @Test @@ -189,7 +189,7 @@ class SASTest : InstrumentedTest { assertEquals("Request should be cancelled with m.unknown_method", CancelCode.UnknownMethod, cancelReason) - cryptoTestData.close() + cryptoTestData.cleanUp(mTestHelper) } @Test @@ -227,7 +227,7 @@ class SASTest : InstrumentedTest { val cancelReq = canceledToDeviceEvent!!.content.toModel()!! assertEquals("Request should be cancelled with m.unknown_method", CancelCode.UnknownMethod.value, cancelReq.code) - cryptoTestData.close() + cryptoTestData.cleanUp(mTestHelper) } @Test @@ -265,7 +265,7 @@ class SASTest : InstrumentedTest { val cancelReq = canceledToDeviceEvent!!.content.toModel()!! assertEquals("Request should be cancelled with m.unknown_method", CancelCode.UnknownMethod.value, cancelReq.code) - cryptoTestData.close() + cryptoTestData.cleanUp(mTestHelper) } private fun fakeBobStart(bobSession: Session, @@ -334,7 +334,7 @@ class SASTest : InstrumentedTest { mTestHelper.await(aliceCreatedLatch) mTestHelper.await(aliceCancelledLatch) - cryptoTestData.close() + cryptoTestData.cleanUp(mTestHelper) } /** @@ -393,7 +393,7 @@ class SASTest : InstrumentedTest { assertTrue("all agreed Short Code should be known by alice", startReq!!.shortAuthenticationStrings!!.contains(it)) } - cryptoTestData.close() + cryptoTestData.cleanUp(mTestHelper) } @Test @@ -449,7 +449,7 @@ class SASTest : InstrumentedTest { assertEquals("Should have same SAS", aliceTx.getShortCodeRepresentation(SasMode.DECIMAL), bobTx.getShortCodeRepresentation(SasMode.DECIMAL)) - cryptoTestData.close() + cryptoTestData.cleanUp(mTestHelper) } @Test @@ -514,6 +514,6 @@ class SASTest : InstrumentedTest { assertTrue("alice device should be verified from bob point of view", aliceDeviceInfoFromBobPOV!!.isVerified) assertTrue("bob device should be verified from alice point of view", bobDeviceInfoFromAlicePOV!!.isVerified) - cryptoTestData.close() + cryptoTestData.cleanUp(mTestHelper) } } diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/verification/qrcode/VerificationTest.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/verification/qrcode/VerificationTest.kt index d62aef077e..ae8a1ad718 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/verification/qrcode/VerificationTest.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/internal/crypto/verification/qrcode/VerificationTest.kt @@ -227,6 +227,6 @@ class VerificationTest : InstrumentedTest { pr.otherCanScanQrCode() shouldBe expectedResultForBob.otherCanScanQrCode } - cryptoTestData.close() + cryptoTestData.cleanUp(mTestHelper) } }