1
/* This file is part of the KDE project
2
Copyright (C) 2002 Werner Trobin <trobin@kde.org>
3
Copyright (C) 2008 Thomas Zander <zander@kde.org>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to
17
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
* Boston, MA 02110-1301, USA.
23
#include <kcmdlineargs.h>
24
#include <kapplication.h>
27
#include <KoEncryptionChecker.h>
33
#include <qtest_kde.h>
35
class TestStorage : public QObject
45
char getch(QIODevice * dev);
49
char TestStorage::getch(QIODevice * dev)
56
void TestStorage::storage_data()
58
QTest::addColumn<int>("type");
59
QTest::addColumn<QString>("testFile");
61
QTest::newRow("tar") << (int) KoStore::Tar << "test.tgz";
62
QTest::newRow("directory") << (int) KoStore::Directory << "testdir/maindoc.xml";
63
QTest::newRow("zip") << (int) KoStore::Zip <<"test.zip";
65
if (KoEncryptionChecker::isEncryptionSupported()) {
66
QTest::newRow("Encrypted") << (int) KoStore::Encrypted << "testEncrypted.zip";
71
void TestStorage::storage()
73
const char* const test1 = "This test checks whether we're able to write to some arbitrary directory.\n";
74
const char* const testDir = "0";
75
const char* const testDirResult = "0/";
76
const char* const test2 = "This time we try to append the given relative path to the current dir.\n";
77
const char* const test3 = "<xml>Hello World</xml>";
78
const char* const testDir2 = "test2/with/a";
79
const char* const testDir2Result = "0/test2/with/a/";
80
const char* const test4 = "<xml>Heureka, it works</xml>";
83
QFETCH(QString, testFile);
84
KoStore::Backend backend = static_cast<KoStore::Backend>(type);
86
if (QFile::exists(testFile))
87
QFile::remove(testFile);
89
QDir dirTest(testFile);
90
if (dirTest.exists()) {
92
system(QByteArray("rm -rf ") + QFile::encodeName(testFile)); // QDir::rmdir isn't recursive!
94
QFAIL("build dir not empty");
98
KoStore* store = KoStore::createStore(testFile, KoStore::Write, "", backend);
100
QVERIFY(store->bad() == false);
102
if (store->isEncrypted())
103
store->setPassword("password");
105
QVERIFY(store->open("test1/with/a/relative/dir.txt"));
106
for (int i = 0; i < 100; ++i)
107
store->write(test1, strlen(test1));
110
store->enterDirectory(testDir);
111
QCOMPARE(store->currentPath(), QString(testDirResult));
113
QVERIFY(store->open("test2/with/a/relative/dir.txt"));
114
for (int i = 0; i < 100; ++i)
115
store->write(test2, strlen(test2));
118
QVERIFY(store->open("root"));
119
store->write(test3, strlen(test3));
122
store->enterDirectory(testDir2);
123
QCOMPARE(store->currentPath(), QString(testDir2Result));
125
QVERIFY(store->open("root"));
126
store->write(test4, strlen(test4));
133
store = KoStore::createStore(testFile, KoStore::Read, "", backend);
134
QVERIFY(store->bad() == false);
136
if (store->isEncrypted())
137
store->setPassword("password");
139
QVERIFY (store->open("test1/with/a/relative/dir.txt"));
140
QIODevice* dev = store->device();
141
int i = 0, lim = strlen(test1), count = 0;
142
while (static_cast<char>(getch(dev)) == test1[i++]) {
149
QCOMPARE(count, 100);
151
store->enterDirectory(testDir);
152
QCOMPARE (store->currentPath(), QString(testDirResult));
154
QVERIFY (store->open("test2/with/a/relative/dir.txt"));
155
dev = store->device();
159
while (static_cast<char>(getch(dev)) == test2[i++]) {
166
QCOMPARE(count, 100);
168
store->enterDirectory(testDir2);
169
store->pushDirectory();
171
while (store->leaveDirectory()) {
174
store->enterDirectory(testDir);
175
QCOMPARE (store->currentPath(), QString(testDirResult));
177
QVERIFY (store->open("root"));
178
QCOMPARE (store->size(), (qint64) 22);
179
dev = store->device();
180
QByteArray dataReadBack = dev->read(strlen(test3));
182
QCOMPARE (dataReadBack, QByteArray(test3));
184
store->popDirectory();
185
QCOMPARE(store->currentPath(), QString(testDir2Result));
187
QVERIFY (store->hasFile("relative/dir.txt"));
189
QVERIFY (store->open("root"));
191
store->read(buf, 28);
194
QVERIFY(strncmp(buf, test4, 28) == 0);
199
QFile::remove(testFile);
203
void TestStorage::storage2_data()
205
QTest::addColumn<int>("type");
206
QTest::addColumn<QString>("testFile");
208
QTest::newRow("tar") << (int) KoStore::Tar << "test.tgz";
209
QTest::newRow("directory") << (int) KoStore::Directory << "testdir/maindoc.xml";
210
QTest::newRow("zip") << (int) KoStore::Zip <<"test.zip";
213
void TestStorage::storage2()
216
QFETCH(QString, testFile);
217
KoStore::Backend backend = static_cast<KoStore::Backend>(type);
219
if (QFile::exists(testFile))
220
QFile::remove(testFile);
222
QDir dirTest(testFile);
223
if (dirTest.exists()) {
225
system(QByteArray("rm -rf ") + QFile::encodeName(testFile)); // QDir::rmdir isn't recursive!
227
QFAIL("build dir not empty");
231
KoStore* store = KoStore::createStore(testFile, KoStore::Write, "", backend);
232
QVERIFY(store->bad() == false);
235
QVERIFY (store->open("layer"));
238
sprintf(str, "1,2,3,4\n");
239
store->write(str, strlen(str));
240
memset(str, '\0', DATALEN);
241
store->write(str, DATALEN);
246
store = KoStore::createStore(testFile, KoStore::Read, "", backend);
247
QVERIFY(store->bad() == false);
249
QVERIFY (store->open("layer"));
251
QIODevice *stream = store->device(); // << Possible suspect!
253
stream->readLine(str2, DATALEN); // << as is this
254
qint64 len = store->read(str2, DATALEN);
255
QCOMPARE(len, (qint64) DATALEN);
259
QFile::remove(testFile);
262
QTEST_KDEMAIN(TestStorage, NoGUI)
263
#include <TestStorage.moc>