Skip to content

Commit 72bd2df

Browse files
author
Matteo Brichese
committed
refactoring test class
1 parent d69dd98 commit 72bd2df

File tree

2 files changed

+69
-129
lines changed

2 files changed

+69
-129
lines changed

unit_test/aestest.cpp

Lines changed: 61 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -4,122 +4,101 @@
44
#include <QCryptographicHash>
55
#include "qaesencryption.h"
66

7-
void AesTest::ECB128Crypt()
7+
void AesTest::initTestCase()
88
{
9-
QByteArray hexText, keyHex, outputHex;
10-
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
9+
quint8 key_16[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
10+
for (int i=0; i<16; i++)
11+
key16.append(key_16[i]);
1112

12-
const quint8 key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
13+
quint8 key_24[24] = { 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5, 0x62, 0xf8,
14+
0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b};
15+
for (int i=0; i<24; i++)
16+
key24.append(key_24[i]);
1317

14-
const quint8 text[16] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
15-
const quint8 output[16] = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97};
18+
quint8 key_32[32]= { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
19+
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 };
20+
for (int i=0; i<32; i++)
21+
key32.append(key_32[i]);
1622

23+
quint8 iv_16[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
24+
for (int i=0; i<16; i++)
25+
iv.append(iv_16[i]);
26+
27+
quint8 in_text[16] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a };
28+
quint8 out_text[16] = { 0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97 };
29+
quint8 out_text_2[16] = { 0xbd, 0x33, 0x4f, 0x1d, 0x6e, 0x45, 0xf2, 0x5f, 0xf7, 0x12, 0xa2, 0x14, 0x57, 0x1f, 0xa5, 0xcc };
30+
quint8 out_text_3[16] = { 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c, 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8 };
1731

1832
for (int i=0; i<16; i++){
19-
keyHex.append(key[i]);
20-
hexText.append(text[i]);
21-
outputHex.append(output[i]);
33+
in.append(in_text[i]);
34+
outECB128.append(out_text[i]);
35+
outECB192.append(out_text_2[i]);
36+
outECB256.append(out_text_3[i]);
2237
}
2338

24-
QCOMPARE(encryption.encode(hexText, keyHex), outputHex);
39+
quint8 text_cbc[64] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
40+
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
41+
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
42+
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
43+
44+
quint8 output_cbc[64] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
45+
0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
46+
0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
47+
0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
48+
49+
for (int i=0; i<64; i++){
50+
inCBC128.append(text_cbc[i]);
51+
outCBC128.append(output_cbc[i]);
52+
}
2553
}
2654

27-
void AesTest::ECB128Decrypt()
55+
56+
//==================ECB TESTING=========================
57+
58+
void AesTest::ECB128Crypt()
2859
{
29-
QByteArray hexText, keyHex, outputHex;
60+
QByteArray hexText, outputHex;
3061
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
3162

32-
const quint8 key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
33-
const quint8 text[16] = {0x43, 0xb1, 0xcd, 0x7f, 0x59, 0x8e, 0xce, 0x23, 0x88, 0x1b, 0x00, 0xe3, 0xed, 0x03, 0x06, 0x88};
34-
const quint8 output[16] = {0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef};
63+
QCOMPARE(encryption.encode(in, key16), outECB128);
64+
}
3565

36-
for (int i=0; i<16; i++){
37-
keyHex.append(key[i]);
38-
hexText.append(text[i]);
39-
outputHex.append(output[i]);
40-
}
66+
void AesTest::ECB128Decrypt()
67+
{
68+
QByteArray hexText, outputHex;
69+
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
4170

42-
QCOMPARE(encryption.decode(hexText, keyHex), outputHex);
71+
QCOMPARE(encryption.decode(outECB128, key16), in);
4372
}
4473

4574
void AesTest::ECB192Crypt()
4675
{
47-
QByteArray hexText, keyHex, outputHex;
76+
QByteArray outputHex;
4877
QAESEncryption encryption(QAESEncryption::AES_192, QAESEncryption::ECB);
4978

50-
uint8_t key[24] = { 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5, 0x62, 0xf8,
51-
0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b};
52-
uint8_t text[16] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
53-
uint8_t output[16] = { 0xbd, 0x33, 0x4f, 0x1d, 0x6e, 0x45, 0xf2, 0x5f, 0xf7, 0x12, 0xa2, 0x14, 0x57, 0x1f, 0xa5, 0xcc };
54-
55-
for (int i=0; i<16 ; i++){
56-
hexText.append(text[i]);
57-
outputHex.append(output[i]);
58-
}
59-
for (int i=0; i<24 ; i++)
60-
keyHex.append(key[i]);
61-
62-
QCOMPARE(encryption.encode(hexText, keyHex), outputHex);
79+
QCOMPARE(encryption.encode(in, key24), outECB192);
6380
}
6481

