Skip to content

Commit

Permalink
Verify SFTP retries when dealing with peer resets
Browse files Browse the repository at this point in the history
This verifies that Flysystem SFTP adapter goes through all the
configured tries before throwing an `UnableToConnectToSftpHost`
exception.

Reverting the fix applied in
#1451 makes the test to
fail due to an unexpected `RuntimeException`.

Applying this for phpseclib v2 is a bit tricky, though, due to the lack
of knowledge on what has happened in the flow (e.g. `false` is returned
when a connection cannot be established or when an authentication error
happened). To make it work, the adapter for that version would need to
verify if there's a connection or not all the time - which doesn't sound
very good to me.
  • Loading branch information
lcobucci committed Apr 22, 2022
1 parent 3d7706a commit 464a8b1
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/PhpseclibV3/SftpConnectionProviderTest.php
Expand Up @@ -4,6 +4,7 @@

namespace League\Flysystem\PhpseclibV3;

use League\Flysystem\AdapterTestUtilities\ToxiproxyManagement;
use phpseclib3\Net\SFTP;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -241,6 +242,52 @@ public function providing_an_invalid_password(): void
$provider->provideConnection();
}

/**
* @test
*/
public function retries_several_times_until_failure(): void
{
$connectivityChecker = new class implements ConnectivityChecker {
/** @var int */
public $calls = 0;

public function isConnected(SFTP $connection): bool
{
++$this->calls;

return $connection->isConnected();
}
};

$managesConnectionToxics = ToxiproxyManagement::forServer();
$managesConnectionToxics->resetPeerOnRequest('sftp', 10);

$maxTries = 5;

$provider = SftpConnectionProvider::fromArray(
[
'host' => 'localhost',
'username' => 'bar',
'privateKey' => __DIR__ . '/../../test_files/sftp/id_rsa',
'passphrase' => 'secret',
'port' => 8222,
'maxTries' => $maxTries,
'timeout' => 1,
'connectivityChecker' => $connectivityChecker,
]
);

$this->expectException(UnableToConnectToSftpHost::class);

try {
$provider->provideConnection();
} finally {
$managesConnectionToxics->removeAllToxics();

self::assertSame($maxTries + 1, $connectivityChecker->calls);
}
}

private function computeFingerPrint(string $publicKey): string
{
$content = explode(' ', $publicKey, 3);
Expand Down

0 comments on commit 464a8b1

Please sign in to comment.