// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // Unit coverage for the half of StunClient that owns the reflexive state: the // store-on-success / clear-on-failure rule and the string the call offers are // built from. The socket flight and the codec are deliberately not here - a // flight needs a server, and the codec is already pinned in NetworkManagerTest. // The test drives the two outcome seams the socket handler is built on. #include "../network/StunClient.h" #include #include using namespace koutnet; // The sockets hide behind setReflexive()/clearReflexive(); this subclass // exposes the seam to the test so nothing needs a network. class ExposedStun : public StunClient { public: using StunClient::clearReflexive; using StunClient::setReflexive; using StunClient::StunClient; }; class StunClientTest : public QObject { Q_OBJECT private Q_SLOTS: void reflexiveStartsEmpty(); void successStoresAndEmits(); void failureClearsAndEmits(); void clearingAnEmptyCacheIsSilent(); void reconfigurationKeepsTheLastAnswer(); void serverDefaults(); }; void StunClientTest::reflexiveStartsEmpty() { StunClient client; QVERIFY2(client.reflexiveAddress().isEmpty(), "no flight has succeeded, so there is nothing to publish in a call offer"); } void StunClientTest::successStoresAndEmits() { ExposedStun client; QSignalSpy changed(&client, &StunClient::reflexiveChanged); client.setReflexive(QStringLiteral("1.2.3.4"), 54321); QCOMPARE(client.reflexiveAddress(), QStringLiteral("1.2.3.4:54321")); QCOMPARE(changed.count(), 1); QCOMPARE(changed.at(0).at(0).toString(), QStringLiteral("1.2.3.4:54321")); // a second flight that answers moves the mapping along. client.setReflexive(QStringLiteral("5.6.7.8"), 54321); QCOMPARE(client.reflexiveAddress(), QStringLiteral("5.6.7.8:54321")); QCOMPARE(changed.count(), 2); } void StunClientTest::failureClearsAndEmits() { ExposedStun client; client.setReflexive(QStringLiteral("1.2.3.4"), 54321); QSignalSpy changed(&client, &StunClient::reflexiveChanged); client.clearReflexive(); QVERIFY2(client.reflexiveAddress().isEmpty(), "an unanswered flight must not leave a stale candidate in the call offer"); QCOMPARE(changed.count(), 1); QCOMPARE(changed.at(0).at(0).toString(), QString()); } void StunClientTest::clearingAnEmptyCacheIsSilent() { ExposedStun client; QSignalSpy changed(&client, &StunClient::reflexiveChanged); client.clearReflexive(); QCOMPARE(changed.count(), 0); QVERIFY(client.reflexiveAddress().isEmpty()); } void StunClientTest::reconfigurationKeepsTheLastAnswer() { ExposedStun client(QStringLiteral("stun.l.google.com"), 19302); client.setReflexive(QStringLiteral("9.9.9.9"), 19302); client.setServer(QStringLiteral("stun.example.net"), 3478); QCOMPARE(client.server(), QStringLiteral("stun.example.net")); QCOMPARE(client.port(), 3478); QCOMPARE(client.reflexiveAddress(), QStringLiteral("9.9.9.9:19302")); // wrenching the configuration is not the same as the mapping having moved: // the offer keeps the last real answer until the next flight speaks. } void StunClientTest::serverDefaults() { StunClient client; QCOMPARE(client.server(), QStringLiteral("stun.l.google.com")); QCOMPARE(client.port(), kStunDefaultPort); QCOMPARE(client.port(), 19302); } QTEST_GUILESS_MAIN(StunClientTest) #include "StunClientTest.moc"