UserMountCacheTest.php 17 KB

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