updaterlegacy.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 UpdaterLegacy extends \Test\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. /**
  21. * @var \OC\Files\Cache\Cache $cache
  22. */
  23. private $cache;
  24. private static $user;
  25. protected function setUp() {
  26. parent::setUp();
  27. $this->storage = new \OC\Files\Storage\Temporary(array());
  28. $textData = "dummy file data\n";
  29. $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
  30. $this->storage->mkdir('folder');
  31. $this->storage->file_put_contents('foo.txt', $textData);
  32. $this->storage->file_put_contents('foo.png', $imgData);
  33. $this->storage->file_put_contents('folder/bar.txt', $textData);
  34. $this->storage->file_put_contents('folder/bar2.txt', $textData);
  35. $this->scanner = $this->storage->getScanner();
  36. $this->scanner->scan('');
  37. $this->cache = $this->storage->getCache();
  38. if (!self::$user) {
  39. self::$user = $this->getUniqueID();
  40. }
  41. \OC_User::createUser(self::$user, 'password');
  42. $this->loginAsUser(self::$user);
  43. Filesystem::init(self::$user, '/' . self::$user . '/files');
  44. Filesystem::clearMounts();
  45. Filesystem::mount($this->storage, array(), '/' . self::$user . '/files');
  46. \OC_Hook::clear('OC_Filesystem');
  47. }
  48. protected function tearDown() {
  49. if ($this->cache) {
  50. $this->cache->clear();
  51. }
  52. $result = \OC_User::deleteUser(self::$user);
  53. $this->assertTrue($result);
  54. $this->logout();
  55. parent::tearDown();
  56. }
  57. public function testWrite() {
  58. $textSize = strlen("dummy file data\n");
  59. $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
  60. $this->cache->put('foo.txt', array('mtime' => 100, 'storage_mtime' => 150));
  61. $rootCachedData = $this->cache->get('');
  62. $this->assertEquals(3 * $textSize + $imageSize, $rootCachedData['size']);
  63. $fooCachedData = $this->cache->get('foo.txt');
  64. Filesystem::file_put_contents('foo.txt', 'asd');
  65. $cachedData = $this->cache->get('foo.txt');
  66. $this->assertEquals(3, $cachedData['size']);
  67. $this->assertInternalType('string', $fooCachedData['etag']);
  68. $this->assertInternalType('string', $cachedData['etag']);
  69. $this->assertNotSame($fooCachedData['etag'], $cachedData['etag']);
  70. $cachedData = $this->cache->get('');
  71. $this->assertEquals(2 * $textSize + $imageSize + 3, $cachedData['size']);
  72. $this->assertInternalType('string', $rootCachedData['etag']);
  73. $this->assertInternalType('string', $cachedData['etag']);
  74. $this->assertNotSame($rootCachedData['etag'], $cachedData['etag']);
  75. $rootCachedData = $cachedData;
  76. $this->assertFalse($this->cache->inCache('bar.txt'));
  77. Filesystem::file_put_contents('bar.txt', 'asd');
  78. $this->assertTrue($this->cache->inCache('bar.txt'));
  79. $cachedData = $this->cache->get('bar.txt');
  80. $this->assertEquals(3, $cachedData['size']);
  81. $mtime = $cachedData['mtime'];
  82. $cachedData = $this->cache->get('');
  83. $this->assertEquals(2 * $textSize + $imageSize + 2 * 3, $cachedData['size']);
  84. $this->assertInternalType('string', $rootCachedData['etag']);
  85. $this->assertInternalType('string', $cachedData['etag']);
  86. $this->assertNotSame($rootCachedData['etag'], $cachedData['etag']);
  87. $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $mtime);
  88. }
  89. public function testWriteWithMountPoints() {
  90. $storage2 = new \OC\Files\Storage\Temporary(array());
  91. $storage2->getScanner()->scan(''); //initialize etags
  92. $cache2 = $storage2->getCache();
  93. Filesystem::mount($storage2, array(), '/' . self::$user . '/files/folder/substorage');
  94. $folderCachedData = $this->cache->get('folder');
  95. $substorageCachedData = $cache2->get('');
  96. Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
  97. $this->assertTrue($cache2->inCache('foo.txt'));
  98. $cachedData = $cache2->get('foo.txt');
  99. $this->assertEquals(3, $cachedData['size']);
  100. $mtime = $cachedData['mtime'];
  101. $cachedData = $cache2->get('');
  102. $this->assertInternalType('string', $substorageCachedData['etag']);
  103. $this->assertInternalType('string', $cachedData['etag']);
  104. $this->assertNotSame($substorageCachedData['etag'], $cachedData['etag']);
  105. $cachedData = $this->cache->get('folder');
  106. $this->assertInternalType('string', $folderCachedData['etag']);
  107. $this->assertInternalType('string', $cachedData['etag']);
  108. $this->assertNotSame($folderCachedData['etag'], $cachedData['etag']);
  109. }
  110. public function testDelete() {
  111. $textSize = strlen("dummy file data\n");
  112. $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
  113. $rootCachedData = $this->cache->get('');
  114. $this->assertEquals(3 * $textSize + $imageSize, $rootCachedData['size']);
  115. $this->assertTrue($this->cache->inCache('foo.txt'));
  116. Filesystem::unlink('foo.txt');
  117. $this->assertFalse($this->cache->inCache('foo.txt'));
  118. $cachedData = $this->cache->get('');
  119. $this->assertEquals(2 * $textSize + $imageSize, $cachedData['size']);
  120. $this->assertInternalType('string', $rootCachedData['etag']);
  121. $this->assertInternalType('string', $cachedData['etag']);
  122. $this->assertNotSame($rootCachedData['etag'], $cachedData['etag']);
  123. $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $cachedData['mtime']);
  124. $rootCachedData = $cachedData;
  125. Filesystem::mkdir('bar_folder');
  126. $this->assertTrue($this->cache->inCache('bar_folder'));
  127. $cachedData = $this->cache->get('');
  128. $this->assertInternalType('string', $rootCachedData['etag']);
  129. $this->assertInternalType('string', $cachedData['etag']);
  130. $this->assertNotSame($rootCachedData['etag'], $cachedData['etag']);
  131. $rootCachedData = $cachedData;
  132. Filesystem::rmdir('bar_folder');
  133. $this->assertFalse($this->cache->inCache('bar_folder'));
  134. $cachedData = $this->cache->get('');
  135. $this->assertInternalType('string', $rootCachedData['etag']);
  136. $this->assertInternalType('string', $cachedData['etag']);
  137. $this->assertNotSame($rootCachedData['etag'], $cachedData['etag']);
  138. $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $cachedData['mtime']);
  139. }
  140. public function testDeleteWithMountPoints() {
  141. $storage2 = new \OC\Files\Storage\Temporary(array());
  142. $cache2 = $storage2->getCache();
  143. Filesystem::mount($storage2, array(), '/' . self::$user . '/files/folder/substorage');
  144. Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
  145. $this->assertTrue($cache2->inCache('foo.txt'));
  146. $folderCachedData = $this->cache->get('folder');
  147. $substorageCachedData = $cache2->get('');
  148. Filesystem::unlink('folder/substorage/foo.txt');
  149. $this->assertFalse($cache2->inCache('foo.txt'));
  150. $cachedData = $cache2->get('');
  151. $this->assertInternalType('string', $substorageCachedData['etag']);
  152. $this->assertInternalType('string', $cachedData['etag']);
  153. $this->assertNotSame($substorageCachedData['etag'], $cachedData['etag']);
  154. $this->assertGreaterThanOrEqual($substorageCachedData['mtime'], $cachedData['mtime']);
  155. $cachedData = $this->cache->get('folder');
  156. $this->assertInternalType('string', $folderCachedData['etag']);
  157. $this->assertInternalType('string', $cachedData['etag']);
  158. $this->assertNotSame($folderCachedData['etag'], $cachedData['etag']);
  159. $this->assertGreaterThanOrEqual($folderCachedData['mtime'], $cachedData['mtime']);
  160. }
  161. public function testRename() {
  162. $textSize = strlen("dummy file data\n");
  163. $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo.png');
  164. $rootCachedData = $this->cache->get('');
  165. $this->assertEquals(3 * $textSize + $imageSize, $rootCachedData['size']);
  166. $this->assertTrue($this->cache->inCache('foo.txt'));
  167. $fooCachedData = $this->cache->get('foo.txt');
  168. $this->assertFalse($this->cache->inCache('bar.txt'));
  169. Filesystem::rename('foo.txt', 'bar.txt');
  170. $this->assertFalse($this->cache->inCache('foo.txt'));
  171. $this->assertTrue($this->cache->inCache('bar.txt'));
  172. $cachedData = $this->cache->get('bar.txt');
  173. $this->assertEquals($fooCachedData['fileid'], $cachedData['fileid']);
  174. $mtime = $cachedData['mtime'];
  175. $cachedData = $this->cache->get('');
  176. $this->assertEquals(3 * $textSize + $imageSize, $cachedData['size']);
  177. $this->assertInternalType('string', $rootCachedData['etag']);
  178. $this->assertInternalType('string', $cachedData['etag']);
  179. $this->assertNotSame($rootCachedData['etag'], $cachedData['etag']);
  180. }
  181. public function testRenameExtension() {
  182. $fooCachedData = $this->cache->get('foo.txt');
  183. $this->assertEquals('text/plain', $fooCachedData['mimetype']);
  184. Filesystem::rename('foo.txt', 'foo.abcd');
  185. $fooCachedData = $this->cache->get('foo.abcd');
  186. $this->assertEquals('application/octet-stream', $fooCachedData['mimetype']);
  187. }
  188. public function testRenameWithMountPoints() {
  189. $storage2 = new \OC\Files\Storage\Temporary(array());
  190. $cache2 = $storage2->getCache();
  191. Filesystem::mount($storage2, array(), '/' . self::$user . '/files/folder/substorage');
  192. Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
  193. $this->assertTrue($cache2->inCache('foo.txt'));
  194. $folderCachedData = $this->cache->get('folder');
  195. $substorageCachedData = $cache2->get('');
  196. $fooCachedData = $cache2->get('foo.txt');
  197. Filesystem::rename('folder/substorage/foo.txt', 'folder/substorage/bar.txt');
  198. $this->assertFalse($cache2->inCache('foo.txt'));
  199. $this->assertTrue($cache2->inCache('bar.txt'));
  200. $cachedData = $cache2->get('bar.txt');
  201. $this->assertEquals($fooCachedData['fileid'], $cachedData['fileid']);
  202. $mtime = $cachedData['mtime'];
  203. $cachedData = $cache2->get('');
  204. $this->assertInternalType('string', $substorageCachedData['etag']);
  205. $this->assertInternalType('string', $cachedData['etag']);
  206. $this->assertNotSame($substorageCachedData['etag'], $cachedData['etag']);
  207. // rename can cause mtime change - invalid assert
  208. // $this->assertEquals($mtime, $cachedData['mtime']);
  209. $cachedData = $this->cache->get('folder');
  210. $this->assertInternalType('string', $folderCachedData['etag']);
  211. $this->assertInternalType('string', $cachedData['etag']);
  212. $this->assertNotSame($folderCachedData['etag'], $cachedData['etag']);
  213. // rename can cause mtime change - invalid assert
  214. // $this->assertEquals($mtime, $cachedData['mtime']);
  215. }
  216. public function testTouch() {
  217. $rootCachedData = $this->cache->get('');
  218. $fooCachedData = $this->cache->get('foo.txt');
  219. Filesystem::touch('foo.txt');
  220. $cachedData = $this->cache->get('foo.txt');
  221. $this->assertInternalType('string', $fooCachedData['etag']);
  222. $this->assertInternalType('string', $cachedData['etag']);
  223. $this->assertGreaterThanOrEqual($fooCachedData['mtime'], $cachedData['mtime']);
  224. $cachedData = $this->cache->get('');
  225. $this->assertInternalType('string', $rootCachedData['etag']);
  226. $this->assertInternalType('string', $cachedData['etag']);
  227. $this->assertNotSame($rootCachedData['etag'], $cachedData['etag']);
  228. $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $cachedData['mtime']);
  229. $rootCachedData = $cachedData;
  230. $time = 1371006070;
  231. $barCachedData = $this->cache->get('folder/bar.txt');
  232. $folderCachedData = $this->cache->get('folder');
  233. $this->cache->put('', ['mtime' => $time - 100]);
  234. Filesystem::touch('folder/bar.txt', $time);
  235. $cachedData = $this->cache->get('folder/bar.txt');
  236. $this->assertInternalType('string', $barCachedData['etag']);
  237. $this->assertInternalType('string', $cachedData['etag']);
  238. $this->assertNotSame($barCachedData['etag'], $cachedData['etag']);
  239. $this->assertEquals($time, $cachedData['mtime']);
  240. $cachedData = $this->cache->get('folder');
  241. $this->assertInternalType('string', $folderCachedData['etag']);
  242. $this->assertInternalType('string', $cachedData['etag']);
  243. $this->assertNotSame($folderCachedData['etag'], $cachedData['etag']);
  244. $cachedData = $this->cache->get('');
  245. $this->assertInternalType('string', $rootCachedData['etag']);
  246. $this->assertInternalType('string', $cachedData['etag']);
  247. $this->assertNotSame($rootCachedData['etag'], $cachedData['etag']);
  248. $this->assertEquals($time, $cachedData['mtime']);
  249. }
  250. public function testTouchWithMountPoints() {
  251. $storage2 = new \OC\Files\Storage\Temporary(array());
  252. $cache2 = $storage2->getCache();
  253. Filesystem::mount($storage2, array(), '/' . self::$user . '/files/folder/substorage');
  254. Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
  255. $this->assertTrue($cache2->inCache('foo.txt'));
  256. $folderCachedData = $this->cache->get('folder');
  257. $substorageCachedData = $cache2->get('');
  258. $fooCachedData = $cache2->get('foo.txt');
  259. $cachedData = $cache2->get('foo.txt');
  260. $time = 1371006070;
  261. $this->cache->put('folder', ['mtime' => $time - 100]);
  262. Filesystem::touch('folder/substorage/foo.txt', $time);
  263. $cachedData = $cache2->get('foo.txt');
  264. $this->assertInternalType('string', $fooCachedData['etag']);
  265. $this->assertInternalType('string', $cachedData['etag']);
  266. $this->assertNotSame($fooCachedData['etag'], $cachedData['etag']);
  267. $this->assertEquals($time, $cachedData['mtime']);
  268. $cachedData = $cache2->get('');
  269. $this->assertInternalType('string', $substorageCachedData['etag']);
  270. $this->assertInternalType('string', $cachedData['etag']);
  271. $this->assertNotSame($substorageCachedData['etag'], $cachedData['etag']);
  272. $cachedData = $this->cache->get('folder');
  273. $this->assertInternalType('string', $folderCachedData['etag']);
  274. $this->assertInternalType('string', $cachedData['etag']);
  275. $this->assertNotSame($folderCachedData['etag'], $cachedData['etag']);
  276. $this->assertEquals($time, $cachedData['mtime']);
  277. }
  278. }