UserMountCacheTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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), $this->createMock(LoggerInterface::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) {
  75. $rootId = $this->createCacheEntry('', $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);
  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('/bar/', $cachedMounts[$this->keyForMount($mount2)]->getMountPoint());
  194. $this->assertEquals($user1, $cachedMounts[$this->keyForMount($mount2)]->getUser());
  195. $this->assertEquals($id2, $cachedMounts[$this->keyForMount($mount2)]->getRootId());
  196. $this->assertEquals(2, $cachedMounts[$this->keyForMount($mount2)]->getStorageId());
  197. $cachedMounts = $this->cache->getMountsForUser($user3);
  198. $this->assertEmpty($cachedMounts);
  199. }
  200. public function testGetMountsByStorageId() {
  201. $user1 = $this->userManager->get('u1');
  202. $user2 = $this->userManager->get('u2');
  203. [$storage1, $id1] = $this->getStorage(1);
  204. [$storage2, $id2] = $this->getStorage(2);
  205. $mount1 = new MountPoint($storage1, '/foo/');
  206. $mount2 = new MountPoint($storage2, '/bar/');
  207. $this->cache->registerMounts($user1, [$mount1, $mount2]);
  208. $this->cache->registerMounts($user2, [$mount2]);
  209. $this->clearCache();
  210. $cachedMounts = $this->cache->getMountsForStorageId(2);
  211. $this->sortMounts($cachedMounts);
  212. $this->assertCount(2, $cachedMounts);
  213. $this->assertEquals('/bar/', $cachedMounts[0]->getMountPoint());
  214. $this->assertEquals($user1, $cachedMounts[0]->getUser());
  215. $this->assertEquals($id2, $cachedMounts[0]->getRootId());
  216. $this->assertEquals(2, $cachedMounts[0]->getStorageId());
  217. $this->assertEquals('/bar/', $cachedMounts[1]->getMountPoint());
  218. $this->assertEquals($user2, $cachedMounts[1]->getUser());
  219. $this->assertEquals($id2, $cachedMounts[1]->getRootId());
  220. $this->assertEquals(2, $cachedMounts[1]->getStorageId());
  221. }
  222. public function testGetMountsByRootId() {
  223. $user1 = $this->userManager->get('u1');
  224. $user2 = $this->userManager->get('u2');
  225. [$storage1, $id1] = $this->getStorage(1);
  226. [$storage2, $id2] = $this->getStorage(2);
  227. $mount1 = new MountPoint($storage1, '/foo/');
  228. $mount2 = new MountPoint($storage2, '/bar/');
  229. $this->cache->registerMounts($user1, [$mount1, $mount2]);
  230. $this->cache->registerMounts($user2, [$mount2]);
  231. $this->clearCache();
  232. $cachedMounts = $this->cache->getMountsForRootId($id2);
  233. $this->sortMounts($cachedMounts);
  234. $this->assertCount(2, $cachedMounts);
  235. $this->assertEquals('/bar/', $cachedMounts[0]->getMountPoint());
  236. $this->assertEquals($user1, $cachedMounts[0]->getUser());
  237. $this->assertEquals($id2, $cachedMounts[0]->getRootId());
  238. $this->assertEquals(2, $cachedMounts[0]->getStorageId());
  239. $this->assertEquals('/bar/', $cachedMounts[1]->getMountPoint());
  240. $this->assertEquals($user2, $cachedMounts[1]->getUser());
  241. $this->assertEquals($id2, $cachedMounts[1]->getRootId());
  242. $this->assertEquals(2, $cachedMounts[1]->getStorageId());
  243. }
  244. private function sortMounts(&$mounts) {
  245. usort($mounts, function (ICachedMountInfo $a, ICachedMountInfo $b) {
  246. return strcmp($a->getUser()->getUID(), $b->getUser()->getUID());
  247. });
  248. }
  249. private function createCacheEntry($internalPath, $storageId, $size = 0) {
  250. $internalPath = trim($internalPath, '/');
  251. $inserted = $this->connection->insertIfNotExist('*PREFIX*filecache', [
  252. 'storage' => $storageId,
  253. 'path' => $internalPath,
  254. 'path_hash' => md5($internalPath),
  255. 'parent' => -1,
  256. 'name' => basename($internalPath),
  257. 'mimetype' => 0,
  258. 'mimepart' => 0,
  259. 'size' => $size,
  260. 'storage_mtime' => 0,
  261. 'encrypted' => 0,
  262. 'unencrypted_size' => 0,
  263. 'etag' => '',
  264. 'permissions' => 31
  265. ], ['storage', 'path_hash']);
  266. if ($inserted) {
  267. $id = (int)$this->connection->lastInsertId('*PREFIX*filecache');
  268. $this->fileIds[] = $id;
  269. } else {
  270. $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` =?';
  271. $query = $this->connection->prepare($sql);
  272. $query->execute([$storageId, md5($internalPath)]);
  273. return (int)$query->fetchOne();
  274. }
  275. return $id;
  276. }
  277. public function testGetMountsForFileIdRootId() {
  278. $user1 = $this->userManager->get('u1');
  279. [$storage1, $rootId] = $this->getStorage(2);
  280. $mount1 = new MountPoint($storage1, '/foo/');
  281. $this->cache->registerMounts($user1, [$mount1]);
  282. $this->clearCache();
  283. $cachedMounts = $this->cache->getMountsForFileId($rootId);
  284. $this->assertCount(1, $cachedMounts);
  285. $this->assertEquals('/foo/', $cachedMounts[0]->getMountPoint());
  286. $this->assertEquals($user1, $cachedMounts[0]->getUser());
  287. $this->assertEquals($rootId, $cachedMounts[0]->getRootId());
  288. $this->assertEquals(2, $cachedMounts[0]->getStorageId());
  289. }
  290. public function testGetMountsForFileIdSubFolder() {
  291. $user1 = $this->userManager->get('u1');
  292. $fileId = $this->createCacheEntry('/foo/bar', 2);
  293. [$storage1, $rootId] = $this->getStorage(2);
  294. $mount1 = new MountPoint($storage1, '/foo/');
  295. $this->cache->registerMounts($user1, [$mount1]);
  296. $this->clearCache();
  297. $cachedMounts = $this->cache->getMountsForFileId($fileId);
  298. $this->assertCount(1, $cachedMounts);
  299. $this->assertEquals('/foo/', $cachedMounts[0]->getMountPoint());
  300. $this->assertEquals($user1, $cachedMounts[0]->getUser());
  301. $this->assertEquals($rootId, $cachedMounts[0]->getRootId());
  302. $this->assertEquals(2, $cachedMounts[0]->getStorageId());
  303. $this->assertEquals('foo/bar', $cachedMounts[0]->getInternalPath());
  304. $this->assertEquals('/foo/foo/bar', $cachedMounts[0]->getPath());
  305. }
  306. public function testGetMountsForFileIdSubFolderMount() {
  307. $user1 = $this->userManager->get('u1');
  308. [$storage1, $rootId] = $this->getStorage(2);
  309. $folderId = $this->createCacheEntry('/foo', 2);
  310. $fileId = $this->createCacheEntry('/foo/bar', 2);
  311. $mount1 = $this->getMockBuilder('\OC\Files\Mount\MountPoint')
  312. ->setConstructorArgs([$storage1, '/'])
  313. ->setMethods(['getStorageRootId'])
  314. ->getMock();
  315. $mount1->expects($this->any())
  316. ->method('getStorageRootId')
  317. ->willReturn($folderId);
  318. $this->cache->registerMounts($user1, [$mount1]);
  319. $this->clearCache();
  320. $cachedMounts = $this->cache->getMountsForFileId($fileId);
  321. $this->assertCount(1, $cachedMounts);
  322. $this->assertEquals('/', $cachedMounts[0]->getMountPoint());
  323. $this->assertEquals($user1, $cachedMounts[0]->getUser());
  324. $this->assertEquals($folderId, $cachedMounts[0]->getRootId());
  325. $this->assertEquals(2, $cachedMounts[0]->getStorageId());
  326. $this->assertEquals('foo', $cachedMounts[0]->getRootInternalPath());
  327. $this->assertEquals('bar', $cachedMounts[0]->getInternalPath());
  328. $this->assertEquals('/bar', $cachedMounts[0]->getPath());
  329. }
  330. public function testGetMountsForFileIdSubFolderMountOutside() {
  331. $user1 = $this->userManager->get('u1');
  332. [$storage1, $rootId] = $this->getStorage(2);
  333. $folderId = $this->createCacheEntry('/foo', 2);
  334. $fileId = $this->createCacheEntry('/bar/asd', 2);
  335. $mount1 = $this->getMockBuilder('\OC\Files\Mount\MountPoint')
  336. ->setConstructorArgs([$storage1, '/foo/'])
  337. ->setMethods(['getStorageRootId'])
  338. ->getMock();
  339. $mount1->expects($this->any())
  340. ->method('getStorageRootId')
  341. ->willReturn($folderId);
  342. $this->cache->registerMounts($user1, [$mount1]);
  343. $this->cache->registerMounts($user1, [$mount1]);
  344. $this->clearCache();
  345. $cachedMounts = $this->cache->getMountsForFileId($fileId);
  346. $this->assertCount(0, $cachedMounts);
  347. }
  348. public function testGetMountsForFileIdDeletedUser() {
  349. $user1 = $this->userManager->get('u1');
  350. [$storage1, $rootId] = $this->getStorage(2);
  351. $rootId = $this->createCacheEntry('', 2);
  352. $mount1 = new MountPoint($storage1, '/foo/');
  353. $this->cache->registerMounts($user1, [$mount1]);
  354. $user1->delete();
  355. $this->clearCache();
  356. $cachedMounts = $this->cache->getMountsForFileId($rootId);
  357. $this->assertEmpty($cachedMounts);
  358. }
  359. public function testGetUsedSpaceForUsers() {
  360. $user1 = $this->userManager->get('u1');
  361. $user2 = $this->userManager->get('u2');
  362. /** @var Storage $storage1 */
  363. [$storage1, $rootId] = $this->getStorage(2);
  364. $folderId = $this->createCacheEntry('files', 2, 100);
  365. $fileId = $this->createCacheEntry('files/foo', 2, 7);
  366. $storage1->getCache()->put($folderId, ['size' => 100]);
  367. $storage1->getCache()->update($fileId, ['size' => 70]);
  368. $mount1 = $this->getMockBuilder(MountPoint::class)
  369. ->setConstructorArgs([$storage1, '/u1/'])
  370. ->setMethods(['getStorageRootId', 'getNumericStorageId'])
  371. ->getMock();
  372. $mount1->expects($this->any())
  373. ->method('getStorageRootId')
  374. ->willReturn($rootId);
  375. $mount1->expects($this->any())
  376. ->method('getNumericStorageId')
  377. ->willReturn(2);
  378. $this->cache->registerMounts($user1, [$mount1]);
  379. $result = $this->cache->getUsedSpaceForUsers([$user1, $user2]);
  380. $this->assertEquals(['u1' => 100], $result);
  381. }
  382. public function testMigrateMountProvider() {
  383. $user1 = $this->userManager->get('u1');
  384. [$storage1, $rootId] = $this->getStorage(2);
  385. $rootId = $this->createCacheEntry('', 2);
  386. $mount1 = new MountPoint($storage1, '/foo/');
  387. $this->cache->registerMounts($user1, [$mount1]);
  388. $this->clearCache();
  389. $cachedMounts = $this->cache->getMountsForUser($user1);
  390. $this->assertCount(1, $cachedMounts);
  391. $this->assertEquals('', $cachedMounts[$this->keyForMount($mount1)]->getMountProvider());
  392. $mount1 = new MountPoint($storage1, '/foo/', null, null, null, null, 'dummy');
  393. $this->cache->registerMounts($user1, [$mount1], ['dummy']);
  394. $this->clearCache();
  395. $cachedMounts = $this->cache->getMountsForUser($user1);
  396. $this->assertCount(1, $cachedMounts);
  397. $this->assertEquals('dummy', $cachedMounts[$this->keyForMount($mount1)]->getMountProvider());
  398. }
  399. }