Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasFella committed May 16, 2024
1 parent 79c03b4 commit 334b021
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Quotient/keyimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ KeyImport::KeyImport(QObject* parent)
{
}

KeyImport::Error KeyImport::importKeys(QString data, const QString& passphrase, Connection* connection)
Expected<QJsonArray, KeyImport::Error> KeyImport::decrypt(QString data, const QString& passphrase)
{
data.remove(QLatin1String("-----BEGIN MEGOLM SESSION DATA-----"));
data.remove(QLatin1String("-----END MEGOLM SESSION DATA-----"));
Expand Down Expand Up @@ -72,8 +72,17 @@ KeyImport::Error KeyImport::importKeys(QString data, const QString& passphrase,
qCWarning(E2EE) << "Failed to decrypt data";
return OtherError;
}
const auto array = QJsonDocument::fromJson(plain.value()).array();
for (const auto& key : array) {
return QJsonDocument::fromJson(plain.value()).array();
}

KeyImport::Error KeyImport::importKeys(QString data, const QString& passphrase, Connection* connection)
{
auto result = decrypt(data, passphrase);
if (!result.has_value()) {
return result.error();
}

for (const auto& key : result.value()) {
const auto& keyObject = key.toObject();
const auto& room = connection->room(keyObject[QStringLiteral("room_id")].toString());
if (!room) {
Expand Down
7 changes: 7 additions & 0 deletions Quotient/keyimport.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

#include <QObject>

#include "expected.h"
#include "quotient_export.h"

class TestKeyImport;

namespace Quotient
{
class Connection;
Expand All @@ -31,6 +34,10 @@ class QUOTIENT_API KeyImport : public QObject
explicit KeyImport(QObject* parent = nullptr);

Q_INVOKABLE Error importKeys(QString data, const QString& passphrase, Quotient::Connection* connection);

friend class ::TestKeyImport;
private:
Quotient::Expected<QJsonArray, Error> decrypt(QString data, const QString& passphrase);
};

}
1 change: 1 addition & 0 deletions autotests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ if(${PROJECT_NAME}_ENABLE_E2EE)
quotient_add_test(NAME testcryptoutils)
quotient_add_test(NAME testkeyverification)
quotient_add_test(NAME testcrosssigning)
quotient_add_test(NAME testkeyimport)
endif()
15 changes: 15 additions & 0 deletions autotests/key-export.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-----BEGIN MEGOLM SESSION DATA-----
ATQrLZT5CO0z1mJGtJcBaa4mabBB0pRII3RsKeXzvtKSAAehIFJ3016VcwFNTWDHPXC6cixX6u9YXWP7rTk7Z5vMVkIPRkUND2SbGmdrXYDWJgN/Ueg++VQZKkTWbiIQ
6l/h9Xo3Jp8Quskrb1g9kqerF0s9m+z5xfp2plwCzsYwEtvm7mFbS2v5yvY8Gc9c4oaMRU/+W1ZqBB0AznubfcvmU+EbWXtCKk+7EhnCbeD12ve9YLL4G/gfiE9afeRk
T318G8xYYT1eCMt/wxXAiSSmjwXc+Vp1n6qbg7KIGHWmu8eNvnj55aRu9Kk0+jtBXGhrMCGnjiJ3XDyPnjZnBJV81Cb3FIE8o3AAZGZ1aGgxK/PSBScaCI4aoWURd1oP
4SXNgehJdahqhLNfulagns9Cxx3lo/IlRG9tf28gDE6Bb3IFj5GbH8SUICylHZsJSD+vTSXb7aUNNqjcqowNS4evGFJCzOtfDqkoHyfqiln+8rMQ1OL0MuqEcX/2G6Tt
encqwrJN/6dMgdInABXArSgM1sqiBe59ZRVoml2cSDGekvpsq6sufmS/tg/25tvLVDRs7nwXLRSELAEo3WuWeBugUuJsYWn0b7Nb+ye3TW4ep7tvGh3v1TIhV4UQ9nbv
c6xPJnKS7CHw+xXUEXDg+4o20wY9+6w/lb59549/86GLaa0jPNr4AuV6QcSFy0kXXt2E2y0uzfRUEiUJmMT/YcgiSrgQR4Hc+lVMIDIPhhY5bzgCPytt7u78Y2faX+ef
9gU+1vHHvc5WeVt3TlVoEx2nDeCMSR/a/waBIV0bkSq0WwgIrYioNNTQ0SchAGHXN5eERJKAuXMd4n6khhXAI0+XCRQFgNn9b20hCCjEq4yb6GQ4cRHmvjtY12Z11VDH
4L7rmGFN0bvcmvjvrirZXj7Myi0engMHUhvd3zNhVKMpHzPghyDTClUWfnA/amZe1VN12jrkSAxSoLmQYALCBooIu7z/G8jZAZrgr72OmesYg4QE8A0UR+Y2cEWoOKvg
QvmBMnnu/kBIOqhdFfAcoJlWbTcYH/AJJsbqMsb9RmOhmBch8kSXDq7oTuV4iGT6xp47ZejI1St40oAXRjYEL4uJkXyZTvK/jkdjkFPbxHH8zAIcrOWWnczs8sfE7OMa
ASeiPjD0UWNEm8vXJIXE50zvHg0tDoIHlWSrt3khW+UlEEvK4J5oJYRTAUfKzXZrbZaq+C6Q0z30ksuE45aLtSdODlsKAc1fksscqycmVU22/SfvQje5Dv/uD9i8J86Y
+6OuXTzVw0H21+2tIBAjKaCM4jRFDruc/stok5Ixhxepl7184blfhZ4uRbt1hYGjcPfl6viB7lcboN4+i2WyB0xcpaZpQD6UBrglmFO05XKmSfjegR00YhmfS8yFXygs
fBJPNYihZoMmyRJjXqkETS7YVUSRPruMA+y8tyrwRO199LXaBbWORBI4EbFeVZlndGeileYQ6JNB7JdFu+RkeUkvkJPAyM8YD7NxR/5t+vNToJGUshy41CGbnVs30gxz
OPV+SCzfqD8qIjFiDWjsIGj7Hqm0qA==
-----END MEGOLM SESSION DATA-----
37 changes: 37 additions & 0 deletions autotests/testkeyimport.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-FileCopyrightText: 2024 Tobias Fella <[email protected]>
// SPDX-License-Identifier: LGPL-2.1-or-later

#include <Quotient/e2ee/cryptoutils.h>

#include <QTest>

#include <Quotient/keyimport.h>

class TestKeyImport : public QObject
{
Q_OBJECT
private slots:
void testImport();
};

using namespace Quotient;

void TestKeyImport::testImport()
{
KeyImport keyImport;

auto path = QString::fromUtf8(__FILE__);
path = path.left(path.lastIndexOf(QDir::separator()));
path += QStringLiteral("/key-export.data");
QFile file(path);
file.open(QIODevice::ReadOnly);
auto data = file.readAll();
QVERIFY(!data.isEmpty());
const auto result = keyImport.decrypt(QString::fromUtf8(data), QStringLiteral("123passphrase"));
QVERIFY(result.has_value());
const auto &json = result.value();
QCOMPARE(json.size(), 2);
}

QTEST_GUILESS_MAIN(TestKeyImport)
#include "testkeyimport.moc"

0 comments on commit 334b021

Please sign in to comment.