1
0

CacheJailTest.php 8.3 KB

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