SftpTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Tests\Storage;
  8. use OCA\Files_External\Lib\Storage\SFTP;
  9. /**
  10. * Class SftpTest
  11. *
  12. * @group DB
  13. *
  14. * @package OCA\Files_External\Tests\Storage
  15. */
  16. class SftpTest extends \Test\Files\Storage\Storage {
  17. /**
  18. * @var SFTP instance
  19. */
  20. protected $instance;
  21. private $config;
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $id = $this->getUniqueID();
  25. $this->config = include('files_external/tests/config.sftp.php');
  26. if (!is_array($this->config) or !$this->config['run']) {
  27. $this->markTestSkipped('SFTP backend not configured');
  28. }
  29. $this->config['root'] .= '/' . $id; //make sure we have an new empty folder to work in
  30. $this->instance = new SFTP($this->config);
  31. $this->instance->mkdir('/');
  32. }
  33. protected function tearDown(): void {
  34. if ($this->instance) {
  35. $this->instance->rmdir('/');
  36. }
  37. parent::tearDown();
  38. }
  39. /**
  40. * @dataProvider configProvider
  41. */
  42. public function testStorageId($config, $expectedStorageId) {
  43. $instance = new SFTP($config);
  44. $this->assertEquals($expectedStorageId, $instance->getId());
  45. }
  46. public function configProvider() {
  47. return [
  48. [
  49. // no root path
  50. [
  51. 'run' => true,
  52. 'host' => 'somehost',
  53. 'user' => 'someuser',
  54. 'password' => 'somepassword',
  55. 'root' => '',
  56. ],
  57. 'sftp::someuser@somehost//',
  58. ],
  59. [
  60. // without leading nor trailing slash
  61. [
  62. 'run' => true,
  63. 'host' => 'somehost',
  64. 'user' => 'someuser',
  65. 'password' => 'somepassword',
  66. 'root' => 'remotedir/subdir',
  67. ],
  68. 'sftp::someuser@somehost//remotedir/subdir/',
  69. ],
  70. [
  71. // regular path
  72. [
  73. 'run' => true,
  74. 'host' => 'somehost',
  75. 'user' => 'someuser',
  76. 'password' => 'somepassword',
  77. 'root' => '/remotedir/subdir/',
  78. ],
  79. 'sftp::someuser@somehost//remotedir/subdir/',
  80. ],
  81. [
  82. // different port
  83. [
  84. 'run' => true,
  85. 'host' => 'somehost:8822',
  86. 'user' => 'someuser',
  87. 'password' => 'somepassword',
  88. 'root' => 'remotedir/subdir/',
  89. ],
  90. 'sftp::someuser@somehost:8822//remotedir/subdir/',
  91. ],
  92. [
  93. // ipv6 with port
  94. [
  95. 'run' => true,
  96. 'host' => 'FE80:0000:0000:0000:0202:B3FF:FE1E:8329',
  97. 'user' => 'someuser',
  98. 'password' => 'somepassword',
  99. 'root' => 'remotedir/subdir/',
  100. ],
  101. 'sftp::someuser@FE80:0000:0000:0000:0202:B3FF:FE1E:8329//remotedir/subdir/',
  102. ],
  103. [
  104. // ipv6 without port
  105. [
  106. 'run' => true,
  107. 'host' => 'FE80:0000:0000:0000:0202:B3FF:FE1E:8329:8822',
  108. 'user' => 'someuser',
  109. 'password' => 'somepassword',
  110. 'root' => 'remotedir/subdir/',
  111. ],
  112. 'sftp::someuser@FE80:0000:0000:0000:0202:B3FF:FE1E:8329:8822//remotedir/subdir/',
  113. ],
  114. [
  115. // collapsed ipv6 with port
  116. [
  117. 'run' => true,
  118. 'host' => 'FE80::0202:B3FF:FE1E:8329:8822',
  119. 'user' => 'someuser',
  120. 'password' => 'somepassword',
  121. 'root' => 'remotedir/subdir/',
  122. ],
  123. 'sftp::someuser@FE80::0202:B3FF:FE1E:8329:8822//remotedir/subdir/',
  124. ],
  125. ];
  126. }
  127. }