SmbTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Tests\Storage;
  8. use OC\Files\Notify\Change;
  9. use OC\Files\Notify\RenameChange;
  10. use OCA\Files_External\Lib\Storage\SMB;
  11. use OCP\Files\Notify\IChange;
  12. use PHPUnit\Framework\ExpectationFailedException;
  13. /**
  14. * Class SmbTest
  15. *
  16. * @group DB
  17. *
  18. * @package OCA\Files_External\Tests\Storage
  19. */
  20. class SmbTest extends \Test\Files\Storage\Storage {
  21. /**
  22. * @var SMB instance
  23. */
  24. protected $instance;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $id = $this->getUniqueID();
  28. $config = include('files_external/tests/config.smb.php');
  29. if (!is_array($config) or !$config['run']) {
  30. $this->markTestSkipped('Samba backend not configured');
  31. }
  32. if (substr($config['root'], -1, 1) != '/') {
  33. $config['root'] .= '/';
  34. }
  35. $config['root'] .= $id; //make sure we have an new empty folder to work in
  36. $this->instance = new SMB($config);
  37. $this->instance->mkdir('/');
  38. }
  39. protected function tearDown(): void {
  40. if ($this->instance) {
  41. $this->instance->rmdir('');
  42. }
  43. parent::tearDown();
  44. }
  45. public function directoryProvider() {
  46. // doesn't support leading/trailing spaces
  47. return [['folder']];
  48. }
  49. public function testRenameWithSpaces(): void {
  50. $this->instance->mkdir('with spaces');
  51. $result = $this->instance->rename('with spaces', 'foo bar');
  52. $this->assertTrue($result);
  53. $this->assertTrue($this->instance->is_dir('foo bar'));
  54. }
  55. public function testStorageId(): void {
  56. $this->instance = new SMB([
  57. 'host' => 'testhost',
  58. 'user' => 'testuser',
  59. 'password' => 'somepass',
  60. 'share' => 'someshare',
  61. 'root' => 'someroot',
  62. ]);
  63. $this->assertEquals('smb::testuser@testhost//someshare//someroot/', $this->instance->getId());
  64. $this->instance = null;
  65. }
  66. public function testNotifyGetChanges(): void {
  67. $lastError = null;
  68. for ($i = 0; $i < 5; $i++) {
  69. try {
  70. $this->tryTestNotifyGetChanges();
  71. return;
  72. } catch (ExpectationFailedException $e) {
  73. $lastError = $e;
  74. $this->tearDown();
  75. $this->setUp();
  76. sleep(1);
  77. }
  78. }
  79. throw $lastError;
  80. }
  81. private function tryTestNotifyGetChanges(): void {
  82. $notifyHandler = $this->instance->notify('');
  83. sleep(1); //give time for the notify to start
  84. $this->instance->file_put_contents('/newfile.txt', 'test content');
  85. sleep(1);
  86. $this->instance->rename('/newfile.txt', 'renamed.txt');
  87. sleep(1);
  88. $this->instance->unlink('/renamed.txt');
  89. sleep(1); //time for all changes to be processed
  90. /** @var IChange[] $changes */
  91. $changes = [];
  92. $count = 0;
  93. // wait up to 10 seconds for incoming changes
  94. while (count($changes) < 3 && $count < 10) {
  95. $changes = array_merge($changes, $notifyHandler->getChanges());
  96. $count++;
  97. sleep(1);
  98. }
  99. $notifyHandler->stop();
  100. // depending on the server environment, the initial create might be detected as a change instead
  101. if ($changes[0]->getType() === IChange::MODIFIED) {
  102. $expected = [
  103. new Change(IChange::MODIFIED, 'newfile.txt'),
  104. new RenameChange(IChange::RENAMED, 'newfile.txt', 'renamed.txt'),
  105. new Change(IChange::REMOVED, 'renamed.txt')
  106. ];
  107. } else {
  108. $expected = [
  109. new Change(IChange::ADDED, 'newfile.txt'),
  110. new RenameChange(IChange::RENAMED, 'newfile.txt', 'renamed.txt'),
  111. new Change(IChange::REMOVED, 'renamed.txt')
  112. ];
  113. }
  114. foreach ($expected as $expectedChange) {
  115. $this->assertTrue(in_array($expectedChange, $changes), "Expected changes are:\n" . print_r($expected, true) . PHP_EOL . 'Expected to find: ' . PHP_EOL . print_r($expectedChange, true) . "\nGot:\n" . print_r($changes, true));
  116. }
  117. }
  118. public function testNotifyListen(): void {
  119. $notifyHandler = $this->instance->notify('');
  120. usleep(100 * 1000); //give time for the notify to start
  121. $this->instance->file_put_contents('/newfile.txt', 'test content');
  122. $this->instance->unlink('/newfile.txt');
  123. usleep(100 * 1000); //time for all changes to be processed
  124. $result = null;
  125. // since the notify handler buffers until we start listening we will get the above changes
  126. $notifyHandler->listen(function (IChange $change) use (&$result) {
  127. $result = $change;
  128. return false;//stop listening
  129. });
  130. // depending on the server environment, the initial create might be detected as a change instead
  131. if ($result->getType() === IChange::ADDED) {
  132. $this->assertEquals(new Change(IChange::ADDED, 'newfile.txt'), $result);
  133. } else {
  134. $this->assertEquals(new Change(IChange::MODIFIED, 'newfile.txt'), $result);
  135. }
  136. }
  137. public function testRenameRoot(): void {
  138. // root can't be renamed
  139. $this->assertFalse($this->instance->rename('', 'foo1'));
  140. $this->instance->mkdir('foo2');
  141. $this->assertFalse($this->instance->rename('foo2', ''));
  142. $this->instance->rmdir('foo2');
  143. }
  144. public function testUnlinkRoot(): void {
  145. // root can't be deleted
  146. $this->assertFalse($this->instance->unlink(''));
  147. }
  148. public function testRmdirRoot(): void {
  149. // root can't be deleted
  150. $this->assertFalse($this->instance->rmdir(''));
  151. }
  152. }