1- package com.threegap.bitnagil.security
1+ package com.threegap.bitnagil.security.crypto
22
3+ import com.threegap.bitnagil.security.keystore.KeyProvider
34import org.junit.Assert.assertEquals
45import org.junit.Assert.assertNotEquals
56import org.junit.Assert.assertThrows
@@ -8,7 +9,7 @@ import javax.crypto.BadPaddingException
89import javax.crypto.KeyGenerator
910import javax.crypto.SecretKey
1011
11- class CryptoTest {
12+ class SecureCryptoTest {
1213 private class FakeKeyProvider : KeyProvider {
1314 private val key: SecretKey =
1415 KeyGenerator
@@ -19,8 +20,8 @@ class CryptoTest {
1920 override fun getKey (): SecretKey = key
2021 }
2122
22- private val crypto =
23- Crypto (
23+ private val secureCrypto =
24+ SecureCrypto (
2425 keyProvider = FakeKeyProvider (),
2526 transformation = " AES/CBC/PKCS5Padding" ,
2627 )
@@ -31,8 +32,8 @@ class CryptoTest {
3132 val original = " 테스트 데이터" .toByteArray()
3233
3334 // when
34- val encrypted = crypto .encrypt(original)
35- val decrypted = crypto .decrypt(encrypted)
35+ val encrypted = secureCrypto .encrypt(original)
36+ val decrypted = secureCrypto .decrypt(encrypted)
3637
3738 // then
3839 assertEquals(String (original), String (decrypted))
@@ -44,8 +45,8 @@ class CryptoTest {
4445 val input = " 같은 입력" .toByteArray()
4546
4647 // when
47- val encrypted1 = crypto .encrypt(input)
48- val encrypted2 = crypto .encrypt(input)
48+ val encrypted1 = secureCrypto .encrypt(input)
49+ val encrypted2 = secureCrypto .encrypt(input)
4950
5051 // then
5152 assertNotEquals(encrypted1.toList(), encrypted2.toList())
@@ -58,27 +59,27 @@ class CryptoTest {
5859
5960 // when & then
6061 assertThrows(IllegalArgumentException ::class .java) {
61- crypto .decrypt(invalid)
62+ secureCrypto .decrypt(invalid)
6263 }
6364 }
6465
6566 @Test
6667 fun `빈 바이트 배열 암호화 시 예외가 발생하지 않아야 한다` () {
6768 val input = ByteArray (0 )
68- val encrypted = crypto .encrypt(input)
69- val decrypted = crypto .decrypt(encrypted)
69+ val encrypted = secureCrypto .encrypt(input)
70+ val decrypted = secureCrypto .decrypt(encrypted)
7071 assertEquals(String (input), String (decrypted))
7172 }
7273
7374 @Test
7475 fun `IV 일부가 조작된 경우 복호화하면 원래 데이터와 달라야 한다` () {
7576 // given
7677 val original = " iv 테스트" .toByteArray()
77- val encrypted = crypto .encrypt(original)
78+ val encrypted = secureCrypto .encrypt(original)
7879 encrypted[0 ] = encrypted[0 ].inc()
7980
8081 // when
81- val decrypted = crypto .decrypt(encrypted)
82+ val decrypted = secureCrypto .decrypt(encrypted)
8283
8384 // then
8485 assertNotEquals(String (original), String (decrypted))
@@ -88,20 +89,20 @@ class CryptoTest {
8889 fun `암호화된 데이터가 조작된 경우 복호화 실패해야 한다` () {
8990 // given
9091 val original = " 데이터 조작" .toByteArray()
91- val encrypted = crypto .encrypt(original)
92+ val encrypted = secureCrypto .encrypt(original)
9293 encrypted[encrypted.lastIndex] = encrypted.last().inc()
9394
9495 // when & then
9596 assertThrows(BadPaddingException ::class .java) {
96- crypto .decrypt(encrypted)
97+ secureCrypto .decrypt(encrypted)
9798 }
9899 }
99100
100101 @Test
101102 fun `다른 키로 복호화하면 실패해야 한다` () {
102103 // given
103104 val original = " 다른 키 테스트" .toByteArray()
104- val encrypted = crypto .encrypt(original)
105+ val encrypted = secureCrypto .encrypt(original)
105106
106107 val otherKeyProvider =
107108 object : KeyProvider {
@@ -112,11 +113,11 @@ class CryptoTest {
112113 }
113114 }
114115
115- val otherCrypto = Crypto (otherKeyProvider, " AES/CBC/PKCS5Padding" )
116+ val otherSecureCrypto = SecureCrypto (otherKeyProvider, " AES/CBC/PKCS5Padding" )
116117
117118 // when & then
118119 assertThrows(BadPaddingException ::class .java) {
119- otherCrypto .decrypt(encrypted)
120+ otherSecureCrypto .decrypt(encrypted)
120121 }
121122 }
122123}
0 commit comments