UpdaterLegacyTest.php 12 KB

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