File indexing completed on 2024-06-16 04:14:02

0001 /* This file is part of the KDE project
0002    SPDX-FileCopyrightText: 2002 Werner Trobin <trobin@kde.org>
0003    SPDX-FileCopyrightText: 2008 Thomas Zander <zander@kde.org>
0004 
0005    SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include <QFile>
0009 #include <QDir>
0010 
0011 #include <KoStore.h>
0012 #include <OdfDebug.h>
0013 #include <stdlib.h>
0014 
0015 #include <string.h>
0016 
0017 #include <QTest>
0018 
0019 class TestStorage : public QObject
0020 {
0021     Q_OBJECT
0022 private Q_SLOTS:
0023     void storage_data();
0024     void storage();
0025     void storage2_data();
0026     void storage2();
0027 
0028 private:
0029     char getch(QIODevice * dev);
0030 };
0031 
0032 
0033 char TestStorage::getch(QIODevice * dev)
0034 {
0035     char c = 0;
0036     dev->getChar(&c);
0037     return c;
0038 }
0039 
0040 void TestStorage::storage_data()
0041 {
0042     QTest::addColumn<int>("type");
0043     QTest::addColumn<QString>("testFile");
0044 
0045     QTest::newRow("directory") << (int) KoStore::Directory << "testdir";
0046     QTest::newRow("zip") << (int) KoStore::Zip <<"test.zip";
0047 }
0048 
0049 void TestStorage::storage()
0050 {
0051     const char test1[] = "This test checks whether we're able to write to some arbitrary directory.\n";
0052     const char testDir[] = "0";
0053     const char testDirResult[] = "0/";
0054     const char test2[] = "This time we try to append the given relative path to the current dir.\n";
0055     const char test3[] = "<xml>Hello World</xml>";
0056     const char testDir2[] = "test2/with/a";
0057     const char testDir2Result[] = "0/test2/with/a/";
0058     const char test4[] = "<xml>Heureka, it works</xml>";
0059 
0060     QFETCH(int, type);
0061     QFETCH(QString, testFile);
0062     KoStore::Backend backend = static_cast<KoStore::Backend>(type);
0063 
0064     if (QFile::exists(testFile))
0065         QFile::remove(testFile);
0066 
0067     QDir dirTest(testFile);
0068     if (dirTest.exists()) {
0069 #ifdef Q_OS_UNIX
0070         QByteArray ba = QByteArray("rm -rf ") + QFile::encodeName(testFile);
0071         Q_UNUSED(system(ba.constData()));       // QDir::rmdir isn't recursive!
0072 #else
0073         QFAIL("build dir not empty");
0074 #endif
0075     }
0076 
0077     KoStore* store = KoStore::createStore(testFile, KoStore::Write, "", backend);
0078     QVERIFY(store);
0079     QVERIFY(store->bad() == false);
0080 
0081     QVERIFY(store->open("test1/with/a/relative/dir.txt"));
0082     for (int i = 0; i < 100; ++i)
0083         store->write(test1, strlen(test1));
0084     store->close();
0085 
0086     store->enterDirectory(testDir);
0087     QCOMPARE(store->currentPath(), QString(testDirResult));
0088 
0089     QVERIFY(store->open("test2/with/a/relative/dir.txt"));
0090     for (int i = 0; i < 100; ++i)
0091         store->write(test2, strlen(test2));
0092     store->close();
0093 
0094     QVERIFY(store->open("root"));
0095     store->write(test3, strlen(test3));
0096     store->close();
0097 
0098     store->enterDirectory(testDir2);
0099     QCOMPARE(store->currentPath(), QString(testDir2Result));
0100 
0101     QVERIFY(store->open("root"));
0102     store->write(test4, strlen(test4));
0103     store->close();
0104 
0105     if (store->isOpen())
0106         store->close();
0107     delete store;
0108 
0109     store = KoStore::createStore(testFile, KoStore::Read, "", backend);
0110     QVERIFY(store->bad() == false);
0111 
0112     QVERIFY (store->open("test1/with/a/relative/dir.txt"));
0113     QIODevice* dev = store->device();
0114     int i = 0,  lim = strlen(test1),  count = 0;
0115     while (static_cast<char>(getch(dev)) == test1[i++]) {
0116         if (i == lim) {
0117             i = 0;
0118             ++count;
0119         }
0120     }
0121     store->close();
0122     QCOMPARE(count, 100);
0123 
0124     store->enterDirectory(testDir);
0125     QCOMPARE (store->currentPath(), QString(testDirResult));
0126 
0127     QVERIFY (store->open("test2/with/a/relative/dir.txt"));
0128     dev = store->device();
0129     i = 0;
0130     lim = strlen(test2);
0131     count = 0;
0132     while (static_cast<char>(getch(dev)) == test2[i++]) {
0133         if (i == lim) {
0134             i = 0;
0135             ++count;
0136         }
0137     }
0138     store->close();
0139     QCOMPARE(count, 100);
0140 
0141     store->enterDirectory(testDir2);
0142     store->pushDirectory();
0143 
0144     while (store->leaveDirectory()) {
0145         ;
0146     }
0147     store->enterDirectory(testDir);
0148     QCOMPARE (store->currentPath(), QString(testDirResult));
0149 
0150     QVERIFY (store->open("root"));
0151     QCOMPARE (store->size(), (qint64) 22);
0152     dev = store->device();
0153     QByteArray dataReadBack = dev->read(strlen(test3));
0154     store->close();
0155     QCOMPARE (dataReadBack, QByteArray(test3));
0156 
0157     store->popDirectory();
0158     QCOMPARE(store->currentPath(), QString(testDir2Result));
0159 
0160     QVERIFY (store->hasFile("relative/dir.txt"));
0161     
0162     QVERIFY (store->open("root"));
0163     char buf[29];
0164     store->read(buf, 28);
0165     buf[28] = '\0';
0166     store->close();
0167     QVERIFY(strncmp(buf, test4, 28) == 0);
0168 
0169     if (store->isOpen())
0170         store->close();
0171     delete store;
0172     QFile::remove(testFile);
0173 }
0174 
0175 #define DATALEN 64
0176 void TestStorage::storage2_data()
0177 {
0178     QTest::addColumn<int>("type");
0179     QTest::addColumn<QString>("testFile");
0180 
0181     QTest::newRow("directory") << (int) KoStore::Directory << "testdir";
0182     QTest::newRow("zip") << (int) KoStore::Zip <<"test.zip";
0183 }
0184 
0185 void TestStorage::storage2()
0186 {
0187     QFETCH(int, type);
0188     QFETCH(QString, testFile);
0189     KoStore::Backend backend = static_cast<KoStore::Backend>(type);
0190 
0191     if (QFile::exists(testFile))
0192         QFile::remove(testFile);
0193 
0194     QDir dirTest(testFile);
0195     if (dirTest.exists()) {
0196 #ifdef Q_OS_UNIX
0197         QByteArray ba = QByteArray("rm -rf ") + QFile::encodeName(testFile);
0198         Q_UNUSED(system(ba.constData()));       // QDir::rmdir isn't recursive!
0199 #else
0200         QFAIL("build dir not empty");
0201 #endif
0202     }
0203 
0204     KoStore* store = KoStore::createStore(testFile, KoStore::Write, "", backend);
0205     QVERIFY(store->bad() == false);
0206 
0207     // Write
0208     QVERIFY (store->open("layer"));
0209     char str[DATALEN];
0210 
0211     sprintf(str, "1,2,3,4\n");
0212     store->write(str, strlen(str));
0213     memset(str, '\0', DATALEN);
0214     store->write(str, DATALEN);
0215 
0216     store->close();
0217     delete store;
0218 
0219     store = KoStore::createStore(testFile, KoStore::Read, "", backend);
0220     QVERIFY(store->bad() == false);
0221     // Read back
0222     QVERIFY (store->open("layer"));
0223     char str2[DATALEN];
0224     QIODevice *stream = store->device(); // << Possible suspect!
0225 
0226     stream->readLine(str2, DATALEN);      // << as is this
0227     qint64 len = store->read(str2, DATALEN);
0228     QCOMPARE(len, (qint64) DATALEN);
0229     store->close();
0230     delete store;
0231 
0232     QFile::remove(testFile);
0233 }
0234 
0235 QTEST_GUILESS_MAIN(TestStorage)
0236 #include <TestStorage.moc>
0237