You need to sign in or sign up before continuing.
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
}
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) {
saltedPwd, _ := SaltPassword("P@ssw0rd")
assert.NotZero(t, len(saltedPwd), "Should give len")
}
func TestSaltPasswordError(t *testing.T) {
errormdl.IsTestingNegetiveCaseOn = true
_, err := SaltPassword("P@ssw0rd")
errormdl.IsTestingNegetiveCaseOn = false
assert.Error(t, err, "This should return error")
}
func BenchmarkSaltPassword(b *testing.B) {
for i := 0; i < b.N; i++ {
SaltPassword("P@ssw0rd")
}
}
func TestCheckPasswordHash(t *testing.T) {
match := CheckPasswordHash("$2a$04$hers/Xb2u00e8wg4e.S7Cu7JbUm4TTR4ED3wU7HTNuuwNGJxOqMZu", "P@ssw0rd")
assert.True(t, match)
}
func BenchmarkCheckPasswordHash(b *testing.B) {
for i := 0; i < b.N; i++ {
CheckPasswordHash("$2a$04$hers/Xb2u00e8wg4e.S7Cu7JbUm4TTR4ED3wU7HTNuuwNGJxOqMZu", "P@ssw0rd")
}
}