filesystem.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. class OC_FILEYSYSTEM_Test extends OC_TestCase
  3. {
  4. public function setup(){
  5. OC_Util::setupFS('testuser','testcase');
  6. }
  7. public function tearDown(){
  8. OC_Filesystem::chroot('');
  9. OC_Filesystem::delTree('/testuser');
  10. OC_Util::tearDownFS();
  11. }
  12. public function isDir(){
  13. $this->assertEquals(true, OC_Filesystem::is_dir('/'),'Root is not a directory');
  14. }
  15. public function fileExists(){
  16. $this->assertEquals(false, OC_Filesystem::file_exists('/dummy'),'Unexpected result with non-existing file');
  17. OC_Filesystem::file_put_contents('/dummy','foo');
  18. $this->assertEquals(true, OC_Filesystem::file_exists('/dummy'),'Unexpected result with existing file');
  19. }
  20. public function mkdir(){
  21. OC_Filesystem::mkdir('/dummy');
  22. $this->assertEquals(true, OC_Filesystem::file_exists('/dummy'),'No such file or directory after creating folder');
  23. $this->assertEquals(true, OC_Filesystem::is_dir('/dummy'),'File created instead of filder');
  24. }
  25. public function rmdir(){
  26. OC_Filesystem::mkdir('/dummy');
  27. OC_Filesystem::rmdir('/dummy');
  28. $this->assertEquals(false, OC_Filesystem::file_exists('/dummy'),'Folder still exists after removing');
  29. }
  30. public function isFile(){
  31. $this->assertEquals(false, OC_Filesystem::is_file('/'),'Root is a file');
  32. OC_Filesystem::file_put_contents('/dummy','foo');
  33. $this->assertEquals(true, OC_Filesystem::is_file('/dummy'),'Created file is not said to be a file');
  34. }
  35. public function opendir(){
  36. OC_Filesystem::file_put_contents('/dummy1','foo');
  37. OC_Filesystem::file_put_contents('/dummy2','foo');
  38. $dh=OC_Filesystem::opendir('/');
  39. if(!$dh){
  40. $this->fail('Failed to open root');
  41. }
  42. $dummy1Found=false;
  43. $dummy2Found=false;
  44. while($file=readdir($dh)){
  45. if($file=='dummy1'){
  46. $dummy1Found=true;
  47. }elseif($file=='dummy2'){
  48. $dummy2Found=true;
  49. }elseif($file!='.' and $file!='..'){
  50. $this->fail('Unexpected filename when reading opened dir');
  51. }
  52. }
  53. $this->assertEquals(true,$dummy1Found,'Not all files found when reading opened dir');
  54. $this->assertEquals(true,$dummy2Found,'Not all files found when reading opened dir');
  55. }
  56. public function filesize(){
  57. OC_Filesystem::file_put_contents('/dummy','1234567890');
  58. $this->assertEquals(10, OC_Filesystem::filesize('/dummy'),'Unexpected filesize');
  59. }
  60. public function stat(){
  61. OC_Filesystem::file_put_contents('/dummy','foo');
  62. $time=time();
  63. $stat=OC_Filesystem::stat('/dummy');
  64. $this->assertEquals(true,abs($time-$stat['atime'])<1,'Unexpected access time');//there can be small difference between those values due to running time
  65. $this->assertEquals(true,abs($time-$stat['ctime'])<1,'Unexpected creation time');
  66. $this->assertEquals(true,abs($time-$stat['mtime'])<1,'Unexpected modified time');
  67. $this->assertEquals(3,$stat['size'],'Unexpected filesize');
  68. }
  69. public function filetype(){
  70. OC_Filesystem::file_put_contents('/dummyFile','foo');
  71. OC_Filesystem::mkdir('/dummyFolder');
  72. $this->assertEquals('file', OC_Filesystem::filetype('/dummyFile'),'Unexpected filetype of file');
  73. $this->assertEquals('dir', OC_Filesystem::filetype('/dummyFolder'),'Unexpected filetype of folder');
  74. }
  75. public function readfile(){
  76. OC_Filesystem::file_put_contents('/dummy','foo');
  77. ob_start();
  78. OC_Filesystem::readfile('/dummy');
  79. $this->assertEquals('foo', ob_get_contents(),'Unexpected output of readfile');
  80. @ob_end_clean();
  81. }
  82. public function isReadable(){
  83. OC_Filesystem::file_put_contents('/dummy','foo');
  84. $this->assertEquals(true, OC_Filesystem::is_readable('/dummy'),'Can\'t read created file');
  85. }
  86. public function isWritable(){
  87. OC_Filesystem::file_put_contents('/dummy','foo');
  88. $this->assertEquals(true, OC_Filesystem::is_writeable('/dummy'),'Can\'t write created file');
  89. }
  90. public function fileatime(){
  91. OC_Filesystem::file_put_contents('/dummy','foo');
  92. $time=time();
  93. $this->assertEquals(true,abs($time-OC_Filesystem::fileatime('/dummy'))<1,'Unexpected access time');//there can be small difference between those values due to running time
  94. }
  95. public function filectime(){
  96. OC_Filesystem::file_put_contents('/dummy','foo');
  97. $time=time();
  98. $this->assertEquals(true,abs($time-OC_Filesystem::filectime('/dummy'))<1,'Unexpected creation time');//there can be small difference between those values due to running time
  99. }
  100. public function filemtime(){
  101. OC_Filesystem::file_put_contents('/dummy','foo');
  102. $time=time();
  103. $this->assertEquals(true,abs($time-OC_Filesystem::filemtime('/dummy'))<1,'Unexpected modified time');//there can be small difference between those values due to running time
  104. }
  105. public function fileGetContents(){
  106. OC_Filesystem::file_put_contents('/dummy','foo');
  107. $this->assertEquals('foo', OC_Filesystem::file_get_contents('/dummy'),'Unexpected content of file');
  108. }
  109. public function unlink(){
  110. OC_Filesystem::file_put_contents('/dummy','foo');
  111. OC_Filesystem::unlink('/dummy');
  112. $this->assertEquals(false, OC_Filesystem::file_exists('/dummy'),'File still exists after deletion');
  113. }
  114. public function rename(){
  115. OC_Filesystem::file_put_contents('/dummy','foo');
  116. OC_Filesystem::rename('/dummy','/bar');
  117. $this->assertEquals(true, OC_Filesystem::file_exists('/bar'),'New file doesnt exists after moving');
  118. $this->assertEquals(false, OC_Filesystem::file_exists('/dummy'),'Old file still exists after moving');
  119. $this->assertEquals('foo', OC_Filesystem::file_get_contents('/bar'),'Unexpected content of file after moving');
  120. }
  121. public function copy(){
  122. OC_Filesystem::file_put_contents('/dummy','foo');
  123. OC_Filesystem::copy('/dummy','/bar');
  124. $this->assertEquals(true, OC_Filesystem::file_exists('/bar'),'New file doesnt exists after copying');
  125. $this->assertEquals(true, OC_Filesystem::file_exists('/dummy'),'Old file doesnt exists after copying');
  126. $this->assertEquals('foo', OC_Filesystem::file_get_contents('/bar'),'Unexpected content of file after copying');
  127. }
  128. public function fopen(){
  129. OC_Filesystem::file_put_contents('/dummy','foo');
  130. $fh=OC_Filesystem::fopen('/dummy','r');
  131. if(!$fh){
  132. $this->fail('Cant open file for reading');
  133. }
  134. $content=fread($fh,3);
  135. $this->assertEquals('foo', $content,'Unexpected content of file');
  136. fclose($fh);
  137. $fh=OC_Filesystem::fopen('/dummy','a');
  138. fwrite($fh,'bar',3);
  139. fclose($fh);
  140. $this->assertEquals('foobar', OC_Filesystem::file_get_contents('/dummy'),'Unexpected content of file after appending');
  141. $fh=OC_Filesystem::fopen('/dummy','w');
  142. fwrite($fh,'bar',3);
  143. fclose($fh);
  144. $this->assertEquals('bar', OC_Filesystem::file_get_contents('/dummy'),'Unexpected content of file after writing');
  145. }
  146. public function toTmpFile(){
  147. OC_Filesystem::file_put_contents('/dummy','foo');
  148. $tmp=OC_Filesystem::toTmpFile('/dummy');
  149. $this->assertEquals('foo', file_get_contents($tmp),'Unexpected content of temporary file');
  150. unlink($tmp);
  151. }
  152. public function fromTmpFile(){
  153. OC_Filesystem::file_put_contents('/dummy','foo');
  154. $tmp=OC_Filesystem::toTmpFile('/dummy');
  155. OC_Filesystem::fromTmpFile($tmp,'/bar');
  156. $this->assertEquals('foo', OC_Filesystem::file_get_contents('/bar'),'Unexpected content of new file');
  157. $this->assertEquals(false, file_exists($tmp),'Temporary file still exists');
  158. }
  159. public function getMimeType(){
  160. OC_Filesystem::file_put_contents('/dummy','some plain text');
  161. $this->assertEquals('text/plain', OC_Filesystem::getMimeType('/dummy'),'Unexpected mimetype of pain text file');
  162. OC_Filesystem::file_put_contents('/dummy',"<?xml version='1.0'?>\n</dummy>");
  163. $this->assertEquals('application/xml', OC_Filesystem::getMimeType('/dummy'),'Unexpected mimetype of xml file');
  164. }
  165. public function delTree(){
  166. OC_Filesystem::mkdir('/dummy');
  167. OC_Filesystem::file_put_contents('/dummy/bar','foo');
  168. OC_Filesystem::delTree('/dummy');
  169. $this->assertEquals(false, OC_Filesystem::file_exists('/dummy/bar'),'File in deleted folder still exists');
  170. $this->assertEquals(false, OC_Filesystem::file_exists('/dummy'),'Deleted folder still exists');
  171. }
  172. public function getTree(){
  173. OC_Filesystem::mkdir('/dummy');
  174. OC_Filesystem::file_put_contents('/dummy/bar','foo');
  175. $expected=array('/dummy','/dummy/bar');
  176. $this->assertEquals($expected, OC_Filesystem::getTree('/dummy'),'Unexpected filelist returned');
  177. }
  178. public function hash(){
  179. OC_Filesystem::file_put_contents('/dummy','foo');
  180. $this->assertEquals(md5('foo'), OC_Filesystem::hash('md5','/dummy'),'Unexpected md5 hash of file');
  181. }
  182. public function freeSpace(){
  183. $oldSpace=OC_Filesystem::free_space('/');
  184. OC_Filesystem::file_put_contents('/dummy','foo');
  185. $newSpace=OC_Filesystem::free_space('/');
  186. $this->assertEquals(true, $newSpace<$oldSpace,'free space not smaller after creating a non empty file');
  187. }
  188. public function search(){
  189. OC_Filesystem::file_put_contents('/dummy','foo');
  190. $this->assertEquals(array('/dummy'),OC_Filesystem::search('my'),'unexpected file list after search');
  191. }
  192. }
  193. return 'OC_FILEYSYSTEM_Test';
  194. ?>