archiveReader.cpp 13 KB

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