1
0

UserMountCacheTest.php 16 KB

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