You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WatcherTest.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Vincent Petry <vincent@nextcloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCA\Files_Sharing\Tests;
  31. use OCP\Share\IShare;
  32. /**
  33. * Class WatcherTest
  34. *
  35. * @group DB
  36. */
  37. class WatcherTest extends TestCase {
  38. /** @var \OC\Files\Storage\Storage */
  39. private $ownerStorage;
  40. /** @var \OC\Files\Cache\Cache */
  41. private $ownerCache;
  42. /** @var \OC\Files\Storage\Storage */
  43. private $sharedStorage;
  44. /** @var \OC\Files\Cache\Cache */
  45. private $sharedCache;
  46. /** @var \OCP\Share\IShare */
  47. private $_share;
  48. protected function setUp(): void {
  49. parent::setUp();
  50. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  51. // prepare user1's dir structure
  52. $this->view->mkdir('container');
  53. $this->view->mkdir('container/shareddir');
  54. $this->view->mkdir('container/shareddir/subdir');
  55. [$this->ownerStorage, $internalPath] = $this->view->resolvePath('');
  56. $this->ownerCache = $this->ownerStorage->getCache();
  57. $this->ownerStorage->getScanner()->scan('');
  58. // share "shareddir" with user2
  59. $this->_share = $this->share(
  60. IShare::TYPE_USER,
  61. 'container/shareddir',
  62. self::TEST_FILES_SHARING_API_USER1,
  63. self::TEST_FILES_SHARING_API_USER2,
  64. \OCP\Constants::PERMISSION_ALL
  65. );
  66. $this->_share->setStatus(IShare::STATUS_ACCEPTED);
  67. $this->shareManager->updateShare($this->_share);
  68. // login as user2
  69. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  70. // retrieve the shared storage
  71. $secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
  72. [$this->sharedStorage, $internalPath] = $secondView->resolvePath('files/shareddir');
  73. $this->sharedCache = $this->sharedStorage->getCache();
  74. }
  75. protected function tearDown(): void {
  76. if ($this->sharedCache) {
  77. $this->sharedCache->clear();
  78. }
  79. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  80. if ($this->view) {
  81. $this->shareManager->deleteShare($this->_share);
  82. $this->view->deleteAll('container');
  83. $this->ownerCache->clear();
  84. }
  85. parent::tearDown();
  86. }
  87. /**
  88. * Tests that writing a file using the shared storage will propagate the file
  89. * size to the owner's parent folders.
  90. */
  91. public function testFolderSizePropagationToOwnerStorage() {
  92. $initialSizes = self::getOwnerDirSizes('files/container/shareddir');
  93. $textData = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  94. $dataLen = strlen($textData);
  95. $this->sharedCache->put('bar.txt', ['mtime' => 10, 'storage_mtime' => 10, 'size' => $dataLen, 'mimetype' => 'text/plain']);
  96. $this->sharedStorage->file_put_contents('bar.txt', $textData);
  97. $this->sharedCache->put('', ['mtime' => 10, 'storage_mtime' => 10, 'size' => '-1', 'mimetype' => 'httpd/unix-directory']);
  98. // run the propagation code
  99. $this->sharedStorage->getWatcher()->checkUpdate('');
  100. $this->sharedStorage->getCache()->correctFolderSize('');
  101. // the owner's parent dirs must have increase size
  102. $newSizes = self::getOwnerDirSizes('files/container/shareddir');
  103. $this->assertEquals($initialSizes[''] + $dataLen, $newSizes['']);
  104. $this->assertEquals($initialSizes['files'] + $dataLen, $newSizes['files']);
  105. $this->assertEquals($initialSizes['files/container'] + $dataLen, $newSizes['files/container']);
  106. $this->assertEquals($initialSizes['files/container/shareddir'] + $dataLen, $newSizes['files/container/shareddir']);
  107. // no more updates
  108. $result = $this->sharedStorage->getWatcher()->checkUpdate('');
  109. $this->assertFalse($result);
  110. }
  111. /**
  112. * Tests that writing a file using the shared storage will propagate the file
  113. * size to the owner's parent folders.
  114. */
  115. public function testSubFolderSizePropagationToOwnerStorage() {
  116. $initialSizes = self::getOwnerDirSizes('files/container/shareddir/subdir');
  117. $textData = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  118. $dataLen = strlen($textData);
  119. $this->sharedCache->put('subdir/bar.txt', ['mtime' => 10, 'storage_mtime' => 10, 'size' => $dataLen, 'mimetype' => 'text/plain']);
  120. $this->sharedStorage->file_put_contents('subdir/bar.txt', $textData);
  121. $this->sharedCache->put('subdir', ['mtime' => 10, 'storage_mtime' => 10, 'size' => $dataLen, 'mimetype' => 'text/plain']);
  122. // run the propagation code
  123. $this->sharedStorage->getWatcher()->checkUpdate('subdir');
  124. $this->sharedStorage->getCache()->correctFolderSize('subdir');
  125. // the owner's parent dirs must have increase size
  126. $newSizes = self::getOwnerDirSizes('files/container/shareddir/subdir');
  127. $this->assertEquals($initialSizes[''] + $dataLen, $newSizes['']);
  128. $this->assertEquals($initialSizes['files'] + $dataLen, $newSizes['files']);
  129. $this->assertEquals($initialSizes['files/container'] + $dataLen, $newSizes['files/container']);
  130. $this->assertEquals($initialSizes['files/container/shareddir'] + $dataLen, $newSizes['files/container/shareddir']);
  131. $this->assertEquals($initialSizes['files/container/shareddir/subdir'] + $dataLen, $newSizes['files/container/shareddir/subdir']);
  132. // no more updates
  133. $result = $this->sharedStorage->getWatcher()->checkUpdate('subdir');
  134. $this->assertFalse($result);
  135. }
  136. /**
  137. * Returns the sizes of the path and its parent dirs in a hash
  138. * where the key is the path and the value is the size.
  139. * @param string $path
  140. */
  141. public function getOwnerDirSizes($path) {
  142. $result = [];
  143. while ($path != '' && $path != '' && $path != '.') {
  144. $cachedData = $this->ownerCache->get($path);
  145. $result[$path] = $cachedData['size'];
  146. $path = dirname($path);
  147. }
  148. $cachedData = $this->ownerCache->get('');
  149. $result[''] = $cachedData['size'];
  150. return $result;
  151. }
  152. }