6582
void AesTest::ECB192Decrypt()
6683
{
67-
QByteArray hexText, keyHex, outputHex;
84+
QByteArray hexText;
6885
QAESEncryption encryption(QAESEncryption::AES_192, QAESEncryption::ECB);
6986

70-
uint8_t key[24] = { 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5, 0x62, 0xf8,
71-
0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b};
72-
uint8_t text[16] = { 0xbd, 0x33, 0x4f, 0x1d, 0x6e, 0x45, 0xf2, 0x5f, 0xf7, 0x12, 0xa2, 0x14, 0x57, 0x1f, 0xa5, 0xcc };
73-
uint8_t output[16] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
74-
75-
for (int i=0; i<16 ; i++){
76-
hexText.append(text[i]);
77-
outputHex.append(output[i]);
78-
}
79-
for (int i=0; i<24 ; i++)
80-
keyHex.append(key[i]);
81-
82-
QCOMPARE(encryption.decode(hexText, keyHex), outputHex);
87+
QCOMPARE(encryption.decode(outECB192, key24), in);
8388
}
8489

8590
void AesTest::ECB256Crypt()
8691
{
87-
QByteArray hexText, keyHex, outputHex;
8892
QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB);
8993

90-
uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
91-
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 };
92-
uint8_t text[16] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
93-
uint8_t output[16] = { 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c, 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8 };
94-
95-
for (int i=0; i<16 ; i++){
96-
hexText.append(text[i]);
97-
outputHex.append(output[i]);
98-
}
99-
for (int i=0; i<32 ; i++)
100-
keyHex.append(key[i]);
101-
102-
QCOMPARE(encryption.encode(hexText, keyHex), outputHex);
94+
QCOMPARE(encryption.encode(in, key32), outECB256);
10395
}
10496

10597
void AesTest::ECB256Decrypt()
10698
{
107-
QByteArray hexText, keyHex, outputHex;
10899
QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB);
109100

110-
uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
111-
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 };
112-
uint8_t output[16] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
113-
uint8_t text[16] = { 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c, 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8 };
114-
115-
for (int i=0; i<16 ; i++){
116-
hexText.append(text[i]);
117-
outputHex.append(output[i]);
118-
}
119-
for (int i=0; i<32 ; i++)
120-
keyHex.append(key[i]);
121-
122-
QCOMPARE(encryption.decode(hexText, keyHex), outputHex);
101+
QCOMPARE(encryption.decode(outECB256, key32), in);
123102
}
124103

125104
void AesTest::ECB256String()
@@ -138,65 +117,19 @@ void AesTest::ECB256String()
138117
QCOMPARE(QString(encryption.decode(encodeText, hashKey)), inputStr);
139118
}
140119

120+
121+
//==================CBC TESTING=========================
122+
141123
void AesTest::CBC128Crypt()
142124
{
143-
QByteArray hexText, keyHex, outputHex, ivHex;
144125
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::CBC);
145126

146-
const quint8 key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
147-
const quint8 iv[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
148-
149-
const quint8 text[64] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
150-
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
151-
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
152-
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10};
153-
154-
const quint8 output[64] = {0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
155-
0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
156-
0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
157-
0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7};
158-
159-
for (int i=0; i<64; i++){
160-
if (i<16) {
161-
keyHex.append(key[i]);
162-
ivHex.append(iv[i]);
163-
}
164-
hexText.append(text[i]);
165-
outputHex.append(output[i]);
166-
167-
}
168-
169-
QCOMPARE(encryption.encode(hexText, keyHex, ivHex), outputHex);
127+
QCOMPARE(encryption.encode(inCBC128, key16, iv), outCBC128);
170128
}
171129

172130
void AesTest::CBC128Decrypt()
173131
{
174-
QByteArray hexText, keyHex, outputHex, ivHex;
175132
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::CBC);
176133

177-
const quint8 key[16] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
178-
const quint8 iv[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
179-
180-
const quint8 text[64] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
181-
0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
182-
0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
183-
0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
184-
185-
const quint8 output[64] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
186-
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
187-
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
188-
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
189-
190-
191-
for (int i=0; i<64; i++){
192-
if (i<16) {
193-
keyHex.append(key[i]);
194-
ivHex.append(iv[i]);
195-
}
196-
hexText.append(text[i]);
197-
outputHex.append(output[i]);
198-
199-
}
200-
201-
QCOMPARE(encryption.decode(hexText, keyHex, ivHex), outputHex);
134+
QCOMPARE(encryption.decode(outCBC128, key16, iv), inCBC128);
202135
}

unit_test/aestest.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
#define AESTEST_H
33

44
#include <QObject>
5+
#include <QByteArray>
56
#include <QTest>
67

78
class AesTest : public QObject
89
{
910
Q_OBJECT
1011
private slots:
11-
void initTestCase() {}
12+
void initTestCase();
1213

1314
void ECB128Crypt();
1415
void ECB128Decrypt();
@@ -25,6 +26,12 @@ private slots:
2526
void CBC128Decrypt();
2627

2728
void cleanupTestCase(){}
29+
30+
private:
31+
QByteArray key16, key24, key32;
32+
QByteArray iv;
33+
QByteArray in, outECB128, outECB192, outECB256;
34+
QByteArray inCBC128, outCBC128;
2835
};
2936

3037
#endif // AESTEST_H

0 commit comments

Comments
 (0)