PermissionsMaskTest.php 6.7 KB

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