SmbTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. /**
  36. * Class SmbTest
  37. *
  38. * @group DB
  39. *
  40. * @package OCA\Files_External\Tests\Storage
  41. */
  42. class SmbTest extends \Test\Files\Storage\Storage {
  43. /**
  44. * @var SMB instance
  45. */
  46. protected $instance;
  47. protected function setUp(): void {
  48. parent::setUp();
  49. $id = $this->getUniqueID();
  50. $config = include('files_external/tests/config.smb.php');
  51. if (!is_array($config) or !$config['run']) {
  52. $this->markTestSkipped('Samba backend not configured');
  53. }
  54. if (substr($config['root'], -1, 1) != '/') {
  55. $config['root'] .= '/';
  56. }
  57. $config['root'] .= $id; //make sure we have an new empty folder to work in
  58. $this->instance = new SMB($config);
  59. $this->instance->mkdir('/');
  60. }
  61. protected function tearDown(): void {
  62. if ($this->instance) {
  63. $this->instance->rmdir('');
  64. }
  65. parent::tearDown();
  66. }
  67. public function directoryProvider() {
  68. // doesn't support leading/trailing spaces
  69. return [['folder']];
  70. }
  71. public function testRenameWithSpaces() {
  72. $this->instance->mkdir('with spaces');
  73. $result = $this->instance->rename('with spaces', 'foo bar');
  74. $this->assertTrue($result);
  75. $this->assertTrue($this->instance->is_dir('foo bar'));
  76. }
  77. public function testStorageId() {
  78. $this->instance = new SMB([
  79. 'host' => 'testhost',
  80. 'user' => 'testuser',
  81. 'password' => 'somepass',
  82. 'share' => 'someshare',
  83. 'root' => 'someroot',
  84. ]);
  85. $this->assertEquals('smb::testuser@testhost//someshare//someroot/', $this->instance->getId());
  86. $this->instance = null;
  87. }
  88. public function testNotifyGetChanges() {
  89. $notifyHandler = $this->instance->notify('');
  90. sleep(1); //give time for the notify to start
  91. $this->instance->file_put_contents('/newfile.txt', 'test content');
  92. sleep(1);
  93. $this->instance->rename('/newfile.txt', 'renamed.txt');
  94. sleep(1);
  95. $this->instance->unlink('/renamed.txt');
  96. sleep(1); //time for all changes to be processed
  97. /** @var IChange[] $changes */
  98. $changes = [];
  99. $count = 0;
  100. // wait up to 10 seconds for incoming changes
  101. while (count($changes) < 3 && $count < 10) {
  102. $changes = array_merge($changes, $notifyHandler->getChanges());
  103. $count++;
  104. sleep(1);
  105. }
  106. $notifyHandler->stop();
  107. // depending on the server environment, the initial create might be detected as a change instead
  108. if ($changes[0]->getType() === IChange::MODIFIED) {
  109. $expected = [
  110. new Change(IChange::MODIFIED, 'newfile.txt'),
  111. new RenameChange(IChange::RENAMED, 'newfile.txt', 'renamed.txt'),
  112. new Change(IChange::REMOVED, 'renamed.txt')
  113. ];
  114. } else {
  115. $expected = [
  116. new Change(IChange::ADDED, 'newfile.txt'),
  117. new RenameChange(IChange::RENAMED, 'newfile.txt', 'renamed.txt'),
  118. new Change(IChange::REMOVED, 'renamed.txt')
  119. ];
  120. }
  121. foreach ($expected as $expectedChange) {
  122. $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));
  123. }
  124. }
  125. public function testNotifyListen() {
  126. $notifyHandler = $this->instance->notify('');
  127. usleep(100 * 1000); //give time for the notify to start
  128. $this->instance->file_put_contents('/newfile.txt', 'test content');
  129. $this->instance->unlink('/newfile.txt');
  130. usleep(100 * 1000); //time for all changes to be processed
  131. $result = null;
  132. // since the notify handler buffers until we start listening we will get the above changes
  133. $notifyHandler->listen(function (IChange $change) use (&$result) {
  134. $result = $change;
  135. return false;//stop listening
  136. });
  137. // depending on the server environment, the initial create might be detected as a change instead
  138. if ($result->getType() === IChange::ADDED) {
  139. $this->assertEquals(new Change(IChange::ADDED, 'newfile.txt'), $result);
  140. } else {
  141. $this->assertEquals(new Change(IChange::MODIFIED, 'newfile.txt'), $result);
  142. }
  143. }
  144. public function testRenameRoot() {
  145. // root can't be renamed
  146. $this->assertFalse($this->instance->rename('', 'foo1'));
  147. $this->instance->mkdir('foo2');
  148. $this->assertFalse($this->instance->rename('foo2', ''));
  149. $this->instance->rmdir('foo2');
  150. }
  151. public function testUnlinkRoot() {
  152. // root can't be deleted
  153. $this->assertFalse($this->instance->unlink(''));
  154. }
  155. public function testRmdirRoot() {
  156. // root can't be deleted
  157. $this->assertFalse($this->instance->rmdir(''));
  158. }
  159. }