UserMountCacheTest.php 15 KB

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