SftpTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author hkjolhede <hkjolhede@gmail.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Files_External\Tests\Storage;
  30. use OCA\Files_External\Lib\Storage\SFTP;
  31. /**
  32. * Class SftpTest
  33. *
  34. * @group DB
  35. *
  36. * @package OCA\Files_External\Tests\Storage
  37. */
  38. class SftpTest extends \Test\Files\Storage\Storage {
  39. /**
  40. * @var SFTP instance
  41. */
  42. protected $instance;
  43. private $config;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $id = $this->getUniqueID();
  47. $this->config = include('files_external/tests/config.sftp.php');
  48. if (!is_array($this->config) or !$this->config['run']) {
  49. $this->markTestSkipped('SFTP backend not configured');
  50. }
  51. $this->config['root'] .= '/' . $id; //make sure we have an new empty folder to work in
  52. $this->instance = new SFTP($this->config);
  53. $this->instance->mkdir('/');
  54. }
  55. protected function tearDown(): void {
  56. if ($this->instance) {
  57. $this->instance->rmdir('/');
  58. }
  59. parent::tearDown();
  60. }
  61. /**
  62. * @dataProvider configProvider
  63. */
  64. public function testStorageId($config, $expectedStorageId) {
  65. $instance = new SFTP($config);
  66. $this->assertEquals($expectedStorageId, $instance->getId());
  67. }
  68. public function configProvider() {
  69. return [
  70. [
  71. // no root path
  72. [
  73. 'run' => true,
  74. 'host' => 'somehost',
  75. 'user' => 'someuser',
  76. 'password' => 'somepassword',
  77. 'root' => '',
  78. ],
  79. 'sftp::someuser@somehost//',
  80. ],
  81. [
  82. // without leading nor trailing slash
  83. [
  84. 'run' => true,
  85. 'host' => 'somehost',
  86. 'user' => 'someuser',
  87. 'password' => 'somepassword',
  88. 'root' => 'remotedir/subdir',
  89. ],
  90. 'sftp::someuser@somehost//remotedir/subdir/',
  91. ],
  92. [
  93. // regular path
  94. [
  95. 'run' => true,
  96. 'host' => 'somehost',
  97. 'user' => 'someuser',
  98. 'password' => 'somepassword',
  99. 'root' => '/remotedir/subdir/',
  100. ],
  101. 'sftp::someuser@somehost//remotedir/subdir/',
  102. ],
  103. [
  104. // different port
  105. [
  106. 'run' => true,
  107. 'host' => 'somehost:8822',
  108. 'user' => 'someuser',
  109. 'password' => 'somepassword',
  110. 'root' => 'remotedir/subdir/',
  111. ],
  112. 'sftp::someuser@somehost:8822//remotedir/subdir/',
  113. ],
  114. [
  115. // ipv6 with port
  116. [
  117. 'run' => true,
  118. 'host' => 'FE80:0000:0000:0000:0202:B3FF:FE1E:8329',
  119. 'user' => 'someuser',
  120. 'password' => 'somepassword',
  121. 'root' => 'remotedir/subdir/',
  122. ],
  123. 'sftp::someuser@FE80:0000:0000:0000:0202:B3FF:FE1E:8329//remotedir/subdir/',
  124. ],
  125. [
  126. // ipv6 without port
  127. [
  128. 'run' => true,
  129. 'host' => 'FE80:0000:0000:0000:0202:B3FF:FE1E:8329:8822',
  130. 'user' => 'someuser',
  131. 'password' => 'somepassword',
  132. 'root' => 'remotedir/subdir/',
  133. ],
  134. 'sftp::someuser@FE80:0000:0000:0000:0202:B3FF:FE1E:8329:8822//remotedir/subdir/',
  135. ],
  136. [
  137. // collapsed ipv6 with port
  138. [
  139. 'run' => true,
  140. 'host' => 'FE80::0202:B3FF:FE1E:8329:8822',
  141. 'user' => 'someuser',
  142. 'password' => 'somepassword',
  143. 'root' => 'remotedir/subdir/',
  144. ],
  145. 'sftp::someuser@FE80::0202:B3FF:FE1E:8329:8822//remotedir/subdir/',
  146. ],
  147. ];
  148. }
  149. }