~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to tests/archiveReader.cpp

  • Committer: Mantas Kriaučiūnas
  • Date: 2011-07-18 13:06:25 UTC
  • Revision ID: mantas@akl.lt-20110718130625-c5pvifp61e7kj1ol
Included whole irrlicht SVN libraries to work around launchpad recipe issue with quilt, see https://answers.launchpad.net/launchpad/+question/165193

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "testUtils.h"
 
2
 
 
3
using namespace irr;
 
4
using namespace core;
 
5
using namespace io;
 
6
 
 
7
bool testArchive(IFileSystem* fs, const io::path& archiveName)
 
8
{
 
9
        // make sure there is no archive mounted
 
10
        if ( fs->getFileArchiveCount() )
 
11
        {
 
12
                logTestString("Already mounted archives found\n");
 
13
                return false;
 
14
        }
 
15
 
 
16
        if ( !fs->addFileArchive(archiveName, /*bool ignoreCase=*/true, /*bool ignorePaths=*/false) )
 
17
        {
 
18
                logTestString("Mounting archive failed\n");
 
19
                return false;
 
20
        }
 
21
 
 
22
        // make sure there is an archive mounted
 
23
        if ( !fs->getFileArchiveCount() )
 
24
        {
 
25
                logTestString("Mounted archive not in list\n");
 
26
                return false;
 
27
        }
 
28
 
 
29
        // mount again
 
30
        if ( !fs->addFileArchive(archiveName, /*bool ignoreCase=*/true, /*bool ignorePaths=*/false) )
 
31
        {
 
32
                logTestString("Mounting a second time failed\n");
 
33
                fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
34
                return false;
 
35
        }
 
36
 
 
37
        // make sure there is exactly one archive mounted
 
38
        if ( fs->getFileArchiveCount() != 1 )
 
39
        {
 
40
                logTestString("Duplicate mount not recognized\n");
 
41
                while (fs->getFileArchiveCount())
 
42
                        fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
43
                return false;
 
44
        }
 
45
        if (fs->getFileArchive(0)->getType()==io::EFAT_FOLDER)
 
46
        {
 
47
                // mount again with different path end symbol (either with slash or without)
 
48
                core::stringc newArchiveName=archiveName;
 
49
                if (archiveName.lastChar()=='/')
 
50
                        newArchiveName.erase(newArchiveName.size()-1);
 
51
                else
 
52
                        newArchiveName.append('/');
 
53
                if ( !fs->addFileArchive(newArchiveName, /*bool ignoreCase=*/true, /*bool ignorePaths=*/false) )
 
54
                {
 
55
                        logTestString("Mounting a second time with different name failed\n");
 
56
                        fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
57
                        return false;
 
58
                }
 
59
 
 
60
                // make sure there is exactly one archive mounted
 
61
                if ( fs->getFileArchiveCount() != 1 )
 
62
                {
 
63
                        logTestString("Duplicate mount with different filename not recognized\n");
 
64
                        while (fs->getFileArchiveCount())
 
65
                                fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
66
                        return false;
 
67
                }
 
68
        }
 
69
 
 
70
#if 0
 
71
        // log what we got
 
72
        io::IFileArchive* archive = fs->getFileArchive(fs->getFileArchiveCount()-1);
 
73
        const io::IFileList* fileList = archive->getFileList();
 
74
        for ( u32 f=0; f < fileList->getFileCount(); ++f)
 
75
        {
 
76
                logTestString("File name: %s\n", fileList->getFileName(f).c_str());
 
77
                logTestString("Full path: %s\n", fileList->getFullFileName(f).c_str());
 
78
                logTestString("ID: %d\n", fileList->getID(f));
 
79
        }
 
80
#endif
 
81
 
 
82
        io::path filename("mypath/mypath/myfile.txt");
 
83
        if (!fs->existFile(filename))
 
84
        {
 
85
                logTestString("existFile with deep path failed\n");
 
86
                while (fs->getFileArchiveCount())
 
87
                        fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
88
                return false;
 
89
        }
 
90
 
 
91
        const char* names[] = {"test/test.txt", "mypath/myfile.txt", "mypath/mypath/myfile.txt"};
 
92
        const char* basenames[] = {"test.txt", "myfile.txt", "myfile.txt"};
 
93
        const char* content[] = {"Hello world!", "1est\n", "2est"};
 
94
 
 
95
        for (u32 i=0; i<3; ++i)
 
96
        {
 
97
                if (!fs->existFile(names[i]))
 
98
                {
 
99
                        logTestString("existFile failed\n");
 
100
                        while (fs->getFileArchiveCount())
 
101
                                fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
102
                        return false;
 
103
                }
 
104
 
 
105
                IReadFile* readFile = fs->createAndOpenFile(names[i]);
 
106
                if (!readFile)
 
107
                {
 
108
                        logTestString("createAndOpenFile failed\n");
 
109
                        while (fs->getFileArchiveCount())
 
110
                                fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
111
                        return false;
 
112
                }
 
113
 
 
114
                if (fs->getFileBasename(readFile->getFileName()) != basenames[i])
 
115
                {
 
116
                        logTestString("Wrong filename, file list seems to be corrupt\n");
 
117
                        while (fs->getFileArchiveCount())
 
118
                                fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
119
                        readFile->drop();
 
120
                        return false;
 
121
                }
 
122
                char tmp[13] = {'\0'};
 
123
                readFile->read(tmp, 12);
 
124
                if (strcmp(tmp, content[i]))
 
125
                {
 
126
                        logTestString("Read bad data from archive: %s\n", tmp);
 
127
                        while (fs->getFileArchiveCount())
 
128
                                fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
129
                        readFile->drop();
 
130
                        return false;
 
131
                }
 
132
                readFile->drop();
 
133
        }
 
134
 
 
135
        if (!fs->removeFileArchive(fs->getFileArchiveCount()-1))
 
136
        {
 
137
                logTestString("Couldn't remove archive.\n");
 
138
                return false;
 
139
        }
 
140
 
 
141
        // make sure there is no archive mounted
 
142
        if ( fs->getFileArchiveCount() )
 
143
                return false;
 
144
 
 
145
        return true;
 
146
}
 
