summaryrefslogtreecommitdiffstats
path: root/lib/store/tests/storage_test.cpp
blob: baef1b83783ab83f7677a6d3c979a13230681f03 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* This file is part of the KDE project
   Copyright (C) 2002 Werner Trobin <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <tqfile.h>
#include <tqdir.h>
#include <kcmdlineargs.h>
#include <kapplication.h>

#include <KoStore.h>
#include <kdebug.h>
#include <stdlib.h>

#include <string.h>

namespace {
    const char* const test1 = "This test checks whether we're able to write to some arbitrary directory.\n";
    const char* const testDir = "0";
    const char* const testDirResult = "0/";
    const char* const test2 = "This time we try to append the given relative path to the current dir.\n";
    const char* const test3 = "<xml>Hello World</xml>";
    const char* const testDir2 = "test2/with/a";
    const char* const testDir2Result = "0/test2/with/a/";
    const char* const test4 = "<xml>Heureka, it works</xml>";

    const char* const badStorage = "Ooops, bad storage???";
    const char* const unableToOpen = "Couldn't open storage!";
    const char* const brokenPath = "Path handling broken!";
    const char* const unableToRead = "Couldn't read stream back!";
}

int cleanUp( KoStore* store, const TQString& testFile, const char* error )
{
    TQFile::remove( testFile );
    delete store;
    kdDebug() << error << endl;
    return 1;
}

int test( const char* testName, KoStore::Backend backend, const TQString& testFile )
{
    if ( TQFile::exists( testFile ) )
        TQFile::remove( testFile );
    TQDir dirTest( testFile );
    if ( dirTest.exists() ) {
        system( TQCString( "rm -rf " ) + TQFile::encodeName( testFile ) ); // TQDir::rmdir isn't recursive!
    }

    kdDebug() << "======================="<<testName<<"====================================" << endl;
    KoStore* store = KoStore::createStore( testFile, KoStore::Write, "", backend );
    if ( store->bad() )
        return cleanUp( store, testFile, badStorage );

    if ( store->open( "test1/with/a/relative/dir.txt" ) ) {
        for ( int i = 0; i < 100; ++i )
            store->write( test1, strlen( test1 ) );
        store->close();
    }
    else
        return cleanUp( store, testFile, unableToOpen );

    store->enterDirectory( testDir );
    if ( store->currentPath() != TQString( testDirResult ) )
        return cleanUp( store, testFile, brokenPath );

    if ( store->open( "test2/with/a/relative/dir.txt" ) ) {
        for ( int i = 0; i < 100; ++i )
            store->write( test2, strlen( test2 ) );
        store->close();
    }
    else
        return cleanUp( store, testFile, unableToOpen );

    if ( store->open( "root" ) ) {
        store->write( test3, strlen( test3 ) );
        store->close();
    }
    else
        return cleanUp( store, testFile, unableToOpen );

    store->enterDirectory( testDir2 );
    if ( store->currentPath() != TQString( testDir2Result ) )
        return cleanUp( store, testFile, brokenPath );

    if ( store->open( "root" ) ) {
        store->write( test4, strlen( test4 ) );
        store->close();
    }
    else
        return cleanUp( store, testFile, unableToOpen );

    if ( store->isOpen() )
        store->close();
    delete store;

    kdDebug() << "===========================================================" << endl;

    store = KoStore::createStore( testFile, KoStore::Read, "", backend );
    if ( store->bad() )
        return cleanUp( store, testFile, badStorage );

    if ( store->open( "test1/with/a/relative/dir.txt" ) ) {
        TQIODevice* dev = store->device();
        int i = 0,  lim = strlen( test1 ),  count = 0;
        while ( static_cast<char>( dev->getch() ) == test1[i++] ) {
            if ( i == lim ) {
                i = 0;
                ++count;
            }
        }
        store->close();
        if ( count != 100 )
            return cleanUp( store, testFile, unableToRead );
    }
    else
        return cleanUp( store, testFile, unableToOpen );

    store->enterDirectory( testDir );
    if ( store->currentPath() != TQString( testDirResult ) )
        return cleanUp( store, testFile, brokenPath );

    if ( store->open( "test2/with/a/relative/dir.txt" ) ) {
        TQIODevice* dev = store->device();
        int i = 0,  lim = strlen( test2 ),  count = 0;
        while ( static_cast<char>( dev->getch() ) == test2[i++] ) {
            if ( i == lim ) {
                i = 0;
                ++count;
            }
        }
        store->close();
        if ( count != 100 )
            return cleanUp( store, testFile, unableToRead );
    }
    else
        return cleanUp( store, testFile, unableToOpen );

    store->enterDirectory( testDir2 );
    store->pushDirectory();

    while ( store->leaveDirectory() );
    store->enterDirectory( testDir );
    if ( store->currentPath() != TQString( testDirResult ) )
        return cleanUp( store, testFile, brokenPath );

    if ( store->open( "root" ) ) {
        if ( store->size() == 22 ) {
            TQIODevice* dev = store->device();
            unsigned int i = 0;
            while ( static_cast<char>( dev->getch() ) == test3[i++] );
            store->close();
            if ( ( i - 1 ) != strlen( test3 ) )
                return cleanUp( store, testFile, unableToRead );
        }
        else {
            kdError() << "Wrong size! maindoc.xml is " << store->size() << " should be 22." << endl;
            delete store;
            return 1;
        }
    }
    else {
        kdError() << "Couldn't open storage!" << endl;
        delete store;
        return 1;
    }

    store->popDirectory();
    if ( store->currentPath() != TQString( testDir2Result ) )
        return cleanUp( store, testFile, brokenPath );

    if ( store->open( "root" ) ) {
        char buf[29];
        store->read( buf, 28 );
        buf[28] = '\0';
        store->close();
        if ( strncmp( buf, test4, 28 ) != 0 )
            return cleanUp( store, testFile, unableToRead );
    }
    else
        return cleanUp( store, testFile, unableToOpen );

    if ( store->isOpen() )
        store->close();
    delete store;
    TQFile::remove( testFile );

    kdDebug() << "===========================================================" << endl;
    return 0;
}

int main( int argc, char **argv )
{
    TDECmdLineArgs::init( argc, argv, "storage_test", "A test for the KoStore classes", "1" );
    TDEApplication app( argc, argv );

    // KZip (due to KSaveFile) doesn't support relative filenames
    // So use $PWD as base for the paths explicitely.
    const TQString testDir = TQDir::currentDirPath();
    if ( test( "Tar", KoStore::Tar, testDir+"test.tgz" ) != 0 )
      return 1;
    if ( test( "Directory", KoStore::Directory, testDir+"testdir/maindoc.xml" ) != 0 )
      return 1;
    if ( test( "Zip", KoStore::Zip, testDir+"test.zip" ) != 0 )
      return 1;
}