PermissionsMaskTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Files\Storage\Wrapper;
  8. use OC\Files\Storage\Wrapper\Wrapper;
  9. use OCP\Constants;
  10. use OCP\Files\Cache\IScanner;
  11. /**
  12. * @group DB
  13. */
  14. class PermissionsMaskTest extends \Test\Files\Storage\Storage {
  15. /**
  16. * @var \OC\Files\Storage\Temporary
  17. */
  18. private $sourceStorage;
  19. protected function setUp(): void {
  20. parent::setUp();
  21. $this->sourceStorage = new \OC\Files\Storage\Temporary([]);
  22. $this->instance = $this->getMaskedStorage(Constants::PERMISSION_ALL);
  23. }
  24. protected function tearDown(): void {
  25. $this->sourceStorage->cleanUp();
  26. parent::tearDown();
  27. }
  28. protected function getMaskedStorage($mask) {
  29. return new \OC\Files\Storage\Wrapper\PermissionsMask([
  30. 'storage' => $this->sourceStorage,
  31. 'mask' => $mask
  32. ]);
  33. }
  34. public function testMkdirNoCreate(): void {
  35. $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE);
  36. $this->assertFalse($storage->mkdir('foo'));
  37. $this->assertFalse($storage->file_exists('foo'));
  38. }
  39. public function testRmdirNoDelete(): void {
  40. $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE);
  41. $this->assertTrue($storage->mkdir('foo'));
  42. $this->assertTrue($storage->file_exists('foo'));
  43. $this->assertFalse($storage->rmdir('foo'));
  44. $this->assertTrue($storage->file_exists('foo'));
  45. }
  46. public function testTouchNewFileNoCreate(): void {
  47. $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE);
  48. $this->assertFalse($storage->touch('foo'));
  49. $this->assertFalse($storage->file_exists('foo'));
  50. }
  51. public function testTouchNewFileNoUpdate(): void {
  52. $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE);
  53. $this->assertTrue($storage->touch('foo'));
  54. $this->assertTrue($storage->file_exists('foo'));
  55. }
  56. public function testTouchExistingFileNoUpdate(): void {
  57. $this->sourceStorage->touch('foo');
  58. $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE);
  59. $this->assertFalse($storage->touch('foo'));
  60. }
  61. public function testUnlinkNoDelete(): void {
  62. $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE);
  63. $this->assertTrue($storage->touch('foo'));
  64. $this->assertTrue($storage->file_exists('foo'));
  65. $this->assertFalse($storage->unlink('foo'));
  66. $this->assertTrue($storage->file_exists('foo'));
  67. }
  68. public function testPutContentsNewFileNoUpdate(): void {
  69. $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE);
  70. $this->assertEquals(3, $storage->file_put_contents('foo', 'bar'));
  71. $this->assertEquals('bar', $storage->file_get_contents('foo'));
  72. }
  73. public function testPutContentsNewFileNoCreate(): void {
  74. $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE);
  75. $this->assertFalse($storage->file_put_contents('foo', 'bar'));
  76. }
  77. public function testPutContentsExistingFileNoUpdate(): void {
  78. $this->sourceStorage->touch('foo');
  79. $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE);
  80. $this->assertFalse($storage->file_put_contents('foo', 'bar'));
  81. }
  82. public function testFopenExistingFileNoUpdate(): void {
  83. $this->sourceStorage->touch('foo');
  84. $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE);
  85. $this->assertFalse($storage->fopen('foo', 'w'));
  86. }
  87. public function testFopenNewFileNoCreate(): void {
  88. $storage = $this->getMaskedStorage(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE);
  89. $this->assertFalse($storage->fopen('foo', 'w'));
  90. }
  91. public function testScanNewFiles(): void {
  92. $storage = $this->getMaskedStorage(Constants::PERMISSION_READ + Constants::PERMISSION_CREATE);
  93. $storage->file_put_contents('foo', 'bar');
  94. $storage->getScanner()->scan('');
  95. $this->assertEquals(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE, $this->sourceStorage->getCache()->get('foo')->getPermissions());
  96. $this->assertEquals(Constants::PERMISSION_READ, $storage->getCache()->get('foo')->getPermissions());
  97. }
  98. public function testScanNewWrappedFiles(): void {
  99. $storage = $this->getMaskedStorage(Constants::PERMISSION_READ + Constants::PERMISSION_CREATE);
  100. $wrappedStorage = new Wrapper(['storage' => $storage]);
  101. $wrappedStorage->file_put_contents('foo', 'bar');
  102. $wrappedStorage->getScanner()->scan('');
  103. $this->assertEquals(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE, $this->sourceStorage->getCache()->get('foo')->getPermissions());
  104. $this->assertEquals(Constants::PERMISSION_READ, $storage->getCache()->get('foo')->getPermissions());
  105. }
  106. public function testScanNewFilesNested(): void {
  107. $storage = $this->getMaskedStorage(Constants::PERMISSION_READ + Constants::PERMISSION_CREATE + Constants::PERMISSION_UPDATE);
  108. $nestedStorage = new \OC\Files\Storage\Wrapper\PermissionsMask([
  109. 'storage' => $storage,
  110. 'mask' => Constants::PERMISSION_READ + Constants::PERMISSION_CREATE
  111. ]);
  112. $wrappedStorage = new Wrapper(['storage' => $nestedStorage]);
  113. $wrappedStorage->file_put_contents('foo', 'bar');
  114. $wrappedStorage->getScanner()->scan('');
  115. $this->assertEquals(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE, $this->sourceStorage->getCache()->get('foo')->getPermissions());
  116. $this->assertEquals(Constants::PERMISSION_READ + Constants::PERMISSION_UPDATE, $storage->getCache()->get('foo')->getPermissions());
  117. $this->assertEquals(Constants::PERMISSION_READ, $wrappedStorage->getCache()->get('foo')->getPermissions());
  118. }
  119. public function testScanUnchanged(): void {
  120. $this->sourceStorage->mkdir('foo');
  121. $this->sourceStorage->file_put_contents('foo/bar.txt', 'bar');
  122. $this->sourceStorage->getScanner()->scan('foo');
  123. $storage = $this->getMaskedStorage(Constants::PERMISSION_READ);
  124. $scanner = $storage->getScanner();
  125. $called = false;
  126. $scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function () use (&$called) {
  127. $called = true;
  128. });
  129. $scanner->scan('foo', IScanner::SCAN_RECURSIVE, IScanner::REUSE_ETAG | IScanner::REUSE_SIZE);
  130. $this->assertFalse($called);
  131. }
  132. public function testScanUnchangedWrapped(): void {
  133. $this->sourceStorage->mkdir('foo');
  134. $this->sourceStorage->file_put_contents('foo/bar.txt', 'bar');
  135. $this->sourceStorage->getScanner()->scan('foo');
  136. $storage = $this->getMaskedStorage(Constants::PERMISSION_READ);
  137. $wrappedStorage = new Wrapper(['storage' => $storage]);
  138. $scanner = $wrappedStorage->getScanner();
  139. $called = false;
  140. $scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function () use (&$called) {
  141. $called = true;
  142. });
  143. $scanner->scan('foo', IScanner::SCAN_RECURSIVE, IScanner::REUSE_ETAG | IScanner::REUSE_SIZE);
  144. $this->assertFalse($called);
  145. }
  146. }