2015-06-24 22:15:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Andreas Fischer <bantu@phpbb.com>
|
|
|
|
* @copyright 2015 Andreas Fischer
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
*/
|
|
|
|
|
2022-06-04 15:31:21 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-02-23 02:48:51 +00:00
|
|
|
namespace phpseclib3\Tests\Functional\Net;
|
|
|
|
|
2019-11-07 05:41:40 +00:00
|
|
|
use phpseclib3\Net\SFTP\Stream;
|
2022-08-18 13:05:57 +00:00
|
|
|
use phpseclib3\Net\SSH2;
|
2015-06-24 22:15:19 +00:00
|
|
|
|
2022-02-23 02:48:51 +00:00
|
|
|
class SFTPStreamTest extends SFTPTestCase
|
2015-06-24 22:15:19 +00:00
|
|
|
{
|
2022-06-04 15:31:21 +00:00
|
|
|
public static function setUpBeforeClass(): void
|
2015-07-03 23:18:19 +00:00
|
|
|
{
|
|
|
|
Stream::register();
|
|
|
|
parent::setUpBeforeClass();
|
|
|
|
}
|
|
|
|
|
2022-06-04 15:31:21 +00:00
|
|
|
public function testFopenFcloseCreatesFile(): void
|
2015-06-24 22:15:19 +00:00
|
|
|
{
|
2017-11-27 08:30:14 +00:00
|
|
|
$context = stream_context_create([
|
|
|
|
'sftp' => ['session' => $this->sftp],
|
|
|
|
]);
|
2015-06-24 22:15:19 +00:00
|
|
|
$fp = fopen($this->buildUrl('fooo.txt'), 'wb', false, $context);
|
2020-12-13 01:22:36 +00:00
|
|
|
$this->assertIsResource($fp);
|
2015-06-24 22:15:19 +00:00
|
|
|
fclose($fp);
|
2020-02-12 05:01:18 +00:00
|
|
|
$this->assertSame(0, $this->sftp->filesize('fooo.txt'));
|
2015-06-24 22:15:19 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 16:30:59 +00:00
|
|
|
/**
|
|
|
|
* @group github778
|
|
|
|
*/
|
2022-06-04 15:31:21 +00:00
|
|
|
public function testFilenameWithHash(): void
|
2016-04-10 16:30:59 +00:00
|
|
|
{
|
2017-11-27 08:30:14 +00:00
|
|
|
$context = stream_context_create([
|
|
|
|
'sftp' => ['session' => $this->sftp],
|
|
|
|
]);
|
2016-04-10 16:30:59 +00:00
|
|
|
$fp = fopen($this->buildUrl('te#st.txt'), 'wb', false, $context);
|
2022-08-11 13:12:15 +00:00
|
|
|
fwrite($fp, 'zzzz');
|
2016-04-10 16:30:59 +00:00
|
|
|
fclose($fp);
|
|
|
|
|
2017-12-07 20:08:19 +00:00
|
|
|
$this->assertContains('te#st.txt', $this->sftp->nlist());
|
2016-04-10 16:30:59 +00:00
|
|
|
}
|
|
|
|
|
2016-04-30 21:23:35 +00:00
|
|
|
/**
|
|
|
|
* Tests connection reuse functionality same as ssh2 extension:
|
|
|
|
* {@link http://php.net/manual/en/wrappers.ssh2.php#refsect1-wrappers.ssh2-examples}
|
|
|
|
*/
|
2022-06-04 15:31:21 +00:00
|
|
|
public function testConnectionReuse(): void
|
2016-04-30 21:23:35 +00:00
|
|
|
{
|
2022-08-18 13:05:57 +00:00
|
|
|
$originalConnectionsCount = count(SSH2::getConnections());
|
2016-04-30 21:23:35 +00:00
|
|
|
$session = $this->sftp;
|
|
|
|
$dirs = scandir("sftp://$session/");
|
2022-08-18 13:05:57 +00:00
|
|
|
$this->assertCount($originalConnectionsCount, SSH2::getConnections());
|
2017-11-27 08:30:14 +00:00
|
|
|
$this->assertEquals(['.', '..'], array_slice($dirs, 0, 2));
|
2016-04-30 21:23:35 +00:00
|
|
|
}
|
|
|
|
|
2020-12-19 03:29:09 +00:00
|
|
|
/**
|
|
|
|
* @group github1552
|
|
|
|
*/
|
2022-06-04 15:31:21 +00:00
|
|
|
public function testStreamSelect(): void
|
2020-12-19 03:29:09 +00:00
|
|
|
{
|
|
|
|
$context = stream_context_create([
|
|
|
|
'sftp' => ['session' => $this->sftp],
|
|
|
|
]);
|
|
|
|
$fp = fopen($this->buildUrl('fooo.txt'), 'wb', false, $context);
|
|
|
|
$read = [$fp];
|
|
|
|
$write = $except = null;
|
|
|
|
stream_select($read, $write, $except, 0);
|
|
|
|
}
|
|
|
|
|
2022-06-04 15:31:21 +00:00
|
|
|
protected function buildUrl($suffix): string
|
2015-06-24 22:15:19 +00:00
|
|
|
{
|
|
|
|
return sprintf(
|
|
|
|
'sftp://via-context/%s/%s',
|
|
|
|
$this->sftp->pwd(),
|
|
|
|
$suffix
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|