Ensure calling 'fail()' is not caught by the Exception handler

This commit is contained in:
Benoit Marty 2022-05-18 12:23:28 +02:00
parent f7303789a0
commit 5c9281bc7e
4 changed files with 83 additions and 39 deletions

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk
import junit.framework.TestCase.fail
/**
* Will fail the test if invoking [block] is not throwing a Throwable.
*
* @param message the failure message, if the block does not throw any Throwable
* @param failureBlock a Lambda to be able to do extra check on the thrown Throwable
* @param block the block to test
*/
internal suspend fun mustFail(
message: String = "must fail",
failureBlock: ((Throwable) -> Unit)? = null,
block: suspend () -> Unit,
) {
val isSuccess = try {
block.invoke()
true
} catch (throwable: Throwable) {
failureBlock?.invoke(throwable)
false
}
if (isSuccess) {
fail(message)
}
}

View File

@ -62,6 +62,7 @@ import org.matrix.android.sdk.common.RetryTestRule
import org.matrix.android.sdk.common.SessionTestParams
import org.matrix.android.sdk.common.TestConstants
import org.matrix.android.sdk.common.TestMatrixCallback
import org.matrix.android.sdk.mustFail
import java.util.concurrent.CountDownLatch
@RunWith(JUnit4::class)
@ -525,10 +526,8 @@ class E2eeSanityTests : InstrumentedTest {
// Confirm we can decrypt one but not the other
testHelper.runBlockingTest {
try {
mustFail(message = "Should not be able to decrypt event") {
newBobSession.cryptoService().decryptEvent(firstEventNewBobPov.root, "")
fail("Should not be able to decrypt event")
} catch (_: MXCryptoError) {
}
}

View File

@ -21,7 +21,6 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import junit.framework.TestCase.assertNotNull
import junit.framework.TestCase.assertTrue
import junit.framework.TestCase.fail
import org.amshove.kluent.internal.assertEquals
import org.junit.Assert
import org.junit.Assert.assertNull
@ -47,6 +46,7 @@ import org.matrix.android.sdk.common.CryptoTestHelper
import org.matrix.android.sdk.common.RetryTestRule
import org.matrix.android.sdk.common.SessionTestParams
import org.matrix.android.sdk.common.TestConstants
import org.matrix.android.sdk.mustFail
@RunWith(AndroidJUnit4::class)
@FixMethodOrder(MethodSorters.JVM)
@ -95,12 +95,10 @@ class KeyShareTests : InstrumentedTest {
assertNotNull(receivedEvent)
assert(receivedEvent!!.isEncrypted())
try {
commonTestHelper.runBlockingTest {
commonTestHelper.runBlockingTest {
mustFail {
aliceSession2.cryptoService().decryptEvent(receivedEvent.root, "foo")
}
fail("should fail")
} catch (failure: Throwable) {
}
val outgoingRequestsBefore = aliceSession2.cryptoService().getOutgoingRoomKeyRequests()
@ -168,12 +166,10 @@ class KeyShareTests : InstrumentedTest {
}
}
try {
commonTestHelper.runBlockingTest {
commonTestHelper.runBlockingTest {
mustFail {
aliceSession2.cryptoService().decryptEvent(receivedEvent.root, "foo")
}
fail("should fail")
} catch (failure: Throwable) {
}
// Mark the device as trusted

View File

@ -42,6 +42,7 @@ import org.matrix.android.sdk.common.MockOkHttpInterceptor
import org.matrix.android.sdk.common.RetryTestRule
import org.matrix.android.sdk.common.SessionTestParams
import org.matrix.android.sdk.common.TestConstants
import org.matrix.android.sdk.mustFail
@RunWith(AndroidJUnit4::class)
@FixMethodOrder(MethodSorters.JVM)
@ -96,17 +97,19 @@ class WithHeldTests : InstrumentedTest {
// =============================
// Bob should not be able to decrypt because the keys is withheld
try {
// .. might need to wait a bit for stability?
testHelper.runBlockingTest {
// .. might need to wait a bit for stability?
testHelper.runBlockingTest {
mustFail(
message = "This session should not be able to decrypt",
failureBlock = { failure ->
val type = (failure as MXCryptoError.Base).errorType
val technicalMessage = failure.technicalMessage
Assert.assertEquals("Error should be withheld", MXCryptoError.ErrorType.KEYS_WITHHELD, type)
Assert.assertEquals("Cause should be unverified", WithHeldCode.UNVERIFIED.value, technicalMessage)
}
) {
bobUnverifiedSession.cryptoService().decryptEvent(eventBobPOV.root, "")
}
Assert.fail("This session should not be able to decrypt")
} catch (failure: Throwable) {
val type = (failure as MXCryptoError.Base).errorType
val technicalMessage = failure.technicalMessage
Assert.assertEquals("Error should be withheld", MXCryptoError.ErrorType.KEYS_WITHHELD, type)
Assert.assertEquals("Cause should be unverified", WithHeldCode.UNVERIFIED.value, technicalMessage)
}
// Let's see if the reply we got from bob first session is unverified
@ -137,17 +140,18 @@ class WithHeldTests : InstrumentedTest {
}
// Previous message should still be undecryptable (partially withheld session)
try {
// .. might need to wait a bit for stability?
testHelper.runBlockingTest {
// .. might need to wait a bit for stability?
testHelper.runBlockingTest {
mustFail(
message = "This session should not be able to decrypt",
failureBlock = { failure ->
val type = (failure as MXCryptoError.Base).errorType
val technicalMessage = failure.technicalMessage
Assert.assertEquals("Error should be withheld", MXCryptoError.ErrorType.KEYS_WITHHELD, type)
Assert.assertEquals("Cause should be unverified", WithHeldCode.UNVERIFIED.value, technicalMessage)
}) {
bobUnverifiedSession.cryptoService().decryptEvent(eventBobPOV.root, "")
}
Assert.fail("This session should not be able to decrypt")
} catch (failure: Throwable) {
val type = (failure as MXCryptoError.Base).errorType
val technicalMessage = failure.technicalMessage
Assert.assertEquals("Error should be withheld", MXCryptoError.ErrorType.KEYS_WITHHELD, type)
Assert.assertEquals("Cause should be unverified", WithHeldCode.UNVERIFIED.value, technicalMessage)
}
testHelper.signOutAndClose(aliceSession)
@ -190,17 +194,18 @@ class WithHeldTests : InstrumentedTest {
// Previous message should still be undecryptable (partially withheld session)
val eventBobPOV = bobSession.getRoom(testData.roomId)?.getTimelineEvent(eventId)
try {
// .. might need to wait a bit for stability?
testHelper.runBlockingTest {
// .. might need to wait a bit for stability?
testHelper.runBlockingTest {
mustFail(
message = "This session should not be able to decrypt",
failureBlock = { failure ->
val type = (failure as MXCryptoError.Base).errorType
val technicalMessage = failure.technicalMessage
Assert.assertEquals("Error should be withheld", MXCryptoError.ErrorType.KEYS_WITHHELD, type)
Assert.assertEquals("Cause should be unverified", WithHeldCode.NO_OLM.value, technicalMessage)
}) {
bobSession.cryptoService().decryptEvent(eventBobPOV!!.root, "")
}
Assert.fail("This session should not be able to decrypt")
} catch (failure: Throwable) {
val type = (failure as MXCryptoError.Base).errorType
val technicalMessage = failure.technicalMessage
Assert.assertEquals("Error should be withheld", MXCryptoError.ErrorType.KEYS_WITHHELD, type)
Assert.assertEquals("Cause should be unverified", WithHeldCode.NO_OLM.value, technicalMessage)
}
// Ensure that alice has marked the session to be shared with bob