147
 
 
148
bool testEncryptedZip(IFileSystem* fs)
 
149
{
 
150
        // make sure there is no archive mounted
 
151
        if ( fs->getFileArchiveCount() )
 
152
        {
 
153
                logTestString("Already mounted archives found\n");
 
154
                return false;
 
155
        }
 
156
 
 
157
        const char* archiveName = "media/enc.zip";
 
158
        if ( !fs->addFileArchive(archiveName, /*bool ignoreCase=*/true, /*bool ignorePaths=*/false) )
 
159
        {
 
160
                logTestString("Mounting archive failed\n");
 
161
                return false;
 
162
        }
 
163
 
 
164
        // make sure there is an archive mounted
 
165
        if ( !fs->getFileArchiveCount() )
 
166
        {
 
167
                logTestString("Mounted archive not in list\n");
 
168
                return false;
 
169
        }
 
170
 
 
171
        // mount again
 
172
        if ( !fs->addFileArchive(archiveName, /*bool ignoreCase=*/true, /*bool ignorePaths=*/false) )
 
173
        {
 
174
                logTestString("Mounting a second time failed\n");
 
175
                fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
176
                return false;
 
177
        }
 
178
 
 
179
        // make sure there is exactly one archive mounted
 
180
        if ( fs->getFileArchiveCount() != 1 )
 
181
        {
 
182
                logTestString("Duplicate mount not recognized\n");
 
183
                while (fs->getFileArchiveCount())
 
184
                        fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
185
                return false;
 
186
        }
 
187
 
 
188
        // log what we got
 
189
        io::IFileArchive* archive = fs->getFileArchive(fs->getFileArchiveCount()-1);
 
190
        io::path filename("doc");
 
191
        const io::IFileList* fileList = archive->getFileList();
 
192
        for ( u32 f=0; f < fileList->getFileCount(); ++f)
 
193
        {
 
194
                logTestString("%s name: %s\n", fileList->isDirectory(f)?"Directory":"File", fileList->getFileName(f).c_str());
 
195
                logTestString("Full path: %s\n", fileList->getFullFileName(f).c_str());
 
196
        }
 
197
        if (fileList->findFile(filename) != -1)
 
198
        {
 
199
                logTestString("findFile wrongly succeeded on directory\n");
 
200
                fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
201
                return false;
 
202
        }
 
203
        if (fileList->findFile(filename, true)==-1)
 
204
        {
 
205
                logTestString("findFile failed on directory\n");
 
206
                fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
207
                return false;
 
208
        }
 
209
 
 
210
        filename="doc/readme.txt";
 
211
        if (fileList->findFile(filename)==-1)
 
212
        {
 
213
                logTestString("findFile failed\n");
 
214
                fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
215
                return false;
 
216
        }
 
217
        if (fileList->findFile(filename, true) != -1)
 
218
        {
 
219
                logTestString("findFile wrongly succeeded on non-directory\n");
 
220
                fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
221
                return false;
 
222
        }
 
223
 
 
224
        if (!fs->existFile(filename))
 
225
        {
 
226
                logTestString("existFile failed\n");
 
227
                fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
228
                return false;
 
229
        }
 
230
 
 
231
        filename="doc";
 
232
        if (fs->existFile(filename))
 
233
        {
 
234
                logTestString("existFile succeeded wrongly on directory\n");
 
235
                fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
236
                return false;
 
237
        }
 
238
 
 
239
        filename="doc/readme.txt";
 
240
        IReadFile* readFile = fs->createAndOpenFile(filename);
 
241
        if ( readFile )
 
242
        {
 
243
                logTestString("createAndOpenFile succeeded, even though no password was set.\n");
 
244
                readFile->drop();
 
245
                fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
246
                return false;
 
247
        }
 
248
 
 
249
        archive->Password="33445";
 
250
        readFile = fs->createAndOpenFile(filename);
 
251
#ifdef _IRR_COMPILE_WITH_ZIP_ENCRYPTION_
 
252
        if ( !readFile )
 
253
        {
 
254
                logTestString("createAndOpenFile failed\n");
 
255
                fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
256
                return false;
 
257
        }
 
258
 
 
259
        char tmp[13] = {'\0'};
 
260
        readFile->read(tmp, 12);
 
261
        if (strncmp(tmp, "Linux Users:", 12))
 
262
        {
 
263
                logTestString("Read bad data from archive: %s\n", tmp);
 
264
                return false;
 
265
        }
 
266
#endif
 
267
 
 
268
        if (!fs->removeFileArchive(fs->getFileArchiveCount()-1))
 
269
        {
 
270
                logTestString("Couldn't remove archive.\n");
 
271
                return false;
 
272
        }
 
273
 
 
274
        // make sure there is no archive mounted
 
275
        if ( fs->getFileArchiveCount() )
 
276
                return false;
 
277
 
 
278
        readFile->drop();
 
279
 
 
280
        return true;
 
281
}
 
