Skip to content

Commit

Permalink
test: put parent items into cache in tests
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Sep 18, 2024
1 parent 3c0c81b commit 9ece270
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 50 deletions.
2 changes: 2 additions & 0 deletions apps/files_sharing/tests/External/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OCP\Contacts\IManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Federation\ICloudIdManager;
use OCP\Files\Cache\ICacheEntry;
use OCP\ICacheFactory;
use OCP\IURLGenerator;
use OCP\IUserManager;
Expand Down Expand Up @@ -74,6 +75,7 @@ protected function setUp(): void {
$this->storage,
$this->cloudIdManager->getCloudId($this->remoteUser, 'http://example.com/owncloud')
);
$this->cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]);
$this->cache->put(
'test.txt',
[
Expand Down
40 changes: 19 additions & 21 deletions tests/lib/Files/Cache/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchQuery;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\Search\ISearchComparison;
use OCP\IDBConnection;
use OCP\IUser;
Expand Down Expand Up @@ -148,7 +149,7 @@ public function testFolder($folder): void {
}
$file2 = $folder . '/bar';
$file3 = $folder . '/foo';
$data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
$data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE];
$fileData = [];
$fileData['bar'] = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
$fileData['foo'] = ['size' => 20, 'mtime' => 25, 'mimetype' => 'foo/file'];
Expand Down Expand Up @@ -188,7 +189,7 @@ public function testFolder($folder): void {
}

public function testRemoveRecursive(): void {
$folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
$folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE];
$fileData = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'text/plain'];
$folders = ['folder', 'folder/subfolder', 'folder/sub2', 'folder/sub2/sub3'];
$files = ['folder/foo.txt', 'folder/bar.txt', 'folder/subfolder/asd.txt', 'folder/sub2/qwerty.txt', 'folder/sub2/sub3/foo.txt'];
Expand Down Expand Up @@ -224,7 +225,7 @@ public function testEncryptedFolder(): void {
$file1 = 'folder';
$file2 = 'folder/bar';
$file3 = 'folder/foo';
$data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
$data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE];
$fileData = [];
$fileData['bar'] = ['size' => 1000, 'encrypted' => 1, 'mtime' => 20, 'mimetype' => 'foo/file'];
$fileData['foo'] = ['size' => 20, 'encrypted' => 1, 'mtime' => 25, 'mimetype' => 'foo/file'];
Expand Down Expand Up @@ -267,11 +268,10 @@ public function testRootFolderSizeForNonHomeStorage(): void {
$dir1 = 'knownsize';
$dir2 = 'unknownsize';
$fileData = [];
$fileData[''] = ['size' => -1, 'mtime' => 20, 'mimetype' => 'httpd/unix-directory'];
$fileData[$dir1] = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'httpd/unix-directory'];
$fileData[$dir2] = ['size' => -1, 'mtime' => 25, 'mimetype' => 'httpd/unix-directory'];
$fileData[''] = ['size' => -1, 'mtime' => 20, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE];
$fileData[$dir1] = ['size' => 1000, 'mtime' => 20, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE];
$fileData[$dir2] = ['size' => -1, 'mtime' => 25, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE];

$this->cache->put('', $fileData['']);
$this->cache->put($dir1, $fileData[$dir1]);
$this->cache->put($dir2, $fileData[$dir2]);

