Warning, file /frameworks/karchive/autotests/kcompressiondevicetest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    SPDX-FileCopyrightText: 2015 Luiz Romário Santana Rios <luizromario@gmail.com>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "kcompressiondevicetest.h"
0008 #include "kcompressiondevice_p.h"
0009 
0010 #include <config-compression.h>
0011 
0012 #include <QBuffer>
0013 #include <QDir>
0014 #include <QDirIterator>
0015 #include <QList>
0016 #include <QNetworkReply>
0017 #include <QNetworkRequest>
0018 #include <QTemporaryDir>
0019 #include <QTest>
0020 
0021 QTEST_MAIN(KCompressionDeviceTest)
0022 
0023 static QString archiveFileName(const QString &extension)
0024 {
0025     return QFINDTESTDATA(QString("kcompressiondevice_test.%1").arg(extension));
0026 }
0027 
0028 QNetworkReply *KCompressionDeviceTest::getArchive(const QString &extension)
0029 {
0030     const QString kcompressionTest = archiveFileName(extension);
0031     QNetworkReply *r = qnam.get(QNetworkRequest(QUrl::fromLocalFile(kcompressionTest)));
0032 
0033     QEventLoop l;
0034     connect(&qnam, &QNetworkAccessManager::finished, &l, &QEventLoop::quit);
0035     l.exec();
0036 
0037     return r;
0038 }
0039 
0040 QString KCompressionDeviceTest::formatExtension(KCompressionDevice::CompressionType type) const
0041 {
0042     switch (type) {
0043     case KCompressionDevice::GZip:
0044         return "tar.gz";
0045     case KCompressionDevice::BZip2:
0046         return "tar.bz2";
0047     case KCompressionDevice::Xz:
0048         return "tar.xz";
0049     case KCompressionDevice::Zstd:
0050         return "tar.zst";
0051     case KCompressionDevice::None:
0052         return QString();
0053     }
0054     return QString(); // silence compiler warning
0055 }
0056 
0057 void KCompressionDeviceTest::setDeviceToArchive(QIODevice *d, KCompressionDevice::CompressionType type)
0058 {
0059     KCompressionDevice *devRawPtr = new KCompressionDevice(d, true, type);
0060     archive.reset(new KTar(devRawPtr));
0061     device.reset(devRawPtr);
0062 }
0063 
0064 void KCompressionDeviceTest::testBufferedDevice(KCompressionDevice::CompressionType type)
0065 {
0066     QNetworkReply *r = getArchive(formatExtension(type));
0067     const QByteArray data = r->readAll();
0068     QVERIFY(!data.isEmpty());
0069     const int expectedSize = QFileInfo(archiveFileName(formatExtension(type))).size();
0070     QVERIFY(expectedSize > 0);
0071     QCOMPARE(data.size(), expectedSize);
0072     QBuffer *b = new QBuffer;
0073     b->setData(data);
0074 
0075     setDeviceToArchive(b, type);
0076     testExtraction();
0077 }
0078 
0079 void KCompressionDeviceTest::testExtraction()
0080 {
0081     QTemporaryDir temp;
0082     QString oldCurrentDir = QDir::currentPath();
0083     QDir::setCurrent(temp.path());
0084 
0085     QVERIFY(archive->open(QIODevice::ReadOnly));
0086     QVERIFY(archive->directory()->copyTo("."));
0087     QVERIFY(QDir("examples").exists());
0088     QVERIFY(QDir("examples/bzip2gzip").exists());
0089     QVERIFY(QDir("examples/helloworld").exists());
0090     QVERIFY(QDir("examples/tarlocalfiles").exists());
0091     QVERIFY(QDir("examples/unzipper").exists());
0092 
0093     const QStringList fileList = {
0094         QStringLiteral("examples/bzip2gzip/CMakeLists.txt"),
0095         QStringLiteral("examples/bzip2gzip/main.cpp"),
0096         QStringLiteral("examples/helloworld/CMakeLists.txt"),
0097         QStringLiteral("examples/helloworld/main.cpp"),
0098         QStringLiteral("examples/tarlocalfiles/CMakeLists.txt"),
0099         QStringLiteral("examples/tarlocalfiles/main.cpp"),
0100         QStringLiteral("examples/unzipper/CMakeLists.txt"),
0101         QStringLiteral("examples/unzipper/main.cpp"),
0102     };
0103 
0104     for (const QString &s : fileList) {
0105         QFileInfo extractedFile(s);
0106         QFileInfo sourceFile(QFINDTESTDATA("../" + s));
0107 
0108         QVERIFY(extractedFile.exists());
0109         QCOMPARE(extractedFile.size(), sourceFile.size());
0110     }
0111     QDir::setCurrent(oldCurrentDir);
0112 }
0113 
0114 void KCompressionDeviceTest::regularKTarUsage()
0115 {
0116     archive.reset(new KTar(QFINDTESTDATA("kcompressiondevice_test.tar.gz")));
0117     device.reset();
0118 
0119     testExtraction();
0120 }
0121 
0122 void KCompressionDeviceTest::testGZipBufferedDevice()
0123 {
0124     testBufferedDevice(KCompressionDevice::GZip);
0125 }
0126 
0127 void KCompressionDeviceTest::testBZip2BufferedDevice()
0128 {
0129 #if HAVE_BZIP2_SUPPORT
0130     testBufferedDevice(KCompressionDevice::BZip2);
0131 #else
0132     QSKIP("This test needs bzip2 support");
0133 #endif
0134 }
0135 
0136 void KCompressionDeviceTest::testXzBufferedDevice()
0137 {
0138 #if HAVE_XZ_SUPPORT
0139     testBufferedDevice(KCompressionDevice::Xz);
0140 #else
0141     QSKIP("This test needs xz support");
0142 #endif
0143 }
0144 
0145 void KCompressionDeviceTest::testZstdBufferedDevice()
0146 {
0147 #ifdef HAVE_ZSTD_SUPPORT_FILE
0148     testBufferedDevice(KCompressionDevice::Zstd);
0149 #else
0150     QSKIP("This test needs zstd support");
0151 #endif
0152 }
0153 
0154 void KCompressionDeviceTest::testWriteErrorOnOpen()
0155 {
0156     // GIVEN
0157     QString fileName("/I/dont/exist/kcompressiondevicetest-write.gz");
0158     KCompressionDevice dev(fileName, KCompressionDevice::GZip);
0159     // WHEN
0160     QVERIFY(!dev.open(QIODevice::WriteOnly));
0161     // THEN
0162     QCOMPARE(dev.error(), QFileDevice::OpenError);
0163 #ifdef Q_OS_WIN
0164     QCOMPARE(dev.errorString(), QStringLiteral("The system cannot find the path specified."));
0165 #else
0166     QCOMPARE(dev.errorString(), QStringLiteral("No such file or directory"));
0167 #endif
0168 }
0169 
0170 void KCompressionDeviceTest::testWriteErrorOnClose()
0171 {
0172     // GIVEN
0173     QFile file("kcompressiondevicetest-write.gz");
0174     KCompressionDevice dev(&file, false, KCompressionDevice::GZip);
0175     QVERIFY(dev.open(QIODevice::WriteOnly));
0176     const QByteArray data = "Hello world";
0177     QCOMPARE(dev.write(data), data.size());
0178     // This is nasty, it's just a way to try and trigger an error on flush, without filling up a partition first ;)
0179     file.close();
0180     QVERIFY(file.open(QIODevice::ReadOnly));
0181     QTest::ignoreMessage(QtWarningMsg, "QIODevice::write (QFile, \"kcompressiondevicetest-write.gz\"): ReadOnly device");
0182 
0183     // WHEN
0184     dev.close(); // I want a QVERIFY here... https://bugreports.qt.io/browse/QTBUG-70033
0185 
0186     // THEN
0187     QCOMPARE(int(dev.error()), int(QFileDevice::WriteError));
0188 }
0189 
0190 void KCompressionDeviceTest::testSeekReadUncompressedBuffer_data()
0191 {
0192     QTest::addColumn<int>("dataSize");
0193     QTest::addColumn<int>("realDataPos");
0194     QTest::newRow("1.5buffer") << BUFFER_SIZE + BUFFER_SIZE / 2 << BUFFER_SIZE;
0195     QTest::newRow("5seekbuffer") << 5 * SEEK_BUFFER_SIZE << 4 * SEEK_BUFFER_SIZE;
0196 }
0197 
0198 void KCompressionDeviceTest::testSeekReadUncompressedBuffer()
0199 {
0200     QFETCH(int, dataSize);
0201     QFETCH(int, realDataPos);
0202 
0203     QByteArray ba(dataSize, 0);
0204 
0205     // all data is zero except after realDataPos that it's 0 to 9
0206     for (int i = 0; i < 10; ++i) {
0207         ba[realDataPos + i] = i;
0208     }
0209 
0210     QBuffer b;
0211     b.setData(ba);
0212     QVERIFY(b.open(QIODevice::ReadOnly));
0213 
0214     KCompressionDevice kcd(&b, false, KCompressionDevice::GZip);
0215     QVERIFY(kcd.open(QIODevice::ReadOnly));
0216     QVERIFY(kcd.seek(realDataPos));
0217 
0218     // the 10 bytes after realDataPos should be 0 to 9
0219     const QByteArray kcdData = kcd.read(10);
0220     QCOMPARE(kcdData.size(), 10);
0221     for (int i = 0; i < kcdData.size(); ++i) {
0222         QCOMPARE(kcdData[i], i);
0223     }
0224 }
0225 
0226 #include "moc_kcompressiondevicetest.cpp"