UserMountCacheTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Files\Config;
  8. use OC\DB\QueryBuilder\Literal;
  9. use OC\Files\Mount\MountPoint;
  10. use OC\Files\Storage\Storage;
  11. use OC\User\Manager;
  12. use OCP\Cache\CappedMemoryCache;
  13. use OCP\Diagnostics\IEventLogger;
  14. use OCP\EventDispatcher\IEventDispatcher;
  15. use OCP\Files\Config\ICachedMountInfo;
  16. use OCP\ICacheFactory;
  17. use OCP\IConfig;
  18. use OCP\IDBConnection;
  19. use OCP\IUserManager;
  20. use Psr\Log\LoggerInterface;
  21. use Test\TestCase;
  22. use Test\Util\User\Dummy;
  23. /**
  24. * @group DB
  25. */
  26. class UserMountCacheTest extends TestCase {
  27. /**
  28. * @var IDBConnection
  29. */
  30. private $connection;
  31. /**
  32. * @var IUserManager
  33. */
  34. private $userManager;
  35. /**
  36. * @var \OC\Files\Config\UserMountCache
  37. */
  38. private $cache;
  39. private $fileIds = [];
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $this->fileIds = [];
  43. $this->connection = \OC::$server->getDatabaseConnection();
  44. $config = $this->getMockBuilder(IConfig::class)
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $config
  48. ->expects($this->any())
  49. ->method('getUserValue')
  50. ->willReturnArgument(3);
  51. $config
  52. ->expects($this->any())
  53. ->method('getAppValue')
  54. ->willReturnArgument(2);
  55. $this->userManager = new Manager($config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class));
  56. $userBackend = new Dummy();
  57. $userBackend->createUser('u1', '');
  58. $userBackend->createUser('u2', '');
  59. $userBackend->createUser('u3', '');
  60. $this->userManager->registerBackend($userBackend);
  61. $this->cache = new \OC\Files\Config\UserMountCache($this->connection, $this->userManager, $this->createMock(LoggerInterface::class), $this->createMock(IEventLogger::class));
  62. }
  63. protected function tearDown(): void {
  64. $builder = $this->connection->getQueryBuilder();
  65. $builder->delete('mounts')->execute();
  66. $builder = $this->connection->getQueryBuilder();
  67. foreach ($this->fileIds as $fileId) {
  68. $builder->delete('filecache')
  69. ->where($builder->expr()->eq('fileid', new Literal($fileId)))
  70. ->execute();
  71. }
  72. }
  73. private function getStorage($storageId, $rootInternalPath = '') {
  74. $rootId = $this->createCacheEntry($rootInternalPath, $storageId);
  75. $storageCache = $this->getMockBuilder('\OC\Files\Cache\Storage')
  76. ->disableOriginalConstructor()
  77. ->getMock();
  78. $storageCache->expects($this->any())
  79. ->method('getNumericId')
  80. ->willReturn($storageId);
  81. $cache = $this->getMockBuilder('\OC\Files\Cache\Cache')
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. $cache->expects($this->any())
  85. ->method('getId')
  86. ->willReturn($rootId);
  87. $storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
  88. ->disableOriginalConstructor()
  89. ->getMock();
  90. $storage->expects($this->any())
  91. ->method('getStorageCache')
  92. ->willReturn($storageCache);
  93. $storage->expects($this->any())
  94. ->method('getCache')
  95. ->willReturn($cache);
  96. return [$storage, $rootId];
  97. }
  98. private function clearCache() {
  99. $this->invokePrivate($this->cache, 'mountsForUsers', [new CappedMemoryCache()]);
  100. }
  101. private function keyForMount(MountPoint $mount): string {
  102. return $mount->getStorageRootId().'::'.$mount->getMountPoint();
  103. }
  104. public function testNewMounts() {
  105. $user = $this->userManager->get('u1');
  106. [$storage] = $this->getStorage(10);
  107. $mount = new MountPoint($storage, '/asd/');
  108. $this->cache->registerMounts($user, [$mount]);
  109. $this->clearCache();
  110. $cachedMounts = $this->cache->getMountsForUser($user);
  111. $this->assertCount(1, $cachedMounts);
  112. $cachedMount = $cachedMounts[$this->keyForMount($mount)];
  113. $this->assertEquals('/asd/', $cachedMount->getMountPoint());
  114. $this->assertEquals($user->getUID(), $cachedMount->getUser()->getUID());
  115. $this->assertEquals($storage->getCache()->getId(''), $cachedMount->getRootId());
  116. $this->assertEquals($storage->getStorageCache()->getNumericId(), $cachedMount->getStorageId());
  117. }
  118. public function testSameMounts() {
  119. $user = $this->userManager->get('u1');
  120. [$storage] = $this->getStorage(10);
  121. $mount = new MountPoint($storage, '/asd/');
  122. $this->cache->registerMounts($user, [$mount]);
  123. $this->clearCache();
  124. $this->cache->registerMounts($user, [$mount]);
  125. $this->clearCache();
  126. $cachedMounts = $this->cache->getMountsForUser($user);
  127. $this->assertCount(1, $cachedMounts);
  128. $cachedMount = $cachedMounts[$this->keyForMount($mount)];
  129. $this->assertEquals('/asd/', $cachedMount->getMountPoint());
  130. $this->assertEquals($user->getUID(), $cachedMount->getUser()->getUID());
  131. $this->assertEquals($storage->getCache()->getId(''), $cachedMount->getRootId());
  132. $this->assertEquals($storage->getStorageCache()->getNumericId(), $cachedMount->getStorageId());
  133. }
  134. public function testRemoveMounts() {
  135. $user = $this->userManager->get('u1');
  136. [$storage] = $this->getStorage(10);
  137. $mount = new MountPoint($storage, '/asd/');
  138. $this->cache->registerMounts($user, [$mount]);
  139. $this->clearCache();
  140. $this->cache->registerMounts($user, []);
  141. $this->clearCache();
  142. $cachedMounts = $this->cache->getMountsForUser($user);
  143. $this->assertCount(0, $cachedMounts);
  144. }
  145. public function testChangeMounts() {
  146. $user = $this->userManager->get('u1');
  147. [$storage] = $this->getStorage(10);
  148. $mount = new MountPoint($storage, '/bar/');
  149. $this->cache->registerMounts($user, [$mount]);
  150. $this->clearCache();
  151. $mount = new MountPoint($storage, '/foo/');
  152. $this->cache->registerMounts($user, [$mount]);
  153. $this->clearCache();
  154. $cachedMounts = $this->cache->getMountsForUser($user);
  155. $this->assertCount(1, $cachedMounts);
  156. $cachedMount = $cachedMounts[$this->keyForMount($mount)];
  157. $this->assertEquals('/foo/', $cachedMount->getMountPoint());
  158. }
  159. public function testChangeMountId() {
  160. $user = $this->userManager->get('u1');
  161. [$storage] = $this->getStorage(10);
  162. $mount = new MountPoint($storage, '/foo/', null, null, null, null);
  163. $this->cache->registerMounts($user, [$mount]);
  164. $this->clearCache();
  165. $mount = new MountPoint($storage, '/foo/', null, null, null, 1);
  166. $this->cache->registerMounts($user, [$mount]);
  167. $this->clearCache();
  168. $cachedMounts = $this->cache->getMountsForUser($user);
  169. $this->assertCount(1, $cachedMounts);
  170. $cachedMount = $cachedMounts[$this->keyForMount($mount)];
  171. $this->assertEquals(1, $cachedMount->getMountId());
  172. }
  173. public function testGetMountsForUser() {
  174. $user1 = $this->userManager->get('u1');
  175. $user2 = $this->userManager->get('u2');
  176. $user3 = $this->userManager->get('u3');
  177. [$storage1, $id1] = $this->getStorage(1);
  178. [$storage2, $id2] = $this->getStorage(2, 'foo/bar');
  179. $mount1 = new MountPoint($storage1, '/foo/');
  180. $mount2 = new MountPoint($storage2, '/bar/');
  181. $this->cache->registerMounts($user1, [$mount1, $mount2]);
  182. $this->cache->registerMounts($user2, [$mount2]);
  183. $this->cache->registerMounts($user3, [$mount2]);
  184. $this->clearCache();
  185. $user3->delete();
  186. $cachedMounts = $this->cache->getMountsForUser($user1);
  187. $this->assertCount(2, $cachedMounts);
  188. $this->assertEquals('/foo/', $cachedMounts[$this->keyForMount($mount1)]->getMountPoint());
  189. $this->assertEquals($user1->getUID(), $cachedMounts[$this->keyForMount($mount1)]->getUser()->getUID());
  190. $this->assertEquals($id1, $cachedMounts[$this->keyForMount($mount1)]->getRootId());
  191. $this->assertEquals(1, $cachedMounts[$this->keyForMount($mount1)]->getStorageId());
  192. $this->assertEquals('', $cachedMounts[$this->keyForMount($mount1)]->getRootInternalPath());
  193. $this->assertEquals('/bar/', $cachedMounts[$this->keyForMount($mount2)]->getMountPoint());
  194. $this->assertEquals($user1->getUID(), $cachedMounts[$this->keyForMount($mount2)]->getUser()->getUID());
  195. $this->assertEquals($id2, $cachedMounts[$this->keyForMount($mount2)]->getRootId());
  196. $this->assertEquals(2, $cachedMounts[$this->keyForMount($mount2)]->getStorageId());
  197. $this->assertEquals('foo/bar', $cachedMounts[$this->keyForMount($mount2)]->getRootInternalPath());
  198. $cachedMounts = $this->cache->getMountsForUser($user3);
  199. $this->assertEmpty($cachedMounts);
  200. }
  201. public function testGetMountsByStorageId() {
  202. $user1 = $this->userManager->get('u1');
  203. $user2 = $this->userManager->get('u2');
  204. [$storage1, $id1] = $this->getStorage(1);
  205. [$storage2, $id2] = $this->getStorage(2);
  206. $mount1 = new MountPoint($storage1, '/foo/');
  207. $mount2 = new MountPoint($storage2, '/bar/');
  208. $this->cache->registerMounts($user1, [$mount1, $mount2]);
  209. $this->cache->registerMounts($user2, [$mount2]);
  210. $this->clearCache();
  211. $cachedMounts = $this->cache->getMountsForStorageId(2);
  212. $this->sortMounts($cachedMounts);
  213. $this->assertCount(2, $cachedMounts);
  214. $this->assertEquals('/bar/', $cachedMounts[0]->getMountPoint());
  215. $this->assertEquals($user1->getUID(), $cachedMounts[0]->getUser()->getUID());
  216. $this->assertEquals($id2, $cachedMounts[0]->getRootId());
  217. $this->assertEquals(2, $cachedMounts[0]->getStorageId());
  218. $this->assertEquals('/bar/', $cachedMounts[1]->getMountPoint());
  219. $this->assertEquals($user2->getUID(), $cachedMounts[1]->getUser()->getUID());
  220. $this->assertEquals($id2, $cachedMounts[1]->getRootId());
  221. $this->assertEquals(2, $cachedMounts[1]->getStorageId());
  222. }
  223. public function testGetMountsByRootId() {
  224. $user1 = $this->userManager->get('u1');
  225. $user2 = $this->userManager->get('u2');
  226. [$storage1, $id1] = $this->getStorage(1);
  227. [$storage2, $id2] = $this->getStorage(2);
  228. $mount1 = new MountPoint($storage1, '/foo/');
  229. $mount2 = new MountPoint($storage2, '/bar/');
  230. $this->cache->registerMounts($user1, [$mount1, $mount2]);
  231. $this->cache->registerMounts($user2, [$mount2]);
  232. $this->clearCache();
  233. $cachedMounts = $this->cache->getMountsForRootId($id2);
  234. $this->sortMounts($cachedMounts);
  235. $this->assertCount(2, $cachedMounts);
  236. $this->assertEquals('/bar/', $cachedMounts[0]->getMountPoint());
  237. $this->assertEquals($user1->getUID(), $cachedMounts[0]->getUser()->getUID());
  238. $this->assertEquals($id2, $cachedMounts[0]->getRootId());
  239. $this->assertEquals(2, $cachedMounts[0]->getStorageId());
  240. $this->assertEquals('/bar/', $cachedMounts[1]->getMountPoint());
  241. $this->assertEquals($user2->getUID(), $cachedMounts[1]->getUser()->getUID());
  242. $this->assertEquals($id2, $cachedMounts[1]->getRootId());
  243. $this->assertEquals(2, $cachedMounts[1]->getStorageId());
  244. }
  245. private function sortMounts(&$mounts) {
  246. usort($mounts, function (ICachedMountInfo $a, ICachedMountInfo $b) {
  247. return strcmp($a->getUser()->getUID(), $b->getUser()->getUID());
  248. });
  249. }
  250. private function createCacheEntry($internalPath, $storageId, $size = 0) {
  251. $internalPath = trim($internalPath, '/');
  252. $inserted = $this->connection->insertIfNotExist('*PREFIX*filecache', [
  253. 'storage' => $storageId,
  254. 'path' => $internalPath,
  255. 'path_hash' => md5($internalPath),
  256. 'parent' => -1,
  257. 'name' => basename($internalPath),
  258. 'mimetype' => 0,
  259. 'mimepart' => 0,
  260. 'size' => $size,
  261. 'storage_mtime' => 0,
  262. 'encrypted' => 0,
  263. 'unencrypted_size' => 0,
  264. 'etag' => '',
  265. 'permissions' => 31
  266. ], ['storage', 'path_hash']);
  267. if ($inserted) {
  268. $id = (int)$this->connection->lastInsertId('*PREFIX*filecache');
  269. $this->fileIds[] = $id;
  270. } else {
  271. $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` =?';
  272. $query = $this->connection->prepare($sql);
  273. $query->execute([$storageId, md5($internalPath)]);
  274. return (int)$query->fetchOne();
  275. }
  276. return $id;
  277. }
  278. public function testGetMountsForFileIdRootId() {
  279. $user1 = $this->userManager->get('u1');
  280. [$storage1, $rootId] = $this->getStorage(2);
  281. $mount1 = new MountPoint($storage1, '/foo/');
  282. $this->cache->registerMounts($user1, [$mount1]);
  283. $this->clearCache();
  284. $cachedMounts = $this->cache->getMountsForFileId($rootId);
  285. $this->assertCount(1, $cachedMounts);
  286. $this->assertEquals('/foo/', $cachedMounts[0]->getMountPoint());
  287. $this->assertEquals($user1->getUID(), $cachedMounts[0]->getUser()->getUID());
  288. $this->assertEquals($rootId, $cachedMounts[0]->getRootId());
  289. $this->assertEquals(2, $cachedMounts[0]->getStorageId());
  290. }
  291. public function testGetMountsForFileIdSubFolder() {
  292. $user1 = $this->userManager->get('u1');
  293. $fileId = $this->createCacheEntry('/foo/bar', 2);
  294. [$storage1, $rootId] = $this->getStorage(2);
  295. $mount1 = new MountPoint($storage1, '/foo/');
  296. $this->cache->registerMounts($user1, [$mount1]);
  297. $this->clearCache();
  298. $cachedMounts = $this->cache->getMountsForFileId($fileId);
  299. $this->assertCount(1, $cachedMounts);
  300. $this->assertEquals('/foo/', $cachedMounts[0]->getMountPoint());
  301. $this->assertEquals($user1->getUID(), $cachedMounts[0]->getUser()->getUID());
  302. $this->assertEquals($rootId, $cachedMounts[0]->getRootId());
  303. $this->assertEquals(2, $cachedMounts[0]->getStorageId());
  304. $this->assertEquals('foo/bar', $cachedMounts[0]->getInternalPath());
  305. $this->assertEquals('/foo/foo/bar', $cachedMounts[0]->getPath());
  306. }
  307. public function testGetMountsForFileIdSubFolderMount() {
  308. $user1 = $this->userManager->get('u1');
  309. [$storage1, $rootId] = $this->getStorage(2);
  310. $folderId = $this->createCacheEntry('/foo', 2);
  311. $fileId = $this->createCacheEntry('/foo/bar', 2);
  312. $mount1 = $this->getMockBuilder('\OC\Files\Mount\MountPoint')
  313. ->setConstructorArgs([$storage1, '/'])
  314. ->setMethods(['getStorageRootId'])
  315. ->getMock();
  316. $mount1->expects($this->any())
  317. ->method('getStorageRootId')
  318. ->willReturn($folderId);
  319. $this->cache->registerMounts($user1, [$mount1]);
  320. $this->clearCache();
  321. $cachedMounts = $this->cache->getMountsForFileId($fileId);
  322. $this->assertCount(1, $cachedMounts);
  323. $this->assertEquals('/', $cachedMounts[0]->getMountPoint());
  324. $this->assertEquals($user1->getUID(), $cachedMounts[0]->getUser()->getUID());
  325. $this->assertEquals($folderId, $cachedMounts[0]->getRootId());
  326. $this->assertEquals(2, $cachedMounts[0]->getStorageId());
  327. $this->assertEquals('foo', $cachedMounts[0]->getRootInternalPath());
  328. $this->assertEquals('bar', $cachedMounts[0]->getInternalPath());
  329. $this->assertEquals('/bar', $cachedMounts[0]->getPath());
  330. }
  331. public function testGetMountsForFileIdSubFolderMountOutside() {
  332. $user1 = $this->userManager->get('u1');
  333. [$storage1, $rootId] = $this->getStorage(2);
  334. $folderId = $this->createCacheEntry('/foo', 2);
  335. $fileId = $this->createCacheEntry('/bar/asd', 2);
  336. $mount1 = $this->getMockBuilder('\OC\Files\Mount\MountPoint')
  337. ->setConstructorArgs([$storage1, '/foo/'])
  338. ->setMethods(['getStorageRootId'])
  339. ->getMock();
  340. $mount1->expects($this->any())
  341. ->method('getStorageRootId')
  342. ->willReturn($folderId);
  343. $this->cache->registerMounts($user1, [$mount1]);
  344. $this->cache->registerMounts($user1, [$mount1]);
  345. $this->clearCache();
  346. $cachedMounts = $this->cache->getMountsForFileId($fileId);
  347. $this->assertCount(0, $cachedMounts);
  348. }
  349. public function testGetMountsForFileIdDeletedUser() {
  350. $user1 = $this->userManager->get('u1');
  351. [$storage1, $rootId] = $this->getStorage(2);
  352. $rootId = $this->createCacheEntry('', 2);
  353. $mount1 = new MountPoint($storage1, '/foo/');
  354. $this->cache->registerMounts($user1, [$mount1]);
  355. $user1->delete();
  356. $this->clearCache();
  357. $cachedMounts = $this->cache->getMountsForFileId($rootId);
  358. $this->assertEmpty($cachedMounts);
  359. }
  360. public function testGetUsedSpaceForUsers() {
  361. $user1 = $this->userManager->get('u1');
  362. $user2 = $this->userManager->get('u2');
  363. /** @var Storage $storage1 */
  364. [$storage1, $rootId] = $this->getStorage(2);
  365. $folderId = $this->createCacheEntry('files', 2, 100);
  366. $fileId = $this->createCacheEntry('files/foo', 2, 7);
  367. $storage1->getCache()->put($folderId, ['size' => 100]);
  368. $storage1->getCache()->update($fileId, ['size' => 70]);
  369. $mount1 = $this->getMockBuilder(MountPoint::class)
  370. ->setConstructorArgs([$storage1, '/u1/'])
  371. ->setMethods(['getStorageRootId', 'getNumericStorageId'])
  372. ->getMock();
  373. $mount1->expects($this->any())
  374. ->method('getStorageRootId')
  375. ->willReturn($rootId);
  376. $mount1->expects($this->any())
  377. ->method('getNumericStorageId')
  378. ->willReturn(2);
  379. $this->cache->registerMounts($user1, [$mount1]);
  380. $result = $this->cache->getUsedSpaceForUsers([$user1, $user2]);
  381. $this->assertEquals(['u1' => 100], $result);
  382. }
  383. public function testMigrateMountProvider() {
  384. $user1 = $this->userManager->get('u1');
  385. [$storage1, $rootId] = $this->getStorage(2);
  386. $rootId = $this->createCacheEntry('', 2);
  387. $mount1 = new MountPoint($storage1, '/foo/');
  388. $this->cache->registerMounts($user1, [$mount1]);
  389. $this->clearCache();
  390. $cachedMounts = $this->cache->getMountsForUser($user1);
  391. $this->assertCount(1, $cachedMounts);
  392. $this->assertEquals('', $cachedMounts[$this->keyForMount($mount1)]->getMountProvider());
  393. $mount1 = new MountPoint($storage1, '/foo/', null, null, null, null, 'dummy');
  394. $this->cache->registerMounts($user1, [$mount1], ['dummy']);
  395. $this->clearCache();
  396. $cachedMounts = $this->cache->getMountsForUser($user1);
  397. $this->assertCount(1, $cachedMounts);
  398. $this->assertEquals('dummy', $cachedMounts[$this->keyForMount($mount1)]->getMountProvider());
  399. }
  400. }