282
 
 
283
bool testSpecialZip(IFileSystem* fs)
 
284
{
 
285
        // make sure there is no archive mounted
 
286
        if ( fs->getFileArchiveCount() )
 
287
        {
 
288
                logTestString("Already mounted archives found\n");
 
289
                return false;
 
290
        }
 
291
 
 
292
        const char* archiveName = "media/Monty.zip";
 
293
        if ( !fs->addFileArchive(archiveName, /*bool ignoreCase=*/true, /*bool ignorePaths=*/false) )
 
294
        {
 
295
                logTestString("Mounting archive failed\n");
 
296
                return false;
 
297
        }
 
298
 
 
299
        // make sure there is an archive mounted
 
300
        if ( !fs->getFileArchiveCount() )
 
301
        {
 
302
                logTestString("Mounted archive not in list\n");
 
303
                return false;
 
304
        }
 
305
 
 
306
        // log what we got
 
307
        io::IFileArchive* archive = fs->getFileArchive(fs->getFileArchiveCount()-1);
 
308
        const io::IFileList* fileList = archive->getFileList();
 
309
        for ( u32 f=0; f < fileList->getFileCount(); ++f)
 
310
        {
 
311
                logTestString("%s name: %s\n", fileList->isDirectory(f)?"Directory":"File", fileList->getFileName(f).c_str());
 
312
                logTestString("Full path: %s\n", fileList->getFullFileName(f).c_str());
 
313
        }
 
314
        
 
315
        io::path filename("monty/license.txt");
 
316
        if (!fs->existFile(filename))
 
317
        {
 
318
                logTestString("existFile failed\n");
 
319
                fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
320
                return false;
 
321
        }
 
322
 
 
323
        IReadFile* readFile = fs->createAndOpenFile(filename);
 
324
        if ( !readFile )
 
325
        {
 
326
                logTestString("createAndOpenFile failed\n");
 
327
                fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
328
                return false;
 
329
        }
 
330
 
 
331
        char tmp[6] = {'\0'};
 
332
        readFile->read(tmp, 5);
 
333
        if (strcmp(tmp, "Monty"))
 
334
        {
 
335
                logTestString("Read bad data from archive: %s\n", tmp);
 
336
                readFile->drop();
 
337
                fs->removeFileArchive(fs->getFileArchiveCount()-1);
 
338
                return false;
 
339
        }
 
340
 
 
341
        readFile->drop();
 
342
 
 
343
        if (!fs->removeFileArchive(fs->getFileArchiveCount()-1))
 
344
        {
 
345
                logTestString("Couldn't remove archive.\n");
 
346
                return false;
 
347
        }
 
348
 
 
349
        // make sure there is no archive mounted
 
350
        if ( fs->getFileArchiveCount() )
 
351
                return false;
 
352
 
 
353
        return true;
 
354
}
 
