File: attachmentcompressjobtest.cpp

package info (click to toggle)
messagelib 4%3A24.12.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 83,448 kB
  • sloc: cpp: 106,765; xml: 375; sh: 25; makefile: 15
file content (93 lines) | stat: -rw-r--r-- 3,258 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
  SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>

  SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "attachmentcompressjobtest.h"
#include "qtest_messagecore.h"

#include "messagecore_debug.h"
#include <KZip>
#include <QBuffer>
#include <QTest>

#include <MessageCore/AttachmentCompressJob>
using namespace MessageCore;

QTEST_MAIN(AttachmentCompressJobTest)

void AttachmentCompressJobTest::testCompress()
{
    // Some data.
    QByteArray data;
    for (int i = 0; i < 100; ++i) {
        data += "This is some highly compressible text...\n";
    }
    const QString name = QStringLiteral("name");
    const QString fileName = QStringLiteral("name.txt");
    const QString description = QStringLiteral("description");

    // Create the original part.
    AttachmentPart::Ptr origPart = AttachmentPart::Ptr(new AttachmentPart);
    origPart->setName(name);
    origPart->setFileName(fileName);
    origPart->setDescription(description);
    origPart->setMimeType("text/plain");
    origPart->setEncoding(KMime::Headers::CE7Bit);
    QVERIFY(!origPart->isAutoEncoding());
    origPart->setData(data);
    QVERIFY(!origPart->isCompressed());

    // Compress the part and verify it.
    auto cjob = new AttachmentCompressJob(origPart, this);
    VERIFYEXEC(cjob);
    QCOMPARE(cjob->originalPart(), origPart);
    AttachmentPart::Ptr zipPart = cjob->compressedPart();
    // qCDebug(MESSAGECORE_LOG) << data;
    // qCDebug(MESSAGECORE_LOG) << zipPart->data();
    QVERIFY(zipPart->isAutoEncoding());
    QVERIFY(zipPart->isCompressed());
    QCOMPARE(zipPart->name(), QString(name + QString::fromLatin1(".zip")));
    QCOMPARE(zipPart->fileName(), QString(fileName + QString::fromLatin1(".zip")));
    QCOMPARE(zipPart->description(), description);
    QCOMPARE(zipPart->mimeType(), QByteArrayLiteral("application/zip"));

    // Uncompress the data and verify it.
    // (Stuff below is stolen from KMail code.)
    QByteArray zipData = zipPart->data();
    QBuffer buffer(&zipData);
    KZip zip(&buffer);
    QVERIFY(zip.open(QIODevice::ReadOnly));
    const KArchiveDirectory *dir = zip.directory();
    QCOMPARE(dir->entries().count(), 1);
    const KZipFileEntry *entry = (KZipFileEntry *)dir->entry(dir->entries().at(0));
    QCOMPARE(entry->data(), data);
    QCOMPARE(entry->name(), name);
    zip.close();
}

void AttachmentCompressJobTest::testCompressedSizeLarger()
{
    // Some data.
    QByteArray data("This is short enough that compressing it is not efficient.");
    const QString name = QStringLiteral("name.txt");
    const QString description = QStringLiteral("description");

    // Create the original part.
    AttachmentPart::Ptr origPart = AttachmentPart::Ptr(new AttachmentPart);
    origPart->setName(name);
    origPart->setDescription(description);
    origPart->setMimeType("text/plain");
    origPart->setEncoding(KMime::Headers::CE7Bit);
    QVERIFY(!origPart->isAutoEncoding());
    origPart->setData(data);
    QVERIFY(!origPart->isCompressed());

    // Compress the part and verify that it is aware of its folly.
    auto cjob = new AttachmentCompressJob(origPart, this);
    VERIFYEXEC(cjob);
    QVERIFY(cjob->isCompressedPartLarger());
}

#include "moc_attachmentcompressjobtest.cpp"