Expand Down Expand Up @@ -338,7 +338,7 @@ public function testSearch(): void {
$this->assertEquals(1, count($this->cache->search('foo')));
$this->assertEquals(1, count($this->cache->search('%folder%')));
$this->assertEquals(1, count($this->cache->search('folder%')));
$this->assertEquals(3, count($this->cache->search('%')));
$this->assertEquals(4, count($this->cache->search('%')));

// case insensitive search should match the same files
$this->assertEquals(2, count($this->cache->search('%Foo%')));
Expand Down Expand Up @@ -450,7 +450,7 @@ public function movePathProvider() {
*/
public function testMove($sourceFolder, $targetFolder, $children): void {
$data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/bar'];
$folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
$folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE];

// create folders
foreach ([$sourceFolder, $targetFolder] as $current) {
Expand Down Expand Up @@ -485,7 +485,7 @@ public function testMove($sourceFolder, $targetFolder, $children): void {

public function testMoveFromCache(): void {
$data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/bar'];
$folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
$folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE];

$this->cache2->put('folder', $folderData);
$this->cache2->put('folder/sub', $data);
Expand Down Expand Up @@ -554,6 +554,7 @@ public function testStorageMTime(): void {
public function testLongId(): void {
$storage = new LongId([]);
$cache = $storage->getCache();
$cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]);
$storageId = $storage->getId();
$data = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
$id = $cache->put('foo', $data);
Expand Down Expand Up @@ -582,7 +583,7 @@ public function testWithoutNormalizer(): void {
->method('normalize')
->willReturnArgument(0);

$data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
$data = ['size' => 100, 'mtime' => 50, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE];

// put root folder
$this->assertFalse($cacheMock->get('folder'));
Expand Down Expand Up @@ -621,7 +622,7 @@ public function testWithNormalizer(): void {
// folder name "Schön" with U+0308 (un-normalized)
$folderWith0308 = "\x53\x63\x68\x6f\xcc\x88\x6e";

$data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
$data = ['size' => 100, 'mtime' => 50, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE];

// put root folder
$this->assertFalse($this->cache->get('folder'));
Expand Down Expand Up @@ -660,12 +661,8 @@ public function bogusPathNamesProvider() {
* @dataProvider bogusPathNamesProvider
*/
public function testBogusPaths($bogusPath, $fixedBogusPath): void {
$data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];

// put root folder
$this->assertFalse($this->cache->get(''));
$parentId = $this->cache->put('', $data);
$this->assertGreaterThan(0, $parentId);
$data = ['size' => 100, 'mtime' => 50, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE];
$parentId = $this->cache->getId('');

$this->assertGreaterThan(0, $this->cache->put($bogusPath, $data));

Expand Down Expand Up @@ -719,7 +716,7 @@ public function testEscaping($name): void {
$this->assertTrue($this->cache->inCache($name . 'asd'));
$this->cache->remove($name . 'asd');
$this->assertFalse($this->cache->inCache($name . 'asd'));
$folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
$folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE];
$this->cache->put($name, $folderData);
$this->cache->put('other', $folderData);
$childs = ['asd', 'bar', 'foo', 'sub/folder'];
Expand All @@ -743,8 +740,7 @@ public function testEscaping($name): void {
}

public function testExtended(): void {
$folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
$this->cache->put('', $folderData);
$folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE];

$data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain', 'creation_time' => 20];
$id1 = $this->cache->put('foo1', $data);
Expand Down Expand Up @@ -827,5 +823,7 @@ protected function setUp(): void {
$this->storage2 = new \OC\Files\Storage\Temporary([]);
$this->cache = new \OC\Files\Cache\Cache($this->storage);
$this->cache2 = new \OC\Files\Cache\Cache($this->storage2);
$this->cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]);
$this->cache2->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]);
}
}
4 changes: 4 additions & 0 deletions tests/lib/Files/Cache/MoveFromCacheTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Test\Files\Cache;

use OC\Files\Cache\MoveFromCacheTrait;
use OCP\Files\Cache\ICacheEntry;

class FallBackCrossCacheMoveCache extends \OC\Files\Cache\Cache {
use MoveFromCacheTrait;
Expand All @@ -26,5 +27,8 @@ protected function setUp(): void {
$this->storage2 = new \OC\Files\Storage\Temporary([]);
$this->cache = new FallBackCrossCacheMoveCache($this->storage);
$this->cache2 = new FallBackCrossCacheMoveCache($this->storage2);

$this->cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]);
$this->cache2->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]);
}
}
61 changes: 33 additions & 28 deletions tests/lib/Files/Cache/Wrapper/CacheJailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OC\Files\Storage\Wrapper\Jail;
use OC\User\User;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\Search\ISearchComparison;
use Test\Files\Cache\CacheTest;

Expand All @@ -31,17 +32,19 @@ class CacheJailTest extends CacheTest {

protected function setUp(): void {
parent::setUp();
$this->storage->mkdir('foo');
$this->storage->mkdir('jail');
$this->sourceCache = $this->cache;
$this->cache = new \OC\Files\Cache\Wrapper\CacheJail($this->sourceCache, 'foo');
$this->cache = new \OC\Files\Cache\Wrapper\CacheJail($this->sourceCache, 'jail');
$this->cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]);
}

public function testSearchOutsideJail(): void {
$this->storage->getScanner()->scan('');
$file1 = 'foo/foobar';
$file1 = 'jail/foobar';
$file2 = 'folder/foobar';
$data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];

$this->sourceCache->insert('folder', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]);
$this->sourceCache->put($file1, $data1);
$this->sourceCache->put($file2, $data1);

Expand All @@ -52,20 +55,20 @@ public function testSearchOutsideJail(): void {
$this->assertEquals('foobar', $result[0]['path']);

$result = $this->cache->search('%foo%');
$this->assertCount(2, $result);
$this->assertCount(1, $result);
usort($result, function ($a, $b) {
return $a['path'] <=> $b['path'];
});
$this->assertEquals('', $result[0]['path']);
$this->assertEquals('foobar', $result[1]['path']);
$this->assertEquals('foobar', $result[0]['path']);
}

public function testSearchMimeOutsideJail(): void {
$this->storage->getScanner()->scan('');
$file1 = 'foo/foobar';
$file1 = 'jail/foobar';
$file2 = 'folder/foobar';
$data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];

$this->sourceCache->insert('folder', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]);
$this->sourceCache->put($file1, $data1);
$this->sourceCache->put($file2, $data1);

