// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // Coverage for the voice media plane as a standalone transport: two transports // on the loopback play out a LAN call - the listener, the parallel dial over // candidates, the channel set, the frame reassembly and the pin gate that // admits only identities the chat channel already pinned. No NetworkManager is // involved; the seams the owner would push in (pin gate, seat routing) are // wired straight here. TLS dials need real certificates, built from the same // deterministic seed IdentityCertificate uses everywhere else. #include "../network/VoiceTransport.h" #include "../core/security/IdentityCertificate.h" #include #include #include #include "../network/Protocol.h" using namespace koutnet; namespace { // Deterministic identity, like TlsChannelTest builds: same seed, same key. IdentityCertificate identity(int tag, const QString &id) { return buildIdentityCertificate(QByteArray(32, char(tag)), id); } } // namespace class VoiceTransportTest : public QObject { Q_OBJECT private Q_SLOTS: void portStartsUnboundAndFallsBackWhenTaken(); void plainLanCallCarriesMediaBothWays(); void plainCallClosesOnLastChannel(); void tlsAdmitsPinnedAndSilentlyRefusesStranger(); void tlsMediaRoundTrips(); void dialToADeadPeerEndsTheWindow(); void rejectsBadFramesBeforeBotheringTheWire(); void reconnectAfterHangUpStartsACleanCall(); void secondDialWhileCallUpIsANoop(); private: static void wireSeats(VoiceTransport &t, const QStringList &pinned) { // an owner files "id -> seat" and trusts only its pinned set. t.setSeatLookup([](const QString &id) { return QStringLiteral("seat_") + id; }); t.setPinGate([pinned](const QString &id) { return pinned.contains(id); }); t.setSeatFiler([](const QString &, const QString &) {}); } }; void VoiceTransportTest::portStartsUnboundAndFallsBackWhenTaken() { VoiceTransport t; QCOMPARE(t.port(), 0); QTcpServer holder; QVERIFY(holder.listen(QHostAddress::LocalHost, 0)); const quint16 taken = holder.serverPort(); QVERIFY2(t.listen(taken), "a taken port should fall back to an OS-picked one, not fail"); QVERIFY(t.port() != taken); QVERIFY(t.port() != 0); t.stop(); } void VoiceTransportTest::plainLanCallCarriesMediaBothWays() { VoiceTransport a; VoiceTransport b; QVERIFY(a.listen(0)); const QString target = QStringLiteral("127.0.0.1:%1").arg(a.port()); QSignalSpy aConnected(&a, &VoiceTransport::voiceConnected); QSignalSpy bConnected(&b, &VoiceTransport::voiceConnected); QSignalSpy aMedia(&a, &VoiceTransport::media); QSignalSpy bMedia(&b, &VoiceTransport::media); QVERIFY(b.connectTo(QStringLiteral("talker"), QStringList{target})); // the no-identity path admits the dial by address: the accept side keys the // channel on the socket's address, the dial side on the peer it dialed. QTRY_VERIFY_WITH_TIMEOUT(aConnected.count() == 1 && bConnected.count() == 1, 5000); const QByteArray frameA("hello, plain pipe"); QVERIFY(b.sendMedia(QStringLiteral("talker"), frameA)); QTRY_COMPARE_WITH_TIMEOUT(aMedia.count(), 1, 5000); QCOMPARE(aMedia.at(0).at(0).toString(), QStringLiteral("127.0.0.1")); QCOMPARE(aMedia.at(0).at(1).toByteArray(), frameA); const QByteArray frameB("and back"); QVERIFY(a.sendMedia(QStringLiteral("127.0.0.1"), frameB)); QTRY_COMPARE_WITH_TIMEOUT(bMedia.count(), 1, 5000); QCOMPARE(bMedia.at(0).at(0).toString(), QStringLiteral("talker")); QCOMPARE(bMedia.at(0).at(1).toByteArray(), frameB); } void VoiceTransportTest::plainCallClosesOnLastChannel() { VoiceTransport a; VoiceTransport b; QVERIFY(a.listen(0)); const QString target = QStringLiteral("127.0.0.1:%1").arg(a.port()); QSignalSpy aEnded(&a, &VoiceTransport::callEnded); QSignalSpy bEnded(&b, &VoiceTransport::callEnded); QVERIFY(b.connectTo(QStringLiteral("dream"), QStringList{target})); QTRY_VERIFY_WITH_TIMEOUT(a.isCallUp(QStringLiteral("127.0.0.1")) && b.isCallUp(QStringLiteral("dream")), 5000); b.hangUp(QStringLiteral("dream")); // the remote side sees the call end on the last channel; the side that // hung up knows it did, so it reports nothing. QTRY_VERIFY_WITH_TIMEOUT(aEnded.count() == 1 && !a.isCallUp(QStringLiteral("127.0.0.1")), 5000); QCOMPARE(aEnded.at(0).at(0).toString(), QStringLiteral("127.0.0.1")); QCOMPARE(bEnded.count(), 0); QVERIFY(!b.isCallUp(QStringLiteral("dream"))); } void VoiceTransportTest::tlsAdmitsPinnedAndSilentlyRefusesStranger() { const QString aliceId = QStringLiteral("vt_alice"); const QString bobId = QStringLiteral("vt_bob"); const QString strangerId = QStringLiteral("vt_stranger"); VoiceTransport alice; alice.setCredentials(identity(11, aliceId), /*overTls=*/true); wireSeats(alice, QStringList{bobId}); QVERIFY(alice.listen(0)); const QString target = QStringLiteral("127.0.0.1:%1").arg(alice.port()); QSignalSpy aliceConnected(&alice, &VoiceTransport::voiceConnected); VoiceTransport bob; bob.setCredentials(identity(22, bobId), /*overTls=*/true); QSignalSpy bobConnected(&bob, &VoiceTransport::voiceConnected); QVERIFY(bob.connectTo(QStringLiteral("aliceSeat"), QStringList{target})); QTRY_VERIFY_WITH_TIMEOUT(aliceConnected.count() == 1 && bobConnected.count() == 1, 10000); QCOMPARE(aliceConnected.at(0).at(0).toString(), QStringLiteral("seat_vt_bob")); QCOMPARE(bobConnected.at(0).at(0).toString(), QStringLiteral("aliceSeat")); QVERIFY(alice.isCallUp(QStringLiteral("seat_vt_bob"))); // a stranger with its own certificate is not in the pin set: the server // aborts the handshake in the encrypted() handler, so no channel forms and // the call Alice and Bob share is untouched. VoiceTransport stranger; stranger.setCredentials(identity(33, strangerId), /*overTls=*/true); QSignalSpy strangerEnded(&stranger, &VoiceTransport::callEnded); QVERIFY(stranger.connectTo(QStringLiteral("aliceSeat2"), QStringList{target})); QTRY_VERIFY_WITH_TIMEOUT(strangerEnded.count() == 1, 10000); // the server cut it QVERIFY(!alice.isCallUp(QStringLiteral("seat_vt_stranger"))); QCOMPARE(aliceConnected.count(), 1); // exactly the pinned caller got in QVERIFY(alice.isCallUp(QStringLiteral("seat_vt_bob"))); QVERIFY(bob.isCallUp(QStringLiteral("aliceSeat"))); } void VoiceTransportTest::tlsMediaRoundTrips() { const QString aliceId = QStringLiteral("vt_alice"); const QString bobId = QStringLiteral("vt_bob"); VoiceTransport alice; alice.setCredentials(identity(11, aliceId), /*overTls=*/true); wireSeats(alice, QStringList{bobId}); QVERIFY(alice.listen(0)); const QString target = QStringLiteral("127.0.0.1:%1").arg(alice.port()); VoiceTransport bob; bob.setCredentials(identity(22, bobId), /*overTls=*/true); QSignalSpy aliceMedia(&alice, &VoiceTransport::media); QSignalSpy bobMedia(&bob, &VoiceTransport::media); QVERIFY(bob.connectTo(QStringLiteral("aliceSeat"), QStringList{target})); QTRY_VERIFY_WITH_TIMEOUT(alice.isCallUp(QStringLiteral("seat_vt_bob")) && bob.isCallUp(QStringLiteral("aliceSeat")), 10000); const QByteArray frame("encrypted voice needs no plaintext"); QVERIFY(bob.sendMedia(QStringLiteral("aliceSeat"), frame)); QTRY_COMPARE_WITH_TIMEOUT(aliceMedia.count(), 1, 5000); QCOMPARE(aliceMedia.at(0).at(0).toString(), QStringLiteral("seat_vt_bob")); QCOMPARE(aliceMedia.at(0).at(1).toByteArray(), frame); QVERIFY(alice.sendMedia(QStringLiteral("seat_vt_bob"), frame)); QTRY_COMPARE_WITH_TIMEOUT(bobMedia.count(), 1, 5000); QCOMPARE(bobMedia.at(0).at(0).toString(), QStringLiteral("aliceSeat")); QCOMPARE(bobMedia.at(0).at(1).toByteArray(), frame); } void VoiceTransportTest::dialToADeadPeerEndsTheWindow() { QTcpServer ghost; // occupies a port, then dies so the dial is refused QVERIFY(ghost.listen(QHostAddress::LocalHost, 0)); const quint16 deadPort = ghost.serverPort(); ghost.close(); VoiceTransport t; QSignalSpy errors(&t, &VoiceTransport::errorOccurred); QSignalSpy ended(&t, &VoiceTransport::callEnded); QVERIFY(t.connectTo(QStringLiteral("gone"), QStringList{QStringLiteral("127.0.0.1:%1").arg(deadPort)})); QTRY_VERIFY_WITH_TIMEOUT(errors.count() == 1 && ended.count() == 1, 5000); QVERIFY(!t.isDialing(QStringLiteral("gone"))); QVERIFY(!t.isCallUp(QStringLiteral("gone"))); } void VoiceTransportTest::rejectsBadFramesBeforeBotheringTheWire() { VoiceTransport a; VoiceTransport b; QVERIFY(a.listen(0)); const QString target = QStringLiteral("127.0.0.1:%1").arg(a.port()); QVERIFY(b.connectTo(QStringLiteral("talk"), QStringList{target})); QTRY_VERIFY_WITH_TIMEOUT(b.isCallUp(QStringLiteral("talk")), 5000); // nothing to a peer nobody is talking to QVERIFY(!b.sendMedia(QStringLiteral("nobody"), QByteArray("hi"))); // empty and over-cap frames are refused before any socket is touched QVERIFY(!b.sendMedia(QStringLiteral("talk"), QByteArray())); QVERIFY(!b.sendMedia(QStringLiteral("talk"), QByteArray(int(protocol::kMaxVoiceFrameBytes) + 1, 'x'))); // an exactly-cap frame is the biggest legitimate one const QByteArray maxFrame(int(protocol::kMaxVoiceFrameBytes), 'm'); QVERIFY(b.sendMedia(QStringLiteral("talk"), maxFrame)); } void VoiceTransportTest::reconnectAfterHangUpStartsACleanCall() { VoiceTransport a; VoiceTransport b; QVERIFY(a.listen(0)); const QString target = QStringLiteral("127.0.0.1:%1").arg(a.port()); QSignalSpy aConnected(&a, &VoiceTransport::voiceConnected); QSignalSpy bEnded(&b, &VoiceTransport::callEnded); QVERIFY(b.connectTo(QStringLiteral("twice"), QStringList{target})); QTRY_VERIFY_WITH_TIMEOUT(aConnected.count() == 1, 5000); a.hangUp(QStringLiteral("127.0.0.1")); QTRY_VERIFY_WITH_TIMEOUT(!a.isCallUp(QStringLiteral("127.0.0.1")) && bEnded.count() == 1 && !b.isCallUp(QStringLiteral("twice")), 5000); // the transport is not spent: the same call can be placed and connected again. QVERIFY(b.connectTo(QStringLiteral("twice"), QStringList{target})); QTRY_VERIFY_WITH_TIMEOUT(aConnected.count() == 2 && a.isCallUp(QStringLiteral("127.0.0.1")), 5000); QCOMPARE(b.isCallUp(QStringLiteral("twice")), true); } void VoiceTransportTest::secondDialWhileCallUpIsANoop() { VoiceTransport a; VoiceTransport b; QVERIFY(a.listen(0)); const QString target = QStringLiteral("127.0.0.1:%1").arg(a.port()); QSignalSpy aConnected(&a, &VoiceTransport::voiceConnected); QVERIFY(b.connectTo(QStringLiteral("dupe"), QStringList{target})); QTRY_VERIFY_WITH_TIMEOUT(aConnected.count() == 1, 5000); // a call already up (or a dial already in flight) is reported as in flight, // steering the caller away from a second parallel dial. QVERIFY(b.connectTo(QStringLiteral("dupe"), QStringList{target})); QTRY_VERIFY_WITH_TIMEOUT(aConnected.count() == 1, 2000); QVERIFY(b.isCallUp(QStringLiteral("dupe"))); } QTEST_GUILESS_MAIN(VoiceTransportTest) #include "VoiceTransportTest.moc"