Skip to content

Commit

Permalink
test(config): add missing verifies to test
Browse files Browse the repository at this point in the history
SDK-217

Co-authored-by: davidSchuppa <[email protected]>
Co-authored-by: matusekma <[email protected]>
  • Loading branch information
3 people committed Feb 6, 2025
1 parent 0f7a8ef commit be35da8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,28 @@ class SharedPreferenceCryptoTest {

val result = sharedPreferenceCrypto.decrypt(encryptedBase64, mockSecretKey)

result shouldBe encryptedBase64
result shouldBe null
}

@Test
fun testDecrypt_IllegalArgumentException() {
val IVValue = "Base64EncryptedBase64IV123"
val decryptedBytes = encryptedBase64.toByteArray()
every {
mockCipher.init(any(), mockSecretKey, any<GCMParameterSpec>())
} just Runs
every {
mockCipher.doFinal(any())
} returns decryptedBytes
every {
Base64.decode(
IVValue,
Base64.DEFAULT
)
} throws IllegalArgumentException("bad base-64")

val result = sharedPreferenceCrypto.decrypt(encryptedBase64, mockSecretKey)

result shouldBe null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class EmarsysEncryptedSharedPreferencesV3Test {
@Before
fun setup() {
mockContext = mockk()
mockSharedPreferenceCrypto = mockk()
mockSharedPreferenceCrypto = mockk(relaxed = true)
mockRealPreferences = mockk(relaxed = true)
mockSecretKey = mockk()

Expand Down Expand Up @@ -86,6 +86,21 @@ class EmarsysEncryptedSharedPreferencesV3Test {
result shouldBe "decryptedValue"
}

@Test
fun testGetString_shouldReturnDefaultValue_whenNull() {
every { mockRealPreferences.getString("testKey", null) } returns "encryptedValue"
every {
mockSharedPreferenceCrypto.decrypt(
"encryptedValue",
mockSecretKey
)
} returns null

val result = emarsysEncryptedSharedPreferencesV3.getString("testKey", "defaultValue")

result shouldBe "defaultValue"
}

@Test
fun testGetStringSet() {
val encryptedSet = setOf("encryptedValue1", "encryptedValue2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SharedPreferenceCrypto {
}
}

fun decrypt(value: String, secretKey: SecretKey): String {
fun decrypt(value: String, secretKey: SecretKey): String? {
return try {
val ivBytes = Base64.decode(value.substring(0, 16), Base64.DEFAULT)
val encryptedBytes = Base64.decode(value.substring(16), Base64.DEFAULT)
Expand All @@ -63,10 +63,10 @@ class SharedPreferenceCrypto {
String(decrypted)
} catch (e: GeneralSecurityException) {
e.printStackTrace()
value
null
} catch (e: IllegalArgumentException) {
e.printStackTrace()
value
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class EmarsysEncryptedSharedPreferencesV3(

override fun getStringSet(key: String?, defValues: MutableSet<String>?): MutableSet<String>? {
val encryptedValue = realPreferences.getStringSet(key, null)
return encryptedValue?.map { decryptString(it) }?.toMutableSet() ?: defValues
return encryptedValue?.mapNotNull { decryptString(it) }?.toMutableSet() ?: defValues
}

override fun getInt(key: String?, defValue: Int): Int {
Expand Down Expand Up @@ -79,7 +79,7 @@ class EmarsysEncryptedSharedPreferencesV3(
realPreferences.unregisterOnSharedPreferenceChangeListener(listener)
}

private fun decryptString(value: String): String {
private fun decryptString(value: String): String? {
return sharedPreferenceCrypto.decrypt(value, secretKey)
}

Expand Down

0 comments on commit be35da8

Please sign in to comment.