// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // Coverage for the outgoing file transfer on its own: no sockets, no // NetworkManager, no crypto. The pinned-channel route is a set of seams, so // every refusal - missing file, peer with no live channel, size over the // ceiling - and the announce-then-chunks shape of a success are pinned here, // while NetworkManagerTest keeps the one wired to a real channel. #include "../network/FileTransferSender.h" #include "../network/FileTransferHandler.h" #include "../network/Protocol.h" #include #include #include #include #include using namespace koutnet; namespace { // Reassemble the placed frames back into the file bytes, the way the receive // side does; returns false when the shape is not that of a meta + chunks. bool reassemble(const QVector &frames, QByteArray &outData, QJsonObject &outMeta) { QString tid; for (int i = 0; i < frames.size(); ++i) { const QCborMap f = frames.at(i); const QString type = f.value(QStringLiteral("type")).toString(); if (type == protocol::kMsgFileMeta) { if (i != 0) return false; // the meta must announce before any chunk outMeta = protocol::qjsonFromCbor(f); tid = f.value(QStringLiteral("tid")).toString(); if (tid.isEmpty()) return false; } else if (type == protocol::kMsgFileData) { if (i == 0) return false; // first frame is always the meta if (f.value(QStringLiteral("tid")).toString() != tid) return false; // every chunk belongs to the same transfer outData += f.value(QStringLiteral("data")).toByteArray(); } else { return false; } } return !outMeta.isEmpty() && outData.size() <= outMeta.value(QStringLiteral("size")).toInt(); } } // namespace class FileTransferSenderTest : public QObject { Q_OBJECT private Q_SLOTS: void missingFileReportsAndPlacesNothing(); void peerWithNoChannelIsRefused(); void oversizedTransferIsRefused(); void emptyBytesAreNotAFile(); void successAnnouncesThenChunks(); void multipartTransferReassembles(); void metaCarriesImageFlagAndTimestamp(); void identityAndAddressAreReadFreshPerSend(); }; void FileTransferSenderTest::missingFileReportsAndPlacesNothing() { FileTransferSender sender; QVector placed; sender.setRouter( [](const QString &) { return true; }, [&placed](const QString &, const QCborMap &f) { placed.append(f); }); QSignalSpy errors(&sender, &FileTransferSender::errorOccurred); QVERIFY(!sender.send(QStringLiteral("10.0.0.1"), QStringLiteral("/no/such/path/file.bin"))); QCOMPARE(errors.count(), 1); QVERIFY2(placed.isEmpty(), "a file that cannot be read must not put a single frame on the wire"); } void FileTransferSenderTest::peerWithNoChannelIsRefused() { FileTransferSender sender; QVector placed; // the seam says nobody at that address is listening: the sender must refuse. sender.setRouter( [](const QString &) { return false; }, [&placed](const QString &, const QCborMap &f) { placed.append(f); }); QSignalSpy errors(&sender, &FileTransferSender::errorOccurred); QVERIFY(!sender.sendBytes(QStringLiteral("10.0.0.1"), QByteArrayLiteral("hello"), QStringLiteral("x.bin"))); QCOMPARE(errors.count(), 1); QVERIFY(placed.isEmpty()); // without a router at all the same refusal holds: there is no channel to // put anything on. FileTransferSender unrouted; QSignalSpy unroutedErrors(&unrouted, &FileTransferSender::errorOccurred); QVERIFY(!unrouted.sendBytes(QStringLiteral("10.0.0.1"), QByteArrayLiteral("hello"), QStringLiteral("x.bin"))); QCOMPARE(unroutedErrors.count(), 1); } void FileTransferSenderTest::oversizedTransferIsRefused() { FileTransferSender sender; QVector placed; sender.setRouter( [](const QString &) { return true; }, [&placed](const QString &, const QCborMap &f) { placed.append(f); }); QSignalSpy errors(&sender, &FileTransferSender::errorOccurred); QByteArray big(int(FileTransferHandler::kMaxTransferBytes) + 1, 'b'); QVERIFY(!sender.sendBytes(QStringLiteral("10.0.0.1"), big, QStringLiteral("big.bin"))); QCOMPARE(errors.count(), 1); QVERIFY(placed.isEmpty()); } void FileTransferSenderTest::emptyBytesAreNotAFile() { FileTransferSender sender; QVector placed; sender.setRouter( [](const QString &) { return true; }, [&placed](const QString &, const QCborMap &f) { placed.append(f); }); QSignalSpy errors(&sender, &FileTransferSender::errorOccurred); QVERIFY(!sender.sendBytes(QStringLiteral("10.0.0.1"), QByteArray(), QStringLiteral("empty.bin"))); QCOMPARE(errors.count(), 0); QVERIFY(placed.isEmpty()); } void FileTransferSenderTest::successAnnouncesThenChunks() { FileTransferSender sender; QVector placed; uint64_t placedCount = 0; sender.setRouter( [](const QString &) { return true; }, [&placed, &placedCount](const QString &, const QCborMap &f) { placed.append(f); ++placedCount; }); sender.setLocalIdentity( [] { return QStringLiteral("me#id"); }, [] { return QStringLiteral("192.168.1.10"); }); sender.sendBytes(QStringLiteral("10.0.0.7"), QByteArrayLiteral("tiny payload"), QStringLiteral("note.txt")); QVERIFY(placedCount == 2); // meta + one chunk const QJsonObject meta = protocol::qjsonFromCbor(placed.at(0)); QCOMPARE(meta.value(QStringLiteral("type")).toString(), protocol::kMsgFileMeta); QCOMPARE(meta.value(QStringLiteral("filename")).toString(), QStringLiteral("note.txt")); QCOMPARE(meta.value(QStringLiteral("size")).toInt(), 12); QCOMPARE(meta.value(QStringLiteral("is_image")).toBool(), false); QCOMPARE(meta.value(QStringLiteral("encrypted")).toBool(), false); QCOMPARE(meta.value(QStringLiteral("from_id")).toString(), QStringLiteral("me#id")); QCOMPARE(meta.value(QStringLiteral("from_ip")).toString(), QStringLiteral("192.168.1.10")); QCOMPARE(meta.value(QStringLiteral("to")).toString(), QStringLiteral("10.0.0.7")); QVERIFY2(!meta.value(QStringLiteral("tid")).toString().isEmpty(), "tid is the id both sides key on"); QVERIFY2(meta.value(QStringLiteral("ts")).toDouble() > 0, "a timestamp lets the receiver order the transfer"); const QCborMap chunk = placed.at(1); QCOMPARE(chunk.value(QStringLiteral("tid")).toString(), meta.value(QStringLiteral("tid")).toString()); QCOMPARE(int(chunk.value(QStringLiteral("idx")).toInteger(-1)), 0); QCOMPARE(int(chunk.value(QStringLiteral("total")).toInteger(-1)), 1); QCOMPARE(chunk.value(QStringLiteral("data")).toByteArray(), QByteArrayLiteral("tiny payload")); } void FileTransferSenderTest::multipartTransferReassembles() { FileTransferSender sender; QVector placed; sender.setRouter( [](const QString &) { return true; }, [&placed](const QString &, const QCborMap &f) { placed.append(f); }); sender.setLocalIdentity( [] { return QString(); }, [] { return QString(); }); // larger than the 256 KiB chunk size, so the frames split into several. QByteArray payload(700 * 1024, '\0'); for (int i = 0; i < payload.size(); ++i) payload[i] = char(i % 251); QVERIFY(sender.sendBytes(QStringLiteral("10.0.0.7"), payload, QStringLiteral("movie.mp4"))); QByteArray outData; QJsonObject outMeta; QVERIFY(reassemble(placed, outData, outMeta)); QCOMPARE(outMeta.value(QStringLiteral("filename")).toString(), QStringLiteral("movie.mp4")); QCOMPARE(outMeta.value(QStringLiteral("size")).toInt(), payload.size()); QCOMPARE(outMeta.value(QStringLiteral("is_image")).toBool(), false); QCOMPARE(outData, payload); } void FileTransferSenderTest::metaCarriesImageFlagAndTimestamp() { FileTransferSender sender; QVector placed; sender.setRouter( [](const QString &) { return true; }, [&placed](const QString &, const QCborMap &f) { placed.append(f); }); sender.sendBytes(QStringLiteral("10.0.0.9"), QByteArrayLiteral("pngdata"), QStringLiteral("pic.PNG")); QCOMPARE(placed.at(0).value(QStringLiteral("is_image")).toBool(), true); placed.clear(); sender.sendBytes(QStringLiteral("10.0.0.9"), QByteArrayLiteral("not an image"), QStringLiteral("notes.txt")); QCOMPARE(placed.at(0).value(QStringLiteral("is_image")).toBool(), false); QVERIFY(placed.at(0).value(QStringLiteral("ts")).toDouble() > 0); } void FileTransferSenderTest::identityAndAddressAreReadFreshPerSend() { QString identity = QStringLiteral("first#id"); QString address = QStringLiteral("10.0.0.10"); FileTransferSender sender; QVector placed; sender.setRouter( [](const QString &) { return true; }, [&placed](const QString &, const QCborMap &f) { placed.append(f); }); sender.setLocalIdentity( [&identity] { return identity; }, [&address] { return address; }); sender.sendBytes(QStringLiteral("10.0.0.2"), QByteArrayLiteral("one"), QStringLiteral("a.bin")); QCOMPARE(placed.at(0).value(QStringLiteral("from_id")).toString(), QStringLiteral("first#id")); QCOMPARE(placed.at(0).value(QStringLiteral("from_ip")).toString(), QStringLiteral("10.0.0.10")); // the primary address moved under us (VPN brought up): the next send names // the fresh one, because the lambdas are read at send time, not cached. identity = QStringLiteral("second#id"); address = QStringLiteral("10.0.0.11"); placed.clear(); sender.sendBytes(QStringLiteral("10.0.0.2"), QByteArrayLiteral("two"), QStringLiteral("b.bin")); QCOMPARE(placed.at(0).value(QStringLiteral("from_id")).toString(), QStringLiteral("second#id")); QCOMPARE(placed.at(0).value(QStringLiteral("from_ip")).toString(), QStringLiteral("10.0.0.11")); } QTEST_GUILESS_MAIN(FileTransferSenderTest) #include "FileTransferSenderTest.moc"