UserMountCacheTest.php 15 KB

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