Expand All @@ -78,10 +81,12 @@ public function testSearchMimeOutsideJail(): void {

public function testSearchQueryOutsideJail(): void {
$this->storage->getScanner()->scan('');
$file1 = 'foo/foobar';
$file1 = 'jail/foobar';
$file2 = 'folder/foobar';
$data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];


$this->sourceCache->insert('folder', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]);
$this->sourceCache->put($file1, $data1);
$this->sourceCache->put($file2, $data1);

Expand All @@ -92,19 +97,19 @@ public function testSearchQueryOutsideJail(): void {
$this->assertCount(1, $result);
$this->assertEquals('foobar', $result[0]['path']);

$query = new SearchQuery(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'name', 'foo'), 10, 0, [], $user);
$query = new SearchQuery(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'name', 'jail'), 10, 0, [], $user);
$result = $this->cache->searchQuery($query);
$this->assertCount(1, $result);
$this->assertEquals('', $result[0]['path']);
}

public function testClearKeepEntriesOutsideJail(): void {
$file1 = 'foo/foobar';
$file2 = 'foo/foobar/asd';
$file1 = 'jail/foobar';
$file2 = 'jail/foobar/asd';
$file3 = 'folder/foobar';
$data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
$data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE];

$this->sourceCache->put('foo', $data1);
$this->sourceCache->put('folder', $data1);
$this->sourceCache->put($file1, $data1);
$this->sourceCache->put($file2, $data1);
$this->sourceCache->put($file3, $data1);
Expand All @@ -116,8 +121,8 @@ public function testClearKeepEntriesOutsideJail(): void {
}

public function testGetById(): void {
$data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
$id = $this->sourceCache->put('foo/bar', $data1);
$data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE];
$id = $this->sourceCache->put('jail/bar', $data1);

// path from jailed foo of foo/bar is bar
$path = $this->cache->getPathById($id);
Expand All @@ -126,7 +131,7 @@ public function testGetById(): void {
// path from jailed '' of foo/bar is foo/bar
$this->cache = new \OC\Files\Cache\Wrapper\CacheJail($this->sourceCache, '');
$path = $this->cache->getPathById($id);
$this->assertEquals('foo/bar', $path);
$this->assertEquals('jail/bar', $path);
}

public function testGetIncomplete(): void {
Expand All @@ -135,7 +140,7 @@ public function testGetIncomplete(): void {
}

public function testMoveFromJail(): void {
$folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
$folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE];

$this->sourceCache->put('source', $folderData);
$this->sourceCache->put('source/foo', $folderData);
Expand All @@ -151,7 +156,7 @@ public function testMoveFromJail(): void {
}

public function testMoveToJail(): void {
$folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
$folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE];

$this->sourceCache->put('source', $folderData);
$this->sourceCache->put('source/foo', $folderData);
Expand All @@ -167,7 +172,7 @@ public function testMoveToJail(): void {
}

public function testMoveBetweenJail(): void {
$folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
$folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE];

$this->sourceCache->put('source', $folderData);
$this->sourceCache->put('source/foo', $folderData);
Expand All @@ -185,9 +190,9 @@ public function testMoveBetweenJail(): void {

public function testSearchNested(): void {
$this->storage->getScanner()->scan('');
$file1 = 'foo';
$file2 = 'foo/bar';
$file3 = 'foo/bar/asd';
$file1 = 'jail';
$file2 = 'jail/bar';
$file3 = 'jail/bar/asd';
$data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];

$this->sourceCache->put($file1, $data1);
Expand All @@ -203,9 +208,9 @@ public function testSearchNested(): void {

public function testRootJail(): void {
$this->storage->getScanner()->scan('');
$file1 = 'foo';
$file2 = 'foo/bar';
$file3 = 'foo/bar/asd';
$file1 = 'jail';
$file2 = 'jail/bar';
$file3 = 'jail/bar/asd';
$data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];

$this->sourceCache->put($file1, $data1);
Expand All @@ -216,13 +221,13 @@ public function testRootJail(): void {

$result = $nested->search('%asd%');
$this->assertCount(1, $result);
$this->assertEquals('foo/bar/asd', $result[0]['path']);
$this->assertEquals('jail/bar/asd', $result[0]['path']);
}

public function testWatcher(): void {
$storage = new Jail([
'storage' => $this->storage,
'root' => 'foo'
'root' => 'jail'
]);
$storage->getScanner()->scan('');
$storage->file_put_contents('bar', 'asd');
Expand All @@ -235,7 +240,7 @@ public function testWatcher(): void {
public function testWatcherAfterInnerWatcher(): void {
$storage = new Jail([
'storage' => $this->storage,
'root' => 'foo'
'root' => 'jail'
]);
$storage->getScanner()->scan('');
$storage->file_put_contents('bar', 'asd');
Expand Down
Loading

0 comments on commit 9ece270

Please sign in to comment.