/** * @group server * @medium */ public function testPasswordAuthentication() { $connection = new Connection(\properties::$host, \properties::$port, new PasswordCredential(\properties::$user, \properties::$pass)); $this->assertTrue($connection->connect()); $this->assertTrue($connection->authenticate()); $this->assertTrue($connection->isAuthenticated()); $connection->disconnect(); }
/** * @dataProvider keyFileProvider * @group server * @medium */ public function testNoPublicKey($public, $private, $password) { $credential = new KeyCredential(\properties::$user, null, $private, $password); $connection = new Connection(\properties::$host, \properties::$port, $credential); $this->assertTrue($connection->connect()); $this->assertTrue($connection->authenticate()); $this->assertTrue($connection->isAuthenticated()); $connection->disconnect(); }
/** * Get an authenticated connection ready for transaction testing * * @param bool $doAuth * @throws \Exception * @return Connection */ protected function getConnection($doAuth = true) { $connection = new Connection(\properties::$host, \properties::$port, new PasswordCredential(\properties::$user, \properties::$pass)); if (!$connection->connect()) { throw new \Exception("Error connecting to test server"); } if ($doAuth) { if (!$connection->authenticate()) { throw new \Exception("Error authenticating on test server"); } } return $connection; }
/** * Typically called from Connection::getShell() * * @param Connection $connection * @param Terminal $terminal */ function __construct(Connection $connection, Terminal $terminal) { if (!$connection->isConnected()) { throw new NotConnectedException(); } if (!$connection->isAuthenticated()) { throw new NotAuthenticatedException(); } $this->shell_type = null; $this->connection = $connection; $this->terminal = $terminal; $this->resource = ssh2_shell($connection->getResource(), $terminal->getTerminalType(), $terminal->getEnv(), $terminal->getWidth(), $terminal->getHeight(), $terminal->getDimensionUnitType()); }
/** * Typically called from Connection::execute() * * @param string $command * @param Connection $connection * @param Terminal $terminal * @param string $pty */ function __construct($command, Connection $connection, Terminal $terminal, $pty = null) { $this->command = $command; $this->connection = $connection; $this->terminal = $terminal; if (!$connection->isConnected()) { throw new NotConnectedException(); } if (!$connection->isAuthenticated()) { throw new NotAuthenticatedException(); } $this->resource = ssh2_exec($connection->getResource(), $command, $pty, $terminal->getEnv(), $terminal->getWidth(), $terminal->getHeight(), $terminal->getDimensionUnitType()); }
/** * @small */ public function testBadConnection() { $logger = new Logger(); $connection = new Connection('invalid.host'); $connection->setLogger($logger); $this->assertFalse($connection->connect()); $this->assertFalse($connection->isConnected()); $this->assertContains('Disconnected', $logger->getHistory()); }
/** * @medium * @expectedException \Bravo3\SSH\Exceptions\NotAuthenticatedException * @group server */ public function testNoAuth() { $connection = new Connection(\properties::$host, \properties::$port, new PasswordCredential(\properties::$user, \properties::$pass)); if (!$connection->connect()) { throw new \Exception("Error connecting to test server"); } $shell = new Shell($connection, new Terminal()); // exception here $shell->sendln("hello"); $this->fail(); }
/** * Connect to the host via all tunnel nodes * * @return Connection|null */ protected function connect() { // Connect to target host $this->status(Phase::CONNECTION(), 0, 0, "Connecting to target host " . $this->host->getHostname() . ':' . $this->host->getPort()); $con = new Connection($this->host->getHostname(), $this->host->getPort(), $this->host->getCredential()); $con->setLogger($this->logger); if (!$con->connect()) { $this->status(Phase::ERROR(), 0, 0, "Failed to connect to target host"); $con->disconnectChain(); return null; } if (!$con->authenticate()) { $this->status(Phase::ERROR(), 0, 0, "Failed to authenticate on target host"); $con->disconnectChain(); return null; } return $con; }