-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgoAESTest.kt
86 lines (75 loc) · 3.39 KB
/
AlgoAESTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package core
import com.kitano.crypto.internal.enums.AlgorithmType
import com.kitano.crypto.internal.CryptFactory
import com.kitano.crypto.internal.exceptions.IncorrectKeyException
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
/**
* Test class for the AES algorithm
* Created by KitanoB on 2018/11/11.
*/
class AlgoAESTest {
@Test
fun `when encrypt with aes same string is retrieved after decrypt`() {
val crypt = CryptFactory.createCrypt(AlgorithmType.AES)
val password = "1234567890123456"
val plainText = "This is a test"
val encryptedText = crypt.encrypt(plainText, password, null)
val decryptedText = crypt.decrypt(encryptedText, password, null)
assertEquals(plainText, decryptedText)
}
@Test(expected = IncorrectKeyException::class)
fun `when encrypt with aes same string is not retrieved after decrypt with wrong password`() {
val crypt = CryptFactory.createCrypt(AlgorithmType.AES)
val password = "123456"
val plainText = "This is a test"
val encryptedText = crypt.encrypt(plainText, password, null)
val decryptedText = crypt.decrypt(encryptedText, "test", null)
assertNotEquals(encryptedText, decryptedText)
}
@Test(expected = IncorrectKeyException::class)
fun `when decrypt with different key throws exception`() {
val crypt = CryptFactory.createCrypt(AlgorithmType.AES)
val password1 = "1234567890123456"
val password2 = "6543210987654321"
val plainText = "This is a test"
val encryptedText = crypt.encrypt(plainText, password1, null)
crypt.decrypt(encryptedText, password2, null)
}
@Test(expected = IncorrectKeyException::class)
fun `when encrypt long string with aes does throw`() {
val crypt = CryptFactory.createCrypt(AlgorithmType.AES)
val password = "1234567890123456"
val longString = "a".repeat(10_000)
val encrypted = crypt.encrypt(longString, password, null)
val decrypted = crypt.decrypt(longString, password, null)
assertEquals(encrypted, decrypted)
}
@Test
fun `when encrypt empty string with aes and then decrypt returns empty string`() {
val crypt = CryptFactory.createCrypt(AlgorithmType.AES)
val password = "1234567890123456"
val plainText = ""
val encryptedText = crypt.encrypt(plainText, password, null)
val decryptedText = crypt.decrypt(encryptedText, password, null)
assertEquals(plainText, decryptedText)
}
@Test
fun `when encrypt special characters with aes same string is retrieved after decrypt`() {
val crypt = CryptFactory.createCrypt(AlgorithmType.AES)
val password = "1234567890123456"
val specialString = "这是一个测试"
val encryptedText = crypt.encrypt(specialString, password, null)
val decryptedText = crypt.decrypt(encryptedText, password, null)
assertEquals(specialString, decryptedText)
}
@Test(expected = IncorrectKeyException::class)
fun `when encrypt with aes same string is not retrieved after decrypt null password`() {
val crypt = CryptFactory.createCrypt(AlgorithmType.AES)
val password = "123456"
val plainText = "This is a test"
val encryptedText = crypt.encrypt(plainText, password, null)
crypt.decrypt(encryptedText, "null", null)
}
}