2015-11-26 16:47:53 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test\Files\Config;
|
|
|
|
|
2016-01-22 15:02:21 +00:00
|
|
|
use OC\DB\QueryBuilder\Literal;
|
2015-11-26 16:47:53 +00:00
|
|
|
use OC\Files\Mount\MountPoint;
|
2015-12-01 12:29:20 +00:00
|
|
|
use OC\Log;
|
2015-11-26 16:47:53 +00:00
|
|
|
use OC\User\Manager;
|
2015-12-01 12:29:20 +00:00
|
|
|
use OCP\Files\Config\ICachedMountInfo;
|
2015-11-26 16:47:53 +00:00
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use Test\TestCase;
|
|
|
|
use Test\Util\User\Dummy;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DB
|
|
|
|
*/
|
2016-05-20 13:38:20 +00:00
|
|
|
class UserMountCacheTest extends TestCase {
|
2015-11-26 16:47:53 +00:00
|
|
|
/**
|
|
|
|
* @var IDBConnection
|
|
|
|
*/
|
|
|
|
private $connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var IUserManager
|
|
|
|
*/
|
|
|
|
private $userManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Config\UserMountCache
|
|
|
|
*/
|
|
|
|
private $cache;
|
|
|
|
|
2016-01-22 15:02:21 +00:00
|
|
|
private $fileIds = [];
|
|
|
|
|
2015-11-26 16:47:53 +00:00
|
|
|
public function setUp() {
|
2016-01-22 15:02:21 +00:00
|
|
|
$this->fileIds = [];
|
2015-11-26 16:47:53 +00:00
|
|
|
$this->connection = \OC::$server->getDatabaseConnection();
|
|
|
|
$this->userManager = new Manager(null);
|
|
|
|
$userBackend = new Dummy();
|
|
|
|
$userBackend->createUser('u1', '');
|
|
|
|
$userBackend->createUser('u2', '');
|
|
|
|
$this->userManager->registerBackend($userBackend);
|
2015-12-02 12:36:33 +00:00
|
|
|
$this->cache = new \OC\Files\Config\UserMountCache($this->connection, $this->userManager, $this->getMock('\OC\Log'));
|
2015-11-26 16:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
$builder->delete('mounts')->execute();
|
2016-01-22 15:02:21 +00:00
|
|
|
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
foreach ($this->fileIds as $fileId) {
|
|
|
|
$builder->delete('filecache')
|
|
|
|
->where($builder->expr()->eq('fileid', new Literal($fileId)))
|
|
|
|
->execute();
|
|
|
|
}
|
2015-11-26 16:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getStorage($storageId, $rootId) {
|
|
|
|
$storageCache = $this->getMockBuilder('\OC\Files\Cache\Storage')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$storageCache->expects($this->any())
|
|
|
|
->method('getNumericId')
|
|
|
|
->will($this->returnValue($storageId));
|
|
|
|
|
|
|
|
$cache = $this->getMockBuilder('\OC\Files\Cache\Cache')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$cache->expects($this->any())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue($rootId));
|
|
|
|
|
|
|
|
$storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$storage->expects($this->any())
|
|
|
|
->method('getStorageCache')
|
|
|
|
->will($this->returnValue($storageCache));
|
|
|
|
$storage->expects($this->any())
|
|
|
|
->method('getCache')
|
|
|
|
->will($this->returnValue($cache));
|
|
|
|
|
|
|
|
return $storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function clearCache() {
|
|
|
|
$this->invokePrivate($this->cache, 'mountsForUsers', [[]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNewMounts() {
|
|
|
|
$user = $this->userManager->get('u1');
|
|
|
|
|
|
|
|
$storage = $this->getStorage(10, 20);
|
|
|
|
$mount = new MountPoint($storage, '/asd/');
|
|
|
|
|
|
|
|
$this->cache->registerMounts($user, [$mount]);
|
|
|
|
|
|
|
|
$this->clearCache();
|
|
|
|
|
|
|
|
$cachedMounts = $this->cache->getMountsForUser($user);
|
|
|
|
|
|
|
|
$this->assertCount(1, $cachedMounts);
|
|
|
|
$cachedMount = $cachedMounts[0];
|
|
|
|
$this->assertEquals('/asd/', $cachedMount->getMountPoint());
|
|
|
|
$this->assertEquals($user, $cachedMount->getUser());
|
|
|
|
$this->assertEquals($storage->getCache()->getId(''), $cachedMount->getRootId());
|
|
|
|
$this->assertEquals($storage->getStorageCache()->getNumericId(), $cachedMount->getStorageId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSameMounts() {
|
|
|
|
$user = $this->userManager->get('u1');
|
|
|
|
|
|
|
|
$storage = $this->getStorage(10, 20);
|
|
|
|
$mount = new MountPoint($storage, '/asd/');
|
|
|
|
|
|
|
|
$this->cache->registerMounts($user, [$mount]);
|
|
|
|
|
|
|
|
$this->clearCache();
|
|
|
|
|
|
|
|
$this->cache->registerMounts($user, [$mount]);
|
|
|
|
|
|
|
|
$this->clearCache();
|
|
|
|
|
|
|
|
$cachedMounts = $this->cache->getMountsForUser($user);
|
|
|
|
|
|
|
|
$this->assertCount(1, $cachedMounts);
|
|
|
|
$cachedMount = $cachedMounts[0];
|
|
|
|
$this->assertEquals('/asd/', $cachedMount->getMountPoint());
|
|
|
|
$this->assertEquals($user, $cachedMount->getUser());
|
|
|
|
$this->assertEquals($storage->getCache()->getId(''), $cachedMount->getRootId());
|
|
|
|
$this->assertEquals($storage->getStorageCache()->getNumericId(), $cachedMount->getStorageId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveMounts() {
|
|
|
|
$user = $this->userManager->get('u1');
|
|
|
|
|
|
|
|
$storage = $this->getStorage(10, 20);
|
|
|
|
$mount = new MountPoint($storage, '/asd/');
|
|
|
|
|
|
|
|
$this->cache->registerMounts($user, [$mount]);
|
|
|
|
|
|
|
|
$this->clearCache();
|
|
|
|
|
|
|
|
$this->cache->registerMounts($user, []);
|
|
|
|
|
|
|
|
$this->clearCache();
|
|
|
|
|
|
|
|
$cachedMounts = $this->cache->getMountsForUser($user);
|
|
|
|
|
|
|
|
$this->assertCount(0, $cachedMounts);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testChangeMounts() {
|
|
|
|
$user = $this->userManager->get('u1');
|
|
|
|
|
|
|
|
$storage = $this->getStorage(10, 20);
|
|
|
|
$mount = new MountPoint($storage, '/foo/');
|
|
|
|
|
|
|
|
$this->cache->registerMounts($user, [$mount]);
|
|
|
|
|
|
|
|
$this->clearCache();
|
|
|
|
|
|
|
|
$this->cache->registerMounts($user, [$mount]);
|
|
|
|
|
|
|
|
$this->clearCache();
|
|
|
|
|
|
|
|
$cachedMounts = $this->cache->getMountsForUser($user);
|
|
|
|
|
|
|
|
$this->assertCount(1, $cachedMounts);
|
|
|
|
$cachedMount = $cachedMounts[0];
|
|
|
|
$this->assertEquals('/foo/', $cachedMount->getMountPoint());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMountsForUser() {
|
|
|
|
$user1 = $this->userManager->get('u1');
|
|
|
|
$user2 = $this->userManager->get('u2');
|
|
|
|
|
|
|
|
$mount1 = new MountPoint($this->getStorage(1, 2), '/foo/');
|
|
|
|
$mount2 = new MountPoint($this->getStorage(3, 4), '/bar/');
|
|
|
|
|
|
|
|
$this->cache->registerMounts($user1, [$mount1, $mount2]);
|
|
|
|
$this->cache->registerMounts($user2, [$mount2]);
|
|
|
|
|
|
|
|
$this->clearCache();
|
|
|
|
|
|
|
|
$cachedMounts = $this->cache->getMountsForUser($user1);
|
|
|
|
|
|
|
|
$this->assertCount(2, $cachedMounts);
|
|
|
|
$this->assertEquals('/foo/', $cachedMounts[0]->getMountPoint());
|
|
|
|
$this->assertEquals($user1, $cachedMounts[0]->getUser());
|
|
|
|
$this->assertEquals(2, $cachedMounts[0]->getRootId());
|
|
|
|
$this->assertEquals(1, $cachedMounts[0]->getStorageId());
|
|
|
|
|
|
|
|
$this->assertEquals('/bar/', $cachedMounts[1]->getMountPoint());
|
|
|
|
$this->assertEquals($user1, $cachedMounts[1]->getUser());
|
|
|
|
$this->assertEquals(4, $cachedMounts[1]->getRootId());
|
|
|
|
$this->assertEquals(3, $cachedMounts[1]->getStorageId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMountsByStorageId() {
|
|
|
|
$user1 = $this->userManager->get('u1');
|
|
|
|
$user2 = $this->userManager->get('u2');
|
|
|
|
|
|
|
|
$mount1 = new MountPoint($this->getStorage(1, 2), '/foo/');
|
|
|
|
$mount2 = new MountPoint($this->getStorage(3, 4), '/bar/');
|
|
|
|
|
|
|
|
$this->cache->registerMounts($user1, [$mount1, $mount2]);
|
|
|
|
$this->cache->registerMounts($user2, [$mount2]);
|
|
|
|
|
|
|
|
$this->clearCache();
|
|
|
|
|
|
|
|
$cachedMounts = $this->cache->getMountsForStorageId(3);
|
2016-01-22 15:02:21 +00:00
|
|
|
$this->sortMounts($cachedMounts);
|
2015-11-26 16:47:53 +00:00
|
|
|
|
|
|
|
$this->assertCount(2, $cachedMounts);
|
|
|
|
|
|
|
|
$this->assertEquals('/bar/', $cachedMounts[0]->getMountPoint());
|
|
|
|
$this->assertEquals($user1, $cachedMounts[0]->getUser());
|
|
|
|
$this->assertEquals(4, $cachedMounts[0]->getRootId());
|
|
|
|
$this->assertEquals(3, $cachedMounts[0]->getStorageId());
|
|
|
|
|
|
|
|
$this->assertEquals('/bar/', $cachedMounts[1]->getMountPoint());
|
|
|
|
$this->assertEquals($user2, $cachedMounts[1]->getUser());
|
|
|
|
$this->assertEquals(4, $cachedMounts[1]->getRootId());
|
|
|
|
$this->assertEquals(3, $cachedMounts[1]->getStorageId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMountsByRootId() {
|
|
|
|
$user1 = $this->userManager->get('u1');
|
|
|
|
$user2 = $this->userManager->get('u2');
|
|
|
|
|
|
|
|
$mount1 = new MountPoint($this->getStorage(1, 2), '/foo/');
|
|
|
|
$mount2 = new MountPoint($this->getStorage(3, 4), '/bar/');
|
|
|
|
|
|
|
|
$this->cache->registerMounts($user1, [$mount1, $mount2]);
|
|
|
|
$this->cache->registerMounts($user2, [$mount2]);
|
|
|
|
|
|
|
|
$this->clearCache();
|
|
|
|
|
|
|
|
$cachedMounts = $this->cache->getMountsForRootId(4);
|
2016-01-22 15:02:21 +00:00
|
|
|
$this->sortMounts($cachedMounts);
|
2015-11-26 16:47:53 +00:00
|
|
|
|
|
|
|
$this->assertCount(2, $cachedMounts);
|
|
|
|
|
|
|
|
$this->assertEquals('/bar/', $cachedMounts[0]->getMountPoint());
|
|
|
|
$this->assertEquals($user1, $cachedMounts[0]->getUser());
|
|
|
|
$this->assertEquals(4, $cachedMounts[0]->getRootId());
|
|
|
|
$this->assertEquals(3, $cachedMounts[0]->getStorageId());
|
|
|
|
|
|
|
|
$this->assertEquals('/bar/', $cachedMounts[1]->getMountPoint());
|
|
|
|
$this->assertEquals($user2, $cachedMounts[1]->getUser());
|
|
|
|
$this->assertEquals(4, $cachedMounts[1]->getRootId());
|
|
|
|
$this->assertEquals(3, $cachedMounts[1]->getStorageId());
|
|
|
|
}
|
2016-01-22 15:02:21 +00:00
|
|
|
|
|
|
|
private function sortMounts(&$mounts) {
|
|
|
|
usort($mounts, function (ICachedMountInfo $a, ICachedMountInfo $b) {
|
|
|
|
return strcmp($a->getUser()->getUID(), $b->getUser()->getUID());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createCacheEntry($internalPath, $storageId) {
|
|
|
|
$this->connection->insertIfNotExist('*PREFIX*filecache', [
|
|
|
|
'storage' => $storageId,
|
|
|
|
'path' => $internalPath,
|
|
|
|
'path_hash' => md5($internalPath),
|
|
|
|
'parent' => -1,
|
|
|
|
'name' => basename($internalPath),
|
|
|
|
'mimetype' => 0,
|
|
|
|
'mimepart' => 0,
|
|
|
|
'size' => 0,
|
|
|
|
'storage_mtime' => 0,
|
2016-01-26 13:51:06 +00:00
|
|
|
'encrypted' => 0,
|
2016-01-22 15:02:21 +00:00
|
|
|
'unencrypted_size' => 0,
|
|
|
|
'etag' => '',
|
|
|
|
'permissions' => 31
|
2016-01-26 13:51:06 +00:00
|
|
|
], ['storage', 'path_hash']);
|
2016-01-22 15:02:21 +00:00
|
|
|
$id = (int)$this->connection->lastInsertId('*PREFIX*filecache');
|
|
|
|
$this->fileIds[] = $id;
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMountsForFileIdRootId() {
|
|
|
|
$user1 = $this->userManager->get('u1');
|
|
|
|
|
|
|
|
$rootId = $this->createCacheEntry('', 2);
|
|
|
|
|
|
|
|
$mount1 = new MountPoint($this->getStorage(2, $rootId), '/foo/');
|
|
|
|
|
|
|
|
$this->cache->registerMounts($user1, [$mount1]);
|
|
|
|
|
|
|
|
$this->clearCache();
|
|
|
|
|
|
|
|
$cachedMounts = $this->cache->getMountsForFileId($rootId);
|
|
|
|
|
|
|
|
$this->assertCount(1, $cachedMounts);
|
|
|
|
|
|
|
|
$this->assertEquals('/foo/', $cachedMounts[0]->getMountPoint());
|
|
|
|
$this->assertEquals($user1, $cachedMounts[0]->getUser());
|
|
|
|
$this->assertEquals($rootId, $cachedMounts[0]->getRootId());
|
|
|
|
$this->assertEquals(2, $cachedMounts[0]->getStorageId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMountsForFileIdSubFolder() {
|
|
|
|
$user1 = $this->userManager->get('u1');
|
|
|
|
|
|
|
|
$rootId = $this->createCacheEntry('', 2);
|
|
|
|
$fileId = $this->createCacheEntry('/foo/bar', 2);
|
|
|
|
|
|
|
|
$mount1 = new MountPoint($this->getStorage(2, $rootId), '/foo/');
|
|
|
|
|
|
|
|
$this->cache->registerMounts($user1, [$mount1]);
|
|
|
|
|
|
|
|
$this->clearCache();
|
|
|
|
|
|
|
|
$cachedMounts = $this->cache->getMountsForFileId($fileId);
|
|
|
|
|
|
|
|
$this->assertCount(1, $cachedMounts);
|
|
|
|
|
|
|
|
$this->assertEquals('/foo/', $cachedMounts[0]->getMountPoint());
|
|
|
|
$this->assertEquals($user1, $cachedMounts[0]->getUser());
|
|
|
|
$this->assertEquals($rootId, $cachedMounts[0]->getRootId());
|
|
|
|
$this->assertEquals(2, $cachedMounts[0]->getStorageId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMountsForFileIdSubFolderMount() {
|
|
|
|
$user1 = $this->userManager->get('u1');
|
|
|
|
|
|
|
|
$this->createCacheEntry('', 2);
|
|
|
|
$folderId = $this->createCacheEntry('/foo', 2);
|
|
|
|
$fileId = $this->createCacheEntry('/foo/bar', 2);
|
|
|
|
|
|
|
|
$mount1 = new MountPoint($this->getStorage(2, $folderId), '/foo/');
|
|
|
|
|
|
|
|
$this->cache->registerMounts($user1, [$mount1]);
|
|
|
|
|
|
|
|
$this->clearCache();
|
|
|
|
|
|
|
|
$cachedMounts = $this->cache->getMountsForFileId($fileId);
|
|
|
|
|
|
|
|
$this->assertCount(1, $cachedMounts);
|
|
|
|
|
|
|
|
$this->assertEquals('/foo/', $cachedMounts[0]->getMountPoint());
|
|
|
|
$this->assertEquals($user1, $cachedMounts[0]->getUser());
|
|
|
|
$this->assertEquals($folderId, $cachedMounts[0]->getRootId());
|
|
|
|
$this->assertEquals(2, $cachedMounts[0]->getStorageId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMountsForFileIdSubFolderMountOutside() {
|
|
|
|
$user1 = $this->userManager->get('u1');
|
|
|
|
|
|
|
|
$this->createCacheEntry('', 2);
|
|
|
|
$folderId = $this->createCacheEntry('/foo', 2);
|
|
|
|
$fileId = $this->createCacheEntry('/bar/asd', 2);
|
|
|
|
|
|
|
|
$mount1 = new MountPoint($this->getStorage(2, $folderId), '/foo/');
|
|
|
|
|
|
|
|
$this->cache->registerMounts($user1, [$mount1]);
|
|
|
|
|
|
|
|
$this->clearCache();
|
|
|
|
|
|
|
|
$cachedMounts = $this->cache->getMountsForFileId($fileId);
|
|
|
|
|
|
|
|
$this->assertCount(0, $cachedMounts);
|
|
|
|
}
|
2015-11-26 16:47:53 +00:00
|
|
|
}
|