123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- <?php
- /**
- * Copyright (c) 2012 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\Cache;
- use \OC\Files\Filesystem as Filesystem;
- use OC\Files\Storage\Temporary;
- class Updater extends \PHPUnit_Framework_TestCase {
- /**
- * @var \OC\Files\Storage\Storage $storage
- */
- private $storage;
- /**
- * @var \OC\Files\Cache\Scanner $scanner
- */
- private $scanner;
- private $stateFilesEncryption;
- /**
- * @var \OC\Files\Cache\Cache $cache
- */
- private $cache;
- private static $user;
- public function setUp() {
- // remember files_encryption state
- $this->stateFilesEncryption = \OC_App::isEnabled('files_encryption');
- // we want to tests with the encryption app disabled
- \OC_App::disable('files_encryption');
- $this->storage = new \OC\Files\Storage\Temporary(array());
- $textData = "dummy file data\n";
- $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
- $this->storage->mkdir('folder');
- $this->storage->file_put_contents('foo.txt', $textData);
- $this->storage->file_put_contents('foo.png', $imgData);
- $this->storage->file_put_contents('folder/bar.txt', $textData);
- $this->storage->file_put_contents('folder/bar2.txt', $textData);
- $this->scanner = $this->storage->getScanner();
- $this->scanner->scan('');
- $this->cache = $this->storage->getCache();
- \OC\Files\Filesystem::tearDown();
- if (!self::$user) {
- self::$user = uniqid();
- }
- \OC_User::createUser(self::$user, 'password');
- \OC_User::setUserId(self::$user);
- \OC\Files\Filesystem::init(self::$user, '/' . self::$user . '/files');
- Filesystem::clearMounts();
- Filesystem::mount($this->storage, array(), '/' . self::$user . '/files');
- \OC_Hook::clear('OC_Filesystem');
- \OC_Hook::connect('OC_Filesystem', 'post_write', '\OC\Files\Cache\Updater', 'writeHook');
- \OC_Hook::connect('OC_Filesystem', 'post_delete', '\OC\Files\Cache\Updater', 'deleteHook');
- \OC_Hook::connect('OC_Filesystem', 'post_rename', '\OC\Files\Cache\Updater', 'renameHook');
- \OC_Hook::connect('OC_Filesystem', 'post_touch', '\OC\Files\Cache\Updater', 'touchHook');
- }
- public function tearDown() {
- if ($this->cache) {
- $this->cache->clear();
- }
- $result = \OC_User::deleteUser(self::$user);
- $this->assertTrue($result);
- Filesystem::tearDown();
- // reset app files_encryption
- if ($this->stateFilesEncryption) {
- \OC_App::enable('files_encryption');
- }
- }
- public function testWrite() {
- $textSize = strlen("dummy file data\n");
- $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
- $this->cache->put('foo.txt', array('mtime' => 100));
- $rootCachedData = $this->cache->get('');
- $this->assertEquals(3 * $textSize + $imageSize, $rootCachedData['size']);
- $fooCachedData = $this->cache->get('foo.txt');
- Filesystem::file_put_contents('foo.txt', 'asd');
- $cachedData = $this->cache->get('foo.txt');
- $this->assertEquals(3, $cachedData['size']);
- $this->assertNotEquals($fooCachedData['etag'], $cachedData['etag']);
- $cachedData = $this->cache->get('');
- $this->assertEquals(2 * $textSize + $imageSize + 3, $cachedData['size']);
- $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
- $rootCachedData = $cachedData;
- $this->assertFalse($this->cache->inCache('bar.txt'));
- Filesystem::file_put_contents('bar.txt', 'asd');
- $this->assertTrue($this->cache->inCache('bar.txt'));
- $cachedData = $this->cache->get('bar.txt');
- $this->assertEquals(3, $cachedData['size']);
- $mtime = $cachedData['mtime'];
- $cachedData = $this->cache->get('');
- $this->assertEquals(2 * $textSize + $imageSize + 2 * 3, $cachedData['size']);
- $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
- $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $mtime);
- }
- public function testWriteWithMountPoints() {
- $storage2 = new \OC\Files\Storage\Temporary(array());
- $cache2 = $storage2->getCache();
- Filesystem::mount($storage2, array(), '/' . self::$user . '/files/folder/substorage');
- $folderCachedData = $this->cache->get('folder');
- $substorageCachedData = $cache2->get('');
- Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
- $this->assertTrue($cache2->inCache('foo.txt'));
- $cachedData = $cache2->get('foo.txt');
- $this->assertEquals(3, $cachedData['size']);
- $mtime = $cachedData['mtime'];
- $cachedData = $cache2->get('');
- $this->assertNotEquals($substorageCachedData['etag'], $cachedData['etag']);
- $this->assertEquals($mtime, $cachedData['mtime']);
- $cachedData = $this->cache->get('folder');
- $this->assertNotEquals($folderCachedData['etag'], $cachedData['etag']);
- $this->assertEquals($mtime, $cachedData['mtime']);
- }
- public function testDelete() {
- $textSize = strlen("dummy file data\n");
- $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
- $rootCachedData = $this->cache->get('');
- $this->assertEquals(3 * $textSize + $imageSize, $rootCachedData['size']);
- $this->assertTrue($this->cache->inCache('foo.txt'));
- Filesystem::unlink('foo.txt');
- $this->assertFalse($this->cache->inCache('foo.txt'));
- $cachedData = $this->cache->get('');
- $this->assertEquals(2 * $textSize + $imageSize, $cachedData['size']);
- $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
- $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $cachedData['mtime']);
- $rootCachedData = $cachedData;
- Filesystem::mkdir('bar_folder');
- $this->assertTrue($this->cache->inCache('bar_folder'));
- $cachedData = $this->cache->get('');
- $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
- $rootCachedData = $cachedData;
- Filesystem::rmdir('bar_folder');
- $this->assertFalse($this->cache->inCache('bar_folder'));
- $cachedData = $this->cache->get('');
- $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
- $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $cachedData['mtime']);
- }
- public function testDeleteWithMountPoints() {
- $storage2 = new \OC\Files\Storage\Temporary(array());
- $cache2 = $storage2->getCache();
- Filesystem::mount($storage2, array(), '/' . self::$user . '/files/folder/substorage');
- Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
- $this->assertTrue($cache2->inCache('foo.txt'));
- $folderCachedData = $this->cache->get('folder');
- $substorageCachedData = $cache2->get('');
- Filesystem::unlink('folder/substorage/foo.txt');
- $this->assertFalse($cache2->inCache('foo.txt'));
- $cachedData = $cache2->get('');
- $this->assertNotEquals($substorageCachedData['etag'], $cachedData['etag']);
- $this->assertGreaterThanOrEqual($substorageCachedData['mtime'], $cachedData['mtime']);
- $cachedData = $this->cache->get('folder');
- $this->assertNotEquals($folderCachedData['etag'], $cachedData['etag']);
- $this->assertGreaterThanOrEqual($folderCachedData['mtime'], $cachedData['mtime']);
- }
- public function testRename() {
- $textSize = strlen("dummy file data\n");
- $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
- $rootCachedData = $this->cache->get('');
- $this->assertEquals(3 * $textSize + $imageSize, $rootCachedData['size']);
- $this->assertTrue($this->cache->inCache('foo.txt'));
- $fooCachedData = $this->cache->get('foo.txt');
- $this->assertFalse($this->cache->inCache('bar.txt'));
- Filesystem::rename('foo.txt', 'bar.txt');
- $this->assertFalse($this->cache->inCache('foo.txt'));
- $this->assertTrue($this->cache->inCache('bar.txt'));
- $cachedData = $this->cache->get('bar.txt');
- $this->assertEquals($fooCachedData['fileid'], $cachedData['fileid']);
- $mtime = $cachedData['mtime'];
- $cachedData = $this->cache->get('');
- $this->assertEquals(3 * $textSize + $imageSize, $cachedData['size']);
- $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
- }
- public function testRenameWithMountPoints() {
- $storage2 = new \OC\Files\Storage\Temporary(array());
- $cache2 = $storage2->getCache();
- Filesystem::mount($storage2, array(), '/' . self::$user . '/files/folder/substorage');
- Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
- $this->assertTrue($cache2->inCache('foo.txt'));
- $folderCachedData = $this->cache->get('folder');
- $substorageCachedData = $cache2->get('');
- $fooCachedData = $cache2->get('foo.txt');
- Filesystem::rename('folder/substorage/foo.txt', 'folder/substorage/bar.txt');
- $this->assertFalse($cache2->inCache('foo.txt'));
- $this->assertTrue($cache2->inCache('bar.txt'));
- $cachedData = $cache2->get('bar.txt');
- $this->assertEquals($fooCachedData['fileid'], $cachedData['fileid']);
- $mtime = $cachedData['mtime'];
- $cachedData = $cache2->get('');
- $this->assertNotEquals($substorageCachedData['etag'], $cachedData['etag']);
- // rename can cause mtime change - invalid assert
- // $this->assertEquals($mtime, $cachedData['mtime']);
- $cachedData = $this->cache->get('folder');
- $this->assertNotEquals($folderCachedData['etag'], $cachedData['etag']);
- // rename can cause mtime change - invalid assert
- // $this->assertEquals($mtime, $cachedData['mtime']);
- }
- public function testTouch() {
- $rootCachedData = $this->cache->get('');
- $fooCachedData = $this->cache->get('foo.txt');
- Filesystem::touch('foo.txt');
- $cachedData = $this->cache->get('foo.txt');
- $this->assertNotEquals($fooCachedData['etag'], $cachedData['etag']);
- $this->assertGreaterThanOrEqual($fooCachedData['mtime'], $cachedData['mtime']);
- $cachedData = $this->cache->get('');
- $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
- $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $cachedData['mtime']);
- $rootCachedData = $cachedData;
- $time = 1371006070;
- $barCachedData = $this->cache->get('folder/bar.txt');
- $folderCachedData = $this->cache->get('folder');
- Filesystem::touch('folder/bar.txt', $time);
- $cachedData = $this->cache->get('folder/bar.txt');
- $this->assertNotEquals($barCachedData['etag'], $cachedData['etag']);
- $this->assertEquals($time, $cachedData['mtime']);
- $cachedData = $this->cache->get('folder');
- $this->assertNotEquals($folderCachedData['etag'], $cachedData['etag']);
- $cachedData = $this->cache->get('');
- $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
- $this->assertEquals($time, $cachedData['mtime']);
- }
- public function testTouchWithMountPoints() {
- $storage2 = new \OC\Files\Storage\Temporary(array());
- $cache2 = $storage2->getCache();
- Filesystem::mount($storage2, array(), '/' . self::$user . '/files/folder/substorage');
- Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
- $this->assertTrue($cache2->inCache('foo.txt'));
- $folderCachedData = $this->cache->get('folder');
- $substorageCachedData = $cache2->get('');
- $fooCachedData = $cache2->get('foo.txt');
- $cachedData = $cache2->get('foo.txt');
- $time = 1371006070;
- Filesystem::touch('folder/substorage/foo.txt', $time);
- $cachedData = $cache2->get('foo.txt');
- $this->assertNotEquals($fooCachedData['etag'], $cachedData['etag']);
- $this->assertEquals($time, $cachedData['mtime']);
- $cachedData = $cache2->get('');
- $this->assertNotEquals($substorageCachedData['etag'], $cachedData['etag']);
- $cachedData = $this->cache->get('folder');
- $this->assertNotEquals($folderCachedData['etag'], $cachedData['etag']);
- $this->assertEquals($time, $cachedData['mtime']);
- }
- public function testUpdatePermissionsOnRescanOnlyForUpdatedFile() {
- $permissionsCache = $this->storage->getPermissionsCache();
- $scanner = $this->storage->getScanner();
- $scanner->scan('');
- $cache = $this->storage->getCache();
- $loggedInUser = \OC_User::getUser();
- \OC_User::setUserId(self::$user);
- FileSystem::getDirectoryContent('/');
- $past = time() - 600;
- $cache->put('', array('storage_mtime' => $past));
- $this->assertNotEquals(-1, $permissionsCache->get($cache->getId('foo.txt'), self::$user));
- $this->assertNotEquals(-1, $permissionsCache->get($cache->getId('foo.png'), self::$user));
- $permissionsCache->set($cache->getId('foo.png'), self::$user, 15);
- FileSystem::file_put_contents('/foo.txt', 'asd');
- $this->assertEquals(-1, $permissionsCache->get($cache->getId('foo.txt'), self::$user));
- $this->assertEquals(15, $permissionsCache->get($cache->getId('foo.png'), self::$user));
- FileSystem::getDirectoryContent('/');
- $this->assertEquals(15, $permissionsCache->get($cache->getId('foo.png'), self::$user));
- FileSystem::file_put_contents('/qwerty.txt', 'asd');
- FileSystem::getDirectoryContent('/');
- $this->assertEquals(15, $permissionsCache->get($cache->getId('foo.png'), self::$user));
- \OC_User::setUserId($loggedInUser);
- }
- }
|