LocalTest.php 3.7 KB

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