Skip to content

Commit db59606

Browse files
committed
Chore: 테스트 코드 구체적인 예외 타입 명시
1 parent 00e8757 commit db59606

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

  • core/security/src

core/security/src/main/java/com/threegap/bitnagil/security/Crypto.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@ class Crypto(
1717

1818
fun decrypt(bytes: ByteArray): ByteArray {
1919
val cipher = Cipher.getInstance(transformation)
20+
require(bytes.size >= cipher.blockSize) {
21+
INVALID_INPUT_TOO_SHORT_MSG
22+
}
2023
val iv = bytes.copyOfRange(0, cipher.blockSize)
2124
val data = bytes.copyOfRange(cipher.blockSize, bytes.size)
2225
cipher.init(Cipher.DECRYPT_MODE, keyProvider.getKey(), IvParameterSpec(iv))
2326
return cipher.doFinal(data)
2427
}
28+
29+
companion object {
30+
private const val INVALID_INPUT_TOO_SHORT_MSG = "[ERROR] 복호화할 수 없는 입력입니다."
31+
}
2532
}

core/security/src/test/java/com/threegap/bitnagil/security/CryptoTest.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import org.junit.Assert.assertEquals
44
import org.junit.Assert.assertNotEquals
55
import org.junit.Assert.assertThrows
66
import org.junit.Test
7+
import javax.crypto.BadPaddingException
78
import javax.crypto.KeyGenerator
89
import javax.crypto.SecretKey
910

@@ -51,16 +52,24 @@ class CryptoTest {
5152
}
5253

5354
@Test
54-
fun `잘못된 데이터로 복호화를 시도하면 예외가 발생해야 한다`() {
55+
fun `입력값이 너무 짧은 경우 IllegalArgumentException이 발생해야 한다`() {
5556
// given
5657
val invalid = ByteArray(4) { 0x00 }
5758

5859
// when & then
59-
assertThrows(Exception::class.java) {
60+
assertThrows(IllegalArgumentException::class.java) {
6061
crypto.decrypt(invalid)
6162
}
6263
}
6364

65+
@Test
66+
fun `빈 바이트 배열 암호화 시 예외가 발생하지 않아야 한다`() {
67+
val input = ByteArray(0)
68+
val encrypted = crypto.encrypt(input)
69+
val decrypted = crypto.decrypt(encrypted)
70+
assertEquals(String(input), String(decrypted))
71+
}
72+
6473
@Test
6574
fun `IV 일부가 조작된 경우 복호화하면 원래 데이터와 달라야 한다`() {
6675
// given
@@ -83,7 +92,7 @@ class CryptoTest {
8392
encrypted[encrypted.lastIndex] = encrypted.last().inc()
8493

8594
// when & then
86-
assertThrows(Exception::class.java) {
95+
assertThrows(BadPaddingException::class.java) {
8796
crypto.decrypt(encrypted)
8897
}
8998
}
@@ -106,7 +115,7 @@ class CryptoTest {
106115
val otherCrypto = Crypto(otherKeyProvider, "AES/CBC/PKCS5Padding")
107116

108117
// when & then
109-
assertThrows(Exception::class.java) {
118+
assertThrows(BadPaddingException::class.java) {
110119
otherCrypto.decrypt(encrypted)
111120
}
112121
}

0 commit comments

Comments
 (0)