// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // The frame router on its own: what a decoded frame becomes, with the sockets, // the crypto and the key stores reduced to seams. #include "../network/FrameRouter.h" #include "../network/Protocol.h" #include #include #include using namespace koutnet; // Everything route() can reach back out through is fake here: our own // addresses, the rate gate, the presence sink, the busy reply, the group-key // store and the async decrypt, which is invoked synchronously so a test's // expectation does not race the worker thread. class RouterHarness { public: RouterHarness() { router.setRateGate([this](const QString &) { return rateOk; }); router.setOwnNetwork([this] { return mine; }); router.setPresenceSink([this](const QString &host, const QJsonObject &msg) { Q_UNUSED(msg) arrivedPresence << host; }); router.setBusyReply([this](const QString &toIp) { busyTo << toIp; }); router.setKeySeams( [this](const QString &gid) { return cipherKeys.value(gid); }, [this](const QString &gid) { return pinnedKeys.value(gid); }, [this](const QString &gid, const QString &key) { storedKeys << (gid + QStringLiteral("=") + key); }); router.setCrypto( [this] { return hasCrypto; }, [this](const QString &cipher, const QString &key, const std::function &done) { Q_UNUSED(key) ++decryptRuns; done(cipher + QStringLiteral("/opened"), true); }); } FrameRouter router; QSet mine{QStringLiteral("10.0.0.5")}; bool rateOk = true; QStringList arrivedPresence; QStringList busyTo; QHash cipherKeys; QHash pinnedKeys; QStringList storedKeys; bool hasCrypto = false; int decryptRuns = 0; }; class FrameRouterTest : public QObject { Q_OBJECT private Q_SLOTS: void aMessageClaimingOurOwnAddressIsDropped(); void aBroadcastArrivingFromOurOwnAddressIsDropped(); void aPresenceFrameReachesTheSink(); void aClearMessageIsHandedOnUnopened(); void aSealedMessageIsOpenedThroughTheSeam(); void aChannelPrivateMessageIsNeverDecryptedAgain(); void aPrivateFrameTargetsUsAlone(); void aCallAcceptAnswersOnlyTheRequestWeSent(); void aCallAcceptIsSpentAfterTheSlot(); void aCallRequestWhileWeAreBusyGetsTheBusyReply(); void aCallEndCancelsTheRingOnly(); void aFileMetaFramePassesThroughWhole(); void aGroupInvitePinsOnFirstSight(); void aConflictingGroupInviteSurfacesInsteadOfOverwriting(); void anIdenticalGroupReinviteIsSilent(); void aTypingFrameFires(); void aRateLimitedHostIsDroppedWhole(); }; // A message claiming our own address, and one arriving from our own broadcast, // are both echoes: the sender was us. Anything a peer could believably send // has these fields, so the guard reads before the type. void FrameRouterTest::aMessageClaimingOurOwnAddressIsDropped() { RouterHarness h; QSignalSpy messages(&h.router, &FrameRouter::message); QJsonObject o; o[QStringLiteral("type")] = protocol::kMsgChat; o[QStringLiteral("from_ip")] = QStringLiteral("10.0.0.5"); h.router.route(QStringLiteral("10.0.0.9"), o); QCOMPARE(messages.count(), 0); } void FrameRouterTest::aBroadcastArrivingFromOurOwnAddressIsDropped() { RouterHarness h; QSignalSpy messages(&h.router, &FrameRouter::message); QJsonObject o; o[QStringLiteral("type")] = protocol::kMsgChat; h.router.route(QStringLiteral("10.0.0.5"), o); QCOMPARE(messages.count(), 0); } // presence is the one frame that does not trip the echo guard, and it is // handed to the sink with the arrival host it was stamped with. void FrameRouterTest::aPresenceFrameReachesTheSink() { RouterHarness h; QJsonObject o; o[QStringLiteral("type")] = protocol::kMsgPresence; h.router.route(QStringLiteral("10.0.0.9"), o); QVERIFY2(h.arrivedPresence == QStringList{QStringLiteral("10.0.0.9")}, "presence did not reach the sink"); } // A routed chat frame without a crypto seam is handed on as-is: a station that // never configured keys talks cleartext. void FrameRouterTest::aClearMessageIsHandedOnUnopened() { RouterHarness h; QSignalSpy messages(&h.router, &FrameRouter::message); QJsonObject o; o[QStringLiteral("type")] = protocol::kMsgChat; o[QStringLiteral("text")] = QStringLiteral("hello"); h.router.route(QStringLiteral("10.0.0.9"), o); QCOMPARE(messages.count(), 1); const QJsonObject got = messages.at(0).at(0).toJsonObject(); QCOMPARE(got.value(QStringLiteral("text")).toString(), QStringLiteral("hello")); } // With the crypto seam present, a sealed text goes through the decrypt seam // under the key the cipher-key seam names, and the opened text lands. void FrameRouterTest::aSealedMessageIsOpenedThroughTheSeam() { RouterHarness h; h.hasCrypto = true; h.cipherKeys.insert(QStringLiteral("g1"), QStringLiteral("k1")); QSignalSpy messages(&h.router, &FrameRouter::message); QJsonObject o; o[QStringLiteral("type")] = protocol::kMsgGroup; o[QStringLiteral("gid")] = QStringLiteral("g1"); o[QStringLiteral("text")] = QStringLiteral("cipherblob"); h.router.route(QStringLiteral("10.0.0.9"), o); QCOMPARE(h.decryptRuns, 1); QCOMPARE(messages.count(), 1); const QJsonObject got = messages.at(0).at(0).toJsonObject(); QCOMPARE(got.value(QStringLiteral("text")).toString(), QStringLiteral("cipherblob/opened")); } // A private chat body that came over the TLS channel already arrived decrypted // and authenticated: the gid-less via_tls case is handed on without ever // asking the decrypt seam to touch it. void FrameRouterTest::aChannelPrivateMessageIsNeverDecryptedAgain() { RouterHarness h; h.hasCrypto = true; QSignalSpy messages(&h.router, &FrameRouter::message); QJsonObject o; o[QStringLiteral("type")] = protocol::kMsgChat; o[QStringLiteral("via_tls")] = true; o[QStringLiteral("text")] = QStringLiteral("already open"); h.router.route(QStringLiteral("10.0.0.9"), o); QCOMPARE(h.decryptRuns, 0); QCOMPARE(messages.count(), 1); const QJsonObject got = messages.at(0).at(0).toJsonObject(); QCOMPARE(got.value(QStringLiteral("text")).toString(), QStringLiteral("already open")); } // Only a private addressed to one of our addresses opens; one claiming to be // for some third host is not ours to open. void FrameRouterTest::aPrivateFrameTargetsUsAlone() { RouterHarness h; h.mine = {QStringLiteral("10.0.0.5"), QStringLiteral("192.168.1.4")}; QSignalSpy messages(&h.router, &FrameRouter::message); QJsonObject ours; ours[QStringLiteral("type")] = protocol::kMsgPrivate; ours[QStringLiteral("to")] = QStringLiteral("192.168.1.4"); ours[QStringLiteral("text")] = QStringLiteral("mine"); h.router.route(QStringLiteral("10.0.0.9"), ours); QJsonObject theirs; theirs[QStringLiteral("type")] = protocol::kMsgPrivate; theirs[QStringLiteral("to")] = QStringLiteral("192.168.1.99"); theirs[QStringLiteral("text")] = QStringLiteral("not mine"); h.router.route(QStringLiteral("10.0.0.9"), theirs); QCOMPARE(messages.count(), 1); QCOMPARE(messages.at(0).at(0).toJsonObject().value(QStringLiteral("text")).toString(), QStringLiteral("mine")); } // A call_accept from a peer we never called is a forged accept: somebody with // a channel trying to open our microphone. Without a request in the pending // bucket it is dropped without a sound. With one, it lands. void FrameRouterTest::aCallAcceptAnswersOnlyTheRequestWeSent() { RouterHarness h; QSignalSpy accepted(&h.router, &FrameRouter::callAccepted); QJsonObject o; o[QStringLiteral("type")] = protocol::kMsgCallAccept; o[QStringLiteral("username")] = QStringLiteral("peer"); h.router.route(QStringLiteral("10.0.0.9"), o); QCOMPARE(accepted.count(), 0); h.router.openRequest(QStringLiteral("10.0.0.9")); h.router.route(QStringLiteral("10.0.0.9"), o); QCOMPARE(accepted.count(), 1); } // The pending slot is spent: a second accept for the same call is stale. void FrameRouterTest::aCallAcceptIsSpentAfterTheSlot() { RouterHarness h; QSignalSpy accepted(&h.router, &FrameRouter::callAccepted); QJsonObject o; o[QStringLiteral("type")] = protocol::kMsgCallAccept; h.router.openRequest(QStringLiteral("10.0.0.9")); h.router.route(QStringLiteral("10.0.0.9"), o); h.router.route(QStringLiteral("10.0.0.9"), o); QCOMPARE(accepted.count(), 1); } // A call_req while a call is live gets the busy reply instead of a second // ringing window; idle, it rings. void FrameRouterTest::aCallRequestWhileWeAreBusyGetsTheBusyReply() { RouterHarness h; QSignalSpy requests(&h.router, &FrameRouter::callRequest); QJsonObject o; o[QStringLiteral("type")] = protocol::kMsgCallReq; o[QStringLiteral("username")] = QStringLiteral("peer"); h.router.route(QStringLiteral("10.0.0.9"), o); QCOMPARE(requests.count(), 1); h.router.setActiveCalls({QStringLiteral("10.0.0.9"), QStringLiteral("10.0.0.7")}); h.router.route(QStringLiteral("10.0.0.9"), o); QCOMPARE(requests.count(), 1); QVERIFY2(h.busyTo == QStringList{QStringLiteral("10.0.0.9")}, "busy reply not posted to the caller"); } // call_end cancels a ring we were showing; against a peer with no ring, a // request or a live call it is nothing. void FrameRouterTest::aCallEndCancelsTheRingOnly() { RouterHarness h; QSignalSpy ended(&h.router, &FrameRouter::callEnded); QJsonObject end; end[QStringLiteral("type")] = protocol::kMsgCallEnd; h.router.route(QStringLiteral("10.0.0.7"), end); // bystander: nothing QCOMPARE(ended.count(), 0); QJsonObject req; req[QStringLiteral("type")] = protocol::kMsgCallReq; h.router.route(QStringLiteral("10.0.0.9"), req); // rings us h.router.route(QStringLiteral("10.0.0.9"), end); // cancels the ring QCOMPARE(ended.count(), 1); } // The file meta frame is routing furniture: it passes through whole. void FrameRouterTest::aFileMetaFramePassesThroughWhole() { RouterHarness h; QSignalSpy metas(&h.router, &FrameRouter::fileMeta); QJsonObject o; o[QStringLiteral("type")] = protocol::kMsgFileMeta; o[QStringLiteral("tid")] = QStringLiteral("tid-1"); o[QStringLiteral("size")] = 42; h.router.route(QStringLiteral("10.0.0.9"), o); QCOMPARE(metas.count(), 1); // the host comes first, so the receiving side can bind the transfer to it QCOMPARE(metas.at(0).at(0).toString(), QStringLiteral("10.0.0.9")); const QJsonObject got = metas.at(0).at(1).toJsonObject(); QCOMPARE(got.value(QStringLiteral("tid")).toString(), QStringLiteral("tid-1")); } // First sight of a group pins the invitation's key through the store seam; the // same key again is an idempotent re-invite and a different key is refused to // the UI rather than overwritten. void FrameRouterTest::aGroupInvitePinsOnFirstSight() { RouterHarness h; QSignalSpy invites(&h.router, &FrameRouter::groupInvite); QSignalSpy confirmations(&h.router, &FrameRouter::groupKeyConfirmationRequired); QJsonObject inv; inv[QStringLiteral("type")] = protocol::kMsgGroupInv; inv[QStringLiteral("gid")] = QStringLiteral("g1"); inv[QStringLiteral("gname")] = QStringLiteral("crew"); inv[QStringLiteral("key")] = QStringLiteral("k1"); h.router.route(QStringLiteral("10.0.0.9"), inv); QCOMPARE(invites.count(), 1); QCOMPARE(confirmations.count(), 0); QCOMPARE(h.storedKeys, QStringList{QStringLiteral("g1=k1")}); } void FrameRouterTest::aConflictingGroupInviteSurfacesInsteadOfOverwriting() { RouterHarness h; h.pinnedKeys.insert(QStringLiteral("g1"), QStringLiteral("pinned")); QSignalSpy invites(&h.router, &FrameRouter::groupInvite); QSignalSpy confirmations(&h.router, &FrameRouter::groupKeyConfirmationRequired); QJsonObject inv; inv[QStringLiteral("type")] = protocol::kMsgGroupInv; inv[QStringLiteral("gid")] = QStringLiteral("g1"); inv[QStringLiteral("key")] = QStringLiteral("intruder"); h.router.route(QStringLiteral("10.0.0.9"), inv); QCOMPARE(confirmations.count(), 1); QCOMPARE(confirmations.at(0).at(0).toString(), QStringLiteral("g1")); QCOMPARE(invites.count(), 1); QVERIFY2(h.storedKeys.isEmpty(), "a conflicting key was written"); } void FrameRouterTest::anIdenticalGroupReinviteIsSilent() { RouterHarness h; h.pinnedKeys.insert(QStringLiteral("g1"), QStringLiteral("k1")); QSignalSpy confirmations(&h.router, &FrameRouter::groupKeyConfirmationRequired); QJsonObject inv; inv[QStringLiteral("type")] = protocol::kMsgGroupInv; inv[QStringLiteral("gid")] = QStringLiteral("g1"); inv[QStringLiteral("key")] = QStringLiteral("k1"); h.router.route(QStringLiteral("10.0.0.9"), inv); QCOMPARE(confirmations.count(), 0); QVERIFY2(h.storedKeys.isEmpty(), "an identical key was rewritten"); } void FrameRouterTest::aTypingFrameFires() { RouterHarness h; QSignalSpy typing(&h.router, &FrameRouter::typing); QJsonObject o; o[QStringLiteral("type")] = protocol::kMsgTyping; o[QStringLiteral("username")] = QStringLiteral("anna"); h.router.route(QStringLiteral("10.0.0.9"), o); QCOMPARE(typing.count(), 1); QCOMPARE(typing.at(0).at(0).toString(), QStringLiteral("anna")); QCOMPARE(typing.at(0).at(2).toString(), QStringLiteral("10.0.0.9")); } // A host the rate gate refuses is dropped before any of its types decide: a // flood does not even reach the sink. void FrameRouterTest::aRateLimitedHostIsDroppedWhole() { RouterHarness h; h.rateOk = false; QSignalSpy messages(&h.router, &FrameRouter::message); QJsonObject o; o[QStringLiteral("type")] = protocol::kMsgChat; h.router.route(QStringLiteral("10.0.1.1"), o); QCOMPARE(messages.count(), 0); QVERIFY2(h.arrivedPresence.isEmpty(), "presence from a refused host reached the sink"); } QTEST_GUILESS_MAIN(FrameRouterTest) #include "FrameRouterTest.moc"