-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypt_test.go
More file actions
234 lines (193 loc) · 6.44 KB
/
crypt_test.go
File metadata and controls
234 lines (193 loc) · 6.44 KB
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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package crypter
import (
"encoding/hex"
"testing"
)
var cryptKeys = []string {
"1234567890123456",
"123456789012345678901234",
"12345678901234567890123456789012",
}
func TestDecodeHexString(t *testing.T) {
testStr := "testing string"
hexStr := hex.EncodeToString([]byte(testStr))
decodedStr, err := DecodeHexString(hexStr)
if (string(decodedStr) != testStr) {
t.Error("Should have decoded to " + testStr)
}
if (err != nil) {
t.Error("Returned an error when decoding a valid hex string")
}
testStr = ""
hexStr = hex.EncodeToString([]byte(testStr))
decodedStr, err = DecodeHexString(hexStr)
if (string(decodedStr) != testStr) {
t.Error("Could not decode empty string")
}
if (err != nil) {
t.Error("Returned an error when decoding a valid hex string")
}
}
func TestDecodeHexStringInvalidInput(t *testing.T) {
testStr := "invalid hex"
_, err := DecodeHexString(testStr)
if (err == nil) {
t.Error("Did not return an error when decoding a bad string")
}
}
func TestValidateCryptKey(t *testing.T) {
for _, key := range cryptKeys {
if (!ValidateCryptKey(key)) {
t.Errorf("Rejected a valid Crypt Key: %s", key)
}
}
}
func TestValidateCryptKeyWithInvalidKey(t *testing.T) {
if (ValidateCryptKey("invalidkey")) {
t.Error("Did not reject invalid crypt key")
}
}
func TestNewCryptFromPlainTextBadKey(t *testing.T) {
crypt, err := NewCryptFromPlainText("abc", "abc")
if (crypt != nil || err == nil) {
t.Error("Didnt raise an error with invalid key")
}
}
func TestNewCryptFromPlainText(t *testing.T) {
text := "plain text"
key := cryptKeys[0]
crypt, err := NewCryptFromPlainText(text, key)
if (err != nil) {
t.Error("Returned an error when one was not expected")
}
if (string(crypt.UnencryptedData) != text) {
t.Errorf("Unable to initialize crypt with plain text '%s'", text)
}
if (string(crypt.Key) != key) {
t.Errorf("Unable to initialize crypt with key '%s'", key)
}
}
func TestNewCryptFromUnencryptedDataBadKey(t *testing.T) {
crypt, err := NewCryptFromUnencryptedData([]byte("abc"), "abc")
if (crypt != nil || err == nil) {
t.Error("Didnt raise an error with invalid key")
}
}
func TestNewCryptFromUnencryptedData(t *testing.T) {
text := "plain text"
data := []byte(text)
key := cryptKeys[0]
crypt, err := NewCryptFromUnencryptedData(data, key)
if (err != nil) {
t.Error("Returned an error when one was not expected")
}
if (string(crypt.UnencryptedData) != text) {
t.Errorf("Unable to initialize crypt with plain text '%s'", text)
}
if (string(crypt.Key) != key) {
t.Errorf("Unable to initialize crypt with key '%s'", key)
}
}
func TestNewCryptFromHexCipherTextBadKey(t *testing.T) {
crypt, err := NewCryptFromHexCipherText("abc", "abc")
if (crypt != nil || err == nil) {
t.Error("Didnt raise an error with invalid key")
}
}
func TestNewCryptFromHexCipherText(t *testing.T) {
textStr := "teststring"
hexStr := hex.EncodeToString([]byte(textStr))
key := cryptKeys[1]
crypt, err := NewCryptFromHexCipherText(hexStr, key)
if (err != nil) {
t.Error("Returned an error when one was not expected")
}
if (string(crypt.CipherData) != textStr) {
t.Errorf("Unable to initialize crypt with hex data '%s'", hexStr)
}
if (string(crypt.Key) != key) {
t.Errorf("Unable to initialize crypt with key '%s'", key)
}
}
func TestNewCryptFromCipherDataBadKey(t *testing.T) {
crypt, err := NewCryptFromCipherData([]byte("abc"), "abc")
if (crypt != nil || err == nil) {
t.Error("Didnt raise an error with invalid key")
}
}
func TestNewCryptFromCipherData(t *testing.T) {
textStr := "teststring"
key := cryptKeys[1]
crypt, err := NewCryptFromCipherData([]byte(textStr), key)
if (err != nil) {
t.Error("Returned an error when one was not expected")
}
if (string(crypt.CipherData) != textStr) {
t.Errorf("Unable to initialize crypt with hex data '%s'", textStr)
}
if (string(crypt.Key) != key) {
t.Errorf("Unable to initialize crypt with key '%s'", key)
}
}
func TestEncryptDecryptString(t *testing.T) {
stringToEncrypt := "some string"
key := cryptKeys[0]
crypt, err := NewCryptFromPlainText(stringToEncrypt, key)
encrypted, err := crypt.Encrypt()
if (err != nil) {
t.Error("Unable to encrypt string")
}
if (hex.EncodeToString(crypt.CipherData) != hex.EncodeToString(encrypted)) {
t.Error("Did not set the cipher data after encryption")
}
decrypted, err := crypt.DecryptToString()
if (err != nil) {
t.Error("Unable to encrypt string")
}
if (decrypted != stringToEncrypt) {
t.Errorf("Unable to encrypt/decrypt string: '%s'", stringToEncrypt)
}
crypt, err = NewCryptFromCipherData(encrypted, key)
decrypted, err = crypt.DecryptToString()
if (err != nil) {
t.Error("Unable to encrypt string")
}
if (decrypted != stringToEncrypt) {
t.Errorf("Unable to encrypt/decrypt string: '%s'", stringToEncrypt)
}
}
func TestEncryptDecryptThroughHex(t *testing.T) {
stringToEncrypt := "some string"
key := cryptKeys[0]
crypt, err := NewCryptFromPlainText(stringToEncrypt, key)
encrypted, err := crypt.EncryptToString()
if (err != nil) {
t.Error("Unable to encrypt string")
}
if (hex.EncodeToString(crypt.CipherData) != encrypted) {
t.Error("Did not set the cipher data after encryption")
}
decrypted, err := crypt.DecryptToString()
if (err != nil) {
t.Error("Unable to encrypt string")
}
if (decrypted != stringToEncrypt) {
t.Errorf("Unable to encrypt/decrypt string: '%s'", stringToEncrypt)
}
crypt, err = NewCryptFromHexCipherText(encrypted, key)
decrypted, err = crypt.DecryptToString()
if (err != nil) {
t.Error("Unable to encrypt string")
}
if (decrypted != stringToEncrypt) {
t.Errorf("Unable to encrypt/decrypt string: '%s'", stringToEncrypt)
}
}
func TestDecryptWithInvalidData(t *testing.T) {
key := cryptKeys[0]
crypt, err := NewCryptFromHexCipherText(hex.EncodeToString([]byte("lesthnbsize")), key)
_, err = crypt.Decrypt()
if (err == nil) {
t.Error("Did not fail with invalid text for decryption")
}
}