SmbTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Juan Pablo Villafáñez <jvillafanez@solidgear.es>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Robin McCorkell <robin@mccorkell.me.uk>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Vincent Petry <vincent@nextcloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCA\Files_External\Tests\Storage;
  31. use OC\Files\Notify\Change;
  32. use OC\Files\Notify\RenameChange;
  33. use OCA\Files_External\Lib\Storage\SMB;
  34. use OCP\Files\Notify\IChange;
  35. use PHPUnit\Framework\ExpectationFailedException;
  36. /**
  37. * Class SmbTest
  38. *
  39. * @group DB
  40. *
  41. * @package OCA\Files_External\Tests\Storage
  42. */
  43. class SmbTest extends \Test\Files\Storage\Storage {
  44. /**
  45. * @var SMB instance
  46. */
  47. protected $instance;
  48. protected function setUp(): void {
  49. parent::setUp();
  50. $id = $this->getUniqueID();
  51. $config = include('files_external/tests/config.smb.php');
  52. if (!is_array($config) or !$config['run']) {
  53. $this->markTestSkipped('Samba backend not configured');
  54. }
  55. if (substr($config['root'], -1, 1) != '/') {
  56. $config['root'] .= '/';
  57. }
  58. $config['root'] .= $id; //make sure we have an new empty folder to work in
  59. $this->instance = new SMB($config);
  60. $this->instance->mkdir('/');
  61. }
  62. protected function tearDown(): void {
  63. if ($this->instance) {
  64. $this->instance->rmdir('');
  65. }
  66. parent::tearDown();
  67. }
  68. public function directoryProvider() {
  69. // doesn't support leading/trailing spaces
  70. return [['folder']];
  71. }
  72. public function testRenameWithSpaces() {
  73. $this->instance->mkdir('with spaces');
  74. $result = $this->instance->rename('with spaces', 'foo bar');
  75. $this->assertTrue($result);
  76. $this->assertTrue($this->instance->is_dir('foo bar'));
  77. }
  78. public function testStorageId() {
  79. $this->instance = new SMB([
  80. 'host' => 'testhost',
  81. 'user' => 'testuser',
  82. 'password' => 'somepass',
  83. 'share' => 'someshare',
  84. 'root' => 'someroot',
  85. ]);
  86. $this->assertEquals('smb::testuser@testhost//someshare//someroot/', $this->instance->getId());
  87. $this->instance = null;
  88. }
  89. public function testNotifyGetChanges() {
  90. $lastError = null;
  91. for($i = 0; $i < 5; $i++) {
  92. try {
  93. $this->tryTestNotifyGetChanges();
  94. return;
  95. } catch (ExpectationFailedException $e) {
  96. $lastError = $e;
  97. $this->tearDown();
  98. $this->setUp();
  99. sleep(1);
  100. }
  101. }
  102. throw $lastError;
  103. }
  104. private function tryTestNotifyGetChanges(): void {
  105. $notifyHandler = $this->instance->notify('');
  106. sleep(1); //give time for the notify to start
  107. $this->instance->file_put_contents('/newfile.txt', 'test content');
  108. sleep(1);
  109. $this->instance->rename('/newfile.txt', 'renamed.txt');
  110. sleep(1);
  111. $this->instance->unlink('/renamed.txt');
  112. sleep(1); //time for all changes to be processed
  113. /** @var IChange[] $changes */
  114. $changes = [];
  115. $count = 0;
  116. // wait up to 10 seconds for incoming changes
  117. while (count($changes) < 3 && $count < 10) {
  118. $changes = array_merge($changes, $notifyHandler->getChanges());
  119. $count++;
  120. sleep(1);
  121. }
  122. $notifyHandler->stop();
  123. // depending on the server environment, the initial create might be detected as a change instead
  124. if ($changes[0]->getType() === IChange::MODIFIED) {
  125. $expected = [
  126. new Change(IChange::MODIFIED, 'newfile.txt'),
  127. new RenameChange(IChange::RENAMED, 'newfile.txt', 'renamed.txt'),
  128. new Change(IChange::REMOVED, 'renamed.txt')
  129. ];
  130. } else {
  131. $expected = [
  132. new Change(IChange::ADDED, 'newfile.txt'),
  133. new RenameChange(IChange::RENAMED, 'newfile.txt', 'renamed.txt'),
  134. new Change(IChange::REMOVED, 'renamed.txt')
  135. ];
  136. }
  137. foreach ($expected as $expectedChange) {
  138. $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));
  139. }
  140. }
  141. public function testNotifyListen() {
  142. $notifyHandler = $this->instance->notify('');
  143. usleep(100 * 1000); //give time for the notify to start
  144. $this->instance->file_put_contents('/newfile.txt', 'test content');
  145. $this->instance->unlink('/newfile.txt');
  146. usleep(100 * 1000); //time for all changes to be processed
  147. $result = null;
  148. // since the notify handler buffers until we start listening we will get the above changes
  149. $notifyHandler->listen(function (IChange $change) use (&$result) {
  150. $result = $change;
  151. return false;//stop listening
  152. });
  153. // depending on the server environment, the initial create might be detected as a change instead
  154. if ($result->getType() === IChange::ADDED) {
  155. $this->assertEquals(new Change(IChange::ADDED, 'newfile.txt'), $result);
  156. } else {
  157. $this->assertEquals(new Change(IChange::MODIFIED, 'newfile.txt'), $result);
  158. }
  159. }
  160. public function testRenameRoot() {
  161. // root can't be renamed
  162. $this->assertFalse($this->instance->rename('', 'foo1'));
  163. $this->instance->mkdir('foo2');
  164. $this->assertFalse($this->instance->rename('foo2', ''));
  165. $this->instance->rmdir('foo2');
  166. }
  167. public function testUnlinkRoot() {
  168. // root can't be deleted
  169. $this->assertFalse($this->instance->unlink(''));
  170. }
  171. public function testRmdirRoot() {
  172. // root can't be deleted
  173. $this->assertFalse($this->instance->rmdir(''));
  174. }
  175. }