WatcherTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Morris Jobke <hey@morrisjobke.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 <pvince81@owncloud.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. /**
  31. * Class WatcherTest
  32. *
  33. * @group DB
  34. */
  35. class WatcherTest extends TestCase {
  36. /** @var \OC\Files\Storage\Storage */
  37. private $ownerStorage;
  38. /** @var \OC\Files\Cache\Cache */
  39. private $ownerCache;
  40. /** @var \OC\Files\Storage\Storage */
  41. private $sharedStorage;
  42. /** @var \OC\Files\Cache\Cache */
  43. private $sharedCache;
  44. /** @var \OCP\Share\IShare */
  45. private $_share;
  46. protected function setUp() {
  47. parent::setUp();
  48. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  49. // prepare user1's dir structure
  50. $this->view->mkdir('container');
  51. $this->view->mkdir('container/shareddir');
  52. $this->view->mkdir('container/shareddir/subdir');
  53. list($this->ownerStorage, $internalPath) = $this->view->resolvePath('');
  54. $this->ownerCache = $this->ownerStorage->getCache();
  55. $this->ownerStorage->getScanner()->scan('');
  56. // share "shareddir" with user2
  57. $this->_share = $this->share(
  58. \OCP\Share::SHARE_TYPE_USER,
  59. 'container/shareddir',
  60. self::TEST_FILES_SHARING_API_USER1,
  61. self::TEST_FILES_SHARING_API_USER2,
  62. \OCP\Constants::PERMISSION_ALL
  63. );
  64. // login as user2
  65. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  66. // retrieve the shared storage
  67. $secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
  68. list($this->sharedStorage, $internalPath) = $secondView->resolvePath('files/shareddir');
  69. $this->sharedCache = $this->sharedStorage->getCache();
  70. }
  71. protected function tearDown() {
  72. if ($this->sharedCache) {
  73. $this->sharedCache->clear();
  74. }
  75. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  76. if ($this->view) {
  77. $this->shareManager->deleteShare($this->_share);
  78. $this->view->deleteAll('container');
  79. $this->ownerCache->clear();
  80. }
  81. parent::tearDown();
  82. }
  83. /**
  84. * Tests that writing a file using the shared storage will propagate the file
  85. * size to the owner's parent folders.
  86. */
  87. function testFolderSizePropagationToOwnerStorage() {
  88. $initialSizes = self::getOwnerDirSizes('files/container/shareddir');
  89. $textData = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  90. $dataLen = strlen($textData);
  91. $this->sharedCache->put('bar.txt', array('mtime' => 10, 'storage_mtime' => 10, 'size' => $dataLen, 'mimetype' => 'text/plain'));
  92. $this->sharedStorage->file_put_contents('bar.txt', $textData);
  93. $this->sharedCache->put('', array('mtime' => 10, 'storage_mtime' => 10, 'size' => '-1', 'mimetype' => 'httpd/unix-directory'));
  94. // run the propagation code
  95. $this->sharedStorage->getWatcher()->checkUpdate('');
  96. $this->sharedStorage->getCache()->correctFolderSize('');
  97. // the owner's parent dirs must have increase size
  98. $newSizes = self::getOwnerDirSizes('files/container/shareddir');
  99. $this->assertEquals($initialSizes[''] + $dataLen, $newSizes['']);
  100. $this->assertEquals($initialSizes['files'] + $dataLen, $newSizes['files']);
  101. $this->assertEquals($initialSizes['files/container'] + $dataLen, $newSizes['files/container']);
  102. $this->assertEquals($initialSizes['files/container/shareddir'] + $dataLen, $newSizes['files/container/shareddir']);
  103. // no more updates
  104. $result = $this->sharedStorage->getWatcher()->checkUpdate('');
  105. $this->assertFalse($result);
  106. }
  107. /**
  108. * Tests that writing a file using the shared storage will propagate the file
  109. * size to the owner's parent folders.
  110. */
  111. function testSubFolderSizePropagationToOwnerStorage() {
  112. $initialSizes = self::getOwnerDirSizes('files/container/shareddir/subdir');
  113. $textData = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  114. $dataLen = strlen($textData);
  115. $this->sharedCache->put('subdir/bar.txt', array('mtime' => 10, 'storage_mtime' => 10, 'size' => $dataLen, 'mimetype' => 'text/plain'));
  116. $this->sharedStorage->file_put_contents('subdir/bar.txt', $textData);
  117. $this->sharedCache->put('subdir', array('mtime' => 10, 'storage_mtime' => 10, 'size' => $dataLen, 'mimetype' => 'text/plain'));
  118. // run the propagation code
  119. $this->sharedStorage->getWatcher()->checkUpdate('subdir');
  120. $this->sharedStorage->getCache()->correctFolderSize('subdir');
  121. // the owner's parent dirs must have increase size
  122. $newSizes = self::getOwnerDirSizes('files/container/shareddir/subdir');
  123. $this->assertEquals($initialSizes[''] + $dataLen, $newSizes['']);
  124. $this->assertEquals($initialSizes['files'] + $dataLen, $newSizes['files']);
  125. $this->assertEquals($initialSizes['files/container'] + $dataLen, $newSizes['files/container']);
  126. $this->assertEquals($initialSizes['files/container/shareddir'] + $dataLen, $newSizes['files/container/shareddir']);
  127. $this->assertEquals($initialSizes['files/container/shareddir/subdir'] + $dataLen, $newSizes['files/container/shareddir/subdir']);
  128. // no more updates
  129. $result = $this->sharedStorage->getWatcher()->checkUpdate('subdir');
  130. $this->assertFalse($result);
  131. }
  132. /**
  133. * Returns the sizes of the path and its parent dirs in a hash
  134. * where the key is the path and the value is the size.
  135. * @param string $path
  136. */
  137. function getOwnerDirSizes($path) {
  138. $result = array();
  139. while ($path != '' && $path != '' && $path != '.') {
  140. $cachedData = $this->ownerCache->get($path);
  141. $result[$path] = $cachedData['size'];
  142. $path = dirname($path);
  143. }
  144. $cachedData = $this->ownerCache->get('');
  145. $result[''] = $cachedData['size'];
  146. return $result;
  147. }
  148. }