SmbTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. $changes = [];
  114. $count = 0;
  115. // wait up to 10 seconds for incoming changes
  116. while (count($changes) < 3 && $count < 10) {
  117. $changes = array_merge($changes, $notifyHandler->getChanges());
  118. $count++;
  119. sleep(1);
  120. }
  121. $notifyHandler->stop();
  122. $expected = [
  123. new Change(IChange::ADDED, 'newfile.txt'),
  124. new RenameChange(IChange::RENAMED, 'newfile.txt', 'renamed.txt'),
  125. new Change(IChange::REMOVED, 'renamed.txt')
  126. ];
  127. foreach ($expected as $expectedChange) {
  128. $this->assertTrue(in_array($expectedChange, $changes), 'Actual changes are:' . PHP_EOL . print_r($changes, true) . PHP_EOL . 'Expected to find: ' . PHP_EOL . print_r($expectedChange, true));
  129. }
  130. }
  131. public function testNotifyListen() {
  132. $notifyHandler = $this->instance->notify('');
  133. usleep(100 * 1000); //give time for the notify to start
  134. $this->instance->file_put_contents('/newfile.txt', 'test content');
  135. $this->instance->unlink('/newfile.txt');
  136. usleep(100 * 1000); //time for all changes to be processed
  137. $result = null;
  138. // since the notify handler buffers until we start listening we will get the above changes
  139. $notifyHandler->listen(function (IChange $change) use (&$result) {
  140. $result = $change;
  141. return false;//stop listening
  142. });
  143. $this->assertEquals(new Change(IChange::ADDED, 'newfile.txt'), $result);
  144. }
  145. public function testRenameRoot() {
  146. // root can't be renamed
  147. $this->assertFalse($this->instance->rename('', 'foo1'));
  148. $this->instance->mkdir('foo2');
  149. $this->assertFalse($this->instance->rename('foo2', ''));
  150. $this->instance->rmdir('foo2');
  151. }
  152. public function testUnlinkRoot() {
  153. // root can't be deleted
  154. $this->assertFalse($this->instance->unlink(''));
  155. }
  156. public function testRmdirRoot() {
  157. // root can't be deleted
  158. $this->assertFalse($this->instance->rmdir(''));
  159. }
  160. }