~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to libs/odf/tests/TestStorage.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
19
*/
 
20
 
 
21
#include <QFile>
 
22
#include <QDir>
 
23
#include <kcmdlineargs.h>
 
24
#include <kapplication.h>
 
25
 
 
26
#include <KoStore.h>
 
27
#include <KoEncryptionChecker.h>
 
28
#include <kdebug.h>
 
29
#include <stdlib.h>
 
30
 
 
31
#include <string.h>
 
32
 
 
33
#include <qtest_kde.h>
 
34
 
 
35
class TestStorage : public QObject
 
36
{
 
37
    Q_OBJECT
 
38
private slots:
 
39
    void storage_data();
 
40
    void storage();
 
41
    void storage2_data();
 
42
    void storage2();
 
43
 
 
44
private:
 
45
    char getch(QIODevice * dev);
 
46
};
 
47
 
 
48
 
 
49
char TestStorage::getch(QIODevice * dev)
 
50
{
 
51
    char c = 0;
 
52
    dev->getChar(&c);
 
53
    return c;
 
54
}
 
55
 
 
56
void TestStorage::storage_data()
 
57
{
 
58
    QTest::addColumn<int>("type");
 
59
    QTest::addColumn<QString>("testFile");
 
60
 
 
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";
 
64
#ifdef QCA2
 
65
    if (KoEncryptionChecker::isEncryptionSupported()) {
 
66
        QTest::newRow("Encrypted") << (int) KoStore::Encrypted << "testEncrypted.zip";
 
67
    }
 
68
#endif
 
69
}
 
70
 
 
71
void TestStorage::storage()
 
72
{
 
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>";
 
81
 
 
82
    QFETCH(int, type);
 
83
    QFETCH(QString, testFile);
 
84
    KoStore::Backend backend = static_cast<KoStore::Backend>(type);
 
85
 
 
86
    if (QFile::exists(testFile))
 
87
        QFile::remove(testFile);
 
88
 
 
89
    QDir dirTest(testFile);
 
90
    if (dirTest.exists()) {
 
91
#ifdef Q_OS_UNIX
 
92
        system(QByteArray("rm -rf ") + QFile::encodeName(testFile));       // QDir::rmdir isn't recursive!
 
93
#else
 
94
        QFAIL("build dir not empty");
 
95
#endif
 
96
    }
 
97
 
 
98
    KoStore* store = KoStore::createStore(testFile, KoStore::Write, "", backend);
 
99
    QVERIFY(store);
 
100
    QVERIFY(store->bad() == false);
 
101
 
 
102
    if (store->isEncrypted())
 
103
        store->setPassword("password");
 
104
 
 
105
    QVERIFY(store->open("test1/with/a/relative/dir.txt"));
 
106
    for (int i = 0; i < 100; ++i)
 
107
        store->write(test1, strlen(test1));
 
108
    store->close();
 
109
 
 
110
    store->enterDirectory(testDir);
 
111
    QCOMPARE(store->currentPath(), QString(testDirResult));
 
112
 
 
113
    QVERIFY(store->open("test2/with/a/relative/dir.txt"));
 
114
    for (int i = 0; i < 100; ++i)
 
115
        store->write(test2, strlen(test2));
 
116
    store->close();
 
117
 
 
118
    QVERIFY(store->open("root"));
 
119
    store->write(test3, strlen(test3));
 
120
    store->close();
 
121
 
 
122
    store->enterDirectory(testDir2);
 
123
    QCOMPARE(store->currentPath(), QString(testDir2Result));
 
124
 
 
125
    QVERIFY(store->open("root"));
 
126
    store->write(test4, strlen(test4));
 
127
    store->close();
 
128
 
 
129
    if (store->isOpen())
 
130
        store->close();
 
131
    delete store;
 
132
 
 
133
    store = KoStore::createStore(testFile, KoStore::Read, "", backend);
 
134
    QVERIFY(store->bad() == false);
 
135
 
 
136
    if (store->isEncrypted())
 
137
        store->setPassword("password");
 
138
 
 
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++]) {
 
143
        if (i == lim) {
 
144
            i = 0;
 
145
            ++count;
 
146
        }
 
147
    }
 
148
    store->close();
 
149
    QCOMPARE(count, 100);
 
150
 
 
151
    store->enterDirectory(testDir);
 
