filesystem.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "testUtils.h"
  2. using namespace irr;
  3. using namespace core;
  4. using namespace io;
  5. static bool testgetAbsoluteFilename(io::IFileSystem* fs)
  6. {
  7. bool result=true;
  8. io::path apath = fs->getAbsolutePath("media");
  9. io::path cwd = fs->getWorkingDirectory();
  10. if (apath!=(cwd+"/media"))
  11. {
  12. logTestString("getAbsolutePath failed on existing dir %s\n", apath.c_str());
  13. result = false;
  14. }
  15. apath = fs->getAbsolutePath("../media/");
  16. core::deletePathFromPath(cwd, 1);
  17. if (apath!=(cwd+"media/"))
  18. {
  19. logTestString("getAbsolutePath failed on dir with postfix / %s\n", apath.c_str());
  20. result = false;
  21. }
  22. apath = fs->getAbsolutePath ("../nothere.txt"); // file does not exist
  23. if (apath!=(cwd+"nothere.txt"))
  24. {
  25. logTestString("getAbsolutePath failed on non-existing file %s\n", apath.c_str());
  26. result = false;
  27. }
  28. return result;
  29. }
  30. static bool testFlattenFilename(io::IFileSystem* fs)
  31. {
  32. bool result=true;
  33. io::path tmpString="../tmp";
  34. io::path refString="../tmp/";
  35. fs->flattenFilename(tmpString);
  36. if (tmpString != refString)
  37. {
  38. logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());
  39. result = false;
  40. }
  41. tmpString="tmp/tmp/../";
  42. refString="tmp/";
  43. fs->flattenFilename(tmpString);
  44. if (tmpString != refString)
  45. {
  46. logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());
  47. result = false;
  48. }
  49. tmpString="tmp/tmp/..";
  50. fs->flattenFilename(tmpString);
  51. if (tmpString != refString)
  52. {
  53. logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());
  54. result = false;
  55. }
  56. tmpString="tmp/next/../third";
  57. refString="tmp/third/";
  58. fs->flattenFilename(tmpString);
  59. if (tmpString != refString)
  60. {
  61. logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());
  62. result = false;
  63. }
  64. tmpString="this/tmp/next/../../my/fourth";
  65. refString="this/my/fourth/";
  66. fs->flattenFilename(tmpString);
  67. if (tmpString != refString)
  68. {
  69. logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());
  70. result = false;
  71. }
  72. tmpString="this/is/../../../a/fifth/test/";
  73. refString="../a/fifth/test/";
  74. fs->flattenFilename(tmpString);
  75. if (tmpString != refString)
  76. {
  77. logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());
  78. result = false;
  79. }
  80. tmpString="this/../is/../../a/sixth/test/";
  81. refString="../a/sixth/test/";
  82. fs->flattenFilename(tmpString);
  83. if (tmpString != refString)
  84. {
  85. logTestString("flattening destroys path.\n%s!=%s\n", tmpString.c_str(),refString.c_str());
  86. result = false;
  87. }
  88. return result;
  89. }
  90. static bool testgetRelativeFilename(io::IFileSystem* fs)
  91. {
  92. bool result=true;
  93. io::path apath = fs->getAbsolutePath("media");
  94. io::path cwd = fs->getWorkingDirectory();
  95. if (fs->getRelativeFilename(apath, cwd) != "media")
  96. {
  97. logTestString("getRelativePath failed on %s\n", apath.c_str());
  98. result = false;
  99. }
  100. apath = fs->getAbsolutePath("../media/");
  101. if (fs->getRelativeFilename(apath, cwd) != "../media/")
  102. {
  103. logTestString("getRelativePath failed on %s\n", apath.c_str());
  104. result = false;
  105. }
  106. return result;
  107. }
  108. bool filesystem(void)
  109. {
  110. IrrlichtDevice * device = irr::createDevice(video::EDT_NULL, dimension2d<u32>(1, 1));
  111. assert_log(device);
  112. if(!device)
  113. return false;
  114. io::IFileSystem * fs = device->getFileSystem ();
  115. if ( !fs )
  116. return false;
  117. bool result = true;
  118. io::path workingDir = device->getFileSystem()->getWorkingDirectory();
  119. io::path empty;
  120. if ( fs->existFile(empty) )
  121. {
  122. logTestString("Empty filename should not exist.\n");
  123. result = false;
  124. }
  125. io::path newWd = workingDir + "/media";
  126. bool changed = device->getFileSystem()->changeWorkingDirectoryTo(newWd);
  127. assert_log(changed);
  128. if ( fs->existFile(empty) )
  129. {
  130. logTestString("Empty filename should not exist even in another workingdirectory.\n");
  131. result = false;
  132. }
  133. // The working directory must be restored for the other tests to work.
  134. changed = device->getFileSystem()->changeWorkingDirectoryTo(workingDir.c_str());
  135. assert_log(changed);
  136. // adding a folder archive which just should not really change anything
  137. device->getFileSystem()->addFileArchive( "./" );
  138. if ( fs->existFile(empty) )
  139. {
  140. logTestString("Empty filename should not exist in folder file archive.\n");
  141. result = false;
  142. }
  143. // remove it again to not affect other tests
  144. device->getFileSystem()->removeFileArchive( device->getFileSystem()->getFileArchiveCount() );
  145. result &= testFlattenFilename(fs);
  146. result &= testgetAbsoluteFilename(fs);
  147. result &= testgetRelativeFilename(fs);
  148. device->closeDevice();
  149. device->run();
  150. device->drop();
  151. return result;
  152. }