-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecComponent.test.cpp
More file actions
245 lines (218 loc) · 7.06 KB
/
secComponent.test.cpp
File metadata and controls
245 lines (218 loc) · 7.06 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
235
236
237
238
239
240
241
242
243
244
245
/*
(c) Matthew Slocum 2015
secComponent.test.cpp this file is responsible for unit test and component tests for the secComponent
*/
#include "secComponent.h"
#include <cstring>
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;
int test_encrypt_decrypt_inverting(secComponent * sec, string message, string key, int r, char * ciphertext, char * plaintext) {
int ciphertext_len = sec->encrypt(message, key, r, ciphertext);
int plaintext_len = sec->decrypt(ciphertext, message.length()+10, key, r, plaintext);
for(unsigned int i=0; i<message.length(); i++) {
if(message[i]!=plaintext[i])
return 0;
}
return 1;
}
void zero(char * ar, int len) {
for (int i=0; i<len; i++) {
ar[i] = 0;
}
}
int main(void) {
secComponent * sec = new secComponent();
int r = 20;
unsigned char ciphertext [1000];
unsigned char plaintext[1000];
string key = "password";
string message = "";
//
//'Unit' test for the encryption / decryption protocols
//
cout << endl << "Unit Tests:" << endl;
//test 1
//encrypt+decrypt on a single letter message
zero(ciphertext,1000);
zero(plaintext,1000);
message = "X";
if(test_encrypt_decrypt_inverting(sec, message, key, r, ciphertext, plaintext)) {
cout << "Pass" << endl;
} else {
cout << "secComponent.test Test 1 failed" << endl;
}
//test 2
//encrypt+decrypt on a single word message
zero(ciphertext,1000);
zero(plaintext,1000);
message = "hello";
if(test_encrypt_decrypt_inverting(sec, message, key, r, ciphertext, plaintext)) {
cout << "Pass" << endl;
} else {
cout << "secComponent.test Test 2 failed" << endl;
}
//test 3
//encrypt+decrypt on a multi word message
zero(ciphertext,1000);
zero(plaintext,1000);
message = "hello world";
if(test_encrypt_decrypt_inverting(sec, message, key, r, ciphertext, plaintext)) {
cout << "Pass" << endl;
} else {
cout << "secComponent.test Test 3 failed" << endl;
}
//test 4
//encrypt+decrypt on a multi word message with special characters
zero(ciphertext,1000);
zero(plaintext,1000);
message = "hello world!";
if(test_encrypt_decrypt_inverting(sec, message, key, r, ciphertext, plaintext)) {
cout << "Pass" << endl;
} else {
cout << "secComponent.test Test 4 failed" << endl;
}
//test 5
//encrypt+decrypt on a multi line message
zero(ciphertext,1000);
zero(plaintext,1000);
message = "hello world\r\nfish\r\n\r\nsalmon\r\n";
if(test_encrypt_decrypt_inverting(sec, message, key, r, ciphertext, plaintext)) {
cout << "Pass" << endl;
} else {
cout << "secComponent.test Test 5 failed" << endl;
}
//test 6
//encrypt+decrypt on a multi line message with special characters
zero(ciphertext,1000);
zero(plaintext,1000);
message = "!@#$%%^&*\0x94hello world!\r\nfish\r\n\r\ns@lmon;\r\n";
if(test_encrypt_decrypt_inverting(sec, message, key, r, ciphertext, plaintext)) {
cout << "Pass" << endl;
} else {
cout << "secComponent.test Test 6 failed" << endl;
}
//test 7
//encrypt+decrypt on a multi line message with special characters
zero(ciphertext,1000);
zero(plaintext,1000);
message = string("version: 0.2\r\n")
+ "from: " + "source_user" + "\r\n"
+ "to: " + "dest_user" + "\r\n"
+ "\r\n"
+ "hey look a message!" + "\r\n";
if(test_encrypt_decrypt_inverting(sec, message, key, r, ciphertext, plaintext)) {
cout << "Pass" << endl;
} else {
cout << "secComponent.test Test 7 failed" << endl;
}
//
//Decrytion tests using known good encrypted values
//
cout << endl << "Decryption tests using known good values: " << endl;
//test 8
//decrypt tst/cstest1.cs1
//Key "asdfg".
//Plaintext will be "This is a test of CipherSaber." with no newline.
zero(ciphertext,1000);
zero(plaintext,1000);
key="asdfg";
string check = "This is a test of CipherSaber.";
ifstream cipherFile;
cipherFile.open("tst/cstest1.cs1");
int i=0;
if (cipherFile.is_open()) {
while (!cipherFile.eof()) {
ciphertext[i] = cipherFile.get();
i++;
}
}
cipherFile.close();
sec->decrypt(ciphertext, i-1, key, 1, plaintext);
string out;
for (int k=0; k<i-11; k++) {
out+=plaintext[k];
}
if(out==check) {
cout << "Pass" << endl;
} else {
cout << "secComponent.test Test 8 failed" << endl;
}
//test 9
//decrypt tst/cstest.cs2
//Key "asdfg".
//Plaintext will be "This is a test of CipherSaber-2." with no newline.
zero(ciphertext,1000);
zero(plaintext,1000);
key="asdfg";
check = "This is a test of CipherSaber-2.";
cipherFile.open("tst/cstest.cs2");
i=0;
if (cipherFile.is_open()) {
while (!cipherFile.eof()) {
ciphertext[i] = cipherFile.get();
i++;
}
}
cipherFile.close();
sec->decrypt(ciphertext, i-1, key, 10, plaintext);
out="";
for (int k=0; k<i-11; k++) {
out+=plaintext[k];
}
if(out==check) {
cout << "Pass" << endl;
} else {
cout << "secComponent.test Test 9 failed" << endl;
}
//test 10
//decrypt cipher knight certificate
//key ThomasJefferson
key = "ThomasJefferson";
char crypto_cert[20474];
char plain_cert[20474];
fstream cert;
//read in the crypto file
cipherFile.open("tst/cknight.cs1");
i=0;
if (cipherFile.is_open()) {
while (!cipherFile.eof()) {
crypto_cert[i] = cipherFile.get();
i++;
}
}
cipherFile.close();
//decrypt it
sec->decrypt_long(crypto_cert, i-1, key, 1, plain_cert);
//write it out
cert.open("tst/cknight.gif", fstream::out);
if(cert.is_open()) {
for(int k=0; k<i-1; k++) {
cert << plain_cert[k];
}
}
//Read in known good value
ifstream trueCert;
trueCert.open("tst/cknight.gif.ref");
int flag = 0;
i = 0;
unsigned char c;
if (trueCert.is_open()) {
while (!trueCert.eof()) {
c = trueCert.get();
if(plain_cert[i] != c) {
flag = 1;
}
i++;
}
}
trueCert.close();
//if the dont match fail the test. if they do then pass pass it
if(flag==1) {
cout << "Pass" << endl;
} else {
cout << "secComponent.test Test 10 failed" << endl;
}
return 0;
}