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
|
// SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "QXmppFileEncryption.h"
#include "QcaInitializer_p.h"
#include <QtTest>
using namespace QXmpp;
using namespace QXmpp::Private;
using namespace QXmpp::Private::Encryption;
class tst_QXmppFileEncryption : public QObject
{
Q_OBJECT
private:
Q_SLOT void basic();
Q_SLOT void deviceEncrypt();
Q_SLOT void deviceDecrypt();
Q_SLOT void paddingSize();
};
void tst_QXmppFileEncryption::basic()
{
QcaInitializer encInit;
QByteArray data =
"This is an example text message";
QByteArray key = "12345678901234567890123456789012";
QByteArray iv = "data";
auto encrypted = process(data, Aes256CbcPkcs7, Encode, key, iv);
qDebug() << data.size() << "->" << encrypted.size();
auto decrypted = process(encrypted, Aes256CbcPkcs7, Decode, key, iv);
QCOMPARE(decrypted, data);
}
void tst_QXmppFileEncryption::deviceEncrypt()
{
QcaInitializer encInit;
QByteArray data =
"v2qtI8tx5DxM6axUAZ+xsEwrtb0VYafAPlMWqpVMG+5PBE5wbZ7MZhDUEIdFkxchOIJqt";
QByteArray key = "12345678901234567890123456789012";
QByteArray iv = "12345678901234567890123456789012";
auto buffer = std::make_unique<QBuffer>(&data);
buffer->open(QIODevice::ReadOnly);
EncryptionDevice encDev(std::move(buffer), Aes256CbcPkcs7, key, iv);
auto encrypted = encDev.readAll();
auto decrypted = process(encrypted, Aes256CbcPkcs7, Decode, key, iv);
QCOMPARE(decrypted, data);
}
void tst_QXmppFileEncryption::deviceDecrypt()
{
QcaInitializer encInit;
QByteArray data =
"v2qtI8tx5DxM6axUAZ+xsEwrtb0VYafAPlMWqpVMG+5PBE5wbZ7MZhDUEIdFkxchOIJqt";
QByteArray key = "12345678901234567890123456789012";
QByteArray iv = "12345678901234567890123456789012";
auto encrypted = process(data, Aes256CbcPkcs7, Encode, key, iv);
QByteArray decrypted;
auto buffer = std::make_unique<QBuffer>(&decrypted);
buffer->open(QIODevice::WriteOnly);
DecryptionDevice decDev(std::move(buffer), Aes256CbcPkcs7, key, iv);
decDev.write(encrypted);
QCOMPARE(decrypted, data);
}
void tst_QXmppFileEncryption::paddingSize()
{
constexpr auto MAX_BYTES_TEST = 1024;
QcaInitializer encInit;
QByteArray key = "12345678901234567890123456789012";
QByteArray iv = "12345678901234567890123456789012";
for (int i = 1; i <= MAX_BYTES_TEST; i++) {
QByteArray data(i, 'a');
auto buffer = std::make_unique<QBuffer>(&data);
buffer->open(QIODevice::ReadOnly);
EncryptionDevice encDev(std::move(buffer), Aes256CbcPkcs7, key, iv);
auto reportedSize = encDev.size();
auto encryptedData = encDev.readAll();
QCOMPARE(reportedSize, encryptedData.size());
auto decryptedData = process(encryptedData, Aes256CbcPkcs7, Decode, key, iv);
QCOMPARE(decryptedData, data);
}
}
QTEST_MAIN(tst_QXmppFileEncryption)
#include "tst_qxmppfileencryption.moc"
|