00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <QFile>
00021 #include <QDir>
00022 #include <kcmdlineargs.h>
00023 #include <kapplication.h>
00024
00025 #include <KoStore.h>
00026 #include <kdebug.h>
00027 #include <stdlib.h>
00028
00029 #include <string.h>
00030
00031 namespace {
00032 const char* const test1 = "This test checks whether we're able to write to some arbitrary directory.\n";
00033 const char* const testDir = "0";
00034 const char* const testDirResult = "0/";
00035 const char* const test2 = "This time we try to append the given relative path to the current dir.\n";
00036 const char* const test3 = "<xml>Hello World</xml>";
00037 const char* const testDir2 = "test2/with/a";
00038 const char* const testDir2Result = "0/test2/with/a/";
00039 const char* const test4 = "<xml>Heureka, it works</xml>";
00040
00041 const char* const badStorage = "Ooops, bad storage???";
00042 const char* const unableToOpen = "Couldn't open storage!";
00043 const char* const brokenPath = "Path handling broken!";
00044 const char* const unableToRead = "Couldn't read stream back!";
00045 }
00046
00047 char getch( QIODevice * dev ) {
00048 char c = 0;
00049 dev->getChar( &c );
00050 return c;
00051 }
00052
00053 int cleanUp( KoStore* store, const QString& testFile, const char* error )
00054 {
00055 QFile::remove( testFile );
00056 delete store;
00057 kDebug() << error << endl;
00058 return 1;
00059 }
00060
00061 int test( const char* testName, KoStore::Backend backend, const QString& testFile )
00062 {
00063 if ( QFile::exists( testFile ) )
00064 QFile::remove( testFile );
00065 QDir dirTest( testFile );
00066 if ( dirTest.exists() ) {
00067 system( QByteArray( "rm -rf " ) + QFile::encodeName( testFile ) );
00068 }
00069
00070 kDebug() << "======================="<<testName<<"====================================" << endl;
00071 KoStore* store = KoStore::createStore( testFile, KoStore::Write, "", backend );
00072 if ( store->bad() )
00073 return cleanUp( store, testFile, badStorage );
00074
00075 if ( store->open( "test1/with/a/relative/dir.txt" ) ) {
00076 for ( int i = 0; i < 100; ++i )
00077 store->write( test1, strlen( test1 ) );
00078 store->close();
00079 }
00080 else
00081 return cleanUp( store, testFile, unableToOpen );
00082
00083 store->enterDirectory( testDir );
00084 if ( store->currentPath() != QString( testDirResult ) )
00085 return cleanUp( store, testFile, brokenPath );
00086
00087 if ( store->open( "test2/with/a/relative/dir.txt" ) ) {
00088 for ( int i = 0; i < 100; ++i )
00089 store->write( test2, strlen( test2 ) );
00090 store->close();
00091 }
00092 else
00093 return cleanUp( store, testFile, unableToOpen );
00094
00095 if ( store->open( "root" ) ) {
00096 store->write( test3, strlen( test3 ) );
00097 store->close();
00098 }
00099 else
00100 return cleanUp( store, testFile, unableToOpen );
00101
00102 store->enterDirectory( testDir2 );
00103 if ( store->currentPath() != QString( testDir2Result ) )
00104 return cleanUp( store, testFile, brokenPath );
00105
00106 if ( store->open( "root" ) ) {
00107 store->write( test4, strlen( test4 ) );
00108 store->close();
00109 }
00110 else
00111 return cleanUp( store, testFile, unableToOpen );
00112
00113 if ( store->isOpen() )
00114 store->close();
00115 delete store;
00116
00117 kDebug() << "===========================================================" << endl;
00118
00119 store = KoStore::createStore( testFile, KoStore::Read, "", backend );
00120 if ( store->bad() )
00121 return cleanUp( store, testFile, badStorage );
00122
00123 if ( store->open( "test1/with/a/relative/dir.txt" ) ) {
00124 QIODevice* dev = store->device();
00125 int i = 0, lim = strlen( test1 ), count = 0;
00126 while ( static_cast<char>( getch(dev) ) == test1[i++] ) {
00127 if ( i == lim ) {
00128 i = 0;
00129 ++count;
00130 }
00131 }
00132 store->close();
00133 if ( count != 100 )
00134 return cleanUp( store, testFile, unableToRead );
00135 }
00136 else
00137 return cleanUp( store, testFile, unableToOpen );
00138
00139 store->enterDirectory( testDir );
00140 if ( store->currentPath() != QString( testDirResult ) )
00141 return cleanUp( store, testFile, brokenPath );
00142
00143 if ( store->open( "test2/with/a/relative/dir.txt" ) ) {
00144 QIODevice* dev = store->device();
00145 int i = 0, lim = strlen( test2 ), count = 0;
00146 while ( static_cast<char>( getch(dev) ) == test2[i++] ) {
00147 if ( i == lim ) {
00148 i = 0;
00149 ++count;
00150 }
00151 }
00152 store->close();
00153 if ( count != 100 )
00154 return cleanUp( store, testFile, unableToRead );
00155 }
00156 else
00157 return cleanUp( store, testFile, unableToOpen );
00158
00159 store->enterDirectory( testDir2 );
00160 store->pushDirectory();
00161
00162 while ( store->leaveDirectory() );
00163 store->enterDirectory( testDir );
00164 if ( store->currentPath() != QString( testDirResult ) )
00165 return cleanUp( store, testFile, brokenPath );
00166
00167 if ( store->open( "root" ) ) {
00168 if ( store->size() == 22 ) {
00169 QIODevice* dev = store->device();
00170 unsigned int i = 0;
00171 while ( static_cast<char>( getch(dev) ) == test3[i++] );
00172 store->close();
00173 if ( ( i - 1 ) != strlen( test3 ) )
00174 return cleanUp( store, testFile, unableToRead );
00175 }
00176 else {
00177 kError() << "Wrong size! maindoc.xml is " << store->size() << " should be 22." << endl;
00178 delete store;
00179 return 1;
00180 }
00181 }
00182 else {
00183 kError() << "Couldn't open storage!" << endl;
00184 delete store;
00185 return 1;
00186 }
00187
00188 store->popDirectory();
00189 if ( store->currentPath() != QString( testDir2Result ) )
00190 return cleanUp( store, testFile, brokenPath );
00191
00192 if ( store->open( "root" ) ) {
00193 char buf[29];
00194 store->read( buf, 28 );
00195 buf[28] = '\0';
00196 store->close();
00197 if ( strncmp( buf, test4, 28 ) != 0 )
00198 return cleanUp( store, testFile, unableToRead );
00199 }
00200 else
00201 return cleanUp( store, testFile, unableToOpen );
00202
00203 if ( store->isOpen() )
00204 store->close();
00205 delete store;
00206 QFile::remove( testFile );
00207
00208 kDebug() << "===========================================================" << endl;
00209 return 0;
00210 }
00211
00212 int main( int argc, char **argv )
00213 {
00214 KCmdLineArgs::init( argc, argv, "storage_test", "Storage Test", "A test for the KoStore classes", "1" );
00215
00216 KApplication app(false);
00217
00218
00219
00220 const QString testDir = QDir::currentPath();
00221 if ( test( "Tar", KoStore::Tar, testDir+"test.tgz" ) != 0 )
00222 return 1;
00223 if ( test( "Directory", KoStore::Directory, testDir+"testdir/maindoc.xml" ) != 0 )
00224 return 1;
00225 if ( test( "Zip", KoStore::Zip, testDir+"test.zip" ) != 0 )
00226 return 1;
00227 }