// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL #include #include #include #include #include #include #include #include #include #include #include "../network/FileTransferHandler.h" using koutnet::FileTransferHandler; namespace { QString downloadDir() { return QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + QStringLiteral("/KOutNet"); } // The peer a transfer belongs to, and one that is not it. const QString kSender = QStringLiteral("10.0.0.2"); const QString kOther = QStringLiteral("10.0.0.3"); QJsonObject metaFor(const QString &tid, qint64 size, const QString &filename) { QJsonObject o; o[QStringLiteral("tid")] = tid; o[QStringLiteral("size")] = double(size); o[QStringLiteral("filename")] = filename; return o; } // One transfer, the way the app sets it up: the offer is filed against the host // it arrived from and only starts once the user has consented to it. void offer(FileTransferHandler &handler, const QJsonObject &meta, const QString &host = kSender) { handler.onMetaOffered(host, meta); handler.acceptOffer(meta.value(QStringLiteral("tid")).toString()); } } // namespace class FileTransferHandlerTest : public QObject { Q_OBJECT private Q_SLOTS: void duplicateMetadataKeepsReceivedChunks() { FileTransferHandler handler; QSignalSpy saved(&handler, &FileTransferHandler::fileSaved); const QString tid = QStringLiteral("duplicate-meta"); const QJsonObject meta = metaFor(tid, 4, QStringLiteral("duplicate.bin")); offer(handler, meta); handler.onChunkMessage(kSender, tid, 0, 2, QByteArrayLiteral("ab")); offer(handler, meta); QCOMPARE(handler.pendingBufferedBytes(), 2); handler.onChunkMessage(kSender, tid, 1, 2, QByteArrayLiteral("cd")); QCOMPARE(saved.count(), 1); QCOMPARE(handler.pendingTransferCount(), 0); } // A name is only safe if joining it to the destination cannot point elsewhere. void sanitizedNamesStayInTheFolder_data() { QTest::addColumn("raw"); QTest::newRow("plain") << QStringLiteral("holiday.jpg"); QTest::newRow("relative traversal") << QStringLiteral("../../etc/passwd"); QTest::newRow("deep traversal") << QStringLiteral("../../../../../../../../etc/shadow"); QTest::newRow("absolute") << QStringLiteral("/etc/passwd"); QTest::newRow("absolute home") << QStringLiteral("/home/user/.bashrc"); QTest::newRow("windows traversal") << QStringLiteral("..\\..\\Windows\\System32\\drivers\\etc\\hosts"); QTest::newRow("windows drive") << QStringLiteral("C:\\Windows\\notepad.exe"); QTest::newRow("mixed separators") << QStringLiteral("../..\\../evil.sh"); QTest::newRow("trailing separator") << QStringLiteral("subdir/"); QTest::newRow("dot") << QStringLiteral("."); QTest::newRow("dotdot") << QStringLiteral(".."); QTest::newRow("only dots") << QStringLiteral("...."); QTest::newRow("empty") << QString(); QTest::newRow("space only") << QStringLiteral(" "); QTest::newRow("newline") << QStringLiteral("report\ninjected.sh"); QTest::newRow("carriage return") << QStringLiteral("report\r\n.sh"); QTest::newRow("tab") << QStringLiteral("re\tport.txt"); // the one that matters most: every check in this class reasons about a // QString, and QFile hands the path to an API that stops at the NUL. QTest::newRow("embedded nul") << (QStringLiteral("safe.txt") + QChar(u'\0') + QStringLiteral("/../../etc/passwd")); QTest::newRow("nul then dotdot") << (QStringLiteral("..") + QChar(u'\0')); QTest::newRow("nul first") << (QChar(u'\0') + QStringLiteral("hidden")); QTest::newRow("very long") << QString(5000, QLatin1Char('a')); QTest::newRow("very long traversal") << (QStringLiteral("../") + QString(5000, QLatin1Char('b'))); QTest::newRow("long multibyte") << QString(2000, QChar(0x00E9)); } void sanitizedNamesStayInTheFolder() { QFETCH(QString, raw); const QString name = FileTransferHandler::sanitizeFilename(raw); QVERIFY2(!name.isEmpty(), "an empty name would make the write target the folder itself"); QVERIFY2(!name.contains(QLatin1Char('/')), qPrintable(name)); QVERIFY2(!name.contains(QLatin1Char('\\')), qPrintable(name)); QVERIFY2(name != QLatin1String("."), qPrintable(name)); QVERIFY2(name != QLatin1String(".."), qPrintable(name)); QVERIFY2(name.count(QLatin1Char('.')) != name.size(), qPrintable(name)); for (const QChar c : name) { QVERIFY2(c.unicode() >= 0x20 && c.unicode() != 0x7F, "a control character survived, so the path that was checked " "is not necessarily the path that gets opened"); } QVERIFY2(name.toUtf8().size() <= FileTransferHandler::kMaxFilenameBytes, qPrintable(QStringLiteral("%1 bytes of filename").arg(name.toUtf8().size()))); const QString base = QDir::cleanPath(downloadDir()); const QString joined = QDir::cleanPath(base + QLatin1Char('/') + name); QVERIFY2(joined.startsWith(base + QLatin1Char('/')), qPrintable(QStringLiteral("%1 escapes %2").arg(joined, base))); QCOMPARE(QFileInfo(joined).absolutePath(), base); } // again through the real path: saveToDisk() compares canonical paths, and only a // write proves it. void aHostileNameIsWrittenInsideTheFolder_data() { QTest::addColumn("raw"); QTest::newRow("traversal") << QStringLiteral("../../../../etc/passwd"); QTest::newRow("absolute") << QStringLiteral("/etc/passwd"); QTest::newRow("dotdot") << QStringLiteral(".."); QTest::newRow("long") << QString(4000, QLatin1Char('z')); } void aHostileNameIsWrittenInsideTheFolder() { QFETCH(QString, raw); FileTransferHandler handler; QSignalSpy saved(&handler, &FileTransferHandler::fileSaved); QSignalSpy rejected(&handler, &FileTransferHandler::transferRejected); const QByteArray payload = QByteArrayLiteral("contents"); const QString tid = QStringLiteral("t-name"); offer(handler, metaFor(tid, payload.size(), raw)); handler.onChunkMessage(kSender, tid, 0, 1, payload); QCOMPARE(rejected.count(), 0); QCOMPARE(saved.count(), 1); const QString path = saved.at(0).at(1).toString(); QVERIFY(!path.isEmpty()); const QString base = QFileInfo(downloadDir()).canonicalFilePath(); QVERIFY(!base.isEmpty()); QCOMPARE(QFileInfo(path).absolutePath(), base); QVERIFY2(QFileInfo::exists(path), qPrintable(path)); QCOMPARE(handler.pendingTransferCount(), 0); QCOMPARE(handler.pendingBufferedBytes(), 0); } void announcedSizeMustBeALength_data() { QTest::addColumn("size"); QTest::addColumn("accepted"); QTest::newRow("zero") << QJsonValue(0) << true; QTest::newRow("small") << QJsonValue(1024) << true; QTest::newRow("at the cap") << QJsonValue(double(FileTransferHandler::kMaxTransferBytes)) << true; QTest::newRow("negative") << QJsonValue(-1) << false; QTest::newRow("very negative") << QJsonValue(-1e18) << false; QTest::newRow("over the cap") << QJsonValue(double(FileTransferHandler::kMaxTransferBytes) + 1.0) << false; // Narrowing either of these to qint64 is undefined behaviour, not a large // number. QTest::newRow("absurd") << QJsonValue(1e300) << false; QTest::newRow("infinity") << QJsonValue(std::numeric_limits::infinity()) << false; QTest::newRow("nan") << QJsonValue(std::numeric_limits::quiet_NaN()) << false; QTest::newRow("numeric string") << QJsonValue(QStringLiteral("1024")) << false; QTest::newRow("text") << QJsonValue(QStringLiteral("lots")) << false; QTest::newRow("bool") << QJsonValue(true) << false; QTest::newRow("null") << QJsonValue(QJsonValue::Null) << false; QTest::newRow("missing") << QJsonValue(QJsonValue::Undefined) << false; } void announcedSizeMustBeALength() { QFETCH(QJsonValue, size); QFETCH(bool, accepted); FileTransferHandler handler; QSignalSpy rejected(&handler, &FileTransferHandler::transferRejected); QJsonObject meta; meta[QStringLiteral("tid")] = QStringLiteral("t-size"); meta[QStringLiteral("filename")] = QStringLiteral("thing.bin"); if (!size.isUndefined()) meta[QStringLiteral("size")] = size; offer(handler, meta); QCOMPARE(handler.pendingTransferCount(), accepted ? 1 : 0); QCOMPARE(rejected.count(), accepted ? 0 : 1); if (!accepted) QCOMPARE(handler.pendingBufferedBytes(), 0); } void anIdenticalResendIsAccepted() { FileTransferHandler handler; QSignalSpy rejected(&handler, &FileTransferHandler::transferRejected); QSignalSpy saved(&handler, &FileTransferHandler::fileSaved); const QString tid = QStringLiteral("t-resend"); const QByteArray first = QByteArrayLiteral("aaaa"); const QByteArray second = QByteArrayLiteral("bbbb"); offer(handler, metaFor(tid, 8, QStringLiteral("resend.bin"))); handler.onChunkMessage(kSender, tid, 0, 2, first); QCOMPARE(handler.pendingBufferedBytes(), 4); handler.onChunkMessage(kSender, tid, 0, 2, first); QCOMPARE(rejected.count(), 0); QVERIFY2(handler.pendingBufferedBytes() == 4, "a duplicate of a chunk we already hold was counted twice"); handler.onChunkMessage(kSender, tid, 1, 2, second); QCOMPARE(rejected.count(), 0); QCOMPARE(saved.count(), 1); QCOMPARE(handler.pendingTransferCount(), 0); } // chunks arrive in reverse order; the file must still assemble correctly void outOfOrderChunksAssembleCorrectly() { FileTransferHandler handler; QSignalSpy saved(&handler, &FileTransferHandler::fileSaved); QSignalSpy rejected(&handler, &FileTransferHandler::transferRejected); const QString tid = QStringLiteral("t-ooo"); const QByteArray a = QByteArrayLiteral("aaaa"); const QByteArray b = QByteArrayLiteral("bbbb"); const QByteArray c = QByteArrayLiteral("cccc"); offer(handler, metaFor(tid, 12, QStringLiteral("ooo.bin"))); // send 2, 0, 1 instead of 0, 1, 2 handler.onChunkMessage(kSender, tid, 2, 3, c); QCOMPARE(saved.count(), 0); handler.onChunkMessage(kSender, tid, 0, 3, a); QCOMPARE(saved.count(), 0); handler.onChunkMessage(kSender, tid, 1, 3, b); QCOMPARE(rejected.count(), 0); QCOMPARE(saved.count(), 1); QCOMPARE(handler.pendingTransferCount(), 0); QCOMPARE(handler.pendingBufferedBytes(), 0); } void aConflictingResendKillsTheTransfer() { FileTransferHandler handler; QSignalSpy rejected(&handler, &FileTransferHandler::transferRejected); const QString tid = QStringLiteral("t-conflict"); offer(handler, metaFor(tid, 8, QStringLiteral("conflict.bin"))); handler.onChunkMessage(kSender, tid, 0, 2, QByteArrayLiteral("aaaa")); handler.onChunkMessage(kSender, tid, 0, 2, QByteArrayLiteral("zzzz")); QCOMPARE(rejected.count(), 1); QCOMPARE(handler.pendingTransferCount(), 0); QCOMPARE(handler.pendingBufferedBytes(), 0); } // the accounting bug this replaced: insert() replaces, so every index at one // byte and then again at full size cost nothing the first pass and the cap never // saw the second. void aSecondPassAtFullSizeIsRefused() { FileTransferHandler handler; QSignalSpy rejected(&handler, &FileTransferHandler::transferRejected); const QString tid = QStringLiteral("t-twopass"); const int total = 16; offer(handler, metaFor(tid, 16, QStringLiteral("twopass.bin"))); // one short: the last chunk would flush to disk and empty the // buffers. for (int i = 0; i < total - 1; ++i) handler.onChunkMessage(kSender, tid, i, total, QByteArrayLiteral("x")); QCOMPARE(rejected.count(), 0); QCOMPARE(handler.pendingBufferedBytes(), total - 1); handler.onChunkMessage(kSender, tid, 0, total, QByteArray(4096, 'x')); QVERIFY2(rejected.count() == 1 || handler.pendingBufferedBytes() > total - 1, "a resent index grew the transfer without being charged for it"); QCOMPARE(handler.pendingTransferCount(), 0); QCOMPARE(handler.pendingBufferedBytes(), 0); } // Slow: proving the cap holds means handing over more than the cap. void theByteCapHolds() { FileTransferHandler handler; QSignalSpy rejected(&handler, &FileTransferHandler::transferRejected); const QString tid = QStringLiteral("t-cap"); const int chunkBytes = 4 * 1024 * 1024; const QByteArray chunk(chunkBytes, 'q'); const int total = int(FileTransferHandler::kMaxTransferBytes / chunkBytes) + 2; offer(handler, metaFor(tid, 1024, QStringLiteral("big.bin"))); int sent = 0; for (int i = 0; i < total && rejected.isEmpty(); ++i) { handler.onChunkMessage(kSender, tid, i, total, chunk); ++sent; } QCOMPARE(rejected.count(), 1); QVERIFY2(qint64(sent) * chunkBytes <= FileTransferHandler::kMaxTransferBytes + chunkBytes, "more than the cap was accepted before the refusal"); QCOMPARE(handler.pendingTransferCount(), 0); QCOMPARE(handler.pendingBufferedBytes(), 0); } void nonsenseChunkHeadersAreDropped_data() { QTest::addColumn("idx"); QTest::addColumn("total"); QTest::newRow("negative index") << -1 << 4; QTest::newRow("index past total") << 4 << 4; QTest::newRow("index far past total") << 1'000'000 << 4; QTest::newRow("zero total") << 0 << 0; QTest::newRow("negative total") << 0 << -1; QTest::newRow("absurd total") << 0 << 2'000'000'000; } void nonsenseChunkHeadersAreDropped() { QFETCH(int, idx); QFETCH(int, total); FileTransferHandler handler; QSignalSpy saved(&handler, &FileTransferHandler::fileSaved); const QString tid = QStringLiteral("t-headers"); offer(handler, metaFor(tid, 64, QStringLiteral("headers.bin"))); handler.onChunkMessage(kSender, tid, idx, total, QByteArrayLiteral("data")); QCOMPARE(saved.count(), 0); QCOMPARE(handler.pendingBufferedBytes(), 0); } // A pinned peer that learned somebody else's transfer id must not be able to // put bytes into that file. Both halves of the old bug are here: a chunk // from the wrong host, and the same index sent twice with other contents, // which used to look like a broken sender and cancel the transfer. void chunksFromAnotherHostAreDropped() { FileTransferHandler handler; QSignalSpy saved(&handler, &FileTransferHandler::fileSaved); QSignalSpy rejected(&handler, &FileTransferHandler::transferRejected); const QString tid = QStringLiteral("owned-by-sender"); offer(handler, metaFor(tid, 4, QStringLiteral("owned.bin"))); handler.onChunkMessage(kOther, tid, 0, 2, QByteArrayLiteral("XX")); QCOMPARE(handler.pendingBufferedBytes(), 0); handler.onChunkMessage(kSender, tid, 0, 2, QByteArrayLiteral("ab")); QCOMPARE(handler.pendingBufferedBytes(), 2); // the impostor walks the whole range again to trip the resend check handler.onChunkMessage(kOther, tid, 0, 2, QByteArrayLiteral("zz")); handler.onChunkMessage(kOther, tid, 1, 2, QByteArrayLiteral("zz")); QCOMPARE(handler.pendingTransferCount(), 1); QCOMPARE(rejected.count(), 0); handler.onChunkMessage(kSender, tid, 1, 2, QByteArrayLiteral("cd")); QCOMPARE(saved.count(), 1); QCOMPARE(handler.pendingTransferCount(), 0); } // The offer fixes who owns the id, so a second peer cannot take a transfer // over by announcing the same tid. void aSecondPeerCannotClaimAnAnnouncedId() { FileTransferHandler handler; QSignalSpy saved(&handler, &FileTransferHandler::fileSaved); const QString tid = QStringLiteral("contested"); handler.onMetaOffered(kSender, metaFor(tid, 4, QStringLiteral("contested.bin"))); handler.onMetaOffered(kOther, metaFor(tid, 4, QStringLiteral("contested.bin"))); handler.acceptOffer(tid); handler.onChunkMessage(kOther, tid, 0, 2, QByteArrayLiteral("XX")); handler.onChunkMessage(kSender, tid, 0, 2, QByteArrayLiteral("ab")); handler.onChunkMessage(kSender, tid, 1, 2, QByteArrayLiteral("cd")); QCOMPARE(saved.count(), 1); } // Nothing is buffered before the answer, and declining keeps it that way. void nothingIsKeptBeforeConsent() { FileTransferHandler handler; QSignalSpy saved(&handler, &FileTransferHandler::fileSaved); const QString tid = QStringLiteral("awaiting-consent"); handler.onMetaOffered(kSender, metaFor(tid, 2, QStringLiteral("later.bin"))); QCOMPARE(handler.offeredCount(), 1); handler.onChunkMessage(kSender, tid, 0, 1, QByteArrayLiteral("ab")); QCOMPARE(handler.pendingTransferCount(), 0); QCOMPARE(handler.pendingBufferedBytes(), 0); QCOMPARE(saved.count(), 0); handler.acceptOffer(tid); QCOMPARE(handler.offeredCount(), 0); handler.onChunkMessage(kSender, tid, 0, 1, QByteArrayLiteral("ab")); QCOMPARE(saved.count(), 1); } // A declined offer cannot be accepted afterwards, and a tid that was never // offered is not something consent can conjure up. void declinedOffersStayDeclined() { FileTransferHandler handler; QSignalSpy saved(&handler, &FileTransferHandler::fileSaved); const QString tid = QStringLiteral("declined"); handler.onMetaOffered(kSender, metaFor(tid, 4, QStringLiteral("no.bin"))); handler.declineOffer(tid); QCOMPARE(handler.offeredCount(), 0); handler.acceptOffer(tid); handler.acceptOffer(QStringLiteral("never-offered")); handler.onChunkMessage(kSender, tid, 0, 1, QByteArrayLiteral("ab")); QCOMPARE(handler.pendingTransferCount(), 0); QCOMPARE(saved.count(), 0); } // Offers nobody answers are bounded and swept, like the transfers themselves. void unansweredOffersAreBounded() { FileTransferHandler handler; for (int i = 0; i < FileTransferHandler::kMaxPendingTransfers + 20; ++i) handler.onMetaOffered(kSender, metaFor(QStringLiteral("o-%1").arg(i), 1024, QStringLiteral("o%1.bin").arg(i))); QCOMPARE(handler.offeredCount(), FileTransferHandler::kMaxPendingTransfers); } void chunksWithoutAMetaAreDropped() { FileTransferHandler handler; handler.onChunkMessage(kSender, QStringLiteral("never-announced"), 0, 1, QByteArrayLiteral("data")); QCOMPARE(handler.pendingTransferCount(), 0); QJsonObject oversized; oversized[QStringLiteral("tid")] = QStringLiteral("t-refused"); oversized[QStringLiteral("size")] = 1e300; offer(handler, oversized); handler.onChunkMessage(kSender, QStringLiteral("t-refused"), 0, 1, QByteArrayLiteral("data")); QCOMPARE(handler.pendingTransferCount(), 0); QCOMPARE(handler.pendingBufferedBytes(), 0); offer(handler, metaFor(QString(), 16, QStringLiteral("x.bin"))); QCOMPARE(handler.pendingTransferCount(), 0); } void tooManyConcurrentTransfersAreRefused() { FileTransferHandler handler; QSignalSpy rejected(&handler, &FileTransferHandler::transferRejected); for (int i = 0; i < FileTransferHandler::kMaxPendingTransfers + 20; ++i) { offer(handler, metaFor(QStringLiteral("t-%1").arg(i), 1024, QStringLiteral("f%1.bin").arg(i))); } QCOMPARE(handler.pendingTransferCount(), FileTransferHandler::kMaxPendingTransfers); QCOMPARE(rejected.count(), 20); } }; // the download directory is the one path QStandardPaths test mode does not move: on // Unix it comes from the XDG user dirs, so this would write into real ~/Downloads. int main(int argc, char *argv[]) { QTemporaryDir home; if (!home.isValid()) { qCritical("could not create a scratch home directory"); return 1; } qputenv("HOME", home.path().toLocal8Bit()); // no user-dirs.dirs in there, so DownloadLocation falls back to $HOME/Downloads. qputenv("XDG_CONFIG_HOME", QByteArray(home.path().toLocal8Bit() + "/.config")); QStandardPaths::setTestModeEnabled(true); QCoreApplication app(argc, argv); // without a domain ki18n warns on every string, burying the test output. KLocalizedString::setApplicationDomain(QByteArrayLiteral("koutnet")); QCoreApplication::setOrganizationName(QStringLiteral("koutnet-tests")); QCoreApplication::setApplicationName(QStringLiteral("file-transfer-handler")); FileTransferHandlerTest tc; QTEST_SET_MAIN_SOURCE_PATH return QTest::qExec(&tc, argc, argv); } #include "FileTransferHandlerTest.moc"