aboutsummaryrefslogtreecommitdiff
path: root/tests/qxmppfileencryption/tst_qxmppfileencryption.cpp
diff options
context:
space:
mode:
authorLinus Jahn <lnj@kaidan.im>2022-09-14 22:36:11 +0200
committerLinus Jahn <lnj@kaidan.im>2022-09-24 18:11:58 +0200
commitc8bc1db682c165853ad51e2806f932e4fd0b0597 (patch)
tree8bd06a8b72ef038ee2ffaca8881656df950d84b8 /tests/qxmppfileencryption/tst_qxmppfileencryption.cpp
parent9d9d0b22664c6860a005818e9e787670aec389ff (diff)
downloadqxmpp-c8bc1db682c165853ad51e2806f932e4fd0b0597.tar.gz
Add file encryption functions and Encryption/DecryptionDevice
The devices allow it to encrypt or decrypt data on the fly when reading or writing data.
Diffstat (limited to 'tests/qxmppfileencryption/tst_qxmppfileencryption.cpp')
-rw-r--r--tests/qxmppfileencryption/tst_qxmppfileencryption.cpp107
1 files changed, 107 insertions, 0 deletions
diff --git a/tests/qxmppfileencryption/tst_qxmppfileencryption.cpp b/tests/qxmppfileencryption/tst_qxmppfileencryption.cpp
new file mode 100644
index 00000000..dec935f3
--- /dev/null
+++ b/tests/qxmppfileencryption/tst_qxmppfileencryption.cpp
@@ -0,0 +1,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"