Skip to content

Commit

Permalink
Crash after launch fixed (#3508)
Browse files Browse the repository at this point in the history
* Crash after launch fixed

* Bump version

* reset version code

* Backup disabled

* Added the unit tests

---------

Co-authored-by: Benjamin Mwalimu <[email protected]>
  • Loading branch information
Aleem92 and dubdabasoduba authored Sep 20, 2024
1 parent 8179c81 commit 041762f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class FhirEngineModule {
FhirEngineProvider.init(
FhirEngineConfiguration(
enableEncryptionIfSupported = !BuildConfig.DEBUG,
databaseErrorStrategy = DatabaseErrorStrategy.UNSPECIFIED,
databaseErrorStrategy = DatabaseErrorStrategy.RECREATE_AT_OPEN,
ServerConfiguration(
baseUrl = configService.provideAuthConfiguration().fhirServerBaseUrl,
authenticator = tokenAuthenticator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.smartregister.fhircore.engine.util

import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey
Expand All @@ -34,8 +35,20 @@ import org.smartregister.fhircore.engine.util.extension.encodeJson

@Singleton
class SecureSharedPreference @Inject constructor(@ApplicationContext val context: Context) {
private val secureSharedPreferences: SharedPreferences by lazy {
initEncryptedSharedPreferences()
}

@VisibleForTesting
fun initEncryptedSharedPreferences() =
runCatching { createEncryptedSharedPreferences() }
.getOrElse {
resetSharedPrefs()
createEncryptedSharedPreferences()
}

private val secureSharedPreferences =
@VisibleForTesting
fun createEncryptedSharedPreferences() =
EncryptedSharedPreferences.create(
context,
SECURE_STORAGE_FILE_NAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ internal class SecureSharedPreferenceTest : RobolectricTest() {
secureSharedPreference = spyk(SecureSharedPreference(application))
}

@Test
fun testInitEncryptedSharedPreferences() {
val result = secureSharedPreference.initEncryptedSharedPreferences()
Assert.assertNotNull(result)
}

@Test
fun testInitEncryptedSharedPreferencesHandlesException() {
every { secureSharedPreference.createEncryptedSharedPreferences() } throws
RuntimeException("Exception") andThenAnswer
{
callOriginal()
}

val result = secureSharedPreference.initEncryptedSharedPreferences()

Assert.assertNotNull(result)

verify(exactly = 2) { secureSharedPreference.createEncryptedSharedPreferences() }
verify(exactly = 1) { secureSharedPreference.resetSharedPrefs() }
}

@Test
fun testCreateEncryptedSharedPreferences() {
val result = secureSharedPreference.createEncryptedSharedPreferences()
Assert.assertNotNull(result)
}

@Test
fun testSaveCredentialsAndRetrieveSessionToken() {
secureSharedPreference.saveCredentials(username = "userName", password = "!@#$".toCharArray())
Expand Down
5 changes: 4 additions & 1 deletion android/quest/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
android:theme="@style/AppTheme.NoActionBar"
android:usesCleartextTraffic="false"
android:windowSoftInputMode="adjustResize"
android:allowBackup="false"
android:fullBackupContent="false"
android:dataExtractionRules="@xml/data_extraction_rules"
tools:ignore="UnusedAttribute,LockedOrientationActivity"
tools:replace="android:theme">
tools:replace="android:allowBackup,android:theme">
<profileable
android:shell="true"
tools:targetApi="29" />
Expand Down
17 changes: 17 additions & 0 deletions android/quest/src/main/res/xml/data_extraction_rules.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<data-extraction-rules>
<cloud-backup>
<exclude domain="root" path="." />
<exclude domain="file" path="." />
<exclude domain="database" path="." />
<exclude domain="sharedpref" path="." />
<exclude domain="external" path="." />
</cloud-backup>
<device-transfer>
<exclude domain="root" path="." />
<exclude domain="file" path="." />
<exclude domain="database" path="." />
<exclude domain="sharedpref" path="." />
<exclude domain="external" path="." />
</device-transfer>
</data-extraction-rules>

0 comments on commit 041762f

Please sign in to comment.