ViewTest.php 87 KB

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