Skip to content

Commit

Permalink
[refactor] #4 encrypt unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sangwook123 committed May 1, 2024
1 parent 9c170ae commit a9572fa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion core/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
alias(libs.plugins.sopt.android.library)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.sopt.android.hilt)
alias(libs.plugins.sopt.android.test)
alias(libs.plugins.sopt.plugin.test)
}

android {
Expand Down
19 changes: 10 additions & 9 deletions core/common/src/main/java/org/sopt/common/security/SecurityUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ class SecurityUtil @Inject constructor() : SecurityInterface {
return getSecretKey(keyAlias)
}
private fun generateSecretKey(keyAlias: String): SecretKey {
return keyGenerator.apply {
init(
KeyGenParameterSpec
.Builder(keyAlias, PURPOSE_ENCRYPT or PURPOSE_DECRYPT)
.setBlockModes(BLOCK_MODE_GCM)
.setEncryptionPaddings(ENCRYPTION_PADDING_NONE)
.build()
)
}.generateKey()
val parameterSpec = KeyGenParameterSpec.Builder(
keyAlias,
PURPOSE_ENCRYPT or PURPOSE_DECRYPT
).run {
setBlockModes(BLOCK_MODE_GCM)
setEncryptionPaddings(ENCRYPTION_PADDING_NONE)
build()
}
keyGenerator.init(parameterSpec)
return keyGenerator.generateKey()
}

private fun getSecretKey(keyAlias: String) =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package com.sopt.common

import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.sopt.common.security.SecurityInterface
import org.sopt.common.security.SecurityUtil
import org.sopt.common.security.fake.FakeAndroidKeyStoreProvider

@RunWith(AndroidJUnit4::class)
@RunWith(RobolectricTestRunner::class)
class EncryptTest {
lateinit var securityUtil: SecurityUtil
lateinit var securityUtil: SecurityInterface
val testKey = "test-key"

@Before
fun setup() {
securityUtil = SecurityUtil()
FakeAndroidKeyStoreProvider.setup()
}

@Test
fun encryptTest() {
securityUtil = SecurityUtil()
val testMessage = "testMessage"
val (encryptedData, iv) = securityUtil.encryptData(testKey, testMessage)
val decryptedData = securityUtil.decryptData(testKey, encryptedData, iv)
Expand Down

0 comments on commit a9572fa

Please sign in to comment.