// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // Straight unit coverage for the avatar exchange: a mock channel writer and a // fake seat set stand in for the transport, so the dedup, the sha-carrying // frame and the empty data frame (="the avatar went away") are pinned without // a socket being opened. #include "../network/AvatarExchange.h" #include "../network/Protocol.h" #include #include #include #include #include #include #include using namespace koutnet; class AvatarExchangeTest : public QObject { Q_OBJECT private Q_SLOTS: void setAvatarPushesOnceToNewSeat(); void unchangedAvatarIsNotResent(); void changingAvatarResends(); void onFrameParksBytesAndEmitsFileUrl(); void onFrameEmptyDataClears(); void onFrameOversizeIsIgnored(); }; // The fixture drives the exchange through two fake seams. class Harness { public: Harness() : exchange( [this](const QString &targetAddress, const QJsonObject &payload) { sent.push_back({targetAddress, payload}); return true; }, [this]() { return QSet(seats.cbegin(), seats.cend()); }, [this](const QString &host, const QString &avatarSource) { peerInfo[host] = avatarSource; }) { } struct Sending { QString address; QJsonObject payload; }; QVector sent; // every frame the writer was asked to place QSet seats; QHash peerInfo; AvatarExchange exchange; }; void AvatarExchangeTest::setAvatarPushesOnceToNewSeat() { Harness h; // no seats yet: the picture must not reach anybody until a channel is up. QTemporaryFile image; QVERIFY(image.open()); image.write(QByteArrayLiteral("\x89PNG\r\n\x1a\navatar-bytes")); image.flush(); image.close(); h.exchange.setAvatar(image.fileName()); QVERIFY(h.sent.isEmpty()); // a new seat authenticates (one of its channels only just came up). h.seats.insert(QStringLiteral("127.0.0.1")); h.exchange.onPeerAuthenticated(QStringLiteral("127.0.0.1")); QCOMPARE(h.sent.size(), 1); QCOMPARE(h.sent.at(0).address, QStringLiteral("127.0.0.1")); QCOMPARE(h.sent.at(0).payload.value(QStringLiteral("type")).toString(), QString(protocol::kMsgAvatar)); QCOMPARE(h.sent.at(0).payload.value(QStringLiteral("sha")).toString().size(), 64); QCOMPARE(QByteArray::fromBase64(h.sent.at(0).payload.value(QStringLiteral("data")).toString().toLatin1()), QByteArrayLiteral("\x89PNG\r\n\x1a\navatar-bytes")); } void AvatarExchangeTest::unchangedAvatarIsNotResent() { Harness h; QTemporaryFile image; QVERIFY(image.open()); image.write(QByteArrayLiteral("same-picture")); image.flush(); image.close(); h.exchange.setAvatar(image.fileName()); h.exchange.onPeerAuthenticated(QStringLiteral("10.0.0.5")); const int pushed = h.sent.size(); QVERIFY(pushed == 1); // a second channel-up for the same seat must not push the same sha again. h.exchange.onPeerAuthenticated(QStringLiteral("10.0.0.5")); h.exchange.setAvatar(image.fileName()); QCOMPARE(h.sent.size(), pushed); } void AvatarExchangeTest::changingAvatarResends() { Harness h; h.seats.insert(QStringLiteral("192.168.0.2")); QTemporaryFile first; QVERIFY(first.open()); first.write(QByteArrayLiteral("first-face")); first.flush(); first.close(); h.exchange.setAvatar(first.fileName()); h.exchange.onPeerAuthenticated(QStringLiteral("192.168.0.2")); QCOMPARE(h.sent.size(), 1); const QString firstSha = h.sent.at(0).payload.value(QStringLiteral("sha")).toString(); QTemporaryFile second; QVERIFY(second.open()); second.write(QByteArrayLiteral("second-face")); second.flush(); second.close(); h.exchange.setAvatar(second.fileName()); QCOMPARE(h.sent.size(), 2); const QString secondSha = h.sent.at(1).payload.value(QStringLiteral("sha")).toString(); QVERIFY(secondSha != firstSha); QCOMPARE(QByteArray::fromBase64(h.sent.at(1).payload.value(QStringLiteral("data")).toString().toLatin1()), QByteArrayLiteral("second-face")); } void AvatarExchangeTest::onFrameParksBytesAndEmitsFileUrl() { // isolate the written avatars from the real app data dir QStandardPaths::setTestModeEnabled(true); Harness h; QSignalSpy avatars(&h.exchange, &AvatarExchange::peerAvatar); QJsonObject frame; frame[QStringLiteral("type")] = protocol::kMsgAvatar; frame[QStringLiteral("sha")] = QString::fromLatin1(QCryptographicHash::hash(QByteArrayLiteral("peer-face"), QCryptographicHash::Sha256).toHex()); frame[QStringLiteral("ext")] = QStringLiteral("png"); frame[QStringLiteral("data")] = QString::fromLatin1(QByteArrayLiteral("peer-face").toBase64()); h.exchange.onFrame(QStringLiteral("172.16.0.9"), frame); QCOMPARE(avatars.count(), 1); QCOMPARE(avatars.at(0).at(0).toString(), QStringLiteral("172.16.0.9")); const QString source = avatars.at(0).at(1).toString(); QVERIFY2(source.startsWith(QStringLiteral("file://")), qPrintable(source)); QFile mirror(source.mid(7)); QVERIFY(mirror.open(QIODevice::ReadOnly)); QCOMPARE(mirror.readAll(), QByteArrayLiteral("peer-face")); // the peer info recorder filed it back under the peer's chat id. QCOMPARE(h.peerInfo.value(QStringLiteral("172.16.0.9")), source); } void AvatarExchangeTest::onFrameEmptyDataClears() { QStandardPaths::setTestModeEnabled(true); Harness h; QSignalSpy avatars(&h.exchange, &AvatarExchange::peerAvatar); QJsonObject gone; gone[QStringLiteral("type")] = protocol::kMsgAvatar; gone[QStringLiteral("sha")] = QString(); gone[QStringLiteral("data")] = QString(); h.exchange.onFrame(QStringLiteral("172.16.0.9"), gone); QCOMPARE(avatars.count(), 1); QVERIFY2(avatars.at(0).at(1).toString().isEmpty(), "an empty data frame means the avatar went away"); QVERIFY(h.peerInfo.value(QStringLiteral("172.16.0.9")).isEmpty()); } void AvatarExchangeTest::onFrameOversizeIsIgnored() { Harness h; QSignalSpy avatars(&h.exchange, &AvatarExchange::peerAvatar); QJsonObject huge; huge[QStringLiteral("type")] = protocol::kMsgAvatar; huge[QStringLiteral("sha")] = QStringLiteral("a").repeated(64); huge[QStringLiteral("data")] = QString::fromLatin1(QByteArray(5 * 1024 * 1024, 'x').toBase64()); h.exchange.onFrame(QStringLiteral("172.16.0.9"), huge); QCOMPARE(avatars.count(), 0); QVERIFY(h.peerInfo.isEmpty()); } QTEST_GUILESS_MAIN(AvatarExchangeTest) #include "AvatarExchangeTest.moc"