[network/ruqola] src/core: Adapt code

Laurent Montel <[email protected]> Tue, 4 Aug 2026 11:54:24 +0000 (UTC)
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 6b49f280221a7cdc80cb31b950067fb2800e7d49 by Laurent Montel.
Committed on 04/08/2026 at 11:25.
Pushed by mlaurent into branch 'master'.

Adapt code

M  +20   -12   src/core/autotests/e2ekeymanagertest.cpp
M  +16   -7    src/core/encryption/e2ekeymanager.cpp
M  +22   -17   src/core/localdatabase/autotests/e2edatabasetest.cpp

https://invent.kde.org/network/ruqola/-/commit/6b49f280221a7cdc80cb31b950067fb2800e7d49

diff --git a/src/core/autotests/e2ekeymanagertest.cpp b/src/core/autotests/e2ekeymanagertest.cpp
index 4e44ff5f5b..2a9eb3e0a1 100644
--- a/src/core/autotests/e2ekeymanagertest.cpp
+++ b/src/core/autotests/e2ekeymanagertest.cpp
@@ -16,6 +16,7 @@
 
 #include <QJsonObject>
 #include <QSignalSpy>
+#include <QStandardPaths>
 #include <QTemporaryDir>
 #include <QTest>
 
@@ -25,6 +26,7 @@ QTEST_GUILESS_MAIN(E2eKeyManagerTest)
 E2eKeyManagerTest::E2eKeyManagerTest(QObject *parent)
     : QObject{parent}
 {
+    QStandardPaths::setTestModeEnabled(true);
 }
 
 void E2eKeyManagerTest::shouldHaveDefaultValues()
@@ -59,6 +61,7 @@ void E2eKeyManagerTest::shouldSetNeedToDecryptStatusFromBase64StringPayload()
     QVERIFY(tempDir.isValid());
 
     RocketChatAccount account(tempDir.filePath(u"account.ini"_s));
+    account.setAccountName(u"test-e2ekeymanager"_s);
     account.settings()->setUserId("test-e2e-user-string"_ba);
 
     E2eKeyManager manager(&account);
@@ -74,11 +77,12 @@ void E2eKeyManagerTest::shouldSetNeedToDecryptStatusFromBase64StringPayload()
 
     QByteArray storedEncryptedPrivateKey;
     QByteArray storedPublicKey;
-    QVERIFY(account.localDatabaseManager()->e2EDatabase()->loadKey(u"test-e2e-user-string"_s, storedEncryptedPrivateKey, storedPublicKey));
+    QVERIFY(
+        account.localDatabaseManager()->e2EDatabase()->loadKey(account.accountName(), u"test-e2e-user-string"_s, storedEncryptedPrivateKey, storedPublicKey));
     QCOMPARE(storedEncryptedPrivateKey, encryptedPrivateKey);
     QCOMPARE(storedPublicKey, json["public_key"_L1].toString().toUtf8());
 
-    QVERIFY(account.localDatabaseManager()->e2EDatabase()->deleteKey(u"test-e2e-user-string"_s));
+    QVERIFY(account.localDatabaseManager()->e2EDatabase()->deleteKey(account.accountName(), u"test-e2e-user-string"_s));
 }
 
 void E2eKeyManagerTest::shouldSetNeedToDecryptStatusFromBinaryObjectPayload()
@@ -87,6 +91,7 @@ void E2eKeyManagerTest::shouldSetNeedToDecryptStatusFromBinaryObjectPayload()
     QVERIFY(tempDir.isValid());
 
     RocketChatAccount account(tempDir.filePath(u"account.ini"_s));
+    account.setAccountName(u"test-e2ekeymanager"_s);
     account.settings()->setUserId("test-e2e-user-binary"_ba);
 
     E2eKeyManager manager(&account);
@@ -104,11 +109,12 @@ void E2eKeyManagerTest::shouldSetNeedToDecryptStatusFromBinaryObjectPayload()
 
     QByteArray storedEncryptedPrivateKey;
     QByteArray storedPublicKey;
-    QVERIFY(account.localDatabaseManager()->e2EDatabase()->loadKey(u"test-e2e-user-binary"_s, storedEncryptedPrivateKey, storedPublicKey));
+    QVERIFY(
+        account.localDatabaseManager()->e2EDatabase()->loadKey(account.accountName(), u"test-e2e-user-binary"_s, storedEncryptedPrivateKey, storedPublicKey));
     QCOMPARE(storedEncryptedPrivateKey, encryptedPrivateKey);
     QCOMPARE(storedPublicKey, json["public_key"_L1].toString().toUtf8());
 
-    QVERIFY(account.localDatabaseManager()->e2EDatabase()->deleteKey(u"test-e2e-user-binary"_s));
+    QVERIFY(account.localDatabaseManager()->e2EDatabase()->deleteKey(account.accountName(), u"test-e2e-user-binary"_s));
 }
 
 void E2eKeyManagerTest::shouldHandleMissingOrMalformedServerKeys()
@@ -147,7 +153,7 @@ void E2eKeyManagerTest::shouldHandleMissingOrMalformedServerKeys()
 #endif
     }
 
-    QVERIFY(account.localDatabaseManager()->e2EDatabase()->deleteKey(u"test-e2e-user-generation"_s));
+    QVERIFY(account.localDatabaseManager()->e2EDatabase()->deleteKey(account.accountName(), u"test-e2e-user-generation"_s));
 }
 
 void E2eKeyManagerTest::shouldKeepGenerationStateAndAllowRetryWhenUploadFails()
@@ -180,7 +186,7 @@ void E2eKeyManagerTest::shouldKeepGenerationStateAndAllowRetryWhenUploadFails()
     QCOMPARE(uploadFailedSpy.count(), 2);
     QVERIFY(manager.hasPendingUploadFailure());
 
-    QVERIFY(account.localDatabaseManager()->e2EDatabase()->deleteKey(u"test-e2e-user-upload-retry"_s));
+    QVERIFY(account.localDatabaseManager()->e2EDatabase()->deleteKey(account.accountName(), u"test-e2e-user-upload-retry"_s));
 #endif
 }
 
@@ -206,9 +212,10 @@ void E2eKeyManagerTest::shouldDecodeEncryptionKeyWithValidPassword()
     QVERIFY(tempDir.isValid());
 
     RocketChatAccount account(tempDir.filePath(u"account.ini"_s));
-    account.settings()->setUserId("test-e2e-user-decode-ok"_ba);
-
+    account.setAccountName(u"test-e2ekeymanager"_s);
     const QString userId = u"test-e2e-user-decode-ok"_s;
+    account.settings()->setUserId(userId.toLatin1());
+
     const QString password = u"my-test-password"_s;
     const auto rsaKeyPair = EncryptionUtils::generateRSAKey();
     QVERIFY(!rsaKeyPair.privateKey.isEmpty());
@@ -218,7 +225,7 @@ void E2eKeyManagerTest::shouldDecodeEncryptionKeyWithValidPassword()
     const QByteArray encryptedPrivateKey = EncryptionUtils::encryptPrivateKey(rsaKeyPair.privateKey, masterKey);
     QVERIFY(!encryptedPrivateKey.isEmpty());
 
-    QVERIFY(account.localDatabaseManager()->e2EDatabase()->saveKey(userId, encryptedPrivateKey, rsaKeyPair.publicKey));
+    QVERIFY(account.localDatabaseManager()->e2EDatabase()->saveKey(account.accountName(), userId, encryptedPrivateKey, rsaKeyPair.publicKey));
 
     E2eKeyManager manager(&account);
     manager.setStatus(E2eKeyManager::Status::NeedToDecryptKey);
@@ -230,7 +237,7 @@ void E2eKeyManagerTest::shouldDecodeEncryptionKeyWithValidPassword()
     QCOMPARE(doneSpy.count(), 1);
     QCOMPARE(failedSpy.count(), 0);
 
-    QVERIFY(account.localDatabaseManager()->e2EDatabase()->deleteKey(userId));
+    QVERIFY(account.localDatabaseManager()->e2EDatabase()->deleteKey(account.accountName(), userId));
 #endif
 }
 
@@ -244,6 +251,7 @@ void E2eKeyManagerTest::shouldFailDecodeEncryptionKeyWithWrongPassword()
 
     RocketChatAccount account(tempDir.filePath(u"account.ini"_s));
     account.settings()->setUserId("test-e2e-user-decode-ko"_ba);
+    account.setAccountName(u"test-e2ekeymanager"_s);
 
     const QString userId = u"test-e2e-user-decode-ko"_s;
     const QString password = u"right-password"_s;
@@ -255,7 +263,7 @@ void E2eKeyManagerTest::shouldFailDecodeEncryptionKeyWithWrongPassword()
     const QByteArray encryptedPrivateKey = EncryptionUtils::encryptPrivateKey(rsaKeyPair.privateKey, masterKey);
     QVERIFY(!encryptedPrivateKey.isEmpty());
 
-    QVERIFY(account.localDatabaseManager()->e2EDatabase()->saveKey(userId, encryptedPrivateKey, rsaKeyPair.publicKey));
+    QVERIFY(account.localDatabaseManager()->e2EDatabase()->saveKey(account.accountName(), userId, encryptedPrivateKey, rsaKeyPair.publicKey));
 
     E2eKeyManager manager(&account);
     manager.setStatus(E2eKeyManager::Status::NeedToDecryptKey);
@@ -267,7 +275,7 @@ void E2eKeyManagerTest::shouldFailDecodeEncryptionKeyWithWrongPassword()
     QCOMPARE(doneSpy.count(), 0);
     QCOMPARE(failedSpy.count(), 1);
 
-    QVERIFY(account.localDatabaseManager()->e2EDatabase()->deleteKey(userId));
+    QVERIFY(account.localDatabaseManager()->e2EDatabase()->deleteKey(account.accountName(), userId));
 #endif
 }
 
diff --git a/src/core/encryption/e2ekeymanager.cpp b/src/core/encryption/e2ekeymanager.cpp
index 4e641a1a30..f3d43d3dd5 100644
--- a/src/core/encryption/e2ekeymanager.cpp
+++ b/src/core/encryption/e2ekeymanager.cpp
@@ -62,7 +62,7 @@ bool E2eKeyManager::decodeEncryptionKey(const QString &password)
 
     QByteArray encryptedPrivateKey;
     QByteArray publicKey;
-    if (!mAccount->localDatabaseManager()->e2EDatabase()->loadKey(userId, encryptedPrivateKey, publicKey)) {
+    if (!mAccount->localDatabaseManager()->e2EDatabase()->loadKey(mAccount->accountName(), userId, encryptedPrivateKey, publicKey)) {
         qCWarning(RUQOLA_ENCRYPTION_LOG) << "Unable to decode E2E key: no local encrypted private key found";
         setStatus(Status::NeedToDecryptKey);
         Q_EMIT failedDecodeEncryptionKey();
@@ -197,12 +197,15 @@ void E2eKeyManager::slotPasswordRead(QKeychain::Job *baseJob)
     auto job = qobject_cast<ReadPasswordJob *>(baseJob);
     Q_ASSERT(job);
     if (!job->error()) {
-        // TODO mPassword = job->textData();
+        const QString password = job->textData();
         qCDebug(RUQOLA_ENCRYPTION_LOG) << "OK, we have the password now";
-        // TODO Q_EMIT passwordAvailable();
+        if (!decodeEncryptionKey(password)) {
+            qCDebug(RUQOLA_ENCRYPTION_LOG) << "Impossible to decode encryption key";
+        }
     } else {
         qCWarning(RUQOLA_ENCRYPTION_LOG) << "We have an error during reading password " << job->errorString() << " Account name " << mAccount->accountName();
     }
+    Q_EMIT verifyKeyDone();
 }
 
 void E2eKeyManager::postponeDecryption()
@@ -257,9 +260,7 @@ void E2eKeyManager::fetchMyKeys()
     auto job = new RocketChatRestApi::FetchMyKeysJob(this);
     mAccount->restApi()->initializeRestApiJob(job);
     connect(job, &RocketChatRestApi::FetchMyKeysJob::fetchMyKeysDone, this, [this](const QJsonObject &json) {
-        qDebug() << " RocketChatRestApi::FetchMyKeysJob result " << json << "account name: " << mAccount->accountName();
         verifyExistingKey(json);
-        Q_EMIT verifyKeyDone();
     });
     if (!job->start()) {
         qCDebug(RUQOLA_ENCRYPTION_LOG) << "Impossible to start fetchmykeys job";
@@ -323,6 +324,7 @@ void E2eKeyManager::verifyExistingKey(const QJsonObject &json)
 
     if (!mAccount) {
         setStatus(Status::Unknown);
+        Q_EMIT verifyKeyDone();
         return;
     }
 
@@ -332,8 +334,9 @@ void E2eKeyManager::verifyExistingKey(const QJsonObject &json)
     if (!publicKey.isEmpty() && !encryptedPrivateKey.isEmpty()) {
         const QString userId = QString::fromLatin1(mAccount->settings()->userId());
         if (!userId.isEmpty()) {
-            (void)mAccount->localDatabaseManager()->e2EDatabase()->saveKey(userId, encryptedPrivateKey, publicKey.toUtf8());
+            (void)mAccount->localDatabaseManager()->e2EDatabase()->saveKey(mAccount->accountName(), userId, encryptedPrivateKey, publicKey.toUtf8());
         }
+        readPassword();
         setStatus(Status::NeedToDecryptKey);
         return;
     }
@@ -343,6 +346,7 @@ void E2eKeyManager::verifyExistingKey(const QJsonObject &json)
     if (userId.isEmpty()) {
         qCWarning(RUQOLA_ENCRYPTION_LOG) << "Unable to generate E2E keys: user id is empty";
         setStatus(Status::Unknown);
+        Q_EMIT verifyKeyDone();
         return;
     }
 
@@ -350,6 +354,7 @@ void E2eKeyManager::verifyExistingKey(const QJsonObject &json)
     if (mGeneratedPassword.isEmpty()) {
         qCWarning(RUQOLA_ENCRYPTION_LOG) << "Unable to generate E2E keys: random password generation failed";
         setStatus(Status::Unknown);
+        Q_EMIT verifyKeyDone();
         return;
     }
 
@@ -358,6 +363,7 @@ void E2eKeyManager::verifyExistingKey(const QJsonObject &json)
     if (masterKey.isEmpty() || rsaKeyPair.privateKey.isEmpty() || rsaKeyPair.publicKey.isEmpty()) {
         qCWarning(RUQOLA_ENCRYPTION_LOG) << "Unable to generate E2E keys: prerequisite generation failed";
         setStatus(Status::Unknown);
+        Q_EMIT verifyKeyDone();
         return;
     }
 
@@ -365,14 +371,17 @@ void E2eKeyManager::verifyExistingKey(const QJsonObject &json)
     if (encryptedGeneratedPrivateKey.isEmpty()) {
         qCWarning(RUQOLA_ENCRYPTION_LOG) << "Unable to generate E2E keys: private key encryption failed";
         setStatus(Status::Unknown);
+        Q_EMIT verifyKeyDone();
         return;
     }
 
-    (void)mAccount->localDatabaseManager()->e2EDatabase()->saveKey(userId, encryptedGeneratedPrivateKey, rsaKeyPair.publicKey);
+    (void)mAccount->localDatabaseManager()->e2EDatabase()->saveKey(mAccount->accountName(), userId, encryptedGeneratedPrivateKey, rsaKeyPair.publicKey);
 
+    qCDebug(RUQOLA_ENCRYPTION_LOG) << "rsaKeyPair.publicKey" << rsaKeyPair.publicKey << " encryptedGeneratedPrivateKey " << encryptedGeneratedPrivateKey;
     // Local key material is ready at this point, so keep generation state even if upload cannot start.
     setStatus(Status::NeedToGenerateKey);
     startUploadGeneratedKey(rsaKeyPair.publicKey, encryptedGeneratedPrivateKey);
+    Q_EMIT verifyKeyDone();
 #else
     setStatus(Status::Unknown);
 #endif
diff --git a/src/core/localdatabase/autotests/e2edatabasetest.cpp b/src/core/localdatabase/autotests/e2edatabasetest.cpp
index be8567230d..dda2a2177f 100644
--- a/src/core/localdatabase/autotests/e2edatabasetest.cpp
+++ b/src/core/localdatabase/autotests/e2edatabasetest.cpp
@@ -9,15 +9,19 @@
 #include <QTest>
 
 QTEST_GUILESS_MAIN(E2EDataBaseTest)
-
+using namespace Qt::Literals::StringLiterals;
 const auto testUser = QStringLiteral("testuser");
 const auto otherUser = QStringLiteral("otheruser");
+static QString accountName()
+{
+    return u"myAccount"_s;
+}
 using namespace Qt::Literals::StringLiterals;
 void E2EDataBaseTest::initTestCase()
 {
     E2EDataBase store;
-    QVERIFY(store.deleteKey(testUser));
-    QVERIFY(store.deleteKey(otherUser));
+    QVERIFY(store.deleteKey(accountName(), testUser));
+    QVERIFY(store.deleteKey(accountName(), otherUser));
 }
 
 void E2EDataBaseTest::shouldDefaultValues()
@@ -34,16 +38,17 @@ void E2EDataBaseTest::testSaveLoadDelete()
     const auto priv = rsaKeyPair.publicKey;
     const auto pub = rsaKeyPair.privateKey;
 
-    QVERIFY(store.saveKey(userId, priv, pub));
-    QVERIFY(store.hasKey(userId));
+    QVERIFY(store.saveKey(accountName(), userId, priv, pub));
+    QVERIFY(store.hasKey(accountName(), userId));
 
-    QByteArray loadedPriv, loadedPub;
-    QVERIFY(store.loadKey(userId, loadedPriv, loadedPub));
+    QByteArray loadedPriv;
+    QByteArray loadedPub;
+    QVERIFY(store.loadKey(accountName(), userId, loadedPriv, loadedPub));
     QCOMPARE(loadedPriv, priv);
     QCOMPARE(loadedPub, pub);
 
-    QVERIFY(store.deleteKey(userId));
-    QVERIFY(!store.hasKey(userId));
+    QVERIFY(store.deleteKey(accountName(), userId));
+    QVERIFY(!store.hasKey(accountName(), userId));
 }
 
 void E2EDataBaseTest::testOverwrite()
@@ -58,22 +63,22 @@ void E2EDataBaseTest::testOverwrite()
     const auto priv2 = rsaKeyPair2.privateKey;
     const auto pub2 = rsaKeyPair2.publicKey;
 
-    QVERIFY(store.saveKey(userId, priv1, pub1));
+    QVERIFY(store.saveKey(accountName(), userId, priv1, pub1));
     {
         QByteArray loadedPriv, loadedPub;
-        QVERIFY(store.loadKey(userId, loadedPriv, loadedPub));
+        QVERIFY(store.loadKey(accountName(), userId, loadedPriv, loadedPub));
         QCOMPARE(loadedPriv, priv1);
         QCOMPARE(loadedPub, pub1);
     }
 
-    QVERIFY(store.saveKey(userId, priv2, pub2));
+    QVERIFY(store.saveKey(accountName(), userId, priv2, pub2));
     {
         QByteArray loadedPriv, loadedPub;
-        QVERIFY(store.loadKey(userId, loadedPriv, loadedPub));
+        QVERIFY(store.loadKey(accountName(), userId, loadedPriv, loadedPub));
         QCOMPARE(loadedPriv, priv2);
         QCOMPARE(loadedPub, pub2);
     }
-    QVERIFY(store.deleteKey(userId));
+    QVERIFY(store.deleteKey(accountName(), userId));
 }
 
 void E2EDataBaseTest::testNonExistentKey()
@@ -81,9 +86,9 @@ void E2EDataBaseTest::testNonExistentKey()
     E2EDataBase store;
     const auto userId = otherUser;
     QByteArray priv, pub;
-    QVERIFY(!store.hasKey(userId));
-    QVERIFY(!store.loadKey(userId, priv, pub));
-    QVERIFY(store.deleteKey(userId));
+    QVERIFY(!store.hasKey(accountName(), userId));
+    QVERIFY(!store.loadKey(accountName(), userId, priv, pub));
+    QVERIFY(store.deleteKey(accountName(), userId));
 }
 
 #include "moc_e2edatabasetest.cpp"