CacheJailTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Files\Cache\Wrapper;
  8. use OC\Files\Cache\Wrapper\CacheJail;
  9. use OC\Files\Search\SearchComparison;
  10. use OC\Files\Search\SearchQuery;
  11. use OC\Files\Storage\Wrapper\Jail;
  12. use OC\User\User;
  13. use OCP\EventDispatcher\IEventDispatcher;
  14. use OCP\Files\Search\ISearchComparison;
  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(IEventDispatcher::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. public function testWatcher() {
  173. $storage = new Jail([
  174. 'storage' => $this->storage,
  175. 'root' => 'foo'
  176. ]);
  177. $storage->getScanner()->scan('');
  178. $storage->file_put_contents('bar', 'asd');
  179. $this->assertFalse($this->cache->inCache('bar'));
  180. $storage->getWatcher()->update('bar', ['mimetype' => 'text/plain']);
  181. $this->assertTrue($this->cache->inCache('bar'));
  182. }
  183. public function testWatcherAfterInnerWatcher() {
  184. $storage = new Jail([
  185. 'storage' => $this->storage,
  186. 'root' => 'foo'
  187. ]);
  188. $storage->getScanner()->scan('');
  189. $storage->file_put_contents('bar', 'asd');
  190. // let the underlying storage create it's watcher first
  191. $this->storage->getWatcher();
  192. $this->assertFalse($this->cache->inCache('bar'));
  193. $storage->getWatcher()->update('bar', ['mimetype' => 'text/plain']);
  194. $this->assertTrue($this->cache->inCache('bar'));
  195. }
  196. }