// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL #include "core/media/OutgoingFile.h" #include #include #include #include #include #include #include #include // What goes on the wire when something is attached, which is the one place a // conversation decides how much bandwidth it costs everybody reading it. The // cases below are the ones that are easy to get wrong: a file that must not be // touched, a file that must be, and a name that has to match what was sent. class OutgoingFileTest : public QObject { Q_OBJECT private: // A picture shaped like a photograph: smooth areas next to edges and a // little fine grain, which is what a sensor actually produces. Flat colour // would be the wrong model - jpeg already stores that in a few kilobytes, so // nothing would be learned about the code under test. Random noise would be // the wrong model the other way, being the one picture no encoder helps. static QImage smoothPhoto(int edge) { QImage image(edge, edge, QImage::Format_RGB32); image.fill(Qt::transparent); QPainter painter(&image); painter.setRenderHint(QPainter::Antialiasing); painter.fillRect(image.rect(), QColor(24, 32, 48)); for (int i = 0; i < 12; ++i) { const double f = double(i) / 12.0; const int size = edge / 4 + int(edge / 3.0 * f); const int x = int(edge * (0.5 + 0.42 * std::sin(f * 6.28))); const int y = int(edge * (0.5 + 0.40 * std::cos(f * 4.11))); painter.setBrush(QColor::fromHsv(int(360 * f), 140, 220)); painter.setPen(Qt::NoPen); painter.drawEllipse(QPoint(x, y), size / 2, size / 3); } painter.setBrush(Qt::NoBrush); painter.setPen(QPen(QColor(250, 250, 250), qMax(1, edge / 200))); for (int i = 0; i < 5; ++i) painter.drawLine(0, int(edge * 0.2 * i), edge, int(edge * (0.2 * i + 0.35))); painter.end(); // grain, a dozen levels either side of what is there: enough that jpeg // spends real bytes on it, little enough that the picture is still a // picture and not a noise field. QRandomGenerator random(7); for (int y = 0; y < edge; ++y) { QRgb *line = reinterpret_cast(image.scanLine(y)); for (int x = 0; x < edge; ++x) { const int grain = random.bounded(25) - 12; line[x] = qRgb(qBound(0, qRed(line[x]) + grain, 255), qBound(0, qGreen(line[x]) + grain, 255), qBound(0, qBlue(line[x]) + grain, 255)); } } return image; } // Written outside a test body on purpose: QVERIFY returns from the function // it is written in, so a helper of this shape would only return a path. An // empty answer means the fixture could not be written, which the caller // checks before believing anything about it. // // A photograph arrives as a jpeg written near the encoder's maximum, which // is what a camera and every phone does; writing the fixture at Qt's default // quality instead would produce a file smaller than the one the code is // meant to be beating, and the honest answer would be to leave it alone. static QString write(const QTemporaryDir &dir, const QString &name, const QImage &image, const char *format, int quality = 90) { const QString path = dir.filePath(name); return image.save(path, format, quality) ? path : QString(); } QTemporaryDir m_dir; private Q_SLOTS: void testBigPhotoIsShrunk(); void testSmallFileIsLeftAlone(); void testNotAnImageIsLeftAlone(); void testPreparedFileDecodes(); void testNameIsReasonable(); void testSamePhotoTwiceIsOneFile(); void testMissingFileAnswersWithItself(); void testTruncatedFileIsLeftAlone(); void testSquarePictureKeepsItsShape(); }; void OutgoingFileTest::testBigPhotoIsShrunk() { const QString path = write(m_dir, QStringLiteral("photo.jpg"), smoothPhoto(3000), "JPG"); QVERIFY2(!path.isEmpty(), "the fixture itself could not be written"); const qint64 originalSize = QFileInfo(path).size(); QVERIFY(originalSize > OutgoingFile::kMinBytesToShrink); const OutgoingFile::Prepared prepared = OutgoingFile::prepare(path); QVERIFY(prepared.compressed); QVERIFY2(prepared.after < prepared.before, "a three-thousand-pixel photograph went out no smaller"); QVERIFY(prepared.after < originalSize); QVERIFY(QFileInfo::exists(prepared.path)); QVERIFY(prepared.path != path); // the original is never the file that is sent QCOMPARE(QFileInfo(path).size(), originalSize); // and never written to } void OutgoingFileTest::testSmallFileIsLeftAlone() { // Below the threshold re-encoding costs bytes and quality to save a saving // nobody notices, so the file that goes out is the file that came in. const QString path = write(m_dir, QStringLiteral("icon.png"), smoothPhoto(64), "PNG", -1); QVERIFY2(!path.isEmpty(), "the fixture itself could not be written"); const qint64 originalSize = QFileInfo(path).size(); QVERIFY(originalSize < OutgoingFile::kMinBytesToShrink); const OutgoingFile::Prepared prepared = OutgoingFile::prepare(path); QVERIFY(!prepared.compressed); QCOMPARE(prepared.path, path); QCOMPARE(prepared.name, QStringLiteral("icon.png")); } void OutgoingFileTest::testNotAnImageIsLeftAlone() { // A document has no smaller honest version here, and shrinking it by // re-encoding would only lose it. Written as text so its size is its content. const QString path = m_dir.filePath(QStringLiteral("notes.txt")); QFile file(path); QVERIFY(file.open(QIODevice::WriteOnly)); file.write(QByteArray(200 * 1024, 'x')); file.close(); const OutgoingFile::Prepared prepared = OutgoingFile::prepare(path); QVERIFY(!prepared.compressed); QCOMPARE(prepared.path, path); QCOMPARE(prepared.name, QStringLiteral("notes.txt")); } void OutgoingFileTest::testPreparedFileDecodes() { // The smaller file has to be a picture still, at a sane size, and named for // the format it is in - a body announced as one format and delivered as // another is a file some client opens by its name and finds nothing in. const QString path = write(m_dir, QStringLiteral("holiday.jpg"), smoothPhoto(2400), "JPG"); QVERIFY2(!path.isEmpty(), "the fixture itself could not be written"); const OutgoingFile::Prepared prepared = OutgoingFile::prepare(path); QVERIFY(prepared.compressed); // It decodes, at a size a screen can show, and it is in the format the name // says it is - a body announced as one format and delivered as another is a // file some client opens by its name and finds nothing in. QImage decoded(prepared.path); QVERIFY(!decoded.isNull()); QVERIFY2(std::max(decoded.width(), decoded.height()) <= OutgoingFile::kMaxImageEdge, "the long side was not brought down to what a screen can show"); QVERIFY(std::min(decoded.width(), decoded.height()) > 0); QCOMPARE(QFileInfo(prepared.path).suffix().toLower(), prepared.name.section(QLatin1Char('.'), -1).toLower()); } void OutgoingFileTest::testNameIsReasonable() { // The name is the caption on the picture the receiver sees, so it is the // name the person gave their file, letters and spaces in whatever script // they wrote in - a chat full of photographs called 4f2a1c9e.webp is a chat // nobody can read. What cannot stay is anything that would break a path, a // url or a line of text. const QString path = write(m_dir, QStringLiteral("отпуск 2026 (кот): final.jpg"), smoothPhoto(2000), "JPG"); QVERIFY2(!path.isEmpty(), "the fixture itself could not be written"); const OutgoingFile::Prepared prepared = OutgoingFile::prepare(path); QVERIFY(prepared.compressed); QVERIFY(prepared.name.contains(QStringLiteral("отпуск 2026 (кот)"))); QVERIFY2(!prepared.name.contains(QLatin1Char(':')), "a colon survived, which is a second drive letter on windows"); QVERIFY(prepared.name.endsWith(QLatin1String(".webp")) || prepared.name.endsWith(QLatin1String(".jpg"))); QVERIFY2(!prepared.name.contains(QLatin1String("/")), "a path separator survived into the name"); } void OutgoingFileTest::testSamePhotoTwiceIsOneFile() { // The compressed copy is addressed by what it contains, so sending the same // photograph into three rooms writes it once. const QString first = write(m_dir, QStringLiteral("one.jpg"), smoothPhoto(2000), "JPG"); QVERIFY2(!first.isEmpty(), "the fixture itself could not be written"); const QString second = m_dir.filePath(QStringLiteral("two.jpg")); QVERIFY(smoothPhoto(2000).save(second, "JPG", 90)); const OutgoingFile::Prepared a = OutgoingFile::prepare(first); const OutgoingFile::Prepared b = OutgoingFile::prepare(second); QVERIFY(a.compressed && b.compressed); // One file for the bytes, whether it went out as one attachment or three. QCOMPARE(a.path, b.path); // The names still differ, and should: each recipient is shown the name they // picked, not the name the first sender happened to pick for the same pixels. QCOMPARE(a.name, QStringLiteral("one.webp")); QCOMPARE(b.name, QStringLiteral("two.webp")); } void OutgoingFileTest::testMissingFileAnswersWithItself() { // A file that cannot be read is not a reason to send nothing: the caller // hands the path straight to the backend, which reports the real problem. const QString path = m_dir.filePath(QStringLiteral("gone.png")); const OutgoingFile::Prepared prepared = OutgoingFile::prepare(path); QCOMPARE(prepared.path, path); QVERIFY(!prepared.compressed); QCOMPARE(prepared.after, qint64(0)); } void OutgoingFileTest::testTruncatedFileIsLeftAlone() { // A jpeg with a header and no picture in it: the loader says no, so the // original is what goes out rather than nothing at all. const QString path = m_dir.filePath(QStringLiteral("broken.jpg")); QFile file(path); QVERIFY(file.open(QIODevice::WriteOnly)); file.write(QByteArray(200 * 1024, '\xFF')); file.close(); const OutgoingFile::Prepared prepared = OutgoingFile::prepare(path); QCOMPARE(prepared.path, path); QVERIFY(!prepared.compressed); } void OutgoingFileTest::testSquarePictureKeepsItsShape() { // A sticker is an image/ type and goes through the same path as a // photograph, so the frame it is cut into has to survive it: a square // sticker that comes out stretched is a broken sticker. const QString path = write(m_dir, QStringLiteral("sticker.jpg"), smoothPhoto(1200), "JPG"); QVERIFY2(!path.isEmpty(), "the fixture itself could not be written"); const OutgoingFile::Prepared prepared = OutgoingFile::prepare(path); QVERIFY(prepared.compressed); const QImage decoded(prepared.path); QCOMPARE(decoded.size(), QSize(1200, 1200)); } QTEST_MAIN(OutgoingFileTest) #include "OutgoingFileTest.moc"