Fix non passing tests

This commit is contained in:
Benoit Marty 2022-09-16 22:53:32 +02:00 committed by Benoit Marty
parent 7f5c712e88
commit 0559911f39
2 changed files with 12 additions and 15 deletions

View file

@ -18,8 +18,6 @@ package im.vector.app
import org.matrix.android.sdk.api.util.BuildVersionSdkIntProvider import org.matrix.android.sdk.api.util.BuildVersionSdkIntProvider
class TestBuildVersionSdkIntProvider : BuildVersionSdkIntProvider { class TestBuildVersionSdkIntProvider(var value: Int = 0) : BuildVersionSdkIntProvider {
var value: Int = 0
override fun get() = value override fun get() = value
} }

View file

@ -25,6 +25,7 @@ import android.security.keystore.KeyProperties
import android.util.Base64 import android.util.Base64
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import im.vector.app.TestBuildVersionSdkIntProvider
import im.vector.app.features.pin.PinCodeStore import im.vector.app.features.pin.PinCodeStore
import im.vector.app.features.pin.SharedPrefPinCodeStore import im.vector.app.features.pin.SharedPrefPinCodeStore
import im.vector.app.features.pin.lockscreen.crypto.LockScreenCryptoConstants.ANDROID_KEY_STORE import im.vector.app.features.pin.lockscreen.crypto.LockScreenCryptoConstants.ANDROID_KEY_STORE
@ -32,7 +33,6 @@ import im.vector.app.features.pin.lockscreen.crypto.LockScreenCryptoConstants.LE
import io.mockk.coEvery import io.mockk.coEvery
import io.mockk.coVerify import io.mockk.coVerify
import io.mockk.every import io.mockk.every
import io.mockk.mockk
import io.mockk.spyk import io.mockk.spyk
import io.mockk.verify import io.mockk.verify
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
@ -42,7 +42,6 @@ import org.amshove.kluent.shouldBeEqualTo
import org.junit.After import org.junit.After
import org.junit.Test import org.junit.Test
import org.matrix.android.sdk.api.securestorage.SecretStoringUtils import org.matrix.android.sdk.api.securestorage.SecretStoringUtils
import org.matrix.android.sdk.api.util.BuildVersionSdkIntProvider
import java.math.BigInteger import java.math.BigInteger
import java.security.KeyFactory import java.security.KeyFactory
import java.security.KeyPairGenerator import java.security.KeyPairGenerator
@ -66,9 +65,7 @@ class LegacyPinCodeMigratorTests {
SharedPrefPinCodeStore(PreferenceManager.getDefaultSharedPreferences(InstrumentationRegistry.getInstrumentation().context)) SharedPrefPinCodeStore(PreferenceManager.getDefaultSharedPreferences(InstrumentationRegistry.getInstrumentation().context))
) )
private val keyStore: KeyStore = spyk(KeyStore.getInstance(ANDROID_KEY_STORE)).also { it.load(null) } private val keyStore: KeyStore = spyk(KeyStore.getInstance(ANDROID_KEY_STORE)).also { it.load(null) }
private val buildVersionSdkIntProvider: BuildVersionSdkIntProvider = mockk { private val buildVersionSdkIntProvider = TestBuildVersionSdkIntProvider(Build.VERSION_CODES.M)
every { get() } returns Build.VERSION_CODES.M
}
private val secretStoringUtils: SecretStoringUtils = spyk( private val secretStoringUtils: SecretStoringUtils = spyk(
SecretStoringUtils(context, keyStore, buildVersionSdkIntProvider) SecretStoringUtils(context, keyStore, buildVersionSdkIntProvider)
) )
@ -126,6 +123,7 @@ class LegacyPinCodeMigratorTests {
@Test @Test
fun migratePinCodeM() = runTest { fun migratePinCodeM() = runTest {
val pinCode = "1234" val pinCode = "1234"
buildVersionSdkIntProvider.value = Build.VERSION_CODES.M
saveLegacyPinCode(pinCode) saveLegacyPinCode(pinCode)
legacyPinCodeMigrator.migrate() legacyPinCodeMigrator.migrate()
@ -144,7 +142,7 @@ class LegacyPinCodeMigratorTests {
@Test @Test
fun migratePinCodeL() = runTest { fun migratePinCodeL() = runTest {
val pinCode = "1234" val pinCode = "1234"
every { buildVersionSdkIntProvider.get() } returns Build.VERSION_CODES.LOLLIPOP buildVersionSdkIntProvider.value = Build.VERSION_CODES.LOLLIPOP
saveLegacyPinCode(pinCode) saveLegacyPinCode(pinCode)
legacyPinCodeMigrator.migrate() legacyPinCodeMigrator.migrate()
@ -163,7 +161,7 @@ class LegacyPinCodeMigratorTests {
private fun generateLegacyKey() { private fun generateLegacyKey() {
if (keyStore.containsAlias(LEGACY_PIN_CODE_KEY_ALIAS)) return if (keyStore.containsAlias(LEGACY_PIN_CODE_KEY_ALIAS)) return
if (buildVersionSdkIntProvider.get() >= Build.VERSION_CODES.M) { if (buildVersionSdkIntProvider.isAtLeast(Build.VERSION_CODES.M)) {
generateLegacyKeyM() generateLegacyKeyM()
} else { } else {
generateLegacyKeyL() generateLegacyKeyL()
@ -206,7 +204,7 @@ class LegacyPinCodeMigratorTests {
generateLegacyKey() generateLegacyKey()
val publicKey = keyStore.getCertificate(LEGACY_PIN_CODE_KEY_ALIAS).publicKey val publicKey = keyStore.getCertificate(LEGACY_PIN_CODE_KEY_ALIAS).publicKey
val cipher = getLegacyCipher() val cipher = getLegacyCipher()
if (buildVersionSdkIntProvider.get() >= Build.VERSION_CODES.M) { if (buildVersionSdkIntProvider.isAtLeast(Build.VERSION_CODES.M)) {
val unrestrictedKey = KeyFactory.getInstance(publicKey.algorithm).generatePublic(X509EncodedKeySpec(publicKey.encoded)) val unrestrictedKey = KeyFactory.getInstance(publicKey.algorithm).generatePublic(X509EncodedKeySpec(publicKey.encoded))
val spec = OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT) val spec = OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT)
cipher.init(Cipher.ENCRYPT_MODE, unrestrictedKey, spec) cipher.init(Cipher.ENCRYPT_MODE, unrestrictedKey, spec)
@ -219,14 +217,15 @@ class LegacyPinCodeMigratorTests {
} }
private fun getLegacyCipher(): Cipher { private fun getLegacyCipher(): Cipher {
return when (buildVersionSdkIntProvider.get()) { return if (buildVersionSdkIntProvider.isAtLeast(Build.VERSION_CODES.M)) {
Build.VERSION_CODES.LOLLIPOP, Build.VERSION_CODES.LOLLIPOP_MR1 -> getCipherL() getCipherM()
else -> getCipherM() } else {
getCipherL()
} }
} }
private fun getCipherL(): Cipher { private fun getCipherL(): Cipher {
val provider = if (buildVersionSdkIntProvider.get() < Build.VERSION_CODES.M) "AndroidOpenSSL" else "AndroidKeyStoreBCWorkaround" val provider = if (buildVersionSdkIntProvider.isAtLeast(Build.VERSION_CODES.M)) "AndroidKeyStoreBCWorkaround" else "AndroidOpenSSL"
val transformation = "RSA/ECB/PKCS1Padding" val transformation = "RSA/ECB/PKCS1Padding"
return Cipher.getInstance(transformation, provider) return Cipher.getInstance(transformation, provider)
} }