SmbTest.php 4.7 KB

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