SmbTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 = $notifyHandler->getChanges();
  94. $notifyHandler->stop();
  95. $expected = [
  96. new Change(IChange::ADDED, 'newfile.txt'),
  97. new RenameChange(IChange::RENAMED, 'newfile.txt', 'renamed.txt'),
  98. new Change(IChange::REMOVED, 'renamed.txt')
  99. ];
  100. foreach ($expected as $expectedChange) {
  101. $this->assertContains($expectedChange, $changes, '', false, false); // dont check object identity
  102. }
  103. }
  104. public function testNotifyListen() {
  105. $notifyHandler = $this->instance->notify('');
  106. usleep(100 * 1000); //give time for the notify to start
  107. $this->instance->file_put_contents('/newfile.txt', 'test content');
  108. $this->instance->unlink('/newfile.txt');
  109. usleep(100 * 1000); //time for all changes to be processed
  110. $result = null;
  111. // since the notify handler buffers untill we start listening we will get the above changes
  112. $notifyHandler->listen(function (IChange $change) use (&$result) {
  113. $result = $change;
  114. return false;//stop listening
  115. });
  116. $this->assertEquals(new Change(IChange::ADDED, 'newfile.txt'), $result);
  117. }
  118. public function testRenameRoot() {
  119. // root can't be renamed
  120. $this->assertFalse($this->instance->rename('', 'foo1'));
  121. $this->instance->mkdir('foo2');
  122. $this->assertFalse($this->instance->rename('foo2', ''));
  123. $this->instance->rmdir('foo2');
  124. }
  125. public function testUnlinkRoot() {
  126. // root can't be deleted
  127. $this->assertFalse($this->instance->unlink(''));
  128. }
  129. public function testRmdirRoot() {
  130. // root can't be deleted
  131. $this->assertFalse($this->instance->rmdir(''));
  132. }
  133. }