123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?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;
- class Scanner extends \PHPUnit_Framework_TestCase {
- /**
- * @var \OC\Files\Storage\Storage $storage
- */
- private $storage;
- /**
- * @var \OC\Files\Cache\Scanner $scanner
- */
- private $scanner;
- /**
- * @var \OC\Files\Cache\Cache $cache
- */
- private $cache;
- function setUp() {
- $this->storage = new \OC\Files\Storage\Temporary(array());
- $this->scanner = new \OC\Files\Cache\Scanner($this->storage);
- $this->cache = new \OC\Files\Cache\Cache($this->storage);
- }
- function tearDown() {
- if ($this->cache) {
- $ids = $this->cache->getAll();
- $permissionsCache = $this->storage->getPermissionsCache();
- $permissionsCache->removeMultiple($ids, \OC_User::getUser());
- $this->cache->clear();
- }
- }
- function testFile() {
- $data = "dummy file data\n";
- $this->storage->file_put_contents('foo.txt', $data);
- $this->scanner->scanFile('foo.txt');
- $this->assertEquals($this->cache->inCache('foo.txt'), true);
- $cachedData = $this->cache->get('foo.txt');
- $this->assertEquals($cachedData['size'], strlen($data));
- $this->assertEquals($cachedData['mimetype'], 'text/plain');
- $this->assertNotEquals($cachedData['parent'], -1); //parent folders should be scanned automatically
- $data = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
- $this->storage->file_put_contents('foo.png', $data);
- $this->scanner->scanFile('foo.png');
- $this->assertEquals($this->cache->inCache('foo.png'), true);
- $cachedData = $this->cache->get('foo.png');
- $this->assertEquals($cachedData['size'], strlen($data));
- $this->assertEquals($cachedData['mimetype'], 'image/png');
- }
- private function fillTestFolders() {
- $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);
- }
- function testFolder() {
- $this->fillTestFolders();
- $this->scanner->scan('');
- $this->assertEquals($this->cache->inCache(''), true);
- $this->assertEquals($this->cache->inCache('foo.txt'), true);
- $this->assertEquals($this->cache->inCache('foo.png'), true);
- $this->assertEquals($this->cache->inCache('folder'), true);
- $this->assertEquals($this->cache->inCache('folder/bar.txt'), true);
- $cachedDataText = $this->cache->get('foo.txt');
- $cachedDataText2 = $this->cache->get('foo.txt');
- $cachedDataImage = $this->cache->get('foo.png');
- $cachedDataFolder = $this->cache->get('');
- $cachedDataFolder2 = $this->cache->get('folder');
- $this->assertEquals($cachedDataImage['parent'], $cachedDataText['parent']);
- $this->assertEquals($cachedDataFolder['fileid'], $cachedDataImage['parent']);
- $this->assertEquals($cachedDataFolder['size'], $cachedDataImage['size'] + $cachedDataText['size'] + $cachedDataText2['size']);
- $this->assertEquals($cachedDataFolder2['size'], $cachedDataText2['size']);
- }
- function testShallow() {
- $this->fillTestFolders();
- $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
- $this->assertEquals($this->cache->inCache(''), true);
- $this->assertEquals($this->cache->inCache('foo.txt'), true);
- $this->assertEquals($this->cache->inCache('foo.png'), true);
- $this->assertEquals($this->cache->inCache('folder'), true);
- $this->assertEquals($this->cache->inCache('folder/bar.txt'), false);
- $cachedDataFolder = $this->cache->get('');
- $cachedDataFolder2 = $this->cache->get('folder');
- $this->assertEquals(-1, $cachedDataFolder['size']);
- $this->assertEquals(-1, $cachedDataFolder2['size']);
- $this->scanner->scan('folder', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
- $cachedDataFolder2 = $this->cache->get('folder');
- $this->assertNotEquals($cachedDataFolder2['size'], -1);
- $this->cache->correctFolderSize('folder');
- $cachedDataFolder = $this->cache->get('');
- $this->assertNotEquals($cachedDataFolder['size'], -1);
- }
- function testBackgroundScan() {
- $this->fillTestFolders();
- $this->storage->mkdir('folder2');
- $this->storage->file_put_contents('folder2/bar.txt', 'foobar');
- $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
- $this->assertFalse($this->cache->inCache('folder/bar.txt'));
- $this->assertFalse($this->cache->inCache('folder/2bar.txt'));
- $cachedData = $this->cache->get('');
- $this->assertEquals(-1, $cachedData['size']);
- $this->scanner->backgroundScan();
- $this->assertTrue($this->cache->inCache('folder/bar.txt'));
- $this->assertTrue($this->cache->inCache('folder/bar.txt'));
- $cachedData = $this->cache->get('');
- $this->assertnotEquals(-1, $cachedData['size']);
- $this->assertFalse($this->cache->getIncomplete());
- }
- public function testReuseExisting() {
- $this->fillTestFolders();
- $this->scanner->scan('');
- $oldData = $this->cache->get('');
- $this->storage->unlink('folder/bar.txt');
- $this->cache->put('folder', array('mtime' => $this->storage->filemtime('folder')));
- $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_SIZE);
- $newData = $this->cache->get('');
- $this->assertNotEquals($oldData['etag'], $newData['etag']);
- $this->assertEquals($oldData['size'], $newData['size']);
- $oldData = $newData;
- $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG);
- $newData = $this->cache->get('');
- $this->assertEquals($oldData['etag'], $newData['etag']);
- $this->assertEquals(-1, $newData['size']);
- $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE);
- $oldData = $this->cache->get('');
- $this->assertNotEquals(-1, $oldData['size']);
- $this->scanner->scanFile('', \OC\Files\Cache\Scanner::REUSE_ETAG + \OC\Files\Cache\Scanner::REUSE_SIZE);
- $newData = $this->cache->get('');
- $this->assertEquals($oldData['etag'], $newData['etag']);
- $this->assertEquals($oldData['size'], $newData['size']);
- $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG + \OC\Files\Cache\Scanner::REUSE_SIZE);
- $newData = $this->cache->get('');
- $this->assertEquals($oldData['etag'], $newData['etag']);
- $this->assertEquals($oldData['size'], $newData['size']);
- $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG + \OC\Files\Cache\Scanner::REUSE_SIZE);
- $newData = $this->cache->get('');
- $this->assertEquals($oldData['etag'], $newData['etag']);
- $this->assertEquals($oldData['size'], $newData['size']);
- }
- public function testRemovedFile() {
- $this->fillTestFolders();
- $this->scanner->scan('');
- $this->assertTrue($this->cache->inCache('foo.txt'));
- $this->storage->unlink('foo.txt');
- $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
- $this->assertFalse($this->cache->inCache('foo.txt'));
- }
- public function testRemovedFolder() {
- $this->fillTestFolders();
- $this->scanner->scan('');
- $this->assertTrue($this->cache->inCache('folder/bar.txt'));
- $this->storage->unlink('/folder');
- $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
- $this->assertFalse($this->cache->inCache('folder'));
- $this->assertFalse($this->cache->inCache('folder/bar.txt'));
- }
- public function testScanRemovedFile(){
- $this->fillTestFolders();
- $this->scanner->scan('');
- $this->assertTrue($this->cache->inCache('folder/bar.txt'));
- $this->storage->unlink('folder/bar.txt');
- $this->scanner->scanFile('folder/bar.txt');
- $this->assertFalse($this->cache->inCache('folder/bar.txt'));
- }
- public function testETagRecreation() {
- $this->fillTestFolders();
- $this->scanner->scan('folder/bar.txt');
- // manipulate etag to simulate an empty etag
- $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG);
- $data0 = $this->cache->get('folder/bar.txt');
- $data1 = $this->cache->get('folder');
- $data2 = $this->cache->get('');
- $data0['etag'] = '';
- $this->cache->put('folder/bar.txt', $data0);
- // rescan
- $this->scanner->scan('folder/bar.txt', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG);
- // verify cache content
- $newData0 = $this->cache->get('folder/bar.txt');
- $newData1 = $this->cache->get('folder');
- $newData2 = $this->cache->get('');
- $this->assertNotEmpty($newData0['etag']);
- $this->assertNotEquals($data1['etag'], $newData1['etag']);
- $this->assertNotEquals($data2['etag'], $newData2['etag']);
- }
- }
|