ViewTest.php 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file. */
  7. namespace Test\Files;
  8. use OC\Files\Cache\Watcher;
  9. use OC\Files\Filesystem;
  10. use OC\Files\Mount\MountPoint;
  11. use OC\Files\SetupManager;
  12. use OC\Files\Storage\Common;
  13. use OC\Files\Storage\Storage;
  14. use OC\Files\Storage\Temporary;
  15. use OC\Files\View;
  16. use OC\Share20\ShareDisableChecker;
  17. use OCP\Cache\CappedMemoryCache;
  18. use OCP\Constants;
  19. use OCP\Files\Config\IMountProvider;
  20. use OCP\Files\FileInfo;
  21. use OCP\Files\GenericFileException;
  22. use OCP\Files\Mount\IMountManager;
  23. use OCP\Files\Storage\IStorage;
  24. use OCP\IDBConnection;
  25. use OCP\Lock\ILockingProvider;
  26. use OCP\Lock\LockedException;
  27. use OCP\Share\IShare;
  28. use OCP\Util;
  29. use Test\HookHelper;
  30. use Test\TestMoveableMountPoint;
  31. use Test\Traits\UserTrait;
  32. class TemporaryNoTouch extends Temporary {
  33. public function touch($path, $mtime = null) {
  34. return false;
  35. }
  36. }
  37. class TemporaryNoCross extends Temporary {
  38. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = null) {
  39. return Common::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime);
  40. }
  41. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  42. return Common::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  43. }
  44. }
  45. class TemporaryNoLocal extends Temporary {
  46. public function instanceOfStorage($className) {
  47. if ($className === '\OC\Files\Storage\Local') {
  48. return false;
  49. } else {
  50. return parent::instanceOfStorage($className);
  51. }
  52. }
  53. }
  54. /**
  55. * Class ViewTest
  56. *
  57. * @group DB
  58. *
  59. * @package Test\Files
  60. */
  61. class ViewTest extends \Test\TestCase {
  62. use UserTrait;
  63. /**
  64. * @var \OC\Files\Storage\Storage[] $storages
  65. */
  66. private $storages = [];
  67. /**
  68. * @var string
  69. */
  70. private $user;
  71. /**
  72. * @var \OCP\IUser
  73. */
  74. private $userObject;
  75. /**
  76. * @var \OCP\IGroup
  77. */
  78. private $groupObject;
  79. /** @var \OC\Files\Storage\Storage */
  80. private $tempStorage;
  81. protected function setUp(): void {
  82. parent::setUp();
  83. \OC_Hook::clear();
  84. \OC_User::clearBackends();
  85. \OC_User::useBackend(new \Test\Util\User\Dummy());
  86. //login
  87. $userManager = \OC::$server->getUserManager();
  88. $groupManager = \OC::$server->getGroupManager();
  89. $this->user = 'test';
  90. $this->userObject = $userManager->createUser('test', 'test');
  91. $this->groupObject = $groupManager->createGroup('group1');
  92. $this->groupObject->addUser($this->userObject);
  93. self::loginAsUser($this->user);
  94. /** @var IMountManager $manager */
  95. $manager = \OC::$server->get(IMountManager::class);
  96. $manager->removeMount('/test');
  97. $this->tempStorage = null;
  98. }
  99. protected function tearDown(): void {
  100. \OC_User::setUserId($this->user);
  101. foreach ($this->storages as $storage) {
  102. $cache = $storage->getCache();
  103. $ids = $cache->getAll();
  104. $cache->clear();
  105. }
  106. if ($this->tempStorage) {
  107. system('rm -rf ' . escapeshellarg($this->tempStorage->getDataDir()));
  108. }
  109. self::logout();
  110. /** @var SetupManager $setupManager */
  111. $setupManager = \OC::$server->get(SetupManager::class);
  112. $setupManager->setupRoot();
  113. $this->userObject->delete();
  114. $this->groupObject->delete();
  115. $mountProviderCollection = \OC::$server->getMountProviderCollection();
  116. self::invokePrivate($mountProviderCollection, 'providers', [[]]);
  117. parent::tearDown();
  118. }
  119. /**
  120. * @medium
  121. */
  122. public function testCacheAPI() {
  123. $storage1 = $this->getTestStorage();
  124. $storage2 = $this->getTestStorage();
  125. $storage3 = $this->getTestStorage();
  126. $root = self::getUniqueID('/');
  127. Filesystem::mount($storage1, [], $root . '/');
  128. Filesystem::mount($storage2, [], $root . '/substorage');
  129. Filesystem::mount($storage3, [], $root . '/folder/anotherstorage');
  130. $textSize = strlen("dummy file data\n");
  131. $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo/logo.png');
  132. $storageSize = $textSize * 2 + $imageSize;
  133. $storageInfo = $storage3->getCache()->get('');
  134. $this->assertEquals($storageSize, $storageInfo['size']);
  135. $rootView = new View($root);
  136. $cachedData = $rootView->getFileInfo('/foo.txt');
  137. $this->assertEquals($textSize, $cachedData['size']);
  138. $this->assertEquals('text/plain', $cachedData['mimetype']);
  139. $this->assertNotEquals(-1, $cachedData['permissions']);
  140. $cachedData = $rootView->getFileInfo('/');
  141. $this->assertEquals($storageSize * 3, $cachedData['size']);
  142. $this->assertEquals('httpd/unix-directory', $cachedData['mimetype']);
  143. // get cached data excluding mount points
  144. $cachedData = $rootView->getFileInfo('/', false);
  145. $this->assertEquals($storageSize, $cachedData['size']);
  146. $this->assertEquals('httpd/unix-directory', $cachedData['mimetype']);
  147. $cachedData = $rootView->getFileInfo('/folder');
  148. $this->assertEquals($storageSize + $textSize, $cachedData['size']);
  149. $this->assertEquals('httpd/unix-directory', $cachedData['mimetype']);
  150. $folderData = $rootView->getDirectoryContent('/');
  151. /**
  152. * expected entries:
  153. * folder
  154. * foo.png
  155. * foo.txt
  156. * substorage
  157. */
  158. $this->assertCount(4, $folderData);
  159. $this->assertEquals('folder', $folderData[0]['name']);
  160. $this->assertEquals('foo.png', $folderData[1]['name']);
  161. $this->assertEquals('foo.txt', $folderData[2]['name']);
  162. $this->assertEquals('substorage', $folderData[3]['name']);
  163. $this->assertEquals($storageSize + $textSize, $folderData[0]['size']);
  164. $this->assertEquals($imageSize, $folderData[1]['size']);
  165. $this->assertEquals($textSize, $folderData[2]['size']);
  166. $this->assertEquals($storageSize, $folderData[3]['size']);
  167. $folderData = $rootView->getDirectoryContent('/substorage');
  168. /**
  169. * expected entries:
  170. * folder
  171. * foo.png
  172. * foo.txt
  173. */
  174. $this->assertCount(3, $folderData);
  175. $this->assertEquals('folder', $folderData[0]['name']);
  176. $this->assertEquals('foo.png', $folderData[1]['name']);
  177. $this->assertEquals('foo.txt', $folderData[2]['name']);
  178. $folderView = new View($root . '/folder');
  179. $this->assertEquals($rootView->getFileInfo('/folder'), $folderView->getFileInfo('/'));
  180. $cachedData = $rootView->getFileInfo('/foo.txt');
  181. $this->assertFalse($cachedData['encrypted']);
  182. $id = $rootView->putFileInfo('/foo.txt', ['encrypted' => true]);
  183. $cachedData = $rootView->getFileInfo('/foo.txt');
  184. $this->assertTrue($cachedData['encrypted']);
  185. $this->assertEquals($cachedData['fileid'], $id);
  186. $this->assertFalse($rootView->getFileInfo('/non/existing'));
  187. $this->assertEquals([], $rootView->getDirectoryContent('/non/existing'));
  188. }
  189. /**
  190. * @medium
  191. */
  192. public function testGetPath() {
  193. $storage1 = $this->getTestStorage();
  194. $storage2 = $this->getTestStorage();
  195. $storage3 = $this->getTestStorage();
  196. Filesystem::mount($storage1, [], '/');
  197. Filesystem::mount($storage2, [], '/substorage');
  198. Filesystem::mount($storage3, [], '/folder/anotherstorage');
  199. $rootView = new View('');
  200. $cachedData = $rootView->getFileInfo('/foo.txt');
  201. /** @var int $id1 */
  202. $id1 = $cachedData['fileid'];
  203. $this->assertEquals('/foo.txt', $rootView->getPath($id1));
  204. $cachedData = $rootView->getFileInfo('/substorage/foo.txt');
  205. /** @var int $id2 */
  206. $id2 = $cachedData['fileid'];
  207. $this->assertEquals('/substorage/foo.txt', $rootView->getPath($id2));
  208. $folderView = new View('/substorage');
  209. $this->assertEquals('/foo.txt', $folderView->getPath($id2));
  210. }
  211. public function testGetPathNotExisting() {
  212. $this->expectException(\OCP\Files\NotFoundException::class);
  213. $storage1 = $this->getTestStorage();
  214. Filesystem::mount($storage1, [], '/');
  215. $rootView = new View('');
  216. $cachedData = $rootView->getFileInfo('/foo.txt');
  217. /** @var int $id1 */
  218. $id1 = $cachedData['fileid'];
  219. $folderView = new View('/substorage');
  220. $this->assertNull($folderView->getPath($id1));
  221. }
  222. /**
  223. * @medium
  224. */
  225. public function testMountPointOverwrite() {
  226. $storage1 = $this->getTestStorage(false);
  227. $storage2 = $this->getTestStorage();
  228. $storage1->mkdir('substorage');
  229. Filesystem::mount($storage1, [], '/');
  230. Filesystem::mount($storage2, [], '/substorage');
  231. $rootView = new View('');
  232. $folderContent = $rootView->getDirectoryContent('/');
  233. $this->assertCount(4, $folderContent);
  234. }
  235. public function sharingDisabledPermissionProvider() {
  236. return [
  237. ['no', '', true],
  238. ['yes', 'group1', false],
  239. ];
  240. }
  241. /**
  242. * @dataProvider sharingDisabledPermissionProvider
  243. */
  244. public function testRemoveSharePermissionWhenSharingDisabledForUser($excludeGroups, $excludeGroupsList, $expectedShareable) {
  245. // Reset sharing disabled for users cache
  246. self::invokePrivate(\OC::$server->get(ShareDisableChecker::class), 'sharingDisabledForUsersCache', [new CappedMemoryCache()]);
  247. $config = \OC::$server->getConfig();
  248. $oldExcludeGroupsFlag = $config->getAppValue('core', 'shareapi_exclude_groups', 'no');
  249. $oldExcludeGroupsList = $config->getAppValue('core', 'shareapi_exclude_groups_list', '');
  250. $config->setAppValue('core', 'shareapi_exclude_groups', $excludeGroups);
  251. $config->setAppValue('core', 'shareapi_exclude_groups_list', $excludeGroupsList);
  252. $storage1 = $this->getTestStorage();
  253. $storage2 = $this->getTestStorage();
  254. Filesystem::mount($storage1, [], '/');
  255. Filesystem::mount($storage2, [], '/mount');
  256. $view = new View('/');
  257. $folderContent = $view->getDirectoryContent('');
  258. $this->assertEquals($expectedShareable, $folderContent[0]->isShareable());
  259. $folderContent = $view->getDirectoryContent('mount');
  260. $this->assertEquals($expectedShareable, $folderContent[0]->isShareable());
  261. $config->setAppValue('core', 'shareapi_exclude_groups', $oldExcludeGroupsFlag);
  262. $config->setAppValue('core', 'shareapi_exclude_groups_list', $oldExcludeGroupsList);
  263. // Reset sharing disabled for users cache
  264. self::invokePrivate(\OC::$server->get(ShareDisableChecker::class), 'sharingDisabledForUsersCache', [new CappedMemoryCache()]);
  265. }
  266. public function testCacheIncompleteFolder() {
  267. $storage1 = $this->getTestStorage(false);
  268. Filesystem::mount($storage1, [], '/incomplete');
  269. $rootView = new View('/incomplete');
  270. $entries = $rootView->getDirectoryContent('/');
  271. $this->assertCount(3, $entries);
  272. // /folder will already be in the cache but not scanned
  273. $entries = $rootView->getDirectoryContent('/folder');
  274. $this->assertCount(1, $entries);
  275. }
  276. public function testAutoScan() {
  277. $storage1 = $this->getTestStorage(false);
  278. $storage2 = $this->getTestStorage(false);
  279. Filesystem::mount($storage1, [], '/');
  280. Filesystem::mount($storage2, [], '/substorage');
  281. $textSize = strlen("dummy file data\n");
  282. $rootView = new View('');
  283. $cachedData = $rootView->getFileInfo('/');
  284. $this->assertEquals('httpd/unix-directory', $cachedData['mimetype']);
  285. $this->assertEquals(-1, $cachedData['size']);
  286. $folderData = $rootView->getDirectoryContent('/substorage/folder');
  287. $this->assertEquals('text/plain', $folderData[0]['mimetype']);
  288. $this->assertEquals($textSize, $folderData[0]['size']);
  289. }
  290. /**
  291. * @medium
  292. */
  293. public function testSearch() {
  294. $storage1 = $this->getTestStorage();
  295. $storage2 = $this->getTestStorage();
  296. $storage3 = $this->getTestStorage();
  297. Filesystem::mount($storage1, [], '/');
  298. Filesystem::mount($storage2, [], '/substorage');
  299. Filesystem::mount($storage3, [], '/folder/anotherstorage');
  300. $rootView = new View('');
  301. $results = $rootView->search('foo');
  302. $this->assertCount(6, $results);
  303. $paths = [];
  304. foreach ($results as $result) {
  305. $this->assertEquals($result['path'], Filesystem::normalizePath($result['path']));
  306. $paths[] = $result['path'];
  307. }
  308. $this->assertContains('/foo.txt', $paths);
  309. $this->assertContains('/foo.png', $paths);
  310. $this->assertContains('/substorage/foo.txt', $paths);
  311. $this->assertContains('/substorage/foo.png', $paths);
  312. $this->assertContains('/folder/anotherstorage/foo.txt', $paths);
  313. $this->assertContains('/folder/anotherstorage/foo.png', $paths);
  314. $folderView = new View('/folder');
  315. $results = $folderView->search('bar');
  316. $this->assertCount(2, $results);
  317. $paths = [];
  318. foreach ($results as $result) {
  319. $paths[] = $result['path'];
  320. }
  321. $this->assertContains('/anotherstorage/folder/bar.txt', $paths);
  322. $this->assertContains('/bar.txt', $paths);
  323. $results = $folderView->search('foo');
  324. $this->assertCount(2, $results);
  325. $paths = [];
  326. foreach ($results as $result) {
  327. $paths[] = $result['path'];
  328. }
  329. $this->assertContains('/anotherstorage/foo.txt', $paths);
  330. $this->assertContains('/anotherstorage/foo.png', $paths);
  331. $this->assertCount(6, $rootView->searchByMime('text'));
  332. $this->assertCount(3, $folderView->searchByMime('text'));
  333. }
  334. /**
  335. * @medium
  336. */
  337. public function testWatcher() {
  338. $storage1 = $this->getTestStorage();
  339. Filesystem::mount($storage1, [], '/');
  340. $storage1->getWatcher()->setPolicy(Watcher::CHECK_ALWAYS);
  341. $rootView = new View('');
  342. $cachedData = $rootView->getFileInfo('foo.txt');
  343. $this->assertEquals(16, $cachedData['size']);
  344. $rootView->putFileInfo('foo.txt', ['storage_mtime' => 10]);
  345. $storage1->file_put_contents('foo.txt', 'foo');
  346. clearstatcache();
  347. $cachedData = $rootView->getFileInfo('foo.txt');
  348. $this->assertEquals(3, $cachedData['size']);
  349. }
  350. /**
  351. * @medium
  352. */
  353. public function testCopyBetweenStorageNoCross() {
  354. $storage1 = $this->getTestStorage(true, TemporaryNoCross::class);
  355. $storage2 = $this->getTestStorage(true, TemporaryNoCross::class);
  356. $this->copyBetweenStorages($storage1, $storage2);
  357. }
  358. /**
  359. * @medium
  360. */
  361. public function testCopyBetweenStorageCross() {
  362. $storage1 = $this->getTestStorage();
  363. $storage2 = $this->getTestStorage();
  364. $this->copyBetweenStorages($storage1, $storage2);
  365. }
  366. /**
  367. * @medium
  368. */
  369. public function testCopyBetweenStorageCrossNonLocal() {
  370. $storage1 = $this->getTestStorage(true, TemporaryNoLocal::class);
  371. $storage2 = $this->getTestStorage(true, TemporaryNoLocal::class);
  372. $this->copyBetweenStorages($storage1, $storage2);
  373. }
  374. public function copyBetweenStorages($storage1, $storage2) {
  375. Filesystem::mount($storage1, [], '/');
  376. Filesystem::mount($storage2, [], '/substorage');
  377. $rootView = new View('');
  378. $rootView->mkdir('substorage/emptyfolder');
  379. $rootView->copy('substorage', 'anotherfolder');
  380. $this->assertTrue($rootView->is_dir('/anotherfolder'));
  381. $this->assertTrue($rootView->is_dir('/substorage'));
  382. $this->assertTrue($rootView->is_dir('/anotherfolder/emptyfolder'));
  383. $this->assertTrue($rootView->is_dir('/substorage/emptyfolder'));
  384. $this->assertTrue($rootView->file_exists('/anotherfolder/foo.txt'));
  385. $this->assertTrue($rootView->file_exists('/anotherfolder/foo.png'));
  386. $this->assertTrue($rootView->file_exists('/anotherfolder/folder/bar.txt'));
  387. $this->assertTrue($rootView->file_exists('/substorage/foo.txt'));
  388. $this->assertTrue($rootView->file_exists('/substorage/foo.png'));
  389. $this->assertTrue($rootView->file_exists('/substorage/folder/bar.txt'));
  390. }
  391. /**
  392. * @medium
  393. */
  394. public function testMoveBetweenStorageNoCross() {
  395. $storage1 = $this->getTestStorage(true, TemporaryNoCross::class);
  396. $storage2 = $this->getTestStorage(true, TemporaryNoCross::class);
  397. $this->moveBetweenStorages($storage1, $storage2);
  398. }
  399. /**
  400. * @medium
  401. */
  402. public function testMoveBetweenStorageCross() {
  403. $storage1 = $this->getTestStorage();
  404. $storage2 = $this->getTestStorage();
  405. $this->moveBetweenStorages($storage1, $storage2);
  406. }
  407. /**
  408. * @medium
  409. */
  410. public function testMoveBetweenStorageCrossNonLocal() {
  411. $storage1 = $this->getTestStorage(true, TemporaryNoLocal::class);
  412. $storage2 = $this->getTestStorage(true, TemporaryNoLocal::class);
  413. $this->moveBetweenStorages($storage1, $storage2);
  414. }
  415. public function moveBetweenStorages($storage1, $storage2) {
  416. Filesystem::mount($storage1, [], '/');
  417. Filesystem::mount($storage2, [], '/substorage');
  418. $rootView = new View('');
  419. $rootView->rename('foo.txt', 'substorage/folder/foo.txt');
  420. $this->assertFalse($rootView->file_exists('foo.txt'));
  421. $this->assertTrue($rootView->file_exists('substorage/folder/foo.txt'));
  422. $rootView->rename('substorage/folder', 'anotherfolder');
  423. $this->assertFalse($rootView->is_dir('substorage/folder'));
  424. $this->assertTrue($rootView->file_exists('anotherfolder/foo.txt'));
  425. $this->assertTrue($rootView->file_exists('anotherfolder/bar.txt'));
  426. }
  427. /**
  428. * @medium
  429. */
  430. public function testUnlink() {
  431. $storage1 = $this->getTestStorage();
  432. $storage2 = $this->getTestStorage();
  433. Filesystem::mount($storage1, [], '/');
  434. Filesystem::mount($storage2, [], '/substorage');
  435. $rootView = new View('');
  436. $rootView->file_put_contents('/foo.txt', 'asd');
  437. $rootView->file_put_contents('/substorage/bar.txt', 'asd');
  438. $this->assertTrue($rootView->file_exists('foo.txt'));
  439. $this->assertTrue($rootView->file_exists('substorage/bar.txt'));
  440. $this->assertTrue($rootView->unlink('foo.txt'));
  441. $this->assertTrue($rootView->unlink('substorage/bar.txt'));
  442. $this->assertFalse($rootView->file_exists('foo.txt'));
  443. $this->assertFalse($rootView->file_exists('substorage/bar.txt'));
  444. }
  445. public function rmdirOrUnlinkDataProvider() {
  446. return [['rmdir'], ['unlink']];
  447. }
  448. /**
  449. * @dataProvider rmdirOrUnlinkDataProvider
  450. */
  451. public function testRmdir($method) {
  452. $storage1 = $this->getTestStorage();
  453. Filesystem::mount($storage1, [], '/');
  454. $rootView = new View('');
  455. $rootView->mkdir('sub');
  456. $rootView->mkdir('sub/deep');
  457. $rootView->file_put_contents('/sub/deep/foo.txt', 'asd');
  458. $this->assertTrue($rootView->file_exists('sub/deep/foo.txt'));
  459. $this->assertTrue($rootView->$method('sub'));
  460. $this->assertFalse($rootView->file_exists('sub'));
  461. }
  462. /**
  463. * @medium
  464. */
  465. public function testUnlinkRootMustFail() {
  466. $storage1 = $this->getTestStorage();
  467. $storage2 = $this->getTestStorage();
  468. Filesystem::mount($storage1, [], '/');
  469. Filesystem::mount($storage2, [], '/substorage');
  470. $rootView = new View('');
  471. $rootView->file_put_contents('/foo.txt', 'asd');
  472. $rootView->file_put_contents('/substorage/bar.txt', 'asd');
  473. $this->assertFalse($rootView->unlink(''));
  474. $this->assertFalse($rootView->unlink('/'));
  475. $this->assertFalse($rootView->unlink('substorage'));
  476. $this->assertFalse($rootView->unlink('/substorage'));
  477. }
  478. /**
  479. * @medium
  480. */
  481. public function testTouch() {
  482. $storage = $this->getTestStorage(true, TemporaryNoTouch::class);
  483. Filesystem::mount($storage, [], '/');
  484. $rootView = new View('');
  485. $oldCachedData = $rootView->getFileInfo('foo.txt');
  486. $rootView->touch('foo.txt', 500);
  487. $cachedData = $rootView->getFileInfo('foo.txt');
  488. $this->assertEquals(500, $cachedData['mtime']);
  489. $this->assertEquals($oldCachedData['storage_mtime'], $cachedData['storage_mtime']);
  490. $rootView->putFileInfo('foo.txt', ['storage_mtime' => 1000]); //make sure the watcher detects the change
  491. $rootView->file_put_contents('foo.txt', 'asd');
  492. $cachedData = $rootView->getFileInfo('foo.txt');
  493. $this->assertGreaterThanOrEqual($oldCachedData['mtime'], $cachedData['mtime']);
  494. $this->assertEquals($cachedData['storage_mtime'], $cachedData['mtime']);
  495. }
  496. /**
  497. * @medium
  498. */
  499. public function testTouchFloat() {
  500. $storage = $this->getTestStorage(true, TemporaryNoTouch::class);
  501. Filesystem::mount($storage, [], '/');
  502. $rootView = new View('');
  503. $oldCachedData = $rootView->getFileInfo('foo.txt');
  504. $rootView->touch('foo.txt', 500.5);
  505. $cachedData = $rootView->getFileInfo('foo.txt');
  506. $this->assertEquals(500, $cachedData['mtime']);
  507. }
  508. /**
  509. * @medium
  510. */
  511. public function testViewHooks() {
  512. $storage1 = $this->getTestStorage();
  513. $storage2 = $this->getTestStorage();
  514. $defaultRoot = Filesystem::getRoot();
  515. Filesystem::mount($storage1, [], '/');
  516. Filesystem::mount($storage2, [], $defaultRoot . '/substorage');
  517. \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook');
  518. $rootView = new View('');
  519. $subView = new View($defaultRoot . '/substorage');
  520. $this->hookPath = null;
  521. $rootView->file_put_contents('/foo.txt', 'asd');
  522. $this->assertNull($this->hookPath);
  523. $subView->file_put_contents('/foo.txt', 'asd');
  524. $this->assertEquals('/substorage/foo.txt', $this->hookPath);
  525. }
  526. private $hookPath;
  527. public function dummyHook($params) {
  528. $this->hookPath = $params['path'];
  529. }
  530. public function testSearchNotOutsideView() {
  531. $storage1 = $this->getTestStorage();
  532. Filesystem::mount($storage1, [], '/');
  533. $storage1->rename('folder', 'foo');
  534. $scanner = $storage1->getScanner();
  535. $scanner->scan('');
  536. $view = new View('/foo');
  537. $result = $view->search('.txt');
  538. $this->assertCount(1, $result);
  539. }
  540. /**
  541. * @param bool $scan
  542. * @param string $class
  543. * @return \OC\Files\Storage\Storage
  544. */
  545. private function getTestStorage($scan = true, $class = Temporary::class) {
  546. /**
  547. * @var \OC\Files\Storage\Storage $storage
  548. */
  549. $storage = new $class([]);
  550. $textData = "dummy file data\n";
  551. $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo/logo.png');
  552. $storage->mkdir('folder');
  553. $storage->file_put_contents('foo.txt', $textData);
  554. $storage->file_put_contents('foo.png', $imgData);
  555. $storage->file_put_contents('folder/bar.txt', $textData);
  556. if ($scan) {
  557. $scanner = $storage->getScanner();
  558. $scanner->scan('');
  559. }
  560. $this->storages[] = $storage;
  561. return $storage;
  562. }
  563. /**
  564. * @medium
  565. */
  566. public function testViewHooksIfRootStartsTheSame() {
  567. $storage1 = $this->getTestStorage();
  568. $storage2 = $this->getTestStorage();
  569. $defaultRoot = Filesystem::getRoot();
  570. Filesystem::mount($storage1, [], '/');
  571. Filesystem::mount($storage2, [], $defaultRoot . '_substorage');
  572. \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook');
  573. $subView = new View($defaultRoot . '_substorage');
  574. $this->hookPath = null;
  575. $subView->file_put_contents('/foo.txt', 'asd');
  576. $this->assertNull($this->hookPath);
  577. }
  578. private $hookWritePath;
  579. private $hookCreatePath;
  580. private $hookUpdatePath;
  581. public function dummyHookWrite($params) {
  582. $this->hookWritePath = $params['path'];
  583. }
  584. public function dummyHookUpdate($params) {
  585. $this->hookUpdatePath = $params['path'];
  586. }
  587. public function dummyHookCreate($params) {
  588. $this->hookCreatePath = $params['path'];
  589. }
  590. public function testEditNoCreateHook() {
  591. $storage1 = $this->getTestStorage();
  592. $storage2 = $this->getTestStorage();
  593. $defaultRoot = Filesystem::getRoot();
  594. Filesystem::mount($storage1, [], '/');
  595. Filesystem::mount($storage2, [], $defaultRoot);
  596. \OC_Hook::connect('OC_Filesystem', 'post_create', $this, 'dummyHookCreate');
  597. \OC_Hook::connect('OC_Filesystem', 'post_update', $this, 'dummyHookUpdate');
  598. \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHookWrite');
  599. $view = new View($defaultRoot);
  600. $this->hookWritePath = $this->hookUpdatePath = $this->hookCreatePath = null;
  601. $view->file_put_contents('/asd.txt', 'foo');
  602. $this->assertEquals('/asd.txt', $this->hookCreatePath);
  603. $this->assertNull($this->hookUpdatePath);
  604. $this->assertEquals('/asd.txt', $this->hookWritePath);
  605. $this->hookWritePath = $this->hookUpdatePath = $this->hookCreatePath = null;
  606. $view->file_put_contents('/asd.txt', 'foo');
  607. $this->assertNull($this->hookCreatePath);
  608. $this->assertEquals('/asd.txt', $this->hookUpdatePath);
  609. $this->assertEquals('/asd.txt', $this->hookWritePath);
  610. \OC_Hook::clear('OC_Filesystem', 'post_create');
  611. \OC_Hook::clear('OC_Filesystem', 'post_update');
  612. \OC_Hook::clear('OC_Filesystem', 'post_write');
  613. }
  614. /**
  615. * @dataProvider resolvePathTestProvider
  616. */
  617. public function testResolvePath($expected, $pathToTest) {
  618. $storage1 = $this->getTestStorage();
  619. Filesystem::mount($storage1, [], '/');
  620. $view = new View('');
  621. $result = $view->resolvePath($pathToTest);
  622. $this->assertEquals($expected, $result[1]);
  623. $exists = $view->file_exists($pathToTest);
  624. $this->assertTrue($exists);
  625. $exists = $view->file_exists($result[1]);
  626. $this->assertTrue($exists);
  627. }
  628. public function resolvePathTestProvider() {
  629. return [
  630. ['foo.txt', 'foo.txt'],
  631. ['foo.txt', '/foo.txt'],
  632. ['folder', 'folder'],
  633. ['folder', '/folder'],
  634. ['folder', 'folder/'],
  635. ['folder', '/folder/'],
  636. ['folder/bar.txt', 'folder/bar.txt'],
  637. ['folder/bar.txt', '/folder/bar.txt'],
  638. ['', ''],
  639. ['', '/'],
  640. ];
  641. }
  642. public function testUTF8Names() {
  643. $names = ['虚', '和知しゃ和で', 'regular ascii', 'sɨˈrɪlɪk', 'ѨѬ', 'أنا أحب القراءة كثيرا'];
  644. $storage = new Temporary([]);
  645. Filesystem::mount($storage, [], '/');
  646. $rootView = new View('');
  647. foreach ($names as $name) {
  648. $rootView->file_put_contents('/' . $name, 'dummy content');
  649. }
  650. $list = $rootView->getDirectoryContent('/');
  651. $this->assertCount(count($names), $list);
  652. foreach ($list as $item) {
  653. $this->assertContains($item['name'], $names);
  654. }
  655. $cache = $storage->getCache();
  656. $scanner = $storage->getScanner();
  657. $scanner->scan('');
  658. $list = $cache->getFolderContents('');
  659. $this->assertCount(count($names), $list);
  660. foreach ($list as $item) {
  661. $this->assertContains($item['name'], $names);
  662. }
  663. }
  664. public function xtestLongPath() {
  665. $storage = new Temporary([]);
  666. Filesystem::mount($storage, [], '/');
  667. $rootView = new View('');
  668. $longPath = '';
  669. $ds = DIRECTORY_SEPARATOR;
  670. /*
  671. * 4096 is the maximum path length in file_cache.path in *nix
  672. * 1024 is the max path length in mac
  673. */
  674. $folderName = 'abcdefghijklmnopqrstuvwxyz012345678901234567890123456789';
  675. $tmpdirLength = strlen(\OC::$server->getTempManager()->getTemporaryFolder());
  676. if (\OC_Util::runningOnMac()) {
  677. $depth = ((1024 - $tmpdirLength) / 57);
  678. } else {
  679. $depth = ((4000 - $tmpdirLength) / 57);
  680. }
  681. foreach (range(0, $depth - 1) as $i) {
  682. $longPath .= $ds . $folderName;
  683. $result = $rootView->mkdir($longPath);
  684. $this->assertTrue($result, "mkdir failed on $i - path length: " . strlen($longPath));
  685. $result = $rootView->file_put_contents($longPath . "{$ds}test.txt", 'lorem');
  686. $this->assertEquals(5, $result, "file_put_contents failed on $i");
  687. $this->assertTrue($rootView->file_exists($longPath));
  688. $this->assertTrue($rootView->file_exists($longPath . "{$ds}test.txt"));
  689. }
  690. $cache = $storage->getCache();
  691. $scanner = $storage->getScanner();
  692. $scanner->scan('');
  693. $longPath = $folderName;
  694. foreach (range(0, $depth - 1) as $i) {
  695. $cachedFolder = $cache->get($longPath);
  696. $this->assertTrue(is_array($cachedFolder), "No cache entry for folder at $i");
  697. $this->assertEquals($folderName, $cachedFolder['name'], "Wrong cache entry for folder at $i");
  698. $cachedFile = $cache->get($longPath . '/test.txt');
  699. $this->assertTrue(is_array($cachedFile), "No cache entry for file at $i");
  700. $this->assertEquals('test.txt', $cachedFile['name'], "Wrong cache entry for file at $i");
  701. $longPath .= $ds . $folderName;
  702. }
  703. }
  704. public function testTouchNotSupported() {
  705. $storage = new TemporaryNoTouch([]);
  706. $scanner = $storage->getScanner();
  707. Filesystem::mount($storage, [], '/test/');
  708. $past = time() - 100;
  709. $storage->file_put_contents('test', 'foobar');
  710. $scanner->scan('');
  711. $view = new View('');
  712. $info = $view->getFileInfo('/test/test');
  713. $view->touch('/test/test', $past);
  714. $scanner->scanFile('test', \OC\Files\Cache\Scanner::REUSE_ETAG);
  715. $info2 = $view->getFileInfo('/test/test');
  716. $this->assertSame($info['etag'], $info2['etag']);
  717. }
  718. public function testWatcherEtagCrossStorage() {
  719. $storage1 = new Temporary([]);
  720. $storage2 = new Temporary([]);
  721. $scanner1 = $storage1->getScanner();
  722. $scanner2 = $storage2->getScanner();
  723. $storage1->mkdir('sub');
  724. Filesystem::mount($storage1, [], '/test/');
  725. Filesystem::mount($storage2, [], '/test/sub/storage');
  726. $past = time() - 100;
  727. $storage2->file_put_contents('test.txt', 'foobar');
  728. $scanner1->scan('');
  729. $scanner2->scan('');
  730. $view = new View('');
  731. $storage2->getWatcher('')->setPolicy(Watcher::CHECK_ALWAYS);
  732. $oldFileInfo = $view->getFileInfo('/test/sub/storage/test.txt');
  733. $oldFolderInfo = $view->getFileInfo('/test');
  734. $storage2->getCache()->update($oldFileInfo->getId(), [
  735. 'storage_mtime' => $past,
  736. ]);
  737. $oldEtag = $oldFolderInfo->getEtag();
  738. $view->getFileInfo('/test/sub/storage/test.txt');
  739. $newFolderInfo = $view->getFileInfo('/test');
  740. $this->assertNotEquals($newFolderInfo->getEtag(), $oldEtag);
  741. }
  742. /**
  743. * @dataProvider absolutePathProvider
  744. */
  745. public function testGetAbsolutePath($expectedPath, $relativePath) {
  746. $view = new View('/files');
  747. $this->assertEquals($expectedPath, $view->getAbsolutePath($relativePath));
  748. }
  749. public function testPartFileInfo() {
  750. $storage = new Temporary([]);
  751. $scanner = $storage->getScanner();
  752. Filesystem::mount($storage, [], '/test/');
  753. $storage->file_put_contents('test.part', 'foobar');
  754. $scanner->scan('');
  755. $view = new View('/test');
  756. $info = $view->getFileInfo('test.part');
  757. $this->assertInstanceOf('\OCP\Files\FileInfo', $info);
  758. $this->assertNull($info->getId());
  759. $this->assertEquals(6, $info->getSize());
  760. }
  761. public function absolutePathProvider() {
  762. return [
  763. ['/files/', ''],
  764. ['/files/0', '0'],
  765. ['/files/false', 'false'],
  766. ['/files/true', 'true'],
  767. ['/files/', '/'],
  768. ['/files/test', 'test'],
  769. ['/files/test', '/test'],
  770. ];
  771. }
  772. /**
  773. * @dataProvider chrootRelativePathProvider
  774. */
  775. public function testChrootGetRelativePath($root, $absolutePath, $expectedPath) {
  776. $view = new View('/files');
  777. $view->chroot($root);
  778. $this->assertEquals($expectedPath, $view->getRelativePath($absolutePath));
  779. }
  780. public function chrootRelativePathProvider() {
  781. return $this->relativePathProvider('/');
  782. }
  783. /**
  784. * @dataProvider initRelativePathProvider
  785. */
  786. public function testInitGetRelativePath($root, $absolutePath, $expectedPath) {
  787. $view = new View($root);
  788. $this->assertEquals($expectedPath, $view->getRelativePath($absolutePath));
  789. }
  790. public function initRelativePathProvider() {
  791. return $this->relativePathProvider(null);
  792. }
  793. public function relativePathProvider($missingRootExpectedPath) {
  794. return [
  795. // No root - returns the path
  796. ['', '/files', '/files'],
  797. ['', '/files/', '/files/'],
  798. // Root equals path - /
  799. ['/files/', '/files/', '/'],
  800. ['/files/', '/files', '/'],
  801. ['/files', '/files/', '/'],
  802. ['/files', '/files', '/'],
  803. // False negatives: chroot fixes those by adding the leading slash.
  804. // But setting them up with this root (instead of chroot($root))
  805. // will fail them, although they should be the same.
  806. // TODO init should be fixed, so it also adds the leading slash
  807. ['files/', '/files/', $missingRootExpectedPath],
  808. ['files', '/files/', $missingRootExpectedPath],
  809. ['files/', '/files', $missingRootExpectedPath],
  810. ['files', '/files', $missingRootExpectedPath],
  811. // False negatives: Paths provided to the method should have a leading slash
  812. // TODO input should be checked to have a leading slash
  813. ['/files/', 'files/', null],
  814. ['/files', 'files/', null],
  815. ['/files/', 'files', null],
  816. ['/files', 'files', null],
  817. // with trailing slashes
  818. ['/files/', '/files/0', '0'],
  819. ['/files/', '/files/false', 'false'],
  820. ['/files/', '/files/true', 'true'],
  821. ['/files/', '/files/test', 'test'],
  822. ['/files/', '/files/test/foo', 'test/foo'],
  823. // without trailing slashes
  824. // TODO false expectation: Should match "with trailing slashes"
  825. ['/files', '/files/0', '/0'],
  826. ['/files', '/files/false', '/false'],
  827. ['/files', '/files/true', '/true'],
  828. ['/files', '/files/test', '/test'],
  829. ['/files', '/files/test/foo', '/test/foo'],
  830. // leading slashes
  831. ['/files/', '/files_trashbin/', null],
  832. ['/files', '/files_trashbin/', null],
  833. ['/files/', '/files_trashbin', null],
  834. ['/files', '/files_trashbin', null],
  835. // no leading slashes
  836. ['files/', 'files_trashbin/', null],
  837. ['files', 'files_trashbin/', null],
  838. ['files/', 'files_trashbin', null],
  839. ['files', 'files_trashbin', null],
  840. // mixed leading slashes
  841. ['files/', '/files_trashbin/', null],
  842. ['/files/', 'files_trashbin/', null],
  843. ['files', '/files_trashbin/', null],
  844. ['/files', 'files_trashbin/', null],
  845. ['files/', '/files_trashbin', null],
  846. ['/files/', 'files_trashbin', null],
  847. ['files', '/files_trashbin', null],
  848. ['/files', 'files_trashbin', null],
  849. ['files', 'files_trashbin/test', null],
  850. ['/files', '/files_trashbin/test', null],
  851. ['/files', 'files_trashbin/test', null],
  852. ];
  853. }
  854. public function testFileView() {
  855. $storage = new Temporary([]);
  856. $scanner = $storage->getScanner();
  857. $storage->file_put_contents('foo.txt', 'bar');
  858. Filesystem::mount($storage, [], '/test/');
  859. $scanner->scan('');
  860. $view = new View('/test/foo.txt');
  861. $this->assertEquals('bar', $view->file_get_contents(''));
  862. $fh = tmpfile();
  863. fwrite($fh, 'foo');
  864. rewind($fh);
  865. $view->file_put_contents('', $fh);
  866. $this->assertEquals('foo', $view->file_get_contents(''));
  867. }
  868. /**
  869. * @dataProvider tooLongPathDataProvider
  870. */
  871. public function testTooLongPath($operation, $param0 = null) {
  872. $this->expectException(\OCP\Files\InvalidPathException::class);
  873. $longPath = '';
  874. // 4000 is the maximum path length in file_cache.path
  875. $folderName = 'abcdefghijklmnopqrstuvwxyz012345678901234567890123456789';
  876. $depth = (4000 / 57);
  877. foreach (range(0, $depth + 1) as $i) {
  878. $longPath .= '/' . $folderName;
  879. }
  880. $storage = new Temporary([]);
  881. $this->tempStorage = $storage; // for later hard cleanup
  882. Filesystem::mount($storage, [], '/');
  883. $rootView = new View('');
  884. if ($param0 === '@0') {
  885. $param0 = $longPath;
  886. }
  887. if ($operation === 'hash') {
  888. $param0 = $longPath;
  889. $longPath = 'md5';
  890. }
  891. call_user_func([$rootView, $operation], $longPath, $param0);
  892. }
  893. public function tooLongPathDataProvider() {
  894. return [
  895. ['getAbsolutePath'],
  896. ['getRelativePath'],
  897. ['getMountPoint'],
  898. ['resolvePath'],
  899. ['getLocalFile'],
  900. ['mkdir'],
  901. ['rmdir'],
  902. ['opendir'],
  903. ['is_dir'],
  904. ['is_file'],
  905. ['stat'],
  906. ['filetype'],
  907. ['filesize'],
  908. ['readfile'],
  909. ['isCreatable'],
  910. ['isReadable'],
  911. ['isUpdatable'],
  912. ['isDeletable'],
  913. ['isSharable'],
  914. ['file_exists'],
  915. ['filemtime'],
  916. ['touch'],
  917. ['file_get_contents'],
  918. ['unlink'],
  919. ['deleteAll'],
  920. ['toTmpFile'],
  921. ['getMimeType'],
  922. ['free_space'],
  923. ['getFileInfo'],
  924. ['getDirectoryContent'],
  925. ['getOwner'],
  926. ['getETag'],
  927. ['file_put_contents', 'ipsum'],
  928. ['rename', '@0'],
  929. ['copy', '@0'],
  930. ['fopen', 'r'],
  931. ['fromTmpFile', '@0'],
  932. ['hash'],
  933. ['hasUpdated', 0],
  934. ['putFileInfo', []],
  935. ];
  936. }
  937. public function testRenameCrossStoragePreserveMtime() {
  938. $storage1 = new Temporary([]);
  939. $storage2 = new Temporary([]);
  940. $storage1->mkdir('sub');
  941. $storage1->mkdir('foo');
  942. $storage1->file_put_contents('foo.txt', 'asd');
  943. $storage1->file_put_contents('foo/bar.txt', 'asd');
  944. Filesystem::mount($storage1, [], '/test/');
  945. Filesystem::mount($storage2, [], '/test/sub/storage');
  946. $view = new View('');
  947. $time = time() - 200;
  948. $view->touch('/test/foo.txt', $time);
  949. $view->touch('/test/foo', $time);
  950. $view->touch('/test/foo/bar.txt', $time);
  951. $view->rename('/test/foo.txt', '/test/sub/storage/foo.txt');
  952. $this->assertEquals($time, $view->filemtime('/test/sub/storage/foo.txt'));
  953. $view->rename('/test/foo', '/test/sub/storage/foo');
  954. $this->assertEquals($time, $view->filemtime('/test/sub/storage/foo/bar.txt'));
  955. }
  956. public function testRenameFailDeleteTargetKeepSource() {
  957. $this->doTestCopyRenameFail('rename');
  958. }
  959. public function testCopyFailDeleteTargetKeepSource() {
  960. $this->doTestCopyRenameFail('copy');
  961. }
  962. private function doTestCopyRenameFail($operation) {
  963. $storage1 = new Temporary([]);
  964. /** @var \PHPUnit\Framework\MockObject\MockObject|Temporary $storage2 */
  965. $storage2 = $this->getMockBuilder(TemporaryNoCross::class)
  966. ->setConstructorArgs([[]])
  967. ->setMethods(['fopen', 'writeStream'])
  968. ->getMock();
  969. $storage2->method('writeStream')
  970. ->willThrowException(new GenericFileException("Failed to copy stream"));
  971. $storage1->mkdir('sub');
  972. $storage1->file_put_contents('foo.txt', '0123456789ABCDEFGH');
  973. $storage1->mkdir('dirtomove');
  974. $storage1->file_put_contents('dirtomove/indir1.txt', '0123456'); // fits
  975. $storage1->file_put_contents('dirtomove/indir2.txt', '0123456789ABCDEFGH'); // doesn't fit
  976. $storage2->file_put_contents('existing.txt', '0123');
  977. $storage1->getScanner()->scan('');
  978. $storage2->getScanner()->scan('');
  979. Filesystem::mount($storage1, [], '/test/');
  980. Filesystem::mount($storage2, [], '/test/sub/storage');
  981. // move file
  982. $view = new View('');
  983. $this->assertTrue($storage1->file_exists('foo.txt'));
  984. $this->assertFalse($storage2->file_exists('foo.txt'));
  985. $this->assertFalse($view->$operation('/test/foo.txt', '/test/sub/storage/foo.txt'));
  986. $this->assertFalse($storage2->file_exists('foo.txt'));
  987. $this->assertFalse($storage2->getCache()->get('foo.txt'));
  988. $this->assertTrue($storage1->file_exists('foo.txt'));
  989. // if target exists, it will be deleted too
  990. $this->assertFalse($view->$operation('/test/foo.txt', '/test/sub/storage/existing.txt'));
  991. $this->assertFalse($storage2->file_exists('existing.txt'));
  992. $this->assertFalse($storage2->getCache()->get('existing.txt'));
  993. $this->assertTrue($storage1->file_exists('foo.txt'));
  994. // move folder
  995. $this->assertFalse($view->$operation('/test/dirtomove/', '/test/sub/storage/dirtomove/'));
  996. // since the move failed, the full source tree is kept
  997. $this->assertTrue($storage1->file_exists('dirtomove/indir1.txt'));
  998. $this->assertTrue($storage1->file_exists('dirtomove/indir2.txt'));
  999. // second file not moved/copied
  1000. $this->assertFalse($storage2->file_exists('dirtomove/indir2.txt'));
  1001. $this->assertFalse($storage2->getCache()->get('dirtomove/indir2.txt'));
  1002. }
  1003. public function testDeleteFailKeepCache() {
  1004. /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */
  1005. $storage = $this->getMockBuilder(Temporary::class)
  1006. ->setConstructorArgs([[]])
  1007. ->setMethods(['unlink'])
  1008. ->getMock();
  1009. $storage->expects($this->once())
  1010. ->method('unlink')
  1011. ->willReturn(false);
  1012. $scanner = $storage->getScanner();
  1013. $cache = $storage->getCache();
  1014. $storage->file_put_contents('foo.txt', 'asd');
  1015. $scanner->scan('');
  1016. Filesystem::mount($storage, [], '/test/');
  1017. $view = new View('/test');
  1018. $this->assertFalse($view->unlink('foo.txt'));
  1019. $this->assertTrue($cache->inCache('foo.txt'));
  1020. }
  1021. public function directoryTraversalProvider() {
  1022. return [
  1023. ['../test/'],
  1024. ['..\\test\\my/../folder'],
  1025. ['/test/my/../foo\\'],
  1026. ];
  1027. }
  1028. /**
  1029. * @dataProvider directoryTraversalProvider
  1030. * @param string $root
  1031. */
  1032. public function testConstructDirectoryTraversalException($root) {
  1033. $this->expectException(\Exception::class);
  1034. new View($root);
  1035. }
  1036. public function testRenameOverWrite() {
  1037. $storage = new Temporary([]);
  1038. $scanner = $storage->getScanner();
  1039. $storage->mkdir('sub');
  1040. $storage->mkdir('foo');
  1041. $storage->file_put_contents('foo.txt', 'asd');
  1042. $storage->file_put_contents('foo/bar.txt', 'asd');
  1043. $scanner->scan('');
  1044. Filesystem::mount($storage, [], '/test/');
  1045. $view = new View('');
  1046. $this->assertTrue($view->rename('/test/foo.txt', '/test/foo/bar.txt'));
  1047. }
  1048. public function testSetMountOptionsInStorage() {
  1049. $mount = new MountPoint(Temporary::class, '/asd/', [[]], Filesystem::getLoader(), ['foo' => 'bar']);
  1050. Filesystem::getMountManager()->addMount($mount);
  1051. /** @var \OC\Files\Storage\Common $storage */
  1052. $storage = $mount->getStorage();
  1053. $this->assertEquals($storage->getMountOption('foo'), 'bar');
  1054. }
  1055. public function testSetMountOptionsWatcherPolicy() {
  1056. $mount = new MountPoint(Temporary::class, '/asd/', [[]], Filesystem::getLoader(), ['filesystem_check_changes' => Watcher::CHECK_NEVER]);
  1057. Filesystem::getMountManager()->addMount($mount);
  1058. /** @var \OC\Files\Storage\Common $storage */
  1059. $storage = $mount->getStorage();
  1060. $watcher = $storage->getWatcher();
  1061. $this->assertEquals(Watcher::CHECK_NEVER, $watcher->getPolicy());
  1062. }
  1063. public function testGetAbsolutePathOnNull() {
  1064. $view = new View();
  1065. $this->assertNull($view->getAbsolutePath(null));
  1066. }
  1067. public function testGetRelativePathOnNull() {
  1068. $view = new View();
  1069. $this->assertNull($view->getRelativePath(null));
  1070. }
  1071. public function testNullAsRoot() {
  1072. $this->expectException(\TypeError::class);
  1073. new View(null);
  1074. }
  1075. /**
  1076. * e.g. reading from a folder that's being renamed
  1077. *
  1078. *
  1079. * @dataProvider dataLockPaths
  1080. *
  1081. * @param string $rootPath
  1082. * @param string $pathPrefix
  1083. */
  1084. public function testReadFromWriteLockedPath($rootPath, $pathPrefix) {
  1085. $this->expectException(\OCP\Lock\LockedException::class);
  1086. $rootPath = str_replace('{folder}', 'files', $rootPath);
  1087. $pathPrefix = str_replace('{folder}', 'files', $pathPrefix);
  1088. $view = new View($rootPath);
  1089. $storage = new Temporary([]);
  1090. Filesystem::mount($storage, [], '/');
  1091. $this->assertTrue($view->lockFile($pathPrefix . '/foo/bar', ILockingProvider::LOCK_EXCLUSIVE));
  1092. $view->lockFile($pathPrefix . '/foo/bar/asd', ILockingProvider::LOCK_SHARED);
  1093. }
  1094. /**
  1095. * Reading from a files_encryption folder that's being renamed
  1096. *
  1097. * @dataProvider dataLockPaths
  1098. *
  1099. * @param string $rootPath
  1100. * @param string $pathPrefix
  1101. */
  1102. public function testReadFromWriteUnlockablePath($rootPath, $pathPrefix) {
  1103. $rootPath = str_replace('{folder}', 'files_encryption', $rootPath);
  1104. $pathPrefix = str_replace('{folder}', 'files_encryption', $pathPrefix);
  1105. $view = new View($rootPath);
  1106. $storage = new Temporary([]);
  1107. Filesystem::mount($storage, [], '/');
  1108. $this->assertFalse($view->lockFile($pathPrefix . '/foo/bar', ILockingProvider::LOCK_EXCLUSIVE));
  1109. $this->assertFalse($view->lockFile($pathPrefix . '/foo/bar/asd', ILockingProvider::LOCK_SHARED));
  1110. }
  1111. /**
  1112. * e.g. writing a file that's being downloaded
  1113. *
  1114. *
  1115. * @dataProvider dataLockPaths
  1116. *
  1117. * @param string $rootPath
  1118. * @param string $pathPrefix
  1119. */
  1120. public function testWriteToReadLockedFile($rootPath, $pathPrefix) {
  1121. $this->expectException(\OCP\Lock\LockedException::class);
  1122. $rootPath = str_replace('{folder}', 'files', $rootPath);
  1123. $pathPrefix = str_replace('{folder}', 'files', $pathPrefix);
  1124. $view = new View($rootPath);
  1125. $storage = new Temporary([]);
  1126. Filesystem::mount($storage, [], '/');
  1127. $this->assertTrue($view->lockFile($pathPrefix . '/foo/bar', ILockingProvider::LOCK_SHARED));
  1128. $view->lockFile($pathPrefix . '/foo/bar', ILockingProvider::LOCK_EXCLUSIVE);
  1129. }
  1130. /**
  1131. * Writing a file that's being downloaded
  1132. *
  1133. * @dataProvider dataLockPaths
  1134. *
  1135. * @param string $rootPath
  1136. * @param string $pathPrefix
  1137. */
  1138. public function testWriteToReadUnlockableFile($rootPath, $pathPrefix) {
  1139. $rootPath = str_replace('{folder}', 'files_encryption', $rootPath);
  1140. $pathPrefix = str_replace('{folder}', 'files_encryption', $pathPrefix);
  1141. $view = new View($rootPath);
  1142. $storage = new Temporary([]);
  1143. Filesystem::mount($storage, [], '/');
  1144. $this->assertFalse($view->lockFile($pathPrefix . '/foo/bar', ILockingProvider::LOCK_SHARED));
  1145. $this->assertFalse($view->lockFile($pathPrefix . '/foo/bar', ILockingProvider::LOCK_EXCLUSIVE));
  1146. }
  1147. /**
  1148. * Test that locks are on mount point paths instead of mount root
  1149. */
  1150. public function testLockLocalMountPointPathInsteadOfStorageRoot() {
  1151. $lockingProvider = \OC::$server->getLockingProvider();
  1152. $view = new View('/testuser/files/');
  1153. $storage = new Temporary([]);
  1154. Filesystem::mount($storage, [], '/');
  1155. $mountedStorage = new Temporary([]);
  1156. Filesystem::mount($mountedStorage, [], '/testuser/files/mountpoint');
  1157. $this->assertTrue(
  1158. $view->lockFile('/mountpoint', ILockingProvider::LOCK_EXCLUSIVE, true),
  1159. 'Can lock mount point'
  1160. );
  1161. // no exception here because storage root was not locked
  1162. $mountedStorage->acquireLock('', ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider);
  1163. $thrown = false;
  1164. try {
  1165. $storage->acquireLock('/testuser/files/mountpoint', ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider);
  1166. } catch (LockedException $e) {
  1167. $thrown = true;
  1168. }
  1169. $this->assertTrue($thrown, 'Mount point path was locked on root storage');
  1170. $lockingProvider->releaseAll();
  1171. }
  1172. /**
  1173. * Test that locks are on mount point paths and also mount root when requested
  1174. */
  1175. public function testLockStorageRootButNotLocalMountPoint() {
  1176. $lockingProvider = \OC::$server->getLockingProvider();
  1177. $view = new View('/testuser/files/');
  1178. $storage = new Temporary([]);
  1179. Filesystem::mount($storage, [], '/');
  1180. $mountedStorage = new Temporary([]);
  1181. Filesystem::mount($mountedStorage, [], '/testuser/files/mountpoint');
  1182. $this->assertTrue(
  1183. $view->lockFile('/mountpoint', ILockingProvider::LOCK_EXCLUSIVE, false),
  1184. 'Can lock mount point'
  1185. );
  1186. $thrown = false;
  1187. try {
  1188. $mountedStorage->acquireLock('', ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider);
  1189. } catch (LockedException $e) {
  1190. $thrown = true;
  1191. }
  1192. $this->assertTrue($thrown, 'Mount point storage root was locked on original storage');
  1193. // local mount point was not locked
  1194. $storage->acquireLock('/testuser/files/mountpoint', ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider);
  1195. $lockingProvider->releaseAll();
  1196. }
  1197. /**
  1198. * Test that locks are on mount point paths and also mount root when requested
  1199. */
  1200. public function testLockMountPointPathFailReleasesBoth() {
  1201. $lockingProvider = \OC::$server->getLockingProvider();
  1202. $view = new View('/testuser/files/');
  1203. $storage = new Temporary([]);
  1204. Filesystem::mount($storage, [], '/');
  1205. $mountedStorage = new Temporary([]);
  1206. Filesystem::mount($mountedStorage, [], '/testuser/files/mountpoint.txt');
  1207. // this would happen if someone is writing on the mount point
  1208. $mountedStorage->acquireLock('', ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider);
  1209. $thrown = false;
  1210. try {
  1211. // this actually acquires two locks, one on the mount point and one on the storage root,
  1212. // but the one on the storage root will fail
  1213. $view->lockFile('/mountpoint.txt', ILockingProvider::LOCK_SHARED);
  1214. } catch (LockedException $e) {
  1215. $thrown = true;
  1216. }
  1217. $this->assertTrue($thrown, 'Cannot acquire shared lock because storage root is already locked');
  1218. // from here we expect that the lock on the local mount point was released properly
  1219. // so acquiring an exclusive lock will succeed
  1220. $storage->acquireLock('/testuser/files/mountpoint.txt', ILockingProvider::LOCK_EXCLUSIVE, $lockingProvider);
  1221. $lockingProvider->releaseAll();
  1222. }
  1223. public function dataLockPaths() {
  1224. return [
  1225. ['/testuser/{folder}', ''],
  1226. ['/testuser', '/{folder}'],
  1227. ['', '/testuser/{folder}'],
  1228. ];
  1229. }
  1230. public function pathRelativeToFilesProvider() {
  1231. return [
  1232. ['admin/files', ''],
  1233. ['admin/files/x', 'x'],
  1234. ['/admin/files', ''],
  1235. ['/admin/files/sub', 'sub'],
  1236. ['/admin/files/sub/', 'sub'],
  1237. ['/admin/files/sub/sub2', 'sub/sub2'],
  1238. ['//admin//files/sub//sub2', 'sub/sub2'],
  1239. ];
  1240. }
  1241. /**
  1242. * @dataProvider pathRelativeToFilesProvider
  1243. */
  1244. public function testGetPathRelativeToFiles($path, $expectedPath) {
  1245. $view = new View();
  1246. $this->assertEquals($expectedPath, $view->getPathRelativeToFiles($path));
  1247. }
  1248. public function pathRelativeToFilesProviderExceptionCases() {
  1249. return [
  1250. [''],
  1251. ['x'],
  1252. ['files'],
  1253. ['/files'],
  1254. ['/admin/files_versions/abc'],
  1255. ];
  1256. }
  1257. /**
  1258. * @dataProvider pathRelativeToFilesProviderExceptionCases
  1259. * @param string $path
  1260. */
  1261. public function testGetPathRelativeToFilesWithInvalidArgument($path) {
  1262. $this->expectException(\InvalidArgumentException::class);
  1263. $this->expectExceptionMessage('$absolutePath must be relative to "files"');
  1264. $view = new View();
  1265. $view->getPathRelativeToFiles($path);
  1266. }
  1267. public function testChangeLock() {
  1268. $view = new View('/testuser/files/');
  1269. $storage = new Temporary([]);
  1270. Filesystem::mount($storage, [], '/');
  1271. $view->lockFile('/test/sub', ILockingProvider::LOCK_SHARED);
  1272. $this->assertTrue($this->isFileLocked($view, '/test//sub', ILockingProvider::LOCK_SHARED));
  1273. $this->assertFalse($this->isFileLocked($view, '/test//sub', ILockingProvider::LOCK_EXCLUSIVE));
  1274. $view->changeLock('//test/sub', ILockingProvider::LOCK_EXCLUSIVE);
  1275. $this->assertTrue($this->isFileLocked($view, '/test//sub', ILockingProvider::LOCK_EXCLUSIVE));
  1276. $view->changeLock('test/sub', ILockingProvider::LOCK_SHARED);
  1277. $this->assertTrue($this->isFileLocked($view, '/test//sub', ILockingProvider::LOCK_SHARED));
  1278. $view->unlockFile('/test/sub/', ILockingProvider::LOCK_SHARED);
  1279. $this->assertFalse($this->isFileLocked($view, '/test//sub', ILockingProvider::LOCK_SHARED));
  1280. $this->assertFalse($this->isFileLocked($view, '/test//sub', ILockingProvider::LOCK_EXCLUSIVE));
  1281. }
  1282. public function hookPathProvider() {
  1283. return [
  1284. ['/foo/files', '/foo', true],
  1285. ['/foo/files/bar', '/foo', true],
  1286. ['/foo', '/foo', false],
  1287. ['/foo', '/files/foo', true],
  1288. ['/foo', 'filesfoo', false],
  1289. ['', '/foo/files', true],
  1290. ['', '/foo/files/bar.txt', true],
  1291. ];
  1292. }
  1293. /**
  1294. * @dataProvider hookPathProvider
  1295. * @param $root
  1296. * @param $path
  1297. * @param $shouldEmit
  1298. */
  1299. public function testHookPaths($root, $path, $shouldEmit) {
  1300. $filesystemReflection = new \ReflectionClass(Filesystem::class);
  1301. $defaultRootValue = $filesystemReflection->getProperty('defaultInstance');
  1302. $defaultRootValue->setAccessible(true);
  1303. $oldRoot = $defaultRootValue->getValue();
  1304. $defaultView = new View('/foo/files');
  1305. $defaultRootValue->setValue(null, $defaultView);
  1306. $view = new View($root);
  1307. $result = self::invokePrivate($view, 'shouldEmitHooks', [$path]);
  1308. $defaultRootValue->setValue(null, $oldRoot);
  1309. $this->assertEquals($shouldEmit, $result);
  1310. }
  1311. /**
  1312. * Create test movable mount points
  1313. *
  1314. * @param array $mountPoints array of mount point locations
  1315. * @return array array of MountPoint objects
  1316. */
  1317. private function createTestMovableMountPoints($mountPoints) {
  1318. $mounts = [];
  1319. foreach ($mountPoints as $mountPoint) {
  1320. $storage = $this->getMockBuilder(Storage::class)
  1321. ->setMethods([])
  1322. ->setConstructorArgs([[]])
  1323. ->getMock();
  1324. $storage->method('getId')->willReturn('non-null-id');
  1325. $storage->method('getStorageCache')->willReturnCallback(function () use ($storage) {
  1326. return new \OC\Files\Cache\Storage($storage, true, \OC::$server->get(IDBConnection::class));
  1327. });
  1328. $mounts[] = $this->getMockBuilder(TestMoveableMountPoint::class)
  1329. ->setMethods(['moveMount'])
  1330. ->setConstructorArgs([$storage, $mountPoint])
  1331. ->getMock();
  1332. }
  1333. /** @var IMountProvider|\PHPUnit\Framework\MockObject\MockObject $mountProvider */
  1334. $mountProvider = $this->createMock(IMountProvider::class);
  1335. $mountProvider->expects($this->any())
  1336. ->method('getMountsForUser')
  1337. ->willReturn($mounts);
  1338. $mountProviderCollection = \OC::$server->getMountProviderCollection();
  1339. $mountProviderCollection->registerProvider($mountProvider);
  1340. return $mounts;
  1341. }
  1342. /**
  1343. * Test mount point move
  1344. */
  1345. public function testMountPointMove() {
  1346. self::loginAsUser($this->user);
  1347. [$mount1, $mount2] = $this->createTestMovableMountPoints([
  1348. $this->user . '/files/mount1',
  1349. $this->user . '/files/mount2',
  1350. ]);
  1351. $mount1->expects($this->once())
  1352. ->method('moveMount')
  1353. ->willReturn(true);
  1354. $mount2->expects($this->once())
  1355. ->method('moveMount')
  1356. ->willReturn(true);
  1357. $view = new View('/' . $this->user . '/files/');
  1358. $view->mkdir('sub');
  1359. $this->assertTrue($view->rename('mount1', 'renamed_mount'), 'Can rename mount point');
  1360. $this->assertTrue($view->rename('mount2', 'sub/moved_mount'), 'Can move a mount point into a subdirectory');
  1361. }
  1362. /**
  1363. * Test that moving a mount point into another is forbidden
  1364. */
  1365. public function testMoveMountPointIntoAnother() {
  1366. self::loginAsUser($this->user);
  1367. [$mount1, $mount2] = $this->createTestMovableMountPoints([
  1368. $this->user . '/files/mount1',
  1369. $this->user . '/files/mount2',
  1370. ]);
  1371. $mount1->expects($this->never())
  1372. ->method('moveMount');
  1373. $mount2->expects($this->never())
  1374. ->method('moveMount');
  1375. $view = new View('/' . $this->user . '/files/');
  1376. $this->assertFalse($view->rename('mount1', 'mount2'), 'Cannot overwrite another mount point');
  1377. $this->assertFalse($view->rename('mount1', 'mount2/sub'), 'Cannot move a mount point into another');
  1378. }
  1379. /**
  1380. * Test that moving a mount point into a shared folder is forbidden
  1381. */
  1382. public function testMoveMountPointIntoSharedFolder() {
  1383. self::loginAsUser($this->user);
  1384. [$mount1] = $this->createTestMovableMountPoints([
  1385. $this->user . '/files/mount1',
  1386. ]);
  1387. $mount1->expects($this->never())
  1388. ->method('moveMount');
  1389. $view = new View('/' . $this->user . '/files/');
  1390. $view->mkdir('shareddir');
  1391. $view->mkdir('shareddir/sub');
  1392. $view->mkdir('shareddir/sub2');
  1393. $fileId = $view->getFileInfo('shareddir')->getId();
  1394. $userObject = \OC::$server->getUserManager()->createUser('test2', 'IHateNonMockableStaticClasses');
  1395. $userFolder = \OC::$server->getUserFolder($this->user);
  1396. $shareDir = $userFolder->get('shareddir');
  1397. $shareManager = \OC::$server->getShareManager();
  1398. $share = $shareManager->newShare();
  1399. $share->setSharedWith('test2')
  1400. ->setSharedBy($this->user)
  1401. ->setShareType(IShare::TYPE_USER)
  1402. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  1403. ->setNode($shareDir);
  1404. $shareManager->createShare($share);
  1405. $this->assertFalse($view->rename('mount1', 'shareddir'), 'Cannot overwrite shared folder');
  1406. $this->assertFalse($view->rename('mount1', 'shareddir/sub'), 'Cannot move mount point into shared folder');
  1407. $this->assertFalse($view->rename('mount1', 'shareddir/sub/sub2'), 'Cannot move mount point into shared subfolder');
  1408. $shareManager->deleteShare($share);
  1409. $userObject->delete();
  1410. }
  1411. public function basicOperationProviderForLocks() {
  1412. return [
  1413. // --- write hook ----
  1414. [
  1415. 'touch',
  1416. ['touch-create.txt'],
  1417. 'touch-create.txt',
  1418. 'create',
  1419. ILockingProvider::LOCK_SHARED,
  1420. ILockingProvider::LOCK_EXCLUSIVE,
  1421. ILockingProvider::LOCK_SHARED,
  1422. ],
  1423. [
  1424. 'fopen',
  1425. ['test-write.txt', 'w'],
  1426. 'test-write.txt',
  1427. 'write',
  1428. ILockingProvider::LOCK_SHARED,
  1429. ILockingProvider::LOCK_EXCLUSIVE,
  1430. null,
  1431. // exclusive lock stays until fclose
  1432. ILockingProvider::LOCK_EXCLUSIVE,
  1433. ],
  1434. [
  1435. 'mkdir',
  1436. ['newdir'],
  1437. 'newdir',
  1438. 'write',
  1439. ILockingProvider::LOCK_SHARED,
  1440. ILockingProvider::LOCK_EXCLUSIVE,
  1441. ILockingProvider::LOCK_SHARED,
  1442. ],
  1443. [
  1444. 'file_put_contents',
  1445. ['file_put_contents.txt', 'blah'],
  1446. 'file_put_contents.txt',
  1447. 'write',
  1448. ILockingProvider::LOCK_SHARED,
  1449. ILockingProvider::LOCK_EXCLUSIVE,
  1450. ILockingProvider::LOCK_SHARED,
  1451. ],
  1452. // ---- delete hook ----
  1453. [
  1454. 'rmdir',
  1455. ['dir'],
  1456. 'dir',
  1457. 'delete',
  1458. ILockingProvider::LOCK_SHARED,
  1459. ILockingProvider::LOCK_EXCLUSIVE,
  1460. ILockingProvider::LOCK_SHARED,
  1461. ],
  1462. [
  1463. 'unlink',
  1464. ['test.txt'],
  1465. 'test.txt',
  1466. 'delete',
  1467. ILockingProvider::LOCK_SHARED,
  1468. ILockingProvider::LOCK_EXCLUSIVE,
  1469. ILockingProvider::LOCK_SHARED,
  1470. ],
  1471. // ---- read hook (no post hooks) ----
  1472. [
  1473. 'file_get_contents',
  1474. ['test.txt'],
  1475. 'test.txt',
  1476. 'read',
  1477. ILockingProvider::LOCK_SHARED,
  1478. ILockingProvider::LOCK_SHARED,
  1479. null,
  1480. ],
  1481. [
  1482. 'fopen',
  1483. ['test.txt', 'r'],
  1484. 'test.txt',
  1485. 'read',
  1486. ILockingProvider::LOCK_SHARED,
  1487. ILockingProvider::LOCK_SHARED,
  1488. null,
  1489. ],
  1490. [
  1491. 'opendir',
  1492. ['dir'],
  1493. 'dir',
  1494. 'read',
  1495. ILockingProvider::LOCK_SHARED,
  1496. ILockingProvider::LOCK_SHARED,
  1497. null,
  1498. ],
  1499. // ---- no lock, touch hook ---
  1500. ['touch', ['test.txt'], 'test.txt', 'touch', null, null, null],
  1501. // ---- no hooks, no locks ---
  1502. ['is_dir', ['dir'], 'dir', null],
  1503. ['is_file', ['dir'], 'dir', null],
  1504. ['stat', ['dir'], 'dir', null],
  1505. ['filetype', ['dir'], 'dir', null],
  1506. [
  1507. 'filesize',
  1508. ['dir'],
  1509. 'dir',
  1510. null,
  1511. ILockingProvider::LOCK_SHARED,
  1512. ILockingProvider::LOCK_SHARED,
  1513. ILockingProvider::LOCK_SHARED,
  1514. null,
  1515. /* Return an int */
  1516. 100
  1517. ],
  1518. ['isCreatable', ['dir'], 'dir', null],
  1519. ['isReadable', ['dir'], 'dir', null],
  1520. ['isUpdatable', ['dir'], 'dir', null],
  1521. ['isDeletable', ['dir'], 'dir', null],
  1522. ['isSharable', ['dir'], 'dir', null],
  1523. ['file_exists', ['dir'], 'dir', null],
  1524. ['filemtime', ['dir'], 'dir', null],
  1525. ];
  1526. }
  1527. /**
  1528. * Test whether locks are set before and after the operation
  1529. *
  1530. * @dataProvider basicOperationProviderForLocks
  1531. *
  1532. * @param string $operation operation name on the view
  1533. * @param array $operationArgs arguments for the operation
  1534. * @param string $lockedPath path of the locked item to check
  1535. * @param string $hookType hook type
  1536. * @param int $expectedLockBefore expected lock during pre hooks
  1537. * @param int $expectedLockDuring expected lock during operation
  1538. * @param int $expectedLockAfter expected lock during post hooks
  1539. * @param int $expectedStrayLock expected lock after returning, should
  1540. * be null (unlock) for most operations
  1541. */
  1542. public function testLockBasicOperation(
  1543. $operation,
  1544. $operationArgs,
  1545. $lockedPath,
  1546. $hookType,
  1547. $expectedLockBefore = ILockingProvider::LOCK_SHARED,
  1548. $expectedLockDuring = ILockingProvider::LOCK_SHARED,
  1549. $expectedLockAfter = ILockingProvider::LOCK_SHARED,
  1550. $expectedStrayLock = null,
  1551. $returnValue = true,
  1552. ) {
  1553. $view = new View('/' . $this->user . '/files/');
  1554. /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */
  1555. $storage = $this->getMockBuilder(Temporary::class)
  1556. ->setMethods([$operation])
  1557. ->getMock();
  1558. Filesystem::mount($storage, [], $this->user . '/');
  1559. // work directly on disk because mkdir might be mocked
  1560. $realPath = $storage->getSourcePath('');
  1561. mkdir($realPath . '/files');
  1562. mkdir($realPath . '/files/dir');
  1563. file_put_contents($realPath . '/files/test.txt', 'blah');
  1564. $storage->getScanner()->scan('files');
  1565. $storage->expects($this->once())
  1566. ->method($operation)
  1567. ->willReturnCallback(
  1568. function () use ($view, $lockedPath, &$lockTypeDuring, $returnValue) {
  1569. $lockTypeDuring = $this->getFileLockType($view, $lockedPath);
  1570. return $returnValue;
  1571. }
  1572. );
  1573. $this->assertNull($this->getFileLockType($view, $lockedPath), 'File not locked before operation');
  1574. $this->connectMockHooks($hookType, $view, $lockedPath, $lockTypePre, $lockTypePost);
  1575. // do operation
  1576. call_user_func_array([$view, $operation], $operationArgs);
  1577. if ($hookType !== null) {
  1578. $this->assertEquals($expectedLockBefore, $lockTypePre, 'File locked properly during pre-hook');
  1579. $this->assertEquals($expectedLockAfter, $lockTypePost, 'File locked properly during post-hook');
  1580. $this->assertEquals($expectedLockDuring, $lockTypeDuring, 'File locked properly during operation');
  1581. } else {
  1582. $this->assertNull($lockTypeDuring, 'File not locked during operation');
  1583. }
  1584. $this->assertEquals($expectedStrayLock, $this->getFileLockType($view, $lockedPath));
  1585. }
  1586. /**
  1587. * Test locks for file_put_content with stream.
  1588. * This code path uses $storage->fopen instead
  1589. */
  1590. public function testLockFilePutContentWithStream() {
  1591. $view = new View('/' . $this->user . '/files/');
  1592. $path = 'test_file_put_contents.txt';
  1593. /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */
  1594. $storage = $this->getMockBuilder(Temporary::class)
  1595. ->setMethods(['fopen'])
  1596. ->getMock();
  1597. Filesystem::mount($storage, [], $this->user . '/');
  1598. $storage->mkdir('files');
  1599. $storage->expects($this->once())
  1600. ->method('fopen')
  1601. ->willReturnCallback(
  1602. function () use ($view, $path, &$lockTypeDuring) {
  1603. $lockTypeDuring = $this->getFileLockType($view, $path);
  1604. return fopen('php://temp', 'r+');
  1605. }
  1606. );
  1607. $this->connectMockHooks('write', $view, $path, $lockTypePre, $lockTypePost);
  1608. $this->assertNull($this->getFileLockType($view, $path), 'File not locked before operation');
  1609. // do operation
  1610. $view->file_put_contents($path, fopen('php://temp', 'r+'));
  1611. $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypePre, 'File locked properly during pre-hook');
  1612. $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypePost, 'File locked properly during post-hook');
  1613. $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeDuring, 'File locked properly during operation');
  1614. $this->assertNull($this->getFileLockType($view, $path));
  1615. }
  1616. /**
  1617. * Test locks for fopen with fclose at the end
  1618. */
  1619. public function testLockFopen() {
  1620. $view = new View('/' . $this->user . '/files/');
  1621. $path = 'test_file_put_contents.txt';
  1622. /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */
  1623. $storage = $this->getMockBuilder(Temporary::class)
  1624. ->setMethods(['fopen'])
  1625. ->getMock();
  1626. Filesystem::mount($storage, [], $this->user . '/');
  1627. $storage->mkdir('files');
  1628. $storage->expects($this->once())
  1629. ->method('fopen')
  1630. ->willReturnCallback(
  1631. function () use ($view, $path, &$lockTypeDuring) {
  1632. $lockTypeDuring = $this->getFileLockType($view, $path);
  1633. return fopen('php://temp', 'r+');
  1634. }
  1635. );
  1636. $this->connectMockHooks('write', $view, $path, $lockTypePre, $lockTypePost);
  1637. $this->assertNull($this->getFileLockType($view, $path), 'File not locked before operation');
  1638. // do operation
  1639. $res = $view->fopen($path, 'w');
  1640. $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypePre, 'File locked properly during pre-hook');
  1641. $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeDuring, 'File locked properly during operation');
  1642. $this->assertNull($lockTypePost, 'No post hook, no lock check possible');
  1643. $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeDuring, 'File still locked after fopen');
  1644. fclose($res);
  1645. $this->assertNull($this->getFileLockType($view, $path), 'File unlocked after fclose');
  1646. }
  1647. /**
  1648. * Test locks for fopen with fclose at the end
  1649. *
  1650. * @dataProvider basicOperationProviderForLocks
  1651. *
  1652. * @param string $operation operation name on the view
  1653. * @param array $operationArgs arguments for the operation
  1654. * @param string $path path of the locked item to check
  1655. */
  1656. public function testLockBasicOperationUnlocksAfterException(
  1657. $operation,
  1658. $operationArgs,
  1659. $path
  1660. ) {
  1661. if ($operation === 'touch') {
  1662. $this->markTestSkipped("touch handles storage exceptions internally");
  1663. }
  1664. $view = new View('/' . $this->user . '/files/');
  1665. /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */
  1666. $storage = $this->getMockBuilder(Temporary::class)
  1667. ->setMethods([$operation])
  1668. ->getMock();
  1669. Filesystem::mount($storage, [], $this->user . '/');
  1670. // work directly on disk because mkdir might be mocked
  1671. $realPath = $storage->getSourcePath('');
  1672. mkdir($realPath . '/files');
  1673. mkdir($realPath . '/files/dir');
  1674. file_put_contents($realPath . '/files/test.txt', 'blah');
  1675. $storage->getScanner()->scan('files');
  1676. $storage->expects($this->once())
  1677. ->method($operation)
  1678. ->willReturnCallback(
  1679. function () {
  1680. throw new \Exception('Simulated exception');
  1681. }
  1682. );
  1683. $thrown = false;
  1684. try {
  1685. call_user_func_array([$view, $operation], $operationArgs);
  1686. } catch (\Exception $e) {
  1687. $thrown = true;
  1688. $this->assertEquals('Simulated exception', $e->getMessage());
  1689. }
  1690. $this->assertTrue($thrown, 'Exception was rethrown');
  1691. $this->assertNull($this->getFileLockType($view, $path), 'File got unlocked after exception');
  1692. }
  1693. public function testLockBasicOperationUnlocksAfterLockException() {
  1694. $view = new View('/' . $this->user . '/files/');
  1695. $storage = new Temporary([]);
  1696. Filesystem::mount($storage, [], $this->user . '/');
  1697. $storage->mkdir('files');
  1698. $storage->mkdir('files/dir');
  1699. $storage->file_put_contents('files/test.txt', 'blah');
  1700. $storage->getScanner()->scan('files');
  1701. // get a shared lock
  1702. $handle = $view->fopen('test.txt', 'r');
  1703. $thrown = false;
  1704. try {
  1705. // try (and fail) to get a write lock
  1706. $view->unlink('test.txt');
  1707. } catch (\Exception $e) {
  1708. $thrown = true;
  1709. $this->assertInstanceOf(LockedException::class, $e);
  1710. }
  1711. $this->assertTrue($thrown, 'Exception was rethrown');
  1712. // clean shared lock
  1713. fclose($handle);
  1714. $this->assertNull($this->getFileLockType($view, 'test.txt'), 'File got unlocked');
  1715. }
  1716. /**
  1717. * Test locks for fopen with fclose at the end
  1718. *
  1719. * @dataProvider basicOperationProviderForLocks
  1720. *
  1721. * @param string $operation operation name on the view
  1722. * @param array $operationArgs arguments for the operation
  1723. * @param string $path path of the locked item to check
  1724. * @param string $hookType hook type
  1725. */
  1726. public function testLockBasicOperationUnlocksAfterCancelledHook(
  1727. $operation,
  1728. $operationArgs,
  1729. $path,
  1730. $hookType
  1731. ) {
  1732. $view = new View('/' . $this->user . '/files/');
  1733. /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */
  1734. $storage = $this->getMockBuilder(Temporary::class)
  1735. ->setMethods([$operation])
  1736. ->getMock();
  1737. Filesystem::mount($storage, [], $this->user . '/');
  1738. $storage->mkdir('files');
  1739. Util::connectHook(
  1740. Filesystem::CLASSNAME,
  1741. $hookType,
  1742. HookHelper::class,
  1743. 'cancellingCallback'
  1744. );
  1745. call_user_func_array([$view, $operation], $operationArgs);
  1746. $this->assertNull($this->getFileLockType($view, $path), 'File got unlocked after exception');
  1747. }
  1748. public function lockFileRenameOrCopyDataProvider() {
  1749. return [
  1750. ['rename', ILockingProvider::LOCK_EXCLUSIVE],
  1751. ['copy', ILockingProvider::LOCK_SHARED],
  1752. ];
  1753. }
  1754. /**
  1755. * Test locks for rename or copy operation
  1756. *
  1757. * @dataProvider lockFileRenameOrCopyDataProvider
  1758. *
  1759. * @param string $operation operation to be done on the view
  1760. * @param int $expectedLockTypeSourceDuring expected lock type on source file during
  1761. * the operation
  1762. */
  1763. public function testLockFileRename($operation, $expectedLockTypeSourceDuring) {
  1764. $view = new View('/' . $this->user . '/files/');
  1765. /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */
  1766. $storage = $this->getMockBuilder(Temporary::class)
  1767. ->setMethods([$operation, 'getMetaData', 'filemtime'])
  1768. ->getMock();
  1769. $storage->expects($this->any())
  1770. ->method('getMetaData')
  1771. ->will($this->returnValue([
  1772. 'mtime' => 1885434487,
  1773. 'etag' => '',
  1774. 'mimetype' => 'text/plain',
  1775. 'permissions' => Constants::PERMISSION_ALL,
  1776. 'size' => 3
  1777. ]));
  1778. $storage->expects($this->any())
  1779. ->method('filemtime')
  1780. ->willReturn(123456789);
  1781. $sourcePath = 'original.txt';
  1782. $targetPath = 'target.txt';
  1783. Filesystem::mount($storage, [], $this->user . '/');
  1784. $storage->mkdir('files');
  1785. $view->file_put_contents($sourcePath, 'meh');
  1786. $storage->expects($this->once())
  1787. ->method($operation)
  1788. ->willReturnCallback(
  1789. function () use ($view, $sourcePath, $targetPath, &$lockTypeSourceDuring, &$lockTypeTargetDuring) {
  1790. $lockTypeSourceDuring = $this->getFileLockType($view, $sourcePath);
  1791. $lockTypeTargetDuring = $this->getFileLockType($view, $targetPath);
  1792. return true;
  1793. }
  1794. );
  1795. $this->connectMockHooks($operation, $view, $sourcePath, $lockTypeSourcePre, $lockTypeSourcePost);
  1796. $this->connectMockHooks($operation, $view, $targetPath, $lockTypeTargetPre, $lockTypeTargetPost);
  1797. $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked before operation');
  1798. $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked before operation');
  1799. $view->$operation($sourcePath, $targetPath);
  1800. $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePre, 'Source file locked properly during pre-hook');
  1801. $this->assertEquals($expectedLockTypeSourceDuring, $lockTypeSourceDuring, 'Source file locked properly during operation');
  1802. $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePost, 'Source file locked properly during post-hook');
  1803. $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPre, 'Target file locked properly during pre-hook');
  1804. $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeTargetDuring, 'Target file locked properly during operation');
  1805. $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPost, 'Target file locked properly during post-hook');
  1806. $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked after operation');
  1807. $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked after operation');
  1808. }
  1809. /**
  1810. * simulate a failed copy operation.
  1811. * We expect that we catch the exception, free the lock and re-throw it.
  1812. *
  1813. */
  1814. public function testLockFileCopyException() {
  1815. $this->expectException(\Exception::class);
  1816. $view = new View('/' . $this->user . '/files/');
  1817. /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */
  1818. $storage = $this->getMockBuilder(Temporary::class)
  1819. ->setMethods(['copy'])
  1820. ->getMock();
  1821. $sourcePath = 'original.txt';
  1822. $targetPath = 'target.txt';
  1823. Filesystem::mount($storage, [], $this->user . '/');
  1824. $storage->mkdir('files');
  1825. $view->file_put_contents($sourcePath, 'meh');
  1826. $storage->expects($this->once())
  1827. ->method('copy')
  1828. ->willReturnCallback(
  1829. function () {
  1830. throw new \Exception();
  1831. }
  1832. );
  1833. $this->connectMockHooks('copy', $view, $sourcePath, $lockTypeSourcePre, $lockTypeSourcePost);
  1834. $this->connectMockHooks('copy', $view, $targetPath, $lockTypeTargetPre, $lockTypeTargetPost);
  1835. $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked before operation');
  1836. $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked before operation');
  1837. try {
  1838. $view->copy($sourcePath, $targetPath);
  1839. } catch (\Exception $e) {
  1840. $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked after operation');
  1841. $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked after operation');
  1842. throw $e;
  1843. }
  1844. }
  1845. /**
  1846. * Test rename operation: unlock first path when second path was locked
  1847. */
  1848. public function testLockFileRenameUnlockOnException() {
  1849. self::loginAsUser('test');
  1850. $view = new View('/' . $this->user . '/files/');
  1851. $sourcePath = 'original.txt';
  1852. $targetPath = 'target.txt';
  1853. $view->file_put_contents($sourcePath, 'meh');
  1854. // simulate that the target path is already locked
  1855. $view->lockFile($targetPath, ILockingProvider::LOCK_EXCLUSIVE);
  1856. $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked before operation');
  1857. $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $this->getFileLockType($view, $targetPath), 'Target file is locked before operation');
  1858. $thrown = false;
  1859. try {
  1860. $view->rename($sourcePath, $targetPath);
  1861. } catch (LockedException $e) {
  1862. $thrown = true;
  1863. }
  1864. $this->assertTrue($thrown, 'LockedException thrown');
  1865. $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked after operation');
  1866. $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $this->getFileLockType($view, $targetPath), 'Target file still locked after operation');
  1867. $view->unlockFile($targetPath, ILockingProvider::LOCK_EXCLUSIVE);
  1868. }
  1869. /**
  1870. * Test rename operation: unlock first path when second path was locked
  1871. */
  1872. public function testGetOwner() {
  1873. self::loginAsUser('test');
  1874. $view = new View('/test/files/');
  1875. $path = 'foo.txt';
  1876. $view->file_put_contents($path, 'meh');
  1877. $this->assertEquals('test', $view->getFileInfo($path)->getOwner()->getUID());
  1878. $folderInfo = $view->getDirectoryContent('');
  1879. $folderInfo = array_values(array_filter($folderInfo, function (FileInfo $info) {
  1880. return $info->getName() === 'foo.txt';
  1881. }));
  1882. $this->assertEquals('test', $folderInfo[0]->getOwner()->getUID());
  1883. $subStorage = new Temporary();
  1884. Filesystem::mount($subStorage, [], '/test/files/asd');
  1885. $folderInfo = $view->getDirectoryContent('');
  1886. $folderInfo = array_values(array_filter($folderInfo, function (FileInfo $info) {
  1887. return $info->getName() === 'asd';
  1888. }));
  1889. $this->assertEquals('test', $folderInfo[0]->getOwner()->getUID());
  1890. }
  1891. public function lockFileRenameOrCopyCrossStorageDataProvider() {
  1892. return [
  1893. ['rename', 'moveFromStorage', ILockingProvider::LOCK_EXCLUSIVE],
  1894. ['copy', 'copyFromStorage', ILockingProvider::LOCK_SHARED],
  1895. ];
  1896. }
  1897. /**
  1898. * Test locks for rename or copy operation cross-storage
  1899. *
  1900. * @dataProvider lockFileRenameOrCopyCrossStorageDataProvider
  1901. *
  1902. * @param string $viewOperation operation to be done on the view
  1903. * @param string $storageOperation operation to be mocked on the storage
  1904. * @param int $expectedLockTypeSourceDuring expected lock type on source file during
  1905. * the operation
  1906. */
  1907. public function testLockFileRenameCrossStorage($viewOperation, $storageOperation, $expectedLockTypeSourceDuring) {
  1908. $view = new View('/' . $this->user . '/files/');
  1909. /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */
  1910. $storage = $this->getMockBuilder(Temporary::class)
  1911. ->setMethods([$storageOperation])
  1912. ->getMock();
  1913. /** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage2 */
  1914. $storage2 = $this->getMockBuilder(Temporary::class)
  1915. ->setMethods([$storageOperation, 'getMetaData', 'filemtime'])
  1916. ->getMock();
  1917. $storage2->expects($this->any())
  1918. ->method('getMetaData')
  1919. ->will($this->returnValue([
  1920. 'mtime' => 1885434487,
  1921. 'etag' => '',
  1922. 'mimetype' => 'text/plain',
  1923. 'permissions' => Constants::PERMISSION_ALL,
  1924. 'size' => 3
  1925. ]));
  1926. $storage2->expects($this->any())
  1927. ->method('filemtime')
  1928. ->willReturn(123456789);
  1929. $sourcePath = 'original.txt';
  1930. $targetPath = 'substorage/target.txt';
  1931. Filesystem::mount($storage, [], $this->user . '/');
  1932. Filesystem::mount($storage2, [], $this->user . '/files/substorage');
  1933. $storage->mkdir('files');
  1934. $view->file_put_contents($sourcePath, 'meh');
  1935. $storage->expects($this->never())
  1936. ->method($storageOperation);
  1937. $storage2->expects($this->once())
  1938. ->method($storageOperation)
  1939. ->willReturnCallback(
  1940. function () use ($view, $sourcePath, $targetPath, &$lockTypeSourceDuring, &$lockTypeTargetDuring) {
  1941. $lockTypeSourceDuring = $this->getFileLockType($view, $sourcePath);
  1942. $lockTypeTargetDuring = $this->getFileLockType($view, $targetPath);
  1943. return true;
  1944. }
  1945. );
  1946. $this->connectMockHooks($viewOperation, $view, $sourcePath, $lockTypeSourcePre, $lockTypeSourcePost);
  1947. $this->connectMockHooks($viewOperation, $view, $targetPath, $lockTypeTargetPre, $lockTypeTargetPost);
  1948. $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked before operation');
  1949. $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked before operation');
  1950. $view->$viewOperation($sourcePath, $targetPath);
  1951. $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePre, 'Source file locked properly during pre-hook');
  1952. $this->assertEquals($expectedLockTypeSourceDuring, $lockTypeSourceDuring, 'Source file locked properly during operation');
  1953. $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePost, 'Source file locked properly during post-hook');
  1954. $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPre, 'Target file locked properly during pre-hook');
  1955. $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeTargetDuring, 'Target file locked properly during operation');
  1956. $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPost, 'Target file locked properly during post-hook');
  1957. $this->assertNull($this->getFileLockType($view, $sourcePath), 'Source file not locked after operation');
  1958. $this->assertNull($this->getFileLockType($view, $targetPath), 'Target file not locked after operation');
  1959. }
  1960. /**
  1961. * Test locks when moving a mount point
  1962. */
  1963. public function testLockMoveMountPoint() {
  1964. self::loginAsUser('test');
  1965. [$mount] = $this->createTestMovableMountPoints([
  1966. $this->user . '/files/substorage',
  1967. ]);
  1968. $view = new View('/' . $this->user . '/files/');
  1969. $view->mkdir('subdir');
  1970. $sourcePath = 'substorage';
  1971. $targetPath = 'subdir/substorage_moved';
  1972. $mount->expects($this->once())
  1973. ->method('moveMount')
  1974. ->willReturnCallback(
  1975. function ($target) use ($mount, $view, $sourcePath, $targetPath, &$lockTypeSourceDuring, &$lockTypeTargetDuring, &$lockTypeSharedRootDuring) {
  1976. $lockTypeSourceDuring = $this->getFileLockType($view, $sourcePath, true);
  1977. $lockTypeTargetDuring = $this->getFileLockType($view, $targetPath, true);
  1978. $lockTypeSharedRootDuring = $this->getFileLockType($view, $sourcePath, false);
  1979. $mount->setMountPoint($target);
  1980. return true;
  1981. }
  1982. );
  1983. $this->connectMockHooks('rename', $view, $sourcePath, $lockTypeSourcePre, $lockTypeSourcePost, true);
  1984. $this->connectMockHooks('rename', $view, $targetPath, $lockTypeTargetPre, $lockTypeTargetPost, true);
  1985. // in pre-hook, mount point is still on $sourcePath
  1986. $this->connectMockHooks('rename', $view, $sourcePath, $lockTypeSharedRootPre, $dummy, false);
  1987. // in post-hook, mount point is now on $targetPath
  1988. $this->connectMockHooks('rename', $view, $targetPath, $dummy, $lockTypeSharedRootPost, false);
  1989. $this->assertNull($this->getFileLockType($view, $sourcePath, false), 'Shared storage root not locked before operation');
  1990. $this->assertNull($this->getFileLockType($view, $sourcePath, true), 'Source path not locked before operation');
  1991. $this->assertNull($this->getFileLockType($view, $targetPath, true), 'Target path not locked before operation');
  1992. $view->rename($sourcePath, $targetPath);
  1993. $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePre, 'Source path locked properly during pre-hook');
  1994. $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeSourceDuring, 'Source path locked properly during operation');
  1995. $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeSourcePost, 'Source path locked properly during post-hook');
  1996. $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPre, 'Target path locked properly during pre-hook');
  1997. $this->assertEquals(ILockingProvider::LOCK_EXCLUSIVE, $lockTypeTargetDuring, 'Target path locked properly during operation');
  1998. $this->assertEquals(ILockingProvider::LOCK_SHARED, $lockTypeTargetPost, 'Target path locked properly during post-hook');
  1999. $this->assertNull($lockTypeSharedRootPre, 'Shared storage root not locked during pre-hook');
  2000. $this->assertNull($lockTypeSharedRootDuring, 'Shared storage root not locked during move');
  2001. $this->assertNull($lockTypeSharedRootPost, 'Shared storage root not locked during post-hook');
  2002. $this->assertNull($this->getFileLockType($view, $sourcePath, false), 'Shared storage root not locked after operation');
  2003. $this->assertNull($this->getFileLockType($view, $sourcePath, true), 'Source path not locked after operation');
  2004. $this->assertNull($this->getFileLockType($view, $targetPath, true), 'Target path not locked after operation');
  2005. }
  2006. /**
  2007. * Connect hook callbacks for hook type
  2008. *
  2009. * @param string $hookType hook type or null for none
  2010. * @param View $view view to check the lock on
  2011. * @param string $path path for which to check the lock
  2012. * @param int $lockTypePre variable to receive lock type that was active in the pre-hook
  2013. * @param int $lockTypePost variable to receive lock type that was active in the post-hook
  2014. * @param bool $onMountPoint true to check the mount point instead of the
  2015. * mounted storage
  2016. */
  2017. private function connectMockHooks($hookType, $view, $path, &$lockTypePre, &$lockTypePost, $onMountPoint = false) {
  2018. if ($hookType === null) {
  2019. return;
  2020. }
  2021. $eventHandler = $this->getMockBuilder(\stdclass::class)
  2022. ->setMethods(['preCallback', 'postCallback'])
  2023. ->getMock();
  2024. $eventHandler->expects($this->any())
  2025. ->method('preCallback')
  2026. ->willReturnCallback(
  2027. function () use ($view, $path, $onMountPoint, &$lockTypePre) {
  2028. $lockTypePre = $this->getFileLockType($view, $path, $onMountPoint);
  2029. }
  2030. );
  2031. $eventHandler->expects($this->any())
  2032. ->method('postCallback')
  2033. ->willReturnCallback(
  2034. function () use ($view, $path, $onMountPoint, &$lockTypePost) {
  2035. $lockTypePost = $this->getFileLockType($view, $path, $onMountPoint);
  2036. }
  2037. );
  2038. if ($hookType !== null) {
  2039. Util::connectHook(
  2040. Filesystem::CLASSNAME,
  2041. $hookType,
  2042. $eventHandler,
  2043. 'preCallback'
  2044. );
  2045. Util::connectHook(
  2046. Filesystem::CLASSNAME,
  2047. 'post_' . $hookType,
  2048. $eventHandler,
  2049. 'postCallback'
  2050. );
  2051. }
  2052. }
  2053. /**
  2054. * Returns the file lock type
  2055. *
  2056. * @param View $view view
  2057. * @param string $path path
  2058. * @param bool $onMountPoint true to check the mount point instead of the
  2059. * mounted storage
  2060. *
  2061. * @return int lock type or null if file was not locked
  2062. */
  2063. private function getFileLockType(View $view, $path, $onMountPoint = false) {
  2064. if ($this->isFileLocked($view, $path, ILockingProvider::LOCK_EXCLUSIVE, $onMountPoint)) {
  2065. return ILockingProvider::LOCK_EXCLUSIVE;
  2066. } elseif ($this->isFileLocked($view, $path, ILockingProvider::LOCK_SHARED, $onMountPoint)) {
  2067. return ILockingProvider::LOCK_SHARED;
  2068. }
  2069. return null;
  2070. }
  2071. public function testRemoveMoveableMountPoint() {
  2072. $mountPoint = '/' . $this->user . '/files/mount/';
  2073. // Mock the mount point
  2074. /** @var TestMoveableMountPoint|\PHPUnit\Framework\MockObject\MockObject $mount */
  2075. $mount = $this->createMock(TestMoveableMountPoint::class);
  2076. $mount->expects($this->once())
  2077. ->method('getMountPoint')
  2078. ->willReturn($mountPoint);
  2079. $mount->expects($this->once())
  2080. ->method('removeMount')
  2081. ->willReturn('foo');
  2082. $mount->expects($this->any())
  2083. ->method('getInternalPath')
  2084. ->willReturn('');
  2085. // Register mount
  2086. Filesystem::getMountManager()->addMount($mount);
  2087. // Listen for events
  2088. $eventHandler = $this->getMockBuilder(\stdclass::class)
  2089. ->setMethods(['umount', 'post_umount'])
  2090. ->getMock();
  2091. $eventHandler->expects($this->once())
  2092. ->method('umount')
  2093. ->with([Filesystem::signal_param_path => '/mount']);
  2094. $eventHandler->expects($this->once())
  2095. ->method('post_umount')
  2096. ->with([Filesystem::signal_param_path => '/mount']);
  2097. Util::connectHook(
  2098. Filesystem::CLASSNAME,
  2099. 'umount',
  2100. $eventHandler,
  2101. 'umount'
  2102. );
  2103. Util::connectHook(
  2104. Filesystem::CLASSNAME,
  2105. 'post_umount',
  2106. $eventHandler,
  2107. 'post_umount'
  2108. );
  2109. //Delete the mountpoint
  2110. $view = new View('/' . $this->user . '/files');
  2111. $this->assertEquals('foo', $view->rmdir('mount'));
  2112. }
  2113. public function mimeFilterProvider() {
  2114. return [
  2115. [null, ['test1.txt', 'test2.txt', 'test3.md', 'test4.png']],
  2116. ['text/plain', ['test1.txt', 'test2.txt']],
  2117. ['text/markdown', ['test3.md']],
  2118. ['text', ['test1.txt', 'test2.txt', 'test3.md']],
  2119. ];
  2120. }
  2121. /**
  2122. * @param string $filter
  2123. * @param string[] $expected
  2124. * @dataProvider mimeFilterProvider
  2125. */
  2126. public function testGetDirectoryContentMimeFilter($filter, $expected) {
  2127. $storage1 = new Temporary();
  2128. $root = self::getUniqueID('/');
  2129. Filesystem::mount($storage1, [], $root . '/');
  2130. $view = new View($root);
  2131. $view->file_put_contents('test1.txt', 'asd');
  2132. $view->file_put_contents('test2.txt', 'asd');
  2133. $view->file_put_contents('test3.md', 'asd');
  2134. $view->file_put_contents('test4.png', '');
  2135. $content = $view->getDirectoryContent('', $filter);
  2136. $files = array_map(function (FileInfo $info) {
  2137. return $info->getName();
  2138. }, $content);
  2139. sort($files);
  2140. $this->assertEquals($expected, $files);
  2141. }
  2142. public function testFilePutContentsClearsChecksum() {
  2143. $storage = new Temporary([]);
  2144. $scanner = $storage->getScanner();
  2145. $storage->file_put_contents('foo.txt', 'bar');
  2146. Filesystem::mount($storage, [], '/test/');
  2147. $scanner->scan('');
  2148. $view = new View('/test/foo.txt');
  2149. $view->putFileInfo('.', ['checksum' => '42']);
  2150. $this->assertEquals('bar', $view->file_get_contents(''));
  2151. $fh = tmpfile();
  2152. fwrite($fh, 'fooo');
  2153. rewind($fh);
  2154. clearstatcache();
  2155. $view->file_put_contents('', $fh);
  2156. $this->assertEquals('fooo', $view->file_get_contents(''));
  2157. $data = $view->getFileInfo('.');
  2158. $this->assertEquals('', $data->getChecksum());
  2159. }
  2160. public function testDeleteGhostFile() {
  2161. $storage = new Temporary([]);
  2162. $scanner = $storage->getScanner();
  2163. $cache = $storage->getCache();
  2164. $storage->file_put_contents('foo.txt', 'bar');
  2165. Filesystem::mount($storage, [], '/test/');
  2166. $scanner->scan('');
  2167. $storage->unlink('foo.txt');
  2168. $this->assertTrue($cache->inCache('foo.txt'));
  2169. $view = new View('/test');
  2170. $rootInfo = $view->getFileInfo('');
  2171. $this->assertEquals(3, $rootInfo->getSize());
  2172. $view->unlink('foo.txt');
  2173. $newInfo = $view->getFileInfo('');
  2174. $this->assertFalse($cache->inCache('foo.txt'));
  2175. $this->assertNotEquals($rootInfo->getEtag(), $newInfo->getEtag());
  2176. $this->assertEquals(0, $newInfo->getSize());
  2177. }
  2178. public function testDeleteGhostFolder() {
  2179. $storage = new Temporary([]);
  2180. $scanner = $storage->getScanner();
  2181. $cache = $storage->getCache();
  2182. $storage->mkdir('foo');
  2183. $storage->file_put_contents('foo/foo.txt', 'bar');
  2184. Filesystem::mount($storage, [], '/test/');
  2185. $scanner->scan('');
  2186. $storage->rmdir('foo');
  2187. $this->assertTrue($cache->inCache('foo'));
  2188. $this->assertTrue($cache->inCache('foo/foo.txt'));
  2189. $view = new View('/test');
  2190. $rootInfo = $view->getFileInfo('');
  2191. $this->assertEquals(3, $rootInfo->getSize());
  2192. $view->rmdir('foo');
  2193. $newInfo = $view->getFileInfo('');
  2194. $this->assertFalse($cache->inCache('foo'));
  2195. $this->assertFalse($cache->inCache('foo/foo.txt'));
  2196. $this->assertNotEquals($rootInfo->getEtag(), $newInfo->getEtag());
  2197. $this->assertEquals(0, $newInfo->getSize());
  2198. }
  2199. public function testCreateParentDirectories() {
  2200. $view = $this->getMockBuilder(View::class)
  2201. ->disableOriginalConstructor()
  2202. ->setMethods([
  2203. 'is_file',
  2204. 'file_exists',
  2205. 'mkdir',
  2206. ])
  2207. ->getMock();
  2208. $view->expects($this->exactly(3))
  2209. ->method('is_file')
  2210. ->withConsecutive(
  2211. ['/new'],
  2212. ['/new/folder'],
  2213. ['/new/folder/structure'],
  2214. )
  2215. ->willReturn(false);
  2216. $view->expects($this->exactly(3))
  2217. ->method('file_exists')
  2218. ->withConsecutive(
  2219. ['/new'],
  2220. ['/new/folder'],
  2221. ['/new/folder/structure'],
  2222. )->willReturnOnConsecutiveCalls(
  2223. true,
  2224. false,
  2225. false,
  2226. );
  2227. $view->expects($this->exactly(2))
  2228. ->method('mkdir')
  2229. ->withConsecutive(
  2230. ['/new/folder'],
  2231. ['/new/folder/structure'],
  2232. );
  2233. $this->assertTrue(self::invokePrivate($view, 'createParentDirectories', ['/new/folder/structure']));
  2234. }
  2235. public function testCreateParentDirectoriesWithExistingFile() {
  2236. $view = $this->getMockBuilder(View::class)
  2237. ->disableOriginalConstructor()
  2238. ->setMethods([
  2239. 'is_file',
  2240. 'file_exists',
  2241. 'mkdir',
  2242. ])
  2243. ->getMock();
  2244. $view
  2245. ->expects($this->once())
  2246. ->method('is_file')
  2247. ->with('/file.txt')
  2248. ->willReturn(true);
  2249. $this->assertFalse(self::invokePrivate($view, 'createParentDirectories', ['/file.txt/folder/structure']));
  2250. }
  2251. public function testCacheExtension() {
  2252. $storage = new Temporary([]);
  2253. $scanner = $storage->getScanner();
  2254. $storage->file_put_contents('foo.txt', 'bar');
  2255. $scanner->scan('');
  2256. Filesystem::mount($storage, [], '/test/');
  2257. $view = new View('/test');
  2258. $info = $view->getFileInfo('/foo.txt');
  2259. $this->assertEquals(0, $info->getUploadTime());
  2260. $this->assertEquals(0, $info->getCreationTime());
  2261. $view->putFileInfo('/foo.txt', ['upload_time' => 25]);
  2262. $info = $view->getFileInfo('/foo.txt');
  2263. $this->assertEquals(25, $info->getUploadTime());
  2264. $this->assertEquals(0, $info->getCreationTime());
  2265. }
  2266. public function testFopenGone() {
  2267. $storage = new Temporary([]);
  2268. $scanner = $storage->getScanner();
  2269. $storage->file_put_contents('foo.txt', 'bar');
  2270. $scanner->scan('');
  2271. $cache = $storage->getCache();
  2272. Filesystem::mount($storage, [], '/test/');
  2273. $view = new View('/test');
  2274. $storage->unlink('foo.txt');
  2275. $this->assertTrue($cache->inCache('foo.txt'));
  2276. $this->assertFalse($view->fopen('foo.txt', 'r'));
  2277. $this->assertFalse($cache->inCache('foo.txt'));
  2278. }
  2279. }