UserMountCacheTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 OCP\Cache\CappedMemoryCache;
  13. use OC\User\Manager;
  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 Symfony\Component\EventDispatcher\EventDispatcherInterface;
  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(EventDispatcherInterface::class), $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::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) {
  76. $rootId = $this->createCacheEntry('', $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. public function testNewMounts() {
  104. $user = $this->userManager->get('u1');
  105. [$storage] = $this->getStorage(10);
  106. $mount = new MountPoint($storage, '/asd/');
  107. $this->cache->registerMounts($user, [$mount]);
  108. $this->clearCache();
  109. $cachedMounts = $this->cache->getMountsForUser($user);
  110. $this->assertCount(1, $cachedMounts);
  111. $cachedMount = $cachedMounts[0];
  112. $this->assertEquals('/asd/', $cachedMount->getMountPoint());
  113. $this->assertEquals($user, $cachedMount->getUser());
  114. $this->assertEquals($storage->getCache()->getId(''), $cachedMount->getRootId());
  115. $this->assertEquals($storage->getStorageCache()->getNumericId(), $cachedMount->getStorageId());
  116. }
  117. public function testSameMounts() {
  118. $user = $this->userManager->get('u1');
  119. [$storage] = $this->getStorage(10);
  120. $mount = new MountPoint($storage, '/asd/');
  121. $this->cache->registerMounts($user, [$mount]);
  122. $this->clearCache();
  123. $this->cache->registerMounts($user, [$mount]);
  124. $this->clearCache();
  125. $cachedMounts = $this->cache->getMountsForUser($user);
  126. $this->assertCount(1, $cachedMounts);
  127. $cachedMount = $cachedMounts[0];
  128. $this->assertEquals('/asd/', $cachedMount->getMountPoint());
  129. $this->assertEquals($user, $cachedMount->getUser());
  130. $this->assertEquals($storage->getCache()->getId(''), $cachedMount->getRootId());
  131. $this->assertEquals($storage->getStorageCache()->getNumericId(), $cachedMount->getStorageId());
  132. }
  133. public function testRemoveMounts() {
  134. $user = $this->userManager->get('u1');
  135. [$storage] = $this->getStorage(10);
  136. $mount = new MountPoint($storage, '/asd/');
  137. $this->cache->registerMounts($user, [$mount]);
  138. $this->clearCache();
  139. $this->cache->registerMounts($user, []);
  140. $this->clearCache();
  141. $cachedMounts = $this->cache->getMountsForUser($user);
  142. $this->assertCount(0, $cachedMounts);
  143. }
  144. public function testChangeMounts() {
  145. $user = $this->userManager->get('u1');
  146. [$storage] = $this->getStorage(10);
  147. $mount = new MountPoint($storage, '/bar/');
  148. $this->cache->registerMounts($user, [$mount]);
  149. $this->clearCache();
  150. $mount = new MountPoint($storage, '/foo/');
  151. $this->cache->registerMounts($user, [$mount]);
  152. $this->clearCache();
  153. $cachedMounts = $this->cache->getMountsForUser($user);
  154. $this->assertCount(1, $cachedMounts);
  155. $cachedMount = $cachedMounts[0];
  156. $this->assertEquals('/foo/', $cachedMount->getMountPoint());
  157. }
  158. public function testChangeMountId() {
  159. $user = $this->userManager->get('u1');
  160. [$storage] = $this->getStorage(10);
  161. $mount = new MountPoint($storage, '/foo/', null, null, null, null);
  162. $this->cache->registerMounts($user, [$mount]);
  163. $this->clearCache();
  164. $mount = new MountPoint($storage, '/foo/', null, null, null, 1);
  165. $this->cache->registerMounts($user, [$mount]);
  166. $this->clearCache();
  167. $cachedMounts = $this->cache->getMountsForUser($user);
  168. $this->assertCount(1, $cachedMounts);
  169. $cachedMount = $cachedMounts[0];
  170. $this->assertEquals(1, $cachedMount->getMountId());
  171. }
  172. public function testGetMountsForUser() {
  173. $user1 = $this->userManager->get('u1');
  174. $user2 = $this->userManager->get('u2');
  175. $user3 = $this->userManager->get('u3');
  176. [$storage1, $id1] = $this->getStorage(1);
  177. [$storage2, $id2] = $this->getStorage(2);
  178. $mount1 = new MountPoint($storage1, '/foo/');
  179. $mount2 = new MountPoint($storage2, '/bar/');
  180. $this->cache->registerMounts($user1, [$mount1, $mount2]);
  181. $this->cache->registerMounts($user2, [$mount2]);
  182. $this->cache->registerMounts($user3, [$mount2]);
  183. $this->clearCache();
  184. $user3->delete();
  185. $cachedMounts = $this->cache->getMountsForUser($user1);
  186. $this->assertCount(2, $cachedMounts);
  187. $this->assertEquals('/foo/', $cachedMounts[0]->getMountPoint());
  188. $this->assertEquals($user1, $cachedMounts[0]->getUser());
  189. $this->assertEquals($id1, $cachedMounts[0]->getRootId());
  190. $this->assertEquals(1, $cachedMounts[0]->getStorageId());
  191. $this->assertEquals('/bar/', $cachedMounts[1]->getMountPoint());
  192. $this->assertEquals($user1, $cachedMounts[1]->getUser());
  193. $this->assertEquals($id2, $cachedMounts[1]->getRootId());
  194. $this->assertEquals(2, $cachedMounts[1]->getStorageId());
  195. $cachedMounts = $this->cache->getMountsForUser($user3);
  196. $this->assertEmpty($cachedMounts);
  197. }
  198. public function testGetMountsByStorageId() {
  199. $user1 = $this->userManager->get('u1');
  200. $user2 = $this->userManager->get('u2');
  201. [$storage1, $id1] = $this->getStorage(1);
  202. [$storage2, $id2] = $this->getStorage(2);
  203. $mount1 = new MountPoint($storage1, '/foo/');
  204. $mount2 = new MountPoint($storage2, '/bar/');
  205. $this->cache->registerMounts($user1, [$mount1, $mount2]);
  206. $this->cache->registerMounts($user2, [$mount2]);
  207. $this->clearCache();
  208. $cachedMounts = $this->cache->getMountsForStorageId(2);
  209. $this->sortMounts($cachedMounts);
  210. $this->assertCount(2, $cachedMounts);
  211. $this->assertEquals('/bar/', $cachedMounts[0]->getMountPoint());
  212. $this->assertEquals($user1, $cachedMounts[0]->getUser());
  213. $this->assertEquals($id2, $cachedMounts[0]->getRootId());
  214. $this->assertEquals(2, $cachedMounts[0]->getStorageId());
  215. $this->assertEquals('/bar/', $cachedMounts[1]->getMountPoint());
  216. $this->assertEquals($user2, $cachedMounts[1]->getUser());
  217. $this->assertEquals($id2, $cachedMounts[1]->getRootId());
  218. $this->assertEquals(2, $cachedMounts[1]->getStorageId());
  219. }
  220. public function testGetMountsByRootId() {
  221. $user1 = $this->userManager->get('u1');
  222. $user2 = $this->userManager->get('u2');
  223. [$storage1, $id1] = $this->getStorage(1);
  224. [$storage2, $id2] = $this->getStorage(2);
  225. $mount1 = new MountPoint($storage1, '/foo/');
  226. $mount2 = new MountPoint($storage2, '/bar/');
  227. $this->cache->registerMounts($user1, [$mount1, $mount2]);
  228. $this->cache->registerMounts($user2, [$mount2]);
  229. $this->clearCache();
  230. $cachedMounts = $this->cache->getMountsForRootId($id2);
  231. $this->sortMounts($cachedMounts);
  232. $this->assertCount(2, $cachedMounts);
  233. $this->assertEquals('/bar/', $cachedMounts[0]->getMountPoint());
  234. $this->assertEquals($user1, $cachedMounts[0]->getUser());
  235. $this->assertEquals($id2, $cachedMounts[0]->getRootId());
  236. $this->assertEquals(2, $cachedMounts[0]->getStorageId());
  237. $this->assertEquals('/bar/', $cachedMounts[1]->getMountPoint());
  238. $this->assertEquals($user2, $cachedMounts[1]->getUser());
  239. $this->assertEquals($id2, $cachedMounts[1]->getRootId());
  240. $this->assertEquals(2, $cachedMounts[1]->getStorageId());
  241. }
  242. private function sortMounts(&$mounts) {
  243. usort($mounts, function (ICachedMountInfo $a, ICachedMountInfo $b) {
  244. return strcmp($a->getUser()->getUID(), $b->getUser()->getUID());
  245. });
  246. }
  247. private function createCacheEntry($internalPath, $storageId, $size = 0) {
  248. $internalPath = trim($internalPath, '/');
  249. $inserted = $this->connection->insertIfNotExist('*PREFIX*filecache', [
  250. 'storage' => $storageId,
  251. 'path' => $internalPath,
  252. 'path_hash' => md5($internalPath),
  253. 'parent' => -1,
  254. 'name' => basename($internalPath),
  255. 'mimetype' => 0,
  256. 'mimepart' => 0,
  257. 'size' => $size,
  258. 'storage_mtime' => 0,
  259. 'encrypted' => 0,
  260. 'unencrypted_size' => 0,
  261. 'etag' => '',
  262. 'permissions' => 31
  263. ], ['storage', 'path_hash']);
  264. if ($inserted) {
  265. $id = (int)$this->connection->lastInsertId('*PREFIX*filecache');
  266. $this->fileIds[] = $id;
  267. } else {
  268. $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` =?';
  269. $query = $this->connection->prepare($sql);
  270. $query->execute([$storageId, md5($internalPath)]);
  271. return (int)$query->fetchOne();
  272. }
  273. return $id;
  274. }
  275. public function testGetMountsForFileIdRootId() {
  276. $user1 = $this->userManager->get('u1');
  277. [$storage1, $rootId] = $this->getStorage(2);
  278. $mount1 = new MountPoint($storage1, '/foo/');
  279. $this->cache->registerMounts($user1, [$mount1]);
  280. $this->clearCache();
  281. $cachedMounts = $this->cache->getMountsForFileId($rootId);
  282. $this->assertCount(1, $cachedMounts);
  283. $this->assertEquals('/foo/', $cachedMounts[0]->getMountPoint());
  284. $this->assertEquals($user1, $cachedMounts[0]->getUser());
  285. $this->assertEquals($rootId, $cachedMounts[0]->getRootId());
  286. $this->assertEquals(2, $cachedMounts[0]->getStorageId());
  287. }
  288. public function testGetMountsForFileIdSubFolder() {
  289. $user1 = $this->userManager->get('u1');
  290. $fileId = $this->createCacheEntry('/foo/bar', 2);
  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($fileId);
  296. $this->assertCount(1, $cachedMounts);
  297. $this->assertEquals('/foo/', $cachedMounts[0]->getMountPoint());
  298. $this->assertEquals($user1, $cachedMounts[0]->getUser());
  299. $this->assertEquals($rootId, $cachedMounts[0]->getRootId());
  300. $this->assertEquals(2, $cachedMounts[0]->getStorageId());
  301. $this->assertEquals('foo/bar', $cachedMounts[0]->getInternalPath());
  302. $this->assertEquals('/foo/foo/bar', $cachedMounts[0]->getPath());
  303. }
  304. public function testGetMountsForFileIdSubFolderMount() {
  305. $user1 = $this->userManager->get('u1');
  306. [$storage1, $rootId] = $this->getStorage(2);
  307. $folderId = $this->createCacheEntry('/foo', 2);
  308. $fileId = $this->createCacheEntry('/foo/bar', 2);
  309. $mount1 = $this->getMockBuilder('\OC\Files\Mount\MountPoint')
  310. ->setConstructorArgs([$storage1, '/'])
  311. ->setMethods(['getStorageRootId'])
  312. ->getMock();
  313. $mount1->expects($this->any())
  314. ->method('getStorageRootId')
  315. ->willReturn($folderId);
  316. $this->cache->registerMounts($user1, [$mount1]);
  317. $this->clearCache();
  318. $cachedMounts = $this->cache->getMountsForFileId($fileId);
  319. $this->assertCount(1, $cachedMounts);
  320. $this->assertEquals('/', $cachedMounts[0]->getMountPoint());
  321. $this->assertEquals($user1, $cachedMounts[0]->getUser());
  322. $this->assertEquals($folderId, $cachedMounts[0]->getRootId());
  323. $this->assertEquals(2, $cachedMounts[0]->getStorageId());
  324. $this->assertEquals('foo', $cachedMounts[0]->getRootInternalPath());
  325. $this->assertEquals('bar', $cachedMounts[0]->getInternalPath());
  326. $this->assertEquals('/bar', $cachedMounts[0]->getPath());
  327. }
  328. public function testGetMountsForFileIdSubFolderMountOutside() {
  329. $user1 = $this->userManager->get('u1');
  330. [$storage1, $rootId] = $this->getStorage(2);
  331. $folderId = $this->createCacheEntry('/foo', 2);
  332. $fileId = $this->createCacheEntry('/bar/asd', 2);
  333. $mount1 = $this->getMockBuilder('\OC\Files\Mount\MountPoint')
  334. ->setConstructorArgs([$storage1, '/foo/'])
  335. ->setMethods(['getStorageRootId'])
  336. ->getMock();
  337. $mount1->expects($this->any())
  338. ->method('getStorageRootId')
  339. ->willReturn($folderId);
  340. $this->cache->registerMounts($user1, [$mount1]);
  341. $this->cache->registerMounts($user1, [$mount1]);
  342. $this->clearCache();
  343. $cachedMounts = $this->cache->getMountsForFileId($fileId);
  344. $this->assertCount(0, $cachedMounts);
  345. }
  346. public function testGetMountsForFileIdDeletedUser() {
  347. $user1 = $this->userManager->get('u1');
  348. [$storage1, $rootId] = $this->getStorage(2);
  349. $rootId = $this->createCacheEntry('', 2);
  350. $mount1 = new MountPoint($storage1, '/foo/');
  351. $this->cache->registerMounts($user1, [$mount1]);
  352. $user1->delete();
  353. $this->clearCache();
  354. $cachedMounts = $this->cache->getMountsForFileId($rootId);
  355. $this->assertEmpty($cachedMounts);
  356. }
  357. public function testGetUsedSpaceForUsers() {
  358. $user1 = $this->userManager->get('u1');
  359. $user2 = $this->userManager->get('u2');
  360. /** @var Storage $storage1 */
  361. [$storage1, $rootId] = $this->getStorage(2);
  362. $folderId = $this->createCacheEntry('files', 2, 100);
  363. $fileId = $this->createCacheEntry('files/foo', 2, 7);
  364. $storage1->getCache()->put($folderId, ['size' => 100]);
  365. $storage1->getCache()->update($fileId, ['size' => 70]);
  366. $mount1 = $this->getMockBuilder(MountPoint::class)
  367. ->setConstructorArgs([$storage1, '/u1/'])
  368. ->setMethods(['getStorageRootId', 'getNumericStorageId'])
  369. ->getMock();
  370. $mount1->expects($this->any())
  371. ->method('getStorageRootId')
  372. ->willReturn($rootId);
  373. $mount1->expects($this->any())
  374. ->method('getNumericStorageId')
  375. ->willReturn(2);
  376. $this->cache->registerMounts($user1, [$mount1]);
  377. $result = $this->cache->getUsedSpaceForUsers([$user1, $user2]);
  378. $this->assertEquals(['u1' => 100], $result);
  379. }
  380. public function testMigrateMountProvider() {
  381. $user1 = $this->userManager->get('u1');
  382. [$storage1, $rootId] = $this->getStorage(2);
  383. $rootId = $this->createCacheEntry('', 2);
  384. $mount1 = new MountPoint($storage1, '/foo/');
  385. $this->cache->registerMounts($user1, [$mount1]);
  386. $this->clearCache();
  387. $cachedMounts = $this->cache->getMountsForUser($user1);
  388. $this->assertCount(1, $cachedMounts);
  389. $this->assertEquals('', $cachedMounts[0]->getMountProvider());
  390. $mount1 = new MountPoint($storage1, '/foo/', null, null, null, null, 'dummy');
  391. $this->cache->registerMounts($user1, [$mount1], ['dummy']);
  392. $this->clearCache();
  393. $cachedMounts = $this->cache->getMountsForUser($user1);
  394. $this->assertCount(1, $cachedMounts);
  395. $this->assertEquals('dummy', $cachedMounts[0]->getMountProvider());
  396. }
  397. }