Newer
Older
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package securitymdl
import (
"testing"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
"github.com/stretchr/testify/assert"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
)
// ===========================================================================================
// ============================================= Config ======================================
// ===========================================================================================
func TestSetSecurityConfigBlankValues(t *testing.T) {
secKey := ""
initializationVector := ""
SetSecurityConfig([]byte(secKey), initializationVector)
assert.Equal(t, IV, "AAAAAAAAAAAAAAAA", "Matching")
}
func TestSetSecurityConfig(t *testing.T) {
secKey := "1234567891234567"
initializationVector := "BBBBBBBBBBBBBBBB"
SetSecurityConfig([]byte(secKey), initializationVector)
assert.Equal(t, IV, "BBBBBBBBBBBBBBBB", "Matching")
}
func TestSetSecurityConfigDefaultValues(t *testing.T) {
secKey := "1234567891234567"
initializationVector := "AAAAAAAAAAAAAAAA"
SetSecurityConfig([]byte(secKey), initializationVector)
assert.Equal(t, IV, "AAAAAAAAAAAAAAAA", "Matching")
}
// ===========================================================================================
// =========================================== AESEncrypt ====================================
// ===========================================================================================
func TestAESEncryptSuccess(t *testing.T) {
plainText := "Test for success"
key := "1234567891234567"
encText, encError := AESEncrypt([]byte(plainText), []byte(key))
loggermdl.LogInfo("encrypted text : ", encText)
loggermdl.LogError("error is : ", encError)
assert.Equal(t, "HAtVHhxx9+ULClrO1dkSKMiu6IciRmQ2PcQi4kSsLn4=", string(encText), "Encryption Successful")
}
func TestAESEncryptSmallKeyLength(t *testing.T) {
plainText := "Test for success"
key := "123456789123456"
encText, encError := AESEncrypt([]byte(plainText), []byte(key))
loggermdl.LogInfo("encrypted text : ", encText)
loggermdl.LogError("error is : ", encError)
assert.Error(t, encError, "Error occured due to key size")
}
func TestAESEncryptIVLessThanBlock(t *testing.T) {
plainText := "Test for success"
key := "1234567891234567"
IV = "A"
encText, encError := AESEncrypt([]byte(plainText), []byte(key))
loggermdl.LogInfo("encrypted text : ", encText)
loggermdl.LogError("error is : ", encError)
assert.Error(t, encError, "IV size less than block")
}
// ===========================================================================================
// =========================================== AESDecrypt ====================================
// ===========================================================================================
func TestAESDecryptSuccess(t *testing.T) {
cipherText := []byte("HAtVHhxx9+ULClrO1dkSKMiu6IciRmQ2PcQi4kSsLn4=")
key := "1234567891234567"
IV = "AAAAAAAAAAAAAAAA"
encText, encError := AESDecrypt([]byte(cipherText), []byte(key))
loggermdl.LogInfo("decrypted text : ", string(encText))
loggermdl.LogError("error is : ", encError)
assert.Equal(t, "Test for success", string(encText), "Decryption Successful")
}
func TestAESDecryptSmallKeyLength(t *testing.T) {
cipherText := []byte("HAtVHhxx9+ULClrO1dkSKMiu6IciRmQ2PcQi4kSsLn4=")
key := "123456789123456"
encText, encError := AESDecrypt([]byte(cipherText), []byte(key))
loggermdl.LogInfo("encrypted text : ", encText)
loggermdl.LogError("error is : ", encError)
assert.Error(t, encError, "Error occured due to key size")
}
func TestAESDecryptDecodeError(t *testing.T) {
cipherText := []byte("HAtVHhxx9+ULClrO1dkSKMiu6IciRmQ2PcQi4kSsLn=")
key := "123456789123456"
encText, encError := AESDecrypt([]byte(cipherText), []byte(key))
loggermdl.LogInfo("encrypted text : ", encText)
loggermdl.LogError("error is : ", encError)
assert.Error(t, encError, "Decode error")
}
func TestAESDecryptCipherLessThan1(t *testing.T) {
cipherText := []byte("")
key := "1234567891234567"
encText, encError := AESDecrypt([]byte(cipherText), []byte(key))
loggermdl.LogInfo("encrypted text : ", encText)
loggermdl.LogError("error is : ", encError)
assert.Error(t, encError, "CipherLessThan1")
}
func TestAESDecryptCipherSize(t *testing.T) {
cipherText := []byte("")
key := "1234567891234567"
IV = "A"
encText, encError := AESDecrypt([]byte(cipherText), []byte(key))
loggermdl.LogInfo("encrypted text : ", encText)
loggermdl.LogError("error is : ", encError)
assert.Error(t, encError, "IV size less than block")
}
func TestAESDecryptIVLessThanBlock(t *testing.T) {
cipherText := []byte("")
key := "1234567891234567"
IV = "A"
encText, encError := AESDecrypt([]byte(cipherText), []byte(key))
loggermdl.LogInfo("encrypted text : ", encText)
loggermdl.LogError("error is : ", encError)
assert.Error(t, encError, "IV size less than block")
}
func TestAESDecryptDifferenceCheck(t *testing.T) {
errormdl.IsTestingNegetiveCaseOnCheckInt1 = true
cipherText := []byte("HAtVHhxx9+ULClrO1dkSKMiu6IciRmQ2PcQi4kSsLn4=")
key := "1234567891234567"
IV = "AAAAAAAAAAAAAAAA"
encText, encError := AESDecrypt([]byte(cipherText), []byte(key))
loggermdl.LogInfo("!!!!!!!!!!!!!!!!!!!!!!!!!!encrypted text : ", encText)
loggermdl.LogError("error is : ", encError)
assert.Error(t, encError, " / crypto/cipher: input not full blocks")
errormdl.IsTestingNegetiveCaseOnCheckInt1 = false
}
func TestAESDecryptDifferenceCheck2(t *testing.T) {
errormdl.IsTestingNegetiveCaseOnCheckInt2 = true
cipherText := []byte("HAtVHhxx9+ULClrO1dkSKMiu6IciRmQ2PcQi4kSsLn4=")
key := "1234567891234567"
IV = "AAAAAAAAAAAAAAAA"
encText, encError := AESDecrypt([]byte(cipherText), []byte(key))
loggermdl.LogInfo("!!!!!!!!!!!!!!!!!!!!!!!!!!encrypted text : ", encText)
loggermdl.LogError("error is : ", encError)
assert.Error(t, encError, "length of (length - unpadding) is less than 0 / crypto/cipher: input not full blocks")
errormdl.IsTestingNegetiveCaseOnCheckInt2 = false
}
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
func TestCreateSecurityKey(t *testing.T) {
errormdl.IsTestingNegetiveCaseOnCheckInt2 = true
keyLength := 256
key, _ := CreateSecurityKey(keyLength)
assert.Len(t, key, keyLength, "length is equal")
errormdl.IsTestingNegetiveCaseOnCheckInt2 = false
}
func TestCreateSecurityKeyForOutOfRange(t *testing.T) {
errormdl.IsTestingNegetiveCaseOnCheckInt2 = true
keyLength := 257
_, keyerr := CreateSecurityKey(keyLength)
loggermdl.LogError("error is : ", keyerr)
assert.Error(t, keyerr, "length is out of range,length should be less than 256 bytes (2048 bits)")
errormdl.IsTestingNegetiveCaseOnCheckInt2 = false
}
func BenchmarkCreateSecurityKey(b *testing.B) {
for i := 0; i < b.N; i++ {
CreateSecurityKey(16)
}
}
func TestSaltPassword(t *testing.T) {
errormdl.IsTestingNegetiveCaseOnCheckInt2 = true
saltedPwd, _ := SaltPassword("P@ssw0rd")
assert.NotZero(t, len(saltedPwd), "Should give len")
errormdl.IsTestingNegetiveCaseOnCheckInt2 = false
}
func BenchmarkSaltPassword(b *testing.B) {
for i := 0; i < b.N; i++ {
SaltPassword("P@ssw0rd")
}
}