Pin code: add some tests
This commit is contained in:
parent
8436806571
commit
d6d553e8e0
14 changed files with 371 additions and 45 deletions
|
|
@ -18,8 +18,9 @@ package io.element.android.features.lockscreen.impl.pin
|
|||
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.features.lockscreen.impl.pin.storage.PinCodeStore
|
||||
import io.element.android.libraries.cryptography.api.CryptoService
|
||||
import io.element.android.libraries.cryptography.api.EncryptionDecryptionService
|
||||
import io.element.android.libraries.cryptography.api.EncryptionResult
|
||||
import io.element.android.libraries.cryptography.api.SecretKeyProvider
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import javax.inject.Inject
|
||||
|
||||
|
|
@ -27,7 +28,8 @@ private const val SECRET_KEY_ALIAS = "SECRET_KEY_ALIAS_PIN_CODE"
|
|||
|
||||
@ContributesBinding(AppScope::class)
|
||||
class DefaultPinCodeManager @Inject constructor(
|
||||
private val cryptoService: CryptoService,
|
||||
private val secretKeyProvider: SecretKeyProvider,
|
||||
private val encryptionDecryptionService: EncryptionDecryptionService,
|
||||
private val pinCodeStore: PinCodeStore,
|
||||
) : PinCodeManager {
|
||||
|
||||
|
|
@ -36,16 +38,16 @@ class DefaultPinCodeManager @Inject constructor(
|
|||
}
|
||||
|
||||
override suspend fun createPinCode(pinCode: String) {
|
||||
val secretKey = cryptoService.getOrCreateSecretKey(SECRET_KEY_ALIAS)
|
||||
val encryptedPinCode = cryptoService.encrypt(secretKey, pinCode.toByteArray()).toBase64()
|
||||
val secretKey = secretKeyProvider.getOrCreateKey(SECRET_KEY_ALIAS)
|
||||
val encryptedPinCode = encryptionDecryptionService.encrypt(secretKey, pinCode.toByteArray()).toBase64()
|
||||
pinCodeStore.saveEncryptedPinCode(encryptedPinCode)
|
||||
}
|
||||
|
||||
override suspend fun verifyPinCode(pinCode: String): Boolean {
|
||||
val encryptedPinCode = pinCodeStore.getEncryptedCode() ?: return false
|
||||
return try {
|
||||
val secretKey = cryptoService.getOrCreateSecretKey(SECRET_KEY_ALIAS)
|
||||
val decryptedPinCode = cryptoService.decrypt(secretKey, EncryptionResult.fromBase64(encryptedPinCode))
|
||||
val secretKey = secretKeyProvider.getOrCreateKey(SECRET_KEY_ALIAS)
|
||||
val decryptedPinCode = encryptionDecryptionService.decrypt(secretKey, EncryptionResult.fromBase64(encryptedPinCode))
|
||||
decryptedPinCode.contentEquals(pinCode.toByteArray())
|
||||
} catch (failure: Throwable) {
|
||||
false
|
||||
|
|
|
|||
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* Copyright (c) 2023 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 io.element.android.features.lockscreen.impl.pin
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import io.element.android.features.lockscreen.impl.pin.storage.InMemoryPinCodeStore
|
||||
import io.element.android.libraries.cryptography.impl.AESEncryptionDecryptionService
|
||||
import io.element.android.libraries.cryptography.test.SimpleSecretKeyProvider
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Test
|
||||
|
||||
class DefaultPinCodeManagerTest {
|
||||
|
||||
private val pinCodeStore = InMemoryPinCodeStore()
|
||||
private val secretKeyProvider = SimpleSecretKeyProvider()
|
||||
private val encryptionDecryptionService = AESEncryptionDecryptionService()
|
||||
private val pinCodeManager = DefaultPinCodeManager(secretKeyProvider, encryptionDecryptionService, pinCodeStore)
|
||||
|
||||
@Test
|
||||
fun given_a_pin_code_when_create_and_delete_assert_no_pin_code_left() = runTest {
|
||||
pinCodeManager.createPinCode("1234")
|
||||
assertThat(pinCodeManager.isPinCodeAvailable()).isTrue()
|
||||
pinCodeManager.deletePinCode()
|
||||
assertThat(pinCodeManager.isPinCodeAvailable()).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun given_a_pin_code_when_create_and_verify_with_the_same_pin_succeed() = runTest {
|
||||
val pinCode = "1234"
|
||||
pinCodeManager.createPinCode(pinCode)
|
||||
assertThat(pinCodeManager.verifyPinCode(pinCode)).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun given_a_pin_code_when_create_and_verify_with_a_different_pin_fails() = runTest {
|
||||
pinCodeManager.createPinCode("1234")
|
||||
assertThat(pinCodeManager.verifyPinCode("1235")).isFalse()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* Copyright (c) 2023 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 io.element.android.features.lockscreen.impl.pin.storage
|
||||
|
||||
class InMemoryPinCodeStore : PinCodeStore {
|
||||
|
||||
private var pinCode: String? = null
|
||||
private var remainingAttempts: Int = 3
|
||||
|
||||
override suspend fun getRemainingPinCodeAttemptsNumber(): Int {
|
||||
return remainingAttempts
|
||||
}
|
||||
|
||||
override suspend fun onWrongPin(): Int {
|
||||
return remainingAttempts--
|
||||
}
|
||||
|
||||
override suspend fun resetCounter() {
|
||||
remainingAttempts = 3
|
||||
}
|
||||
|
||||
override fun addListener(listener: PinCodeStore.Listener) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
override fun removeListener(listener: PinCodeStore.Listener) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
override suspend fun getEncryptedCode(): String? {
|
||||
return pinCode
|
||||
}
|
||||
|
||||
override suspend fun saveEncryptedPinCode(pinCode: String) {
|
||||
this.pinCode = pinCode
|
||||
}
|
||||
|
||||
override suspend fun deleteEncryptedPinCode() {
|
||||
pinCode = null
|
||||
}
|
||||
|
||||
override suspend fun hasPinCode(): Boolean {
|
||||
return pinCode != null
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue