updater.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. /**
  3. * Copyright (c) 2012 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;
  9. use \OC\Files\Filesystem as Filesystem;
  10. use OC\Files\Storage\Temporary;
  11. class Updater extends \PHPUnit_Framework_TestCase {
  12. /**
  13. * @var \OC\Files\Storage\Storage $storage
  14. */
  15. private $storage;
  16. /**
  17. * @var \OC\Files\Cache\Scanner $scanner
  18. */
  19. private $scanner;
  20. private $stateFilesEncryption;
  21. /**
  22. * @var \OC\Files\Cache\Cache $cache
  23. */
  24. private $cache;
  25. private static $user;
  26. public function setUp() {
  27. // remember files_encryption state
  28. $this->stateFilesEncryption = \OC_App::isEnabled('files_encryption');
  29. // we want to tests with the encryption app disabled
  30. \OC_App::disable('files_encryption');
  31. $this->storage = new \OC\Files\Storage\Temporary(array());
  32. $textData = "dummy file data\n";
  33. $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
  34. $this->storage->mkdir('folder');
  35. $this->storage->file_put_contents('foo.txt', $textData);
  36. $this->storage->file_put_contents('foo.png', $imgData);
  37. $this->storage->file_put_contents('folder/bar.txt', $textData);
  38. $this->storage->file_put_contents('folder/bar2.txt', $textData);
  39. $this->scanner = $this->storage->getScanner();
  40. $this->scanner->scan('');
  41. $this->cache = $this->storage->getCache();
  42. \OC\Files\Filesystem::tearDown();
  43. if (!self::$user) {
  44. self::$user = uniqid();
  45. }
  46. \OC_User::createUser(self::$user, 'password');
  47. \OC_User::setUserId(self::$user);
  48. \OC\Files\Filesystem::init(self::$user, '/' . self::$user . '/files');
  49. Filesystem::clearMounts();
  50. Filesystem::mount($this->storage, array(), '/' . self::$user . '/files');
  51. \OC_Hook::clear('OC_Filesystem');
  52. \OC_Hook::connect('OC_Filesystem', 'post_write', '\OC\Files\Cache\Updater', 'writeHook');
  53. \OC_Hook::connect('OC_Filesystem', 'post_delete', '\OC\Files\Cache\Updater', 'deleteHook');
  54. \OC_Hook::connect('OC_Filesystem', 'post_rename', '\OC\Files\Cache\Updater', 'renameHook');
  55. \OC_Hook::connect('OC_Filesystem', 'post_touch', '\OC\Files\Cache\Updater', 'touchHook');
  56. }
  57. public function tearDown() {
  58. if ($this->cache) {
  59. $this->cache->clear();
  60. }
  61. $result = \OC_User::deleteUser(self::$user);
  62. $this->assertTrue($result);
  63. Filesystem::tearDown();
  64. // reset app files_encryption
  65. if ($this->stateFilesEncryption) {
  66. \OC_App::enable('files_encryption');
  67. }
  68. }
  69. public function testWrite() {
  70. $textSize = strlen("dummy file data\n");
  71. $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
  72. $this->cache->put('foo.txt', array('mtime' => 100));
  73. $rootCachedData = $this->cache->get('');
  74. $this->assertEquals(3 * $textSize + $imageSize, $rootCachedData['size']);
  75. $fooCachedData = $this->cache->get('foo.txt');
  76. Filesystem::file_put_contents('foo.txt', 'asd');
  77. $cachedData = $this->cache->get('foo.txt');
  78. $this->assertEquals(3, $cachedData['size']);
  79. $this->assertNotEquals($fooCachedData['etag'], $cachedData['etag']);
  80. $cachedData = $this->cache->get('');
  81. $this->assertEquals(2 * $textSize + $imageSize + 3, $cachedData['size']);
  82. $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
  83. $rootCachedData = $cachedData;
  84. $this->assertFalse($this->cache->inCache('bar.txt'));
  85. Filesystem::file_put_contents('bar.txt', 'asd');
  86. $this->assertTrue($this->cache->inCache('bar.txt'));
  87. $cachedData = $this->cache->get('bar.txt');
  88. $this->assertEquals(3, $cachedData['size']);
  89. $mtime = $cachedData['mtime'];
  90. $cachedData = $this->cache->get('');
  91. $this->assertEquals(2 * $textSize + $imageSize + 2 * 3, $cachedData['size']);
  92. $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
  93. $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $mtime);
  94. }
  95. public function testWriteWithMountPoints() {
  96. $storage2 = new \OC\Files\Storage\Temporary(array());
  97. $cache2 = $storage2->getCache();
  98. Filesystem::mount($storage2, array(), '/' . self::$user . '/files/folder/substorage');
  99. $folderCachedData = $this->cache->get('folder');
  100. $substorageCachedData = $cache2->get('');
  101. Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
  102. $this->assertTrue($cache2->inCache('foo.txt'));
  103. $cachedData = $cache2->get('foo.txt');
  104. $this->assertEquals(3, $cachedData['size']);
  105. $mtime = $cachedData['mtime'];
  106. $cachedData = $cache2->get('');
  107. $this->assertNotEquals($substorageCachedData['etag'], $cachedData['etag']);
  108. $this->assertEquals($mtime, $cachedData['mtime']);
  109. $cachedData = $this->cache->get('folder');
  110. $this->assertNotEquals($folderCachedData['etag'], $cachedData['etag']);
  111. $this->assertEquals($mtime, $cachedData['mtime']);
  112. }
  113. public function testDelete() {
  114. $textSize = strlen("dummy file data\n");
  115. $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
  116. $rootCachedData = $this->cache->get('');
  117. $this->assertEquals(3 * $textSize + $imageSize, $rootCachedData['size']);
  118. $this->assertTrue($this->cache->inCache('foo.txt'));
  119. Filesystem::unlink('foo.txt');
  120. $this->assertFalse($this->cache->inCache('foo.txt'));
  121. $cachedData = $this->cache->get('');
  122. $this->assertEquals(2 * $textSize + $imageSize, $cachedData['size']);
  123. $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
  124. $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $cachedData['mtime']);
  125. $rootCachedData = $cachedData;
  126. Filesystem::mkdir('bar_folder');
  127. $this->assertTrue($this->cache->inCache('bar_folder'));
  128. $cachedData = $this->cache->get('');
  129. $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
  130. $rootCachedData = $cachedData;
  131. Filesystem::rmdir('bar_folder');
  132. $this->assertFalse($this->cache->inCache('bar_folder'));
  133. $cachedData = $this->cache->get('');
  134. $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
  135. $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $cachedData['mtime']);
  136. }
  137. public function testDeleteWithMountPoints() {
  138. $storage2 = new \OC\Files\Storage\Temporary(array());
  139. $cache2 = $storage2->getCache();
  140. Filesystem::mount($storage2, array(), '/' . self::$user . '/files/folder/substorage');
  141. Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
  142. $this->assertTrue($cache2->inCache('foo.txt'));
  143. $folderCachedData = $this->cache->get('folder');
  144. $substorageCachedData = $cache2->get('');
  145. Filesystem::unlink('folder/substorage/foo.txt');
  146. $this->assertFalse($cache2->inCache('foo.txt'));
  147. $cachedData = $cache2->get('');
  148. $this->assertNotEquals($substorageCachedData['etag'], $cachedData['etag']);
  149. $this->assertGreaterThanOrEqual($substorageCachedData['mtime'], $cachedData['mtime']);
  150. $cachedData = $this->cache->get('folder');
  151. $this->assertNotEquals($folderCachedData['etag'], $cachedData['etag']);
  152. $this->assertGreaterThanOrEqual($folderCachedData['mtime'], $cachedData['mtime']);
  153. }
  154. public function testRename() {
  155. $textSize = strlen("dummy file data\n");
  156. $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
  157. $rootCachedData = $this->cache->get('');
  158. $this->assertEquals(3 * $textSize + $imageSize, $rootCachedData['size']);
  159. $this->assertTrue($this->cache->inCache('foo.txt'));
  160. $fooCachedData = $this->cache->get('foo.txt');
  161. $this->assertFalse($this->cache->inCache('bar.txt'));
  162. Filesystem::rename('foo.txt', 'bar.txt');
  163. $this->assertFalse($this->cache->inCache('foo.txt'));
  164. $this->assertTrue($this->cache->inCache('bar.txt'));
  165. $cachedData = $this->cache->get('bar.txt');
  166. $this->assertEquals($fooCachedData['fileid'], $cachedData['fileid']);
  167. $mtime = $cachedData['mtime'];
  168. $cachedData = $this->cache->get('');
  169. $this->assertEquals(3 * $textSize + $imageSize, $cachedData['size']);
  170. $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
  171. }
  172. public function testRenameWithMountPoints() {
  173. $storage2 = new \OC\Files\Storage\Temporary(array());
  174. $cache2 = $storage2->getCache();
  175. Filesystem::mount($storage2, array(), '/' . self::$user . '/files/folder/substorage');
  176. Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
  177. $this->assertTrue($cache2->inCache('foo.txt'));
  178. $folderCachedData = $this->cache->get('folder');
  179. $substorageCachedData = $cache2->get('');
  180. $fooCachedData = $cache2->get('foo.txt');
  181. Filesystem::rename('folder/substorage/foo.txt', 'folder/substorage/bar.txt');
  182. $this->assertFalse($cache2->inCache('foo.txt'));
  183. $this->assertTrue($cache2->inCache('bar.txt'));
  184. $cachedData = $cache2->get('bar.txt');
  185. $this->assertEquals($fooCachedData['fileid'], $cachedData['fileid']);
  186. $mtime = $cachedData['mtime'];
  187. $cachedData = $cache2->get('');
  188. $this->assertNotEquals($substorageCachedData['etag'], $cachedData['etag']);
  189. // rename can cause mtime change - invalid assert
  190. // $this->assertEquals($mtime, $cachedData['mtime']);
  191. $cachedData = $this->cache->get('folder');
  192. $this->assertNotEquals($folderCachedData['etag'], $cachedData['etag']);
  193. // rename can cause mtime change - invalid assert
  194. // $this->assertEquals($mtime, $cachedData['mtime']);
  195. }
  196. public function testTouch() {
  197. $rootCachedData = $this->cache->get('');
  198. $fooCachedData = $this->cache->get('foo.txt');
  199. Filesystem::touch('foo.txt');
  200. $cachedData = $this->cache->get('foo.txt');
  201. $this->assertNotEquals($fooCachedData['etag'], $cachedData['etag']);
  202. $this->assertGreaterThanOrEqual($fooCachedData['mtime'], $cachedData['mtime']);
  203. $cachedData = $this->cache->get('');
  204. $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
  205. $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $cachedData['mtime']);
  206. $rootCachedData = $cachedData;
  207. $time = 1371006070;
  208. $barCachedData = $this->cache->get('folder/bar.txt');
  209. $folderCachedData = $this->cache->get('folder');
  210. Filesystem::touch('folder/bar.txt', $time);
  211. $cachedData = $this->cache->get('folder/bar.txt');
  212. $this->assertNotEquals($barCachedData['etag'], $cachedData['etag']);
  213. $this->assertEquals($time, $cachedData['mtime']);
  214. $cachedData = $this->cache->get('folder');
  215. $this->assertNotEquals($folderCachedData['etag'], $cachedData['etag']);
  216. $cachedData = $this->cache->get('');
  217. $this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
  218. $this->assertEquals($time, $cachedData['mtime']);
  219. }
  220. public function testTouchWithMountPoints() {
  221. $storage2 = new \OC\Files\Storage\Temporary(array());
  222. $cache2 = $storage2->getCache();
  223. Filesystem::mount($storage2, array(), '/' . self::$user . '/files/folder/substorage');
  224. Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
  225. $this->assertTrue($cache2->inCache('foo.txt'));
  226. $folderCachedData = $this->cache->get('folder');
  227. $substorageCachedData = $cache2->get('');
  228. $fooCachedData = $cache2->get('foo.txt');
  229. $cachedData = $cache2->get('foo.txt');
  230. $time = 1371006070;
  231. Filesystem::touch('folder/substorage/foo.txt', $time);
  232. $cachedData = $cache2->get('foo.txt');
  233. $this->assertNotEquals($fooCachedData['etag'], $cachedData['etag']);
  234. $this->assertEquals($time, $cachedData['mtime']);
  235. $cachedData = $cache2->get('');
  236. $this->assertNotEquals($substorageCachedData['etag'], $cachedData['etag']);
  237. $cachedData = $this->cache->get('folder');
  238. $this->assertNotEquals($folderCachedData['etag'], $cachedData['etag']);
  239. $this->assertEquals($time, $cachedData['mtime']);
  240. }
  241. public function testUpdatePermissionsOnRescanOnlyForUpdatedFile() {
  242. $permissionsCache = $this->storage->getPermissionsCache();
  243. $scanner = $this->storage->getScanner();
  244. $scanner->scan('');
  245. $cache = $this->storage->getCache();
  246. $loggedInUser = \OC_User::getUser();
  247. \OC_User::setUserId(self::$user);
  248. FileSystem::getDirectoryContent('/');
  249. $past = time() - 600;
  250. $cache->put('', array('storage_mtime' => $past));
  251. $this->assertNotEquals(-1, $permissionsCache->get($cache->getId('foo.txt'), self::$user));
  252. $this->assertNotEquals(-1, $permissionsCache->get($cache->getId('foo.png'), self::$user));
  253. $permissionsCache->set($cache->getId('foo.png'), self::$user, 15);
  254. FileSystem::file_put_contents('/foo.txt', 'asd');
  255. $this->assertEquals(-1, $permissionsCache->get($cache->getId('foo.txt'), self::$user));
  256. $this->assertEquals(15, $permissionsCache->get($cache->getId('foo.png'), self::$user));
  257. FileSystem::getDirectoryContent('/');
  258. $this->assertEquals(15, $permissionsCache->get($cache->getId('foo.png'), self::$user));
  259. FileSystem::file_put_contents('/qwerty.txt', 'asd');
  260. FileSystem::getDirectoryContent('/');
  261. $this->assertEquals(15, $permissionsCache->get($cache->getId('foo.png'), self::$user));
  262. \OC_User::setUserId($loggedInUser);
  263. }
  264. }