Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
I overlooked that KeyException is a subclass of `GeneralSecurityExc…
Browse files Browse the repository at this point in the history
…eption`.

This causes us to continue with `KeyException` which then fails the message matching test.
As the underlying Tink code throws the GeneralSecurityException first, we check the type of the last exception.
  • Loading branch information
d4rken committed Oct 19, 2020
1 parent 47af324 commit 76d89dc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class EncryptionErrorResetTool @Inject constructor(
}
isResetWindowConsumed = true

val keyException = error.causes().singleOrNull { it is GeneralSecurityException }
if (keyException == null) {
val keyException = error.causes().lastOrNull()
if (keyException == null || keyException !is GeneralSecurityException) {
Timber.v("Error has no GeneralSecurityException as cause -> no reset.")
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import testhelpers.BaseIOTest
import testhelpers.preferences.MockSharedPreferences
import java.io.File
import java.security.GeneralSecurityException
import java.security.KeyException
import java.security.KeyStoreException

class EncryptionResetToolTest : BaseIOTest() {
Expand Down Expand Up @@ -169,6 +170,33 @@ class EncryptionResetToolTest : BaseIOTest() {
}
}

@Test
fun `nested exception may have the same base exception type, GeneralSecurityException`() {
// https://github.com/corona-warn-app/cwa-app-android/issues/642#issuecomment-712188157
createMockFiles()

createInstance().tryResetIfNecessary(
CwaSecurityException(
KeyException( // subclass of GeneralSecurityException
"Permantly failed to instantiate encrypted preferences",
SecurityException(
"Could not decrypt key. decryption failed",
GeneralSecurityException("decryption failed")
)
)
)
) shouldBe true

encryptedPrefsFile.exists() shouldBe false
encryptedDatabaseFile.exists() shouldBe false

mockPreferences.dataMapPeek.apply {
this["ea1851.reset.performedAt"] shouldNotBe null
this["ea1851.reset.windowconsumed"] shouldBe true
this["ea1851.reset.shownotice"] shouldBe true
}
}

@Test
fun `we want only a specific type of GeneralSecurityException`() {
createMockFiles()
Expand Down

0 comments on commit 76d89dc

Please sign in to comment.