1
0

UserMountCacheTest.php 13 KB

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