SmbTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. $changes = [];
  98. $count = 0;
  99. // wait up to 10 seconds for incoming changes
  100. while (count($changes) < 3 && $count < 10) {
  101. $changes = array_merge($changes, $notifyHandler->getChanges());
  102. $count++;
  103. sleep(1);
  104. }
  105. $notifyHandler->stop();
  106. $expected = [
  107. new Change(IChange::ADDED, 'newfile.txt'),
  108. new RenameChange(IChange::RENAMED, 'newfile.txt', 'renamed.txt'),
  109. new Change(IChange::REMOVED, 'renamed.txt')
  110. ];
  111. foreach ($expected as $expectedChange) {
  112. $this->assertContains($expectedChange, $changes, 'Actual changes are:' . PHP_EOL . print_r($expected, true), false, false); // dont check object identity
  113. }
  114. }
  115. public function testNotifyListen() {
  116. $notifyHandler = $this->instance->notify('');
  117. usleep(100 * 1000); //give time for the notify to start
  118. $this->instance->file_put_contents('/newfile.txt', 'test content');
  119. $this->instance->unlink('/newfile.txt');
  120. usleep(100 * 1000); //time for all changes to be processed
  121. $result = null;
  122. // since the notify handler buffers until we start listening we will get the above changes
  123. $notifyHandler->listen(function (IChange $change) use (&$result) {
  124. $result = $change;
  125. return false;//stop listening
  126. });
  127. $this->assertEquals(new Change(IChange::ADDED, 'newfile.txt'), $result);
  128. }
  129. public function testRenameRoot() {
  130. // root can't be renamed
  131. $this->assertFalse($this->instance->rename('', 'foo1'));
  132. $this->instance->mkdir('foo2');
  133. $this->assertFalse($this->instance->rename('foo2', ''));
  134. $this->instance->rmdir('foo2');
  135. }
  136. public function testUnlinkRoot() {
  137. // root can't be deleted
  138. $this->assertFalse($this->instance->unlink(''));
  139. }
  140. public function testRmdirRoot() {
  141. // root can't be deleted
  142. $this->assertFalse($this->instance->rmdir(''));
  143. }
  144. }