LocalTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. /**
  24. * Class LocalTest
  25. *
  26. * @group DB
  27. *
  28. * @package Test\Files\Storage
  29. */
  30. class LocalTest extends Storage {
  31. /**
  32. * @var string tmpDir
  33. */
  34. private $tmpDir;
  35. protected function setUp(): void {
  36. parent::setUp();
  37. $this->tmpDir = \OC::$server->getTempManager()->getTemporaryFolder();
  38. $this->instance = new \OC\Files\Storage\Local(['datadir' => $this->tmpDir]);
  39. }
  40. protected function tearDown(): void {
  41. \OC_Helper::rmdirr($this->tmpDir);
  42. parent::tearDown();
  43. }
  44. public function testStableEtag() {
  45. $this->instance->file_put_contents('test.txt', 'foobar');
  46. $etag1 = $this->instance->getETag('test.txt');
  47. $etag2 = $this->instance->getETag('test.txt');
  48. $this->assertEquals($etag1, $etag2);
  49. }
  50. public function testEtagChange() {
  51. $this->instance->file_put_contents('test.txt', 'foo');
  52. $this->instance->touch('test.txt', time() - 2);
  53. $etag1 = $this->instance->getETag('test.txt');
  54. $this->instance->file_put_contents('test.txt', 'bar');
  55. $etag2 = $this->instance->getETag('test.txt');
  56. $this->assertNotEquals($etag1, $etag2);
  57. }
  58. public function testInvalidArgumentsEmptyArray() {
  59. $this->expectException(\InvalidArgumentException::class);
  60. new \OC\Files\Storage\Local([]);
  61. }
  62. public function testInvalidArgumentsNoArray() {
  63. $this->expectException(\InvalidArgumentException::class);
  64. new \OC\Files\Storage\Local(null);
  65. }
  66. public function testDisallowSymlinksOutsideDatadir() {
  67. $this->expectException(\OCP\Files\ForbiddenException::class);
  68. $subDir1 = $this->tmpDir . 'sub1';
  69. $subDir2 = $this->tmpDir . 'sub2';
  70. $sym = $this->tmpDir . 'sub1/sym';
  71. mkdir($subDir1);
  72. mkdir($subDir2);
  73. symlink($subDir2, $sym);
  74. $storage = new \OC\Files\Storage\Local(['datadir' => $subDir1]);
  75. $storage->file_put_contents('sym/foo', 'bar');
  76. }
  77. public function testDisallowSymlinksInsideDatadir() {
  78. $subDir1 = $this->tmpDir . 'sub1';
  79. $subDir2 = $this->tmpDir . 'sub1/sub2';
  80. $sym = $this->tmpDir . 'sub1/sym';
  81. mkdir($subDir1);
  82. mkdir($subDir2);
  83. symlink($subDir2, $sym);
  84. $storage = new \OC\Files\Storage\Local(['datadir' => $subDir1]);
  85. $storage->file_put_contents('sym/foo', 'bar');
  86. $this->addToAssertionCount(1);
  87. }
  88. public function testWriteUmaskFilePutContents() {
  89. $oldMask = umask(0333);
  90. $this->instance->file_put_contents('test.txt', 'sad');
  91. umask($oldMask);
  92. $this->assertTrue($this->instance->isUpdatable('test.txt'));
  93. }
  94. public function testWriteUmaskMkdir() {
  95. $oldMask = umask(0333);
  96. $this->instance->mkdir('test.txt');
  97. umask($oldMask);
  98. $this->assertTrue($this->instance->isUpdatable('test.txt'));
  99. }
  100. public function testWriteUmaskFopen() {
  101. $oldMask = umask(0333);
  102. $handle = $this->instance->fopen('test.txt', 'w');
  103. fwrite($handle, 'foo');
  104. fclose($handle);
  105. umask($oldMask);
  106. $this->assertTrue($this->instance->isUpdatable('test.txt'));
  107. }
  108. public function testWriteUmaskCopy() {
  109. $this->instance->file_put_contents('source.txt', 'sad');
  110. $oldMask = umask(0333);
  111. $this->instance->copy('source.txt', 'test.txt');
  112. umask($oldMask);
  113. $this->assertTrue($this->instance->isUpdatable('test.txt'));
  114. }
  115. }