CacheJailTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * Copyright (c) 2014 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\Cache\Wrapper;
  9. use OC\Files\Cache\Wrapper\CacheJail;
  10. use OC\Files\Search\SearchComparison;
  11. use OC\Files\Search\SearchQuery;
  12. use OC\User\User;
  13. use OCP\Files\Search\ISearchComparison;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use Test\Files\Cache\CacheTest;
  16. /**
  17. * Class CacheJail
  18. *
  19. * @group DB
  20. *
  21. * @package Test\Files\Cache\Wrapper
  22. */
  23. class CacheJailTest extends CacheTest {
  24. /**
  25. * @var \OC\Files\Cache\Cache $sourceCache
  26. */
  27. protected $sourceCache;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->storage->mkdir('foo');
  31. $this->sourceCache = $this->cache;
  32. $this->cache = new \OC\Files\Cache\Wrapper\CacheJail($this->sourceCache, 'foo');
  33. }
  34. public function testSearchOutsideJail() {
  35. $this->storage->getScanner()->scan('');
  36. $file1 = 'foo/foobar';
  37. $file2 = 'folder/foobar';
  38. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];
  39. $this->sourceCache->put($file1, $data1);
  40. $this->sourceCache->put($file2, $data1);
  41. $this->assertCount(2, $this->sourceCache->search('%foobar'));
  42. $result = $this->cache->search('%foobar%');
  43. $this->assertCount(1, $result);
  44. $this->assertEquals('foobar', $result[0]['path']);
  45. $result = $this->cache->search('%foo%');
  46. $this->assertCount(2, $result);
  47. usort($result, function ($a, $b) {
  48. return $a['path'] <=> $b['path'];
  49. });
  50. $this->assertEquals('', $result[0]['path']);
  51. $this->assertEquals('foobar', $result[1]['path']);
  52. }
  53. public function testSearchMimeOutsideJail() {
  54. $this->storage->getScanner()->scan('');
  55. $file1 = 'foo/foobar';
  56. $file2 = 'folder/foobar';
  57. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];
  58. $this->sourceCache->put($file1, $data1);
  59. $this->sourceCache->put($file2, $data1);
  60. $this->assertCount(2, $this->sourceCache->searchByMime('foo/folder'));
  61. $result = $this->cache->search('%foobar%');
  62. $this->assertCount(1, $result);
  63. $this->assertEquals('foobar', $result[0]['path']);
  64. }
  65. public function testSearchQueryOutsideJail() {
  66. $this->storage->getScanner()->scan('');
  67. $file1 = 'foo/foobar';
  68. $file2 = 'folder/foobar';
  69. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];
  70. $this->sourceCache->put($file1, $data1);
  71. $this->sourceCache->put($file2, $data1);
  72. $user = new User('foo', null, $this->createMock(EventDispatcherInterface::class));
  73. $query = new SearchQuery(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'name', 'foobar'), 10, 0, [], $user);
  74. $result = $this->cache->searchQuery($query);
  75. $this->assertCount(1, $result);
  76. $this->assertEquals('foobar', $result[0]['path']);
  77. $query = new SearchQuery(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'name', 'foo'), 10, 0, [], $user);
  78. $result = $this->cache->searchQuery($query);
  79. $this->assertCount(1, $result);
  80. $this->assertEquals('', $result[0]['path']);
  81. }
  82. public function testClearKeepEntriesOutsideJail() {
  83. $file1 = 'foo/foobar';
  84. $file2 = 'foo/foobar/asd';
  85. $file3 = 'folder/foobar';
  86. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  87. $this->sourceCache->put('foo', $data1);
  88. $this->sourceCache->put($file1, $data1);
  89. $this->sourceCache->put($file2, $data1);
  90. $this->sourceCache->put($file3, $data1);
  91. $this->cache->clear();
  92. $this->assertFalse($this->cache->inCache('foobar'));
  93. $this->assertTrue($this->sourceCache->inCache('folder/foobar'));
  94. }
  95. public function testGetById() {
  96. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  97. $id = $this->sourceCache->put('foo/bar', $data1);
  98. // path from jailed foo of foo/bar is bar
  99. $path = $this->cache->getPathById($id);
  100. $this->assertEquals('bar', $path);
  101. // path from jailed '' of foo/bar is foo/bar
  102. $this->cache = new \OC\Files\Cache\Wrapper\CacheJail($this->sourceCache, '');
  103. $path = $this->cache->getPathById($id);
  104. $this->assertEquals('foo/bar', $path);
  105. }
  106. public function testGetIncomplete() {
  107. //not supported
  108. $this->addToAssertionCount(1);
  109. }
  110. public function testMoveFromJail() {
  111. $folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  112. $this->sourceCache->put('source', $folderData);
  113. $this->sourceCache->put('source/foo', $folderData);
  114. $this->sourceCache->put('source/foo/bar', $folderData);
  115. $this->sourceCache->put('target', $folderData);
  116. $jail = new CacheJail($this->sourceCache, 'source');
  117. $this->sourceCache->moveFromCache($jail, 'foo', 'target/foo');
  118. $this->assertTrue($this->sourceCache->inCache('target/foo'));
  119. $this->assertTrue($this->sourceCache->inCache('target/foo/bar'));
  120. }
  121. public function testMoveToJail() {
  122. $folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  123. $this->sourceCache->put('source', $folderData);
  124. $this->sourceCache->put('source/foo', $folderData);
  125. $this->sourceCache->put('source/foo/bar', $folderData);
  126. $this->sourceCache->put('target', $folderData);
  127. $jail = new CacheJail($this->sourceCache, 'target');
  128. $jail->moveFromCache($this->sourceCache, 'source/foo', 'foo');
  129. $this->assertTrue($this->sourceCache->inCache('target/foo'));
  130. $this->assertTrue($this->sourceCache->inCache('target/foo/bar'));
  131. }
  132. public function testMoveBetweenJail() {
  133. $folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  134. $this->sourceCache->put('source', $folderData);
  135. $this->sourceCache->put('source/foo', $folderData);
  136. $this->sourceCache->put('source/foo/bar', $folderData);
  137. $this->sourceCache->put('target', $folderData);
  138. $jail = new CacheJail($this->sourceCache, 'target');
  139. $sourceJail = new CacheJail($this->sourceCache, 'source');
  140. $jail->moveFromCache($sourceJail, 'foo', 'foo');
  141. $this->assertTrue($this->sourceCache->inCache('target/foo'));
  142. $this->assertTrue($this->sourceCache->inCache('target/foo/bar'));
  143. }
  144. public function testSearchNested() {
  145. $this->storage->getScanner()->scan('');
  146. $file1 = 'foo';
  147. $file2 = 'foo/bar';
  148. $file3 = 'foo/bar/asd';
  149. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];
  150. $this->sourceCache->put($file1, $data1);
  151. $this->sourceCache->put($file2, $data1);
  152. $this->sourceCache->put($file3, $data1);
  153. $nested = new \OC\Files\Cache\Wrapper\CacheJail($this->cache, 'bar');
  154. $result = $nested->search('%asd%');
  155. $this->assertCount(1, $result);
  156. $this->assertEquals('asd', $result[0]['path']);
  157. }
  158. public function testRootJail() {
  159. $this->storage->getScanner()->scan('');
  160. $file1 = 'foo';
  161. $file2 = 'foo/bar';
  162. $file3 = 'foo/bar/asd';
  163. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];
  164. $this->sourceCache->put($file1, $data1);
  165. $this->sourceCache->put($file2, $data1);
  166. $this->sourceCache->put($file3, $data1);
  167. $nested = new \OC\Files\Cache\Wrapper\CacheJail($this->sourceCache, '');
  168. $result = $nested->search('%asd%');
  169. $this->assertCount(1, $result);
  170. $this->assertEquals('foo/bar/asd', $result[0]['path']);
  171. }
  172. }