WatcherTest.php 6.4 KB

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