SFTP_KeyTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Robin McCorkell <robin@mccorkell.me.uk>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Ross Nicoll <jrn@jrn.me.uk>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Viktor Szépe <viktor@szepe.net>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Files_External\Tests\Storage;
  28. use OCA\Files_External\Lib\Storage\SFTP_Key;
  29. /**
  30. * Class SFTP_KeyTest
  31. *
  32. * @group DB
  33. *
  34. * @package OCA\Files_External\Tests\Storage
  35. */
  36. class SFTP_KeyTest extends \Test\Files\Storage\Storage {
  37. private $config;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $id = $this->getUniqueID();
  41. $this->config = include('files_external/tests/config.php');
  42. if (! is_array($this->config) or ! isset($this->config['sftp_key']) or ! $this->config['sftp_key']['run']) {
  43. $this->markTestSkipped('SFTP with key backend not configured');
  44. }
  45. // Make sure we have an new empty folder to work in
  46. $this->config['sftp_key']['root'] .= '/' . $id;
  47. $this->instance = new SFTP_Key($this->config['sftp_key']);
  48. $this->instance->mkdir('/');
  49. }
  50. protected function tearDown(): void {
  51. if ($this->instance) {
  52. $this->instance->rmdir('/');
  53. }
  54. parent::tearDown();
  55. }
  56. public function testInvalidAddressShouldThrowException() {
  57. $this->expectException(\InvalidArgumentException::class);
  58. // I'd use example.com for this, but someone decided to break the spec and make it resolve
  59. $this->instance->assertHostAddressValid('notarealaddress...');
  60. }
  61. public function testValidAddressShouldPass() {
  62. $this->assertTrue($this->instance->assertHostAddressValid('localhost'));
  63. }
  64. public function testNegativePortNumberShouldThrowException() {
  65. $this->expectException(\InvalidArgumentException::class);
  66. $this->instance->assertPortNumberValid('-1');
  67. }
  68. public function testNonNumericalPortNumberShouldThrowException() {
  69. $this->expectException(\InvalidArgumentException::class);
  70. $this->instance->assertPortNumberValid('a');
  71. }
  72. public function testHighPortNumberShouldThrowException() {
  73. $this->expectException(\InvalidArgumentException::class);
  74. $this->instance->assertPortNumberValid('65536');
  75. }
  76. public function testValidPortNumberShouldPass() {
  77. $this->assertTrue($this->instance->assertPortNumberValid('22222'));
  78. }
  79. }