UserMountCacheTest.php 15 KB

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