SFTP_KeyTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin McCorkell <robin@mccorkell.me.uk>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Ross Nicoll <jrn@jrn.me.uk>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Viktor Szépe <viktor@szepe.net>
  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 OCA\Files_External\Lib\Storage\SFTP_Key;
  30. /**
  31. * Class SFTP_KeyTest
  32. *
  33. * @group DB
  34. *
  35. * @package OCA\Files_External\Tests\Storage
  36. */
  37. class SFTP_KeyTest extends \Test\Files\Storage\Storage {
  38. private $config;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $id = $this->getUniqueID();
  42. $this->config = include('files_external/tests/config.php');
  43. if (! is_array($this->config) or ! isset($this->config['sftp_key']) or ! $this->config['sftp_key']['run']) {
  44. $this->markTestSkipped('SFTP with key backend not configured');
  45. }
  46. // Make sure we have an new empty folder to work in
  47. $this->config['sftp_key']['root'] .= '/' . $id;
  48. $this->instance = new SFTP_Key($this->config['sftp_key']);
  49. $this->instance->mkdir('/');
  50. }
  51. protected function tearDown(): void {
  52. if ($this->instance) {
  53. $this->instance->rmdir('/');
  54. }
  55. parent::tearDown();
  56. }
  57. public function testInvalidAddressShouldThrowException() {
  58. $this->expectException(\InvalidArgumentException::class);
  59. // I'd use example.com for this, but someone decided to break the spec and make it resolve
  60. $this->instance->assertHostAddressValid('notarealaddress...');
  61. }
  62. public function testValidAddressShouldPass() {
  63. $this->assertTrue($this->instance->assertHostAddressValid('localhost'));
  64. }
  65. public function testNegativePortNumberShouldThrowException() {
  66. $this->expectException(\InvalidArgumentException::class);
  67. $this->instance->assertPortNumberValid('-1');
  68. }
  69. public function testNonNumericalPortNumberShouldThrowException() {
  70. $this->expectException(\InvalidArgumentException::class);
  71. $this->instance->assertPortNumberValid('a');
  72. }
  73. public function testHighPortNumberShouldThrowException() {
  74. $this->expectException(\InvalidArgumentException::class);
  75. $this->instance->assertPortNumberValid('65536');
  76. }
  77. public function testValidPortNumberShouldPass() {
  78. $this->assertTrue($this->instance->assertPortNumberValid('22222'));
  79. }
  80. }