SFTP_KeyTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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_Key;
  9. /**
  10. * Class SFTP_KeyTest
  11. *
  12. * @group DB
  13. *
  14. * @package OCA\Files_External\Tests\Storage
  15. */
  16. class SFTP_KeyTest extends \Test\Files\Storage\Storage {
  17. private $config;
  18. protected function setUp(): void {
  19. parent::setUp();
  20. $id = $this->getUniqueID();
  21. $this->config = include('files_external/tests/config.php');
  22. if (! is_array($this->config) or ! isset($this->config['sftp_key']) or ! $this->config['sftp_key']['run']) {
  23. $this->markTestSkipped('SFTP with key backend not configured');
  24. }
  25. // Make sure we have an new empty folder to work in
  26. $this->config['sftp_key']['root'] .= '/' . $id;
  27. $this->instance = new SFTP_Key($this->config['sftp_key']);
  28. $this->instance->mkdir('/');
  29. }
  30. protected function tearDown(): void {
  31. if ($this->instance) {
  32. $this->instance->rmdir('/');
  33. }
  34. parent::tearDown();
  35. }
  36. public function testInvalidAddressShouldThrowException() {
  37. $this->expectException(\InvalidArgumentException::class);
  38. // I'd use example.com for this, but someone decided to break the spec and make it resolve
  39. $this->instance->assertHostAddressValid('notarealaddress...');
  40. }
  41. public function testValidAddressShouldPass() {
  42. $this->assertTrue($this->instance->assertHostAddressValid('localhost'));
  43. }
  44. public function testNegativePortNumberShouldThrowException() {
  45. $this->expectException(\InvalidArgumentException::class);
  46. $this->instance->assertPortNumberValid('-1');
  47. }
  48. public function testNonNumericalPortNumberShouldThrowException() {
  49. $this->expectException(\InvalidArgumentException::class);
  50. $this->instance->assertPortNumberValid('a');
  51. }
  52. public function testHighPortNumberShouldThrowException() {
  53. $this->expectException(\InvalidArgumentException::class);
  54. $this->instance->assertPortNumberValid('65536');
  55. }
  56. public function testValidPortNumberShouldPass() {
  57. $this->assertTrue($this->instance->assertPortNumberValid('22222'));
  58. }
  59. }