Skip to content

Commit

Permalink
Return null when eof reached in socket
Browse files Browse the repository at this point in the history
  • Loading branch information
joelwurtz committed Feb 25, 2018
1 parent 1171eb3 commit eafddca
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 19 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
"psr/log": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^6.5",
"amphp/phpunit-util": "^1",
"friendsofphp/php-cs-fixer": "^2.9",
"monolog/monolog": "^1.23",
"friendsofphp/php-cs-fixer": "^2.9"
"phpunit/phpunit": "^6.5"
},
"autoload": {
"psr-4": {
Expand Down
22 changes: 16 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="./vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
syntaxCheck="true">
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.0/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<php>
<env name="SHELL_VERBOSITY" value="-1" />
</php>
Expand All @@ -24,4 +31,7 @@
</exclude>
</whitelist>
</filter>
<listeners>
<listener class="Amp\PHPUnit\LoopReset"/>
</listeners>
</phpunit>
8 changes: 5 additions & 3 deletions src/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Amp\ByteStream\InputStream;
use Amp\ByteStream\OutputStream;
use Amp\Deferred;
use Amp\Failure;
use Amp\Promise;
use Amp\Ssh\Channel\ChannelInputStream;
use Amp\Ssh\Channel\ChannelOutputStream;
Expand Down Expand Up @@ -55,7 +56,7 @@ public function __destruct() {

public function start(): Promise {
if ($this->resolved !== null || $this->exitCode !== null) {
throw new \RuntimeException('Process has already been started.');
return new Failure(new \RuntimeException('Process has already been started.'));
}

$this->resolved = new Deferred();
Expand All @@ -79,7 +80,7 @@ public function join(): Promise {
}

if ($this->resolved === null) {
throw new \RuntimeException('Process has not been started.');
return new Failure(new \RuntimeException('Process has not been started.'));
}

return $this->resolved->promise();
Expand All @@ -91,7 +92,7 @@ public function kill() {

public function signal(int $signo): Promise {
if (!$this->isRunning()) {
throw new \RuntimeException('Process is not running.');
return new Failure(new \RuntimeException('Process is not running.'));
}

return $this->session->signal($signo);
Expand Down Expand Up @@ -124,6 +125,7 @@ protected function handleRequests() {
if ($message instanceof ChannelRequestExitStatus) {
$resolved = $this->resolved;
$this->resolved = null;
$this->exitCode = $message->code;
$resolved->resolve($message->code);

$this->session->close();
Expand Down
4 changes: 4 additions & 0 deletions src/Transport/LoggerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public function read(): Promise {
return call(function () {
$packet = yield $this->handler->read();

if ($packet === null) {
return null;
}

if ($packet instanceof Message) {
$this->logger->debug(\sprintf('Receive %s packet', \get_class($packet)));
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/Transport/MessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public function read(): Promise {
return call(function () {
$packet = yield $this->handler->read();

if ($packet === null) {
return null;
}

if ($packet instanceof Message\Message) {
return $packet;
}
Expand Down
25 changes: 23 additions & 2 deletions src/Transport/PayloadReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,19 @@ public function read(): Promise {
the MAC algorithm MUST be "none".
*/
return call(function () {
$packetLength = \unpack('N', yield $this->doReadDecrypted(4))[1];
$packetLengthRead = yield $this->doReadDecrypted(4);

if ($packetLengthRead === null) {
return null;
}

$packetLength = \unpack('N', $packetLengthRead)[1];
$packet = yield $this->doReadDecrypted($packetLength);

if ($packet === null) {
return null;
}

$paddingLength = \unpack('C', $packet)[1];
$payload = \substr($packet, 1, $packetLength - $paddingLength - 1);
$padding = \substr($packet, $packetLength - $paddingLength);
Expand Down Expand Up @@ -102,6 +112,11 @@ private function doReadDecrypted(int $length): Promise {
return call(function () use ($length) {
while (\strlen($this->decryptedBuffer) < $length) {
$rawRead = yield $this->doReadRaw($this->decryption->getBlockSize());

if ($rawRead === null) {
return null;
}

$this->decryptedBuffer .= $this->decryption->decrypt($rawRead);
}

Expand All @@ -115,7 +130,13 @@ private function doReadDecrypted(int $length): Promise {
private function doReadRaw($length): Promise {
return call(function () use ($length) {
while (\strlen($this->cryptedBuffer) < $length) {
$this->cryptedBuffer .= yield $this->socket->read();
$readed = yield $this->socket->read();

if ($readed === null) {
return null;
}

$this->cryptedBuffer .= $readed;
}

$read = \substr($this->cryptedBuffer, 0, $length);
Expand Down
3 changes: 2 additions & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Amp\Ssh;

use Amp\Promise;
use Amp\Ssh\Authentication\Authentication;
use Amp\Ssh\Channel\Dispatcher;
use Amp\Ssh\Transport\LoggerHandler;
Expand All @@ -11,7 +12,7 @@
use Psr\Log\NullLogger;
use function Amp\call;

function connect($uri, Authentication $authentication, LoggerInterface $logger = null, $identification = 'SSH-2.0-AmpSSH_0.1') {
function connect($uri, Authentication $authentication, LoggerInterface $logger = null, $identification = 'SSH-2.0-AmpSSH_0.1'): Promise {
return call(function () use ($uri, $authentication, $identification, $logger) {
$socket = yield \Amp\Socket\connect($uri);
$logger = $logger ?? new NullLogger();
Expand Down
21 changes: 16 additions & 5 deletions tests/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,35 @@

namespace Amp\Ssh\Tests;

use Amp\Delayed;
use Amp\Loop;
use Amp\Ssh\Authentication\UsernamePassword;
use function Amp\Ssh\connect;
use Amp\Ssh\Process;
use PHPUnit\Framework\TestCase;

class ProcessTest extends TestCase
{
public function testProcess()
{
\Amp\Loop::run(function () {
$authentication = new \Amp\Ssh\Authentication\UsernamePassword('root', 'root');
$sshResource = yield \Amp\Ssh\connect('127.0.0.1:2222', $authentication);
Loop::run(function () {
$authentication = new UsernamePassword('root', 'root');
$sshResource = yield connect('127.0.0.1:2222', $authentication);

$process = new \Amp\Ssh\Process($sshResource, 'echo foo');
$process = new Process($sshResource, 'echo foo');

yield $process->start();

self::assertTrue($process->isRunning());

$output = yield $process->getStdout()->read();
yield $process->join();

self::assertEquals("foo\n", $output);

$exitCode = yield $process->join();

self::assertEquals(0, $exitCode);

yield $sshResource->close();
});
}
Expand Down

0 comments on commit eafddca

Please sign in to comment.