355
 
 
356
static bool testMountFile(IFileSystem* fs)
 
357
{
 
358
        bool result = true;
 
359
#if 1
 
360
        fs->changeWorkingDirectoryTo("empty");
 
361
        // log what we got
 
362
        const io::IFileList* fileList = fs->createFileList();
 
363
        for ( u32 f=0; f < fileList->getFileCount(); ++f)
 
364
        {
 
365
                logTestString("File name: %s\n", fileList->getFileName(f).c_str());
 
366
                logTestString("Full path: %s\n", fileList->getFullFileName(f).c_str());
 
367
                logTestString("ID: %d\n", fileList->getID(f));
 
368
        }
 
369
        fileList->drop();
 
370
        fs->changeWorkingDirectoryTo("..");
 
371
#endif
 
372
        if (!fs->addFileArchive("empty"), false)
 
373
                result = false;
 
374
        const IFileList* list = fs->getFileArchive(0)->getFileList();
 
375
#if 1
 
376
        // log what we got
 
377
        io::IFileArchive* archive = fs->getFileArchive(fs->getFileArchiveCount()-1);
 
378
        fileList = archive->getFileList();
 
379
        for ( u32 f=0; f < fileList->getFileCount(); ++f)
 
380
        {
 
381
                logTestString("File name: %s\n", fileList->getFileName(f).c_str());
 
382
                logTestString("Full path: %s\n", fileList->getFullFileName(f).c_str());
 
383
                logTestString("ID: %d\n", fileList->getID(f));
 
384
        }
 
385
#endif
 
386
 
 
387
        if (list->getFileName(0) != "burnings video 0.39b.png")
 
388
                result = false;
 
389
        return result;
 
390
}
 
391
 
 
392
bool archiveReader()
 
393
{
 
394
        IrrlichtDevice * device = irr::createDevice(video::EDT_NULL, dimension2d<u32>(1, 1));
 
395
        assert(device);
 
396
        if(!device)
 
397
                return false;
 
398
        
 
399
        io::IFileSystem * fs = device->getFileSystem ();
 
400
        if ( !fs )
 
401
                return false;
 
402
        
 
403
        bool ret = true;
 
404
        logTestString("Testing mount file.\n");
 
405
        ret &= testArchive(fs, "media/file_with_path");
 
406
        logTestString("Testing mount file.\n");
 
407
        ret &= testArchive(fs, "media/file_with_path/");
 
408
        logTestString("Testing zip files.\n");
 
409
        ret &= testArchive(fs, "media/file_with_path.zip");
 
410
        logTestString("Testing pak files.\n");
 
411
        ret &= testArchive(fs, "media/sample_pakfile.pak");
 
412
        logTestString("Testing npk files.\n");
 
413
        ret &= testArchive(fs, "media/file_with_path.npk");
 
414
        logTestString("Testing encrypted zip files.\n");
 
415
        ret &= testEncryptedZip(fs);
 
416
        logTestString("Testing special zip files.\n");
 
417
        ret &= testSpecialZip(fs);
 
418
//      logTestString("Testing complex mount file.\n");
 
419
//      ret &= testMountFile(fs);
 
420
 
 
421
        device->closeDevice();
 
422
        device->run();
 
423
        device->drop();
 
424
 
 
425
        return ret;
 
426
}
 
427