filesystem.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2012 Robin Appelman icewind@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\Files;
  23. class Filesystem extends \PHPUnit_Framework_TestCase {
  24. /**
  25. * @var array tmpDirs
  26. */
  27. private $tmpDirs=array();
  28. /**
  29. * @return array
  30. */
  31. private function getStorageData() {
  32. $dir = \OC_Helper::tmpFolder();
  33. $this->tmpDirs[] = $dir;
  34. return array('datadir' => $dir);
  35. }
  36. public function tearDown() {
  37. foreach ($this->tmpDirs as $dir) {
  38. \OC_Helper::rmdirr($dir);
  39. }
  40. \OC\Files\Filesystem::clearMounts();
  41. \OC_User::setUserId('');
  42. }
  43. public function setUp() {
  44. \OC_User::setUserId('');
  45. \OC\Files\Filesystem::clearMounts();
  46. }
  47. public function testMount() {
  48. \OC\Files\Filesystem::mount('\OC\Files\Storage\Local',self::getStorageData(),'/');
  49. $this->assertEquals('/',\OC\Files\Filesystem::getMountPoint('/'));
  50. $this->assertEquals('/',\OC\Files\Filesystem::getMountPoint('/some/folder'));
  51. list( , $internalPath)=\OC\Files\Filesystem::resolvePath('/');
  52. $this->assertEquals('',$internalPath);
  53. list( , $internalPath)=\OC\Files\Filesystem::resolvePath('/some/folder');
  54. $this->assertEquals('some/folder',$internalPath);
  55. \OC\Files\Filesystem::mount('\OC\Files\Storage\Local',self::getStorageData(),'/some');
  56. $this->assertEquals('/',\OC\Files\Filesystem::getMountPoint('/'));
  57. $this->assertEquals('/some/',\OC\Files\Filesystem::getMountPoint('/some/folder'));
  58. $this->assertEquals('/some/',\OC\Files\Filesystem::getMountPoint('/some/'));
  59. $this->assertEquals('/some/',\OC\Files\Filesystem::getMountPoint('/some'));
  60. list( , $internalPath)=\OC\Files\Filesystem::resolvePath('/some/folder');
  61. $this->assertEquals('folder',$internalPath);
  62. }
  63. public function testNormalize() {
  64. $this->assertEquals('/', \OC\Files\Filesystem::normalizePath(''));
  65. $this->assertEquals('/', \OC\Files\Filesystem::normalizePath('/'));
  66. $this->assertEquals('/', \OC\Files\Filesystem::normalizePath('/', false));
  67. $this->assertEquals('/', \OC\Files\Filesystem::normalizePath('//'));
  68. $this->assertEquals('/', \OC\Files\Filesystem::normalizePath('//', false));
  69. $this->assertEquals('/path', \OC\Files\Filesystem::normalizePath('/path/'));
  70. $this->assertEquals('/path/', \OC\Files\Filesystem::normalizePath('/path/', false));
  71. $this->assertEquals('/path', \OC\Files\Filesystem::normalizePath('path'));
  72. $this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo//bar/'));
  73. $this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('/foo//bar/', false));
  74. $this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo////bar'));
  75. $this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo/////bar'));
  76. $this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo/bar/.'));
  77. $this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo/bar/./'));
  78. $this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('/foo/bar/./', false));
  79. $this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo/bar/./.'));
  80. $this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo/bar/././'));
  81. $this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('/foo/bar/././', false));
  82. $this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo/./bar/'));
  83. $this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('/foo/./bar/', false));
  84. $this->assertEquals('/foo/.bar', \OC\Files\Filesystem::normalizePath('/foo/.bar/'));
  85. $this->assertEquals('/foo/.bar/', \OC\Files\Filesystem::normalizePath('/foo/.bar/', false));
  86. $this->assertEquals('/foo/.bar/tee', \OC\Files\Filesystem::normalizePath('/foo/.bar/tee'));
  87. // normalize does not resolve '..' (by design)
  88. $this->assertEquals('/foo/..', \OC\Files\Filesystem::normalizePath('/foo/../'));
  89. if (class_exists('Patchwork\PHP\Shim\Normalizer')) {
  90. $this->assertEquals("/foo/bar\xC3\xBC", \OC\Files\Filesystem::normalizePath("/foo/baru\xCC\x88"));
  91. }
  92. }
  93. public function testNormalizeWindowsPaths() {
  94. $this->assertEquals('/', \OC\Files\Filesystem::normalizePath(''));
  95. $this->assertEquals('/', \OC\Files\Filesystem::normalizePath('\\'));
  96. $this->assertEquals('/', \OC\Files\Filesystem::normalizePath('\\', false));
  97. $this->assertEquals('/', \OC\Files\Filesystem::normalizePath('\\\\'));
  98. $this->assertEquals('/', \OC\Files\Filesystem::normalizePath('\\\\', false));
  99. $this->assertEquals('/path', \OC\Files\Filesystem::normalizePath('\\path'));
  100. $this->assertEquals('/path', \OC\Files\Filesystem::normalizePath('\\path', false));
  101. $this->assertEquals('/path', \OC\Files\Filesystem::normalizePath('\\path\\'));
  102. $this->assertEquals('/path/', \OC\Files\Filesystem::normalizePath('\\path\\', false));
  103. $this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\\\bar\\'));
  104. $this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('\\foo\\\\bar\\', false));
  105. $this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\\\\\\\bar'));
  106. $this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\\\\\\\\\bar'));
  107. $this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\bar\\.'));
  108. $this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\bar\\.\\'));
  109. $this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('\\foo\\bar\\.\\', false));
  110. $this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\bar\\.\\.'));
  111. $this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\bar\\.\\.\\'));
  112. $this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('\\foo\\bar\\.\\.\\', false));
  113. $this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\.\\bar\\'));
  114. $this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('\\foo\\.\\bar\\', false));
  115. $this->assertEquals('/foo/.bar', \OC\Files\Filesystem::normalizePath('\\foo\\.bar\\'));
  116. $this->assertEquals('/foo/.bar/', \OC\Files\Filesystem::normalizePath('\\foo\\.bar\\', false));
  117. $this->assertEquals('/foo/.bar/tee', \OC\Files\Filesystem::normalizePath('\\foo\\.bar\\tee'));
  118. // normalize does not resolve '..' (by design)
  119. $this->assertEquals('/foo/..', \OC\Files\Filesystem::normalizePath('\\foo\\..\\'));
  120. if (class_exists('Patchwork\PHP\Shim\Normalizer')) {
  121. $this->assertEquals("/foo/bar\xC3\xBC", \OC\Files\Filesystem::normalizePath("\\foo\\baru\xCC\x88"));
  122. }
  123. }
  124. public function testHooks() {
  125. if(\OC\Files\Filesystem::getView()){
  126. $user = \OC_User::getUser();
  127. }else{
  128. $user=uniqid();
  129. \OC\Files\Filesystem::init($user, '/'.$user.'/files');
  130. }
  131. \OC_Hook::clear('OC_Filesystem');
  132. \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook');
  133. \OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', array(), '/');
  134. $rootView=new \OC\Files\View('');
  135. $rootView->mkdir('/'.$user);
  136. $rootView->mkdir('/'.$user.'/files');
  137. // \OC\Files\Filesystem::file_put_contents('/foo', 'foo');
  138. \OC\Files\Filesystem::mkdir('/bar');
  139. // \OC\Files\Filesystem::file_put_contents('/bar//foo', 'foo');
  140. $tmpFile = \OC_Helper::tmpFile();
  141. file_put_contents($tmpFile, 'foo');
  142. $fh = fopen($tmpFile, 'r');
  143. // \OC\Files\Filesystem::file_put_contents('/bar//foo', $fh);
  144. }
  145. /**
  146. * Tests that a local storage mount is used when passed user
  147. * does not exist.
  148. */
  149. public function testLocalMountWhenUserDoesNotExist() {
  150. $datadir = \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data");
  151. $userId = uniqid('user_');
  152. \OC\Files\Filesystem::initMountPoints($userId);
  153. $homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/');
  154. $this->assertInstanceOf('\OC\Files\Storage\Local', $homeMount);
  155. $this->assertEquals('local::' . $datadir . '/' . $userId . '/', $homeMount->getId());
  156. }
  157. /**
  158. * Tests that the home storage is used for the user's mount point
  159. */
  160. public function testHomeMount() {
  161. $userId = uniqid('user_');
  162. \OC_User::createUser($userId, $userId);
  163. \OC\Files\Filesystem::initMountPoints($userId);
  164. $homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/');
  165. $this->assertInstanceOf('\OC\Files\Storage\Home', $homeMount);
  166. $this->assertEquals('home::' . $userId, $homeMount->getId());
  167. \OC_User::deleteUser($userId);
  168. }
  169. /**
  170. * Tests that the home storage is used in legacy mode
  171. * for the user's mount point
  172. */
  173. public function testLegacyHomeMount() {
  174. $datadir = \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data");
  175. $userId = uniqid('user_');
  176. // insert storage into DB by constructing it
  177. // to make initMountsPoint find its existence
  178. $localStorage = new \OC\Files\Storage\Local(array('datadir' => $datadir . '/' . $userId . '/'));
  179. // this will trigger the insert
  180. $cache = $localStorage->getCache();
  181. \OC_User::createUser($userId, $userId);
  182. \OC\Files\Filesystem::initMountPoints($userId);
  183. $homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/');
  184. $this->assertInstanceOf('\OC\Files\Storage\Home', $homeMount);
  185. $this->assertEquals('local::' . $datadir. '/' . $userId . '/', $homeMount->getId());
  186. \OC_User::deleteUser($userId);
  187. // delete storage entry
  188. $cache->clear();
  189. }
  190. public function dummyHook($arguments) {
  191. $path = $arguments['path'];
  192. $this->assertEquals($path, \OC\Files\Filesystem::normalizePath($path)); //the path passed to the hook should already be normalized
  193. }
  194. }