1
0

ViewTest.php 89 KB

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