CommonTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-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. use OC\Files\Storage\Wrapper\Jail;
  9. use OC\Files\Storage\Wrapper\Wrapper;
  10. use OCP\Files\IFilenameValidator;
  11. use OCP\Files\InvalidCharacterInPathException;
  12. use OCP\Files\InvalidPathException;
  13. use OCP\ITempManager;
  14. use PHPUnit\Framework\MockObject\MockObject;
  15. /**
  16. * Class CommonTest
  17. *
  18. * @group DB
  19. *
  20. * @package Test\Files\Storage
  21. */
  22. class CommonTest extends Storage {
  23. private string $tmpDir;
  24. private IFilenameValidator&MockObject $filenameValidator;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $this->filenameValidator = $this->createMock(IFilenameValidator::class);
  28. $this->overwriteService(IFilenameValidator::class, $this->filenameValidator);
  29. $this->tmpDir = \OCP\Server::get(ITempManager::class)->getTemporaryFolder();
  30. $this->instance = new \OC\Files\Storage\CommonTest(['datadir' => $this->tmpDir]);
  31. }
  32. protected function tearDown(): void {
  33. \OC_Helper::rmdirr($this->tmpDir);
  34. $this->restoreService(IFilenameValidator::class);
  35. parent::tearDown();
  36. }
  37. public function testVerifyPath(): void {
  38. $this->filenameValidator
  39. ->expects($this->once())
  40. ->method('validateFilename')
  41. ->with('invalid:char.txt')
  42. ->willThrowException(new InvalidCharacterInPathException());
  43. $this->expectException(InvalidPathException::class);
  44. $this->instance->verifyPath('/', 'invalid:char.txt');
  45. }
  46. public function testVerifyPathSucceed(): void {
  47. $this->filenameValidator
  48. ->expects($this->once())
  49. ->method('validateFilename')
  50. ->with('valid-char.txt');
  51. $this->instance->verifyPath('/', 'valid-char.txt');
  52. }
  53. public function testMoveFromStorageWrapped(): void {
  54. /** @var \OC\Files\Storage\CommonTest|MockObject $instance */
  55. $instance = $this->getMockBuilder(\OC\Files\Storage\CommonTest::class)
  56. ->onlyMethods(['copyFromStorage', 'rmdir', 'unlink'])
  57. ->setConstructorArgs([['datadir' => $this->tmpDir]])
  58. ->getMock();
  59. $instance->method('copyFromStorage')
  60. ->willThrowException(new \Exception('copy'));
  61. $source = new Wrapper([
  62. 'storage' => $instance,
  63. ]);
  64. $instance->file_put_contents('foo.txt', 'bar');
  65. $instance->moveFromStorage($source, 'foo.txt', 'bar.txt');
  66. $this->assertTrue($instance->file_exists('bar.txt'));
  67. }
  68. public function testMoveFromStorageJailed(): void {
  69. /** @var \OC\Files\Storage\CommonTest|MockObject $instance */
  70. $instance = $this->getMockBuilder(\OC\Files\Storage\CommonTest::class)
  71. ->onlyMethods(['copyFromStorage', 'rmdir', 'unlink'])
  72. ->setConstructorArgs([['datadir' => $this->tmpDir]])
  73. ->getMock();
  74. $instance->method('copyFromStorage')
  75. ->willThrowException(new \Exception('copy'));
  76. $source = new Jail([
  77. 'storage' => $instance,
  78. 'root' => 'foo'
  79. ]);
  80. $source = new Wrapper([
  81. 'storage' => $source
  82. ]);
  83. $instance->mkdir('foo');
  84. $instance->file_put_contents('foo/foo.txt', 'bar');
  85. $instance->moveFromStorage($source, 'foo.txt', 'bar.txt');
  86. $this->assertTrue($instance->file_exists('bar.txt'));
  87. }
  88. public function testMoveFromStorageNestedJail(): void {
  89. /** @var \OC\Files\Storage\CommonTest|MockObject $instance */
  90. $instance = $this->getMockBuilder(\OC\Files\Storage\CommonTest::class)
  91. ->onlyMethods(['copyFromStorage', 'rmdir', 'unlink'])
  92. ->setConstructorArgs([['datadir' => $this->tmpDir]])
  93. ->getMock();
  94. $instance->method('copyFromStorage')
  95. ->willThrowException(new \Exception('copy'));
  96. $source = new Jail([
  97. 'storage' => $instance,
  98. 'root' => 'foo'
  99. ]);
  100. $source = new Wrapper([
  101. 'storage' => $source
  102. ]);
  103. $source = new Jail([
  104. 'storage' => $source,
  105. 'root' => 'bar'
  106. ]);
  107. $source = new Wrapper([
  108. 'storage' => $source
  109. ]);
  110. $instance->mkdir('foo');
  111. $instance->mkdir('foo/bar');
  112. $instance->file_put_contents('foo/bar/foo.txt', 'bar');
  113. $instance->moveFromStorage($source, 'foo.txt', 'bar.txt');
  114. $this->assertTrue($instance->file_exists('bar.txt'));
  115. }
  116. }