UserMountCacheTest.php 18 KB

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