// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // Straight unit coverage for GroupKeyStore: the pin-or-fallback rule both // message directions share, the SecretStore round trip, and mint-once. The // store is forced in-memory first, so nothing reaches a real wallet. #include "../network/GroupKeyStore.h" #include "../core/security/SecretStore.h" #include #include using namespace koutnet; class GroupKeyStoreTest : public QObject { Q_OBJECT private Q_SLOTS: void initTestCase(); void passphraseFallsBackForEverything(); void pinnedKeyBeatsPassphrase(); void clearingKeyFallsBackToPassphrase(); void setKeyPersistsAcrossInstances(); void ensureKeyMintsOnceAndPersists(); void cleanupTestCase(); }; void GroupKeyStoreTest::initTestCase() { SecretStore::setInMemoryOnly(true); QVERIFY2(SecretStore::isInMemoryOnly(), "SecretStore is talking to the real store; this run would write test keys " "into it"); QVERIFY(SecretStore::isAvailable()); } void GroupKeyStoreTest::passphraseFallsBackForEverything() { GroupKeyStore store; QVERIFY(store.passphrase().isEmpty()); // With nothing set there is no key at all, so callers refuse to send // rather than send in the clear. QVERIFY(store.cipherKeyFor(QStringLiteral("g-none")).isEmpty()); store.setPassphrase(QStringLiteral("shared-phrase")); QCOMPARE(store.passphrase(), QStringLiteral("shared-phrase")); // the shared broadcast, and a group that was never pinned a key. QCOMPARE(store.cipherKeyFor(QString()), QStringLiteral("shared-phrase")); QCOMPARE(store.cipherKeyFor(QStringLiteral("g-unknown")), QStringLiteral("shared-phrase")); QVERIFY(store.keyFor(QStringLiteral("g-unknown")).isEmpty()); } void GroupKeyStoreTest::pinnedKeyBeatsPassphrase() { GroupKeyStore store; store.setPassphrase(QStringLiteral("shared-phrase")); store.setKey(QStringLiteral("g1"), QStringLiteral("pinned-1")); QCOMPARE(store.keyFor(QStringLiteral("g1")), QStringLiteral("pinned-1")); // both directions resolve to the pinned key for this group... QCOMPARE(store.cipherKeyFor(QStringLiteral("g1")), QStringLiteral("pinned-1")); // ...and to the passphrase everywhere else, including the shared chat. QCOMPARE(store.cipherKeyFor(QStringLiteral("g2")), QStringLiteral("shared-phrase")); QCOMPARE(store.cipherKeyFor(QString()), QStringLiteral("shared-phrase")); } void GroupKeyStoreTest::clearingKeyFallsBackToPassphrase() { GroupKeyStore store; store.setPassphrase(QStringLiteral("shared-phrase")); store.setKey(QStringLiteral("g1"), QStringLiteral("pinned-1")); store.removeKey(QStringLiteral("g1")); QVERIFY(store.keyFor(QStringLiteral("g1")).isEmpty()); // a dropped pin means the group goes back to the shared passphrase, never // to cleartext. QCOMPARE(store.cipherKeyFor(QStringLiteral("g1")), QStringLiteral("shared-phrase")); } void GroupKeyStoreTest::setKeyPersistsAcrossInstances() { GroupKeyStore first; first.setKey(QStringLiteral("g-persist"), QStringLiteral("k-abc")); // a second store, with a cold cache: the pin came from the secret store. GroupKeyStore second; QCOMPARE(second.keyFor(QStringLiteral("g-persist")), QStringLiteral("k-abc")); QCOMPARE(second.cipherKeyFor(QStringLiteral("g-persist")), QStringLiteral("k-abc")); second.removeKey(QStringLiteral("g-persist")); GroupKeyStore third; QVERIFY2(third.keyFor(QStringLiteral("g-persist")).isEmpty(), "a removed pin was still readable"); } void GroupKeyStoreTest::ensureKeyMintsOnceAndPersists() { GroupKeyStore first; const QString minted = first.ensureKey(QStringLiteral("g-mint")); QCOMPARE(minted.size(), 64); QVERIFY2(QRegularExpression(QStringLiteral("^[0-9a-f]{64}$")).match(minted).hasMatch(), "a minted key is 32 random bytes as hex"); // pinned already: the next invite must ride the same key, not a new one. QCOMPARE(first.ensureKey(QStringLiteral("g-mint")), minted); GroupKeyStore second; QCOMPARE(second.ensureKey(QStringLiteral("g-mint")), minted); second.removeKey(QStringLiteral("g-mint")); GroupKeyStore third; QVERIFY(third.keyFor(QStringLiteral("g-mint")).isEmpty()); } void GroupKeyStoreTest::cleanupTestCase() { SecretStore::setInMemoryOnly(true); } QTEST_GUILESS_MAIN(GroupKeyStoreTest) #include "GroupKeyStoreTest.moc"