1 /*
2 SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "attachmentcompressjobtest.h"
8 #include "qtest_messagecore.h"
9
10 #include "messagecore_debug.h"
11 #include <KZip>
12 #include <QBuffer>
13 #include <QTest>
14
15 #include <MessageCore/AttachmentCompressJob>
16 using namespace MessageCore;
17
QTEST_MAIN(AttachmentCompressJobTest)18 QTEST_MAIN(AttachmentCompressJobTest)
19
20 void AttachmentCompressJobTest::testCompress()
21 {
22 // Some data.
23 QByteArray data;
24 for (int i = 0; i < 100; ++i) {
25 data += "This is some highly compressible text...\n";
26 }
27 const QString name = QStringLiteral("name");
28 const QString fileName = QStringLiteral("name.txt");
29 const QString description = QStringLiteral("description");
30
31 // Create the original part.
32 AttachmentPart::Ptr origPart = AttachmentPart::Ptr(new AttachmentPart);
33 origPart->setName(name);
34 origPart->setFileName(fileName);
35 origPart->setDescription(description);
36 origPart->setMimeType("text/plain");
37 origPart->setEncoding(KMime::Headers::CE7Bit);
38 QVERIFY(!origPart->isAutoEncoding());
39 origPart->setData(data);
40 QVERIFY(!origPart->isCompressed());
41
42 // Compress the part and verify it.
43 auto cjob = new AttachmentCompressJob(origPart, this);
44 VERIFYEXEC(cjob);
45 QCOMPARE(cjob->originalPart(), origPart);
46 AttachmentPart::Ptr zipPart = cjob->compressedPart();
47 // qCDebug(MESSAGECORE_LOG) << data;
48 // qCDebug(MESSAGECORE_LOG) << zipPart->data();
49 QVERIFY(zipPart->isAutoEncoding());
50 QVERIFY(zipPart->isCompressed());
51 QCOMPARE(zipPart->name(), QString(name + QString::fromLatin1(".zip")));
52 QCOMPARE(zipPart->fileName(), QString(fileName + QString::fromLatin1(".zip")));
53 QCOMPARE(zipPart->description(), description);
54 QCOMPARE(zipPart->mimeType(), QByteArrayLiteral("application/zip"));
55
56 // Uncompress the data and verify it.
57 // (Stuff below is stolen from KMail code.)
58 QByteArray zipData = zipPart->data();
59 QBuffer buffer(&zipData);
60 KZip zip(&buffer);
61 QVERIFY(zip.open(QIODevice::ReadOnly));
62 const KArchiveDirectory *dir = zip.directory();
63 QCOMPARE(dir->entries().count(), 1);
64 const KZipFileEntry *entry = (KZipFileEntry *)dir->entry(dir->entries().at(0));
65 QCOMPARE(entry->data(), data);
66 QCOMPARE(entry->name(), name);
67 zip.close();
68 }
69
testCompressedSizeLarger()70 void AttachmentCompressJobTest::testCompressedSizeLarger()
71 {
72 // Some data.
73 QByteArray data("This is short enough that compressing it is not efficient.");
74 const QString name = QStringLiteral("name.txt");
75 const QString description = QStringLiteral("description");
76
77 // Create the original part.
78 AttachmentPart::Ptr origPart = AttachmentPart::Ptr(new AttachmentPart);
79 origPart->setName(name);
80 origPart->setDescription(description);
81 origPart->setMimeType("text/plain");
82 origPart->setEncoding(KMime::Headers::CE7Bit);
83 QVERIFY(!origPart->isAutoEncoding());
84 origPart->setData(data);
85 QVERIFY(!origPart->isCompressed());
86
87 // Compress the part and verify that it is aware of its folly.
88 auto cjob = new AttachmentCompressJob(origPart, this);
89 VERIFYEXEC(cjob);
90 QVERIFY(cjob->isCompressedPartLarger());
91 }
92