mirror of
https://github.com/yogeshpaliyal/KeyPass.git
synced 2026-01-04 01:29:39 -06:00
add more tests (#667)
* add more tests * minor improvements * spotless fix
This commit is contained in:
committed by
GitHub
parent
1a3d346087
commit
ea9234e270
@@ -1,12 +1,14 @@
|
||||
package com.yogeshpaliyal.keypass
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import com.yogeshpaliyal.common.utils.dataStore
|
||||
import com.yogeshpaliyal.common.data.DEFAULT_PASSWORD_LENGTH
|
||||
import com.yogeshpaliyal.common.data.UserSettings
|
||||
import com.yogeshpaliyal.common.utils.getUserSettings
|
||||
import com.yogeshpaliyal.common.utils.setDefaultPasswordLength
|
||||
import com.yogeshpaliyal.common.utils.setKeyPassPassword
|
||||
import com.yogeshpaliyal.common.utils.setUserSettings
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
@@ -27,7 +29,7 @@ class SharedPreferenceUtilsTest {
|
||||
@Test
|
||||
fun getKeyPassPasswordLength_test() = runBlocking {
|
||||
val result = context.getUserSettings().defaultPasswordLength
|
||||
assertEquals(null, result)
|
||||
assertEquals(DEFAULT_PASSWORD_LENGTH, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -38,12 +40,30 @@ class SharedPreferenceUtilsTest {
|
||||
assertEquals(expectedLength, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getKeyPassPassword() = runBlocking {
|
||||
val result = context.getUserSettings().keyPassPassword
|
||||
assertEquals(null, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun setKeyPassPassword() = runBlocking {
|
||||
val password = "hello"
|
||||
context.setKeyPassPassword(password)
|
||||
// val readAsync = async { context.getUserSettings().keyPassPassword }
|
||||
// val writeAsync = async { context.setKeyPassPassword(password) }
|
||||
//
|
||||
// readAsync.await()
|
||||
// writeAsync.await()
|
||||
|
||||
val result = context.getUserSettings().keyPassPassword
|
||||
assertEquals(password, result)
|
||||
}
|
||||
|
||||
@After
|
||||
fun clear() {
|
||||
runBlocking {
|
||||
context.dataStore.edit { preferences ->
|
||||
preferences.clear()
|
||||
}
|
||||
context.setUserSettings(UserSettings())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ abstract class CommonMyApplication : Application(), Configuration.Provider {
|
||||
val previewExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
|
||||
Thread.setDefaultUncaughtExceptionHandler { thread, throwable ->
|
||||
val intent = getCrashActivityIntent(throwable)
|
||||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
startActivity(intent)
|
||||
previewExceptionHandler?.uncaughtException(thread, throwable)
|
||||
}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
package com.yogeshpaliyal.common.utils
|
||||
|
||||
import android.os.Build
|
||||
import android.security.keystore.KeyGenParameterSpec
|
||||
import android.security.keystore.KeyProperties
|
||||
import androidx.annotation.RequiresApi
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.security.KeyStore
|
||||
@@ -12,22 +10,11 @@ import javax.crypto.KeyGenerator
|
||||
import javax.crypto.SecretKey
|
||||
import javax.crypto.spec.IvParameterSpec
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.M)
|
||||
class CryptoManager {
|
||||
private val keyStore = KeyStore.getInstance("AndroidKeyStore").apply {
|
||||
load(null)
|
||||
}
|
||||
|
||||
private val encryptCipher get() = Cipher.getInstance(TRANSFORMATION).apply {
|
||||
init(Cipher.ENCRYPT_MODE, getKey())
|
||||
}
|
||||
|
||||
private fun getDecryptCipherForIv(iv: ByteArray): Cipher {
|
||||
return Cipher.getInstance(TRANSFORMATION).apply {
|
||||
init(Cipher.DECRYPT_MODE, getKey(), IvParameterSpec(iv))
|
||||
}
|
||||
}
|
||||
|
||||
private fun getKey(): SecretKey {
|
||||
val existingKey = keyStore.getEntry("secret", null) as? KeyStore.SecretKeyEntry
|
||||
return existingKey?.secretKey ?: createKey()
|
||||
|
||||
Reference in New Issue
Block a user