1
0

LocalTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2012 Robin Appelman icewind@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\Files\Storage;
  23. use OC\Files\Storage\Wrapper\Jail;
  24. /**
  25. * Class LocalTest
  26. *
  27. * @group DB
  28. *
  29. * @package Test\Files\Storage
  30. */
  31. class LocalTest extends Storage {
  32. /**
  33. * @var string tmpDir
  34. */
  35. private $tmpDir;
  36. protected function setUp(): void {
  37. parent::setUp();
  38. $this->tmpDir = \OC::$server->getTempManager()->getTemporaryFolder();
  39. $this->instance = new \OC\Files\Storage\Local(['datadir' => $this->tmpDir]);
  40. }
  41. protected function tearDown(): void {
  42. \OC_Helper::rmdirr($this->tmpDir);
  43. parent::tearDown();
  44. }
  45. public function testStableEtag() {
  46. $this->instance->file_put_contents('test.txt', 'foobar');
  47. $etag1 = $this->instance->getETag('test.txt');
  48. $etag2 = $this->instance->getETag('test.txt');
  49. $this->assertEquals($etag1, $etag2);
  50. }
  51. public function testEtagChange() {
  52. $this->instance->file_put_contents('test.txt', 'foo');
  53. $this->instance->touch('test.txt', time() - 2);
  54. $etag1 = $this->instance->getETag('test.txt');
  55. $this->instance->file_put_contents('test.txt', 'bar');
  56. $etag2 = $this->instance->getETag('test.txt');
  57. $this->assertNotEquals($etag1, $etag2);
  58. }
  59. public function testInvalidArgumentsEmptyArray() {
  60. $this->expectException(\InvalidArgumentException::class);
  61. new \OC\Files\Storage\Local([]);
  62. }
  63. public function testInvalidArgumentsNoArray() {
  64. $this->expectException(\InvalidArgumentException::class);
  65. new \OC\Files\Storage\Local(null);
  66. }
  67. public function testDisallowSymlinksOutsideDatadir() {
  68. $this->expectException(\OCP\Files\ForbiddenException::class);
  69. $subDir1 = $this->tmpDir . 'sub1';
  70. $subDir2 = $this->tmpDir . 'sub2';
  71. $sym = $this->tmpDir . 'sub1/sym';
  72. mkdir($subDir1);
  73. mkdir($subDir2);
  74. symlink($subDir2, $sym);
  75. $storage = new \OC\Files\Storage\Local(['datadir' => $subDir1]);
  76. $storage->file_put_contents('sym/foo', 'bar');
  77. }
  78. public function testDisallowSymlinksInsideDatadir() {
  79. $subDir1 = $this->tmpDir . 'sub1';
  80. $subDir2 = $this->tmpDir . 'sub1/sub2';
  81. $sym = $this->tmpDir . 'sub1/sym';
  82. mkdir($subDir1);
  83. mkdir($subDir2);
  84. symlink($subDir2, $sym);
  85. $storage = new \OC\Files\Storage\Local(['datadir' => $subDir1]);
  86. $storage->file_put_contents('sym/foo', 'bar');
  87. $this->addToAssertionCount(1);
  88. }
  89. public function testWriteUmaskFilePutContents() {
  90. $oldMask = umask(0333);
  91. $this->instance->file_put_contents('test.txt', 'sad');
  92. umask($oldMask);
  93. $this->assertTrue($this->instance->isUpdatable('test.txt'));
  94. }
  95. public function testWriteUmaskMkdir() {
  96. $oldMask = umask(0333);
  97. $this->instance->mkdir('test.txt');
  98. umask($oldMask);
  99. $this->assertTrue($this->instance->isUpdatable('test.txt'));
  100. }
  101. public function testWriteUmaskFopen() {
  102. $oldMask = umask(0333);
  103. $handle = $this->instance->fopen('test.txt', 'w');
  104. fwrite($handle, 'foo');
  105. fclose($handle);
  106. umask($oldMask);
  107. $this->assertTrue($this->instance->isUpdatable('test.txt'));
  108. }
  109. public function testWriteUmaskCopy() {
  110. $this->instance->file_put_contents('source.txt', 'sad');
  111. $oldMask = umask(0333);
  112. $this->instance->copy('source.txt', 'test.txt');
  113. umask($oldMask);
  114. $this->assertTrue($this->instance->isUpdatable('test.txt'));
  115. }
  116. public function testUnavailableExternal() {
  117. $this->expectException(\OCP\Files\StorageNotAvailableException::class);
  118. $this->instance = new \OC\Files\Storage\Local(['datadir' => $this->tmpDir . '/unexist', 'isExternal' => true]);
  119. }
  120. public function testUnavailableNonExternal() {
  121. $this->instance = new \OC\Files\Storage\Local(['datadir' => $this->tmpDir . '/unexist']);
  122. // no exception thrown
  123. $this->assertNotNull($this->instance);
  124. }
  125. public function testMoveNestedJail(): void {
  126. $this->instance->mkdir('foo');
  127. $this->instance->mkdir('foo/bar');
  128. $this->instance->mkdir('target');
  129. $this->instance->file_put_contents('foo/bar/file.txt', 'foo');
  130. $jail1 = new Jail([
  131. 'storage' => $this->instance,
  132. 'root' => 'foo'
  133. ]);
  134. $jail2 = new Jail([
  135. 'storage' => $jail1,
  136. 'root' => 'bar'
  137. ]);
  138. $jail3 = new Jail([
  139. 'storage' => $this->instance,
  140. 'root' => 'target'
  141. ]);
  142. $jail3->moveFromStorage($jail2, 'file.txt', 'file.txt');
  143. $this->assertTrue($this->instance->file_exists('target/file.txt'));
  144. }
  145. }