152
    QCOMPARE (store->currentPath(), QString(testDirResult));
 
153
 
 
154
    QVERIFY (store->open("test2/with/a/relative/dir.txt"));
 
155
    dev = store->device();
 
156
    i = 0;
 
157
    lim = strlen(test2);
 
158
    count = 0;
 
159
    while (static_cast<char>(getch(dev)) == test2[i++]) {
 
160
        if (i == lim) {
 
161
            i = 0;
 
162
            ++count;
 
163
        }
 
164
    }
 
165
    store->close();
 
166
    QCOMPARE(count, 100);
 
167
 
 
168
    store->enterDirectory(testDir2);
 
169
    store->pushDirectory();
 
170
 
 
171
    while (store->leaveDirectory()) {
 
172
        ;
 
173
    }
 
174
    store->enterDirectory(testDir);
 
175
    QCOMPARE (store->currentPath(), QString(testDirResult));
 
176
 
 
177
    QVERIFY (store->open("root"));
 
178
    QCOMPARE (store->size(), (qint64) 22);
 
179
    dev = store->device();
 
180
    QByteArray dataReadBack = dev->read(strlen(test3));
 
181
    store->close();
 
182
    QCOMPARE (dataReadBack, QByteArray(test3));
 
183
 
 
184
    store->popDirectory();
 
185
    QCOMPARE(store->currentPath(), QString(testDir2Result));
 
186
 
 
187
    QVERIFY (store->hasFile("relative/dir.txt"));
 
188
    
 
189
    QVERIFY (store->open("root"));
 
190
    char buf[29];
 
191
    store->read(buf, 28);
 
192
    buf[28] = '\0';
 
193
    store->close();
 
194
    QVERIFY(strncmp(buf, test4, 28) == 0);
 
195
 
 
196
    if (store->isOpen())
 
197
        store->close();
 
198
    delete store;
 
199
    QFile::remove(testFile);
 
200
}
 
201
 
 
202
#define DATALEN 64
 
203
void TestStorage::storage2_data()
 
204
{
 
205
    QTest::addColumn<int>("type");
 
206
    QTest::addColumn<QString>("testFile");
 
207
 
 
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";
 
211
}
 
212
 
 
213
void TestStorage::storage2()
 
214
{
 
215
    QFETCH(int, type);
 
216
    QFETCH(QString, testFile);
 
217
    KoStore::Backend backend = static_cast<KoStore::Backend>(type);
 
218
 
 
219
    if (QFile::exists(testFile))
 
220
        QFile::remove(testFile);
 
221
 
 
222
    QDir dirTest(testFile);
 
223
    if (dirTest.exists()) {
 
224
#ifdef Q_OS_UNIX
 
225
        system(QByteArray("rm -rf ") + QFile::encodeName(testFile));       // QDir::rmdir isn't recursive!
 
226
#else
 
227
        QFAIL("build dir not empty");
 
228
#endif
 
229
    }
 
230
 
 
231
    KoStore* store = KoStore::createStore(testFile, KoStore::Write, "", backend);
 
232
    QVERIFY(store->bad() == false);
 
233
 
 
234
    // Write
 
235
    QVERIFY (store->open("layer"));
 
236
    char str[DATALEN];
 
237
 
 
238
    sprintf(str, "1,2,3,4\n");
 
239
    store->write(str, strlen(str));
 
240
    memset(str, '\0', DATALEN);
 
241
    store->write(str, DATALEN);
 
242
 
 
243
    store->close();
 
244
    delete store;
 
245
 
 
246
    store = KoStore::createStore(testFile, KoStore::Read, "", backend);
 
247
    QVERIFY(store->bad() == false);
 
248
    // Read back
 
249
    QVERIFY (store->open("layer"));
 
250
    char str2[DATALEN];
 
251
    QIODevice *stream = store->device(); // << Possible suspect!
 
252
 
 
253
    stream->readLine(str2, DATALEN);      // << as is this
 
254
    qint64 len = store->read(str2, DATALEN);
 
255
    QCOMPARE(len, (qint64) DATALEN);
 
256
    store->close();
 
257
    delete store;
 
258
 
 
259
    QFile::remove(testFile);
 
260
}
 
261
 
 
262
QTEST_KDEMAIN(TestStorage, NoGUI)
 
263
#include <TestStorage.moc>
 
264