phpseclib/tests/Functional/Net/SSH2Test.php

200 lines
5.4 KiB
PHP
Raw Normal View History

<?php
/**
* @author Andreas Fischer <bantu@phpbb.com>
* @copyright 2014 Andreas Fischer
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
namespace phpseclib3\Tests\Functional\Net;
use phpseclib3\Net\SSH2;
use phpseclib3\Tests\PhpseclibFunctionalTestCase;
2014-12-10 01:31:41 +00:00
class SSH2Test extends PhpseclibFunctionalTestCase
{
public function testConstructor()
{
2014-12-10 01:31:41 +00:00
$ssh = new SSH2($this->getEnv('SSH_HOSTNAME'));
2020-12-13 01:34:38 +00:00
$this->assertIsObject(
2017-12-07 20:08:19 +00:00
$ssh,
'Could not construct NET_SSH2 object.'
);
return $ssh;
}
/**
2015-03-29 16:07:17 +00:00
* @depends testConstructor
* @group github408
* @group github412
2022-03-09 00:59:30 +00:00
* @param SSH2 $ssh
2015-03-29 16:07:17 +00:00
*/
2014-07-20 20:58:24 +00:00
public function testPreLogin($ssh)
{
$this->assertFalse(
$ssh->isConnected(),
'Failed asserting that SSH2 is not connected after construction.'
);
$this->assertFalse(
$ssh->isAuthenticated(),
'Failed asserting that SSH2 is not authenticated after construction.'
);
2014-07-20 20:58:24 +00:00
$this->assertNotEmpty(
$ssh->getServerPublicHostKey(),
'Failed asserting that a non-empty public host key was fetched.'
);
$this->assertTrue(
$ssh->isConnected(),
'Failed asserting that SSH2 is connected after public key fetch.'
);
$this->assertNotEmpty(
$ssh->getServerIdentification(),
'Failed asserting that the server identifier was set after connect.'
);
return $ssh;
}
/**
2015-03-29 16:07:17 +00:00
* @depends testPreLogin
2022-03-09 00:59:30 +00:00
* @param SSH2 $ssh
2015-03-29 16:07:17 +00:00
*/
public function testBadPassword($ssh)
{
$username = $this->getEnv('SSH_USERNAME');
$password = $this->getEnv('SSH_PASSWORD');
$this->assertFalse(
$ssh->login($username, 'zzz' . $password),
'SSH2 login using password succeeded.'
);
$this->assertTrue(
$ssh->isConnected(),
'Failed asserting that SSH2 is connected after bad login attempt.'
);
$this->assertFalse(
$ssh->isAuthenticated(),
'Failed asserting that SSH2 is not authenticated after bad login attempt.'
);
return $ssh;
}
/**
* @depends testBadPassword
2022-03-09 00:59:30 +00:00
* @param SSH2 $ssh
*/
public function testPasswordLogin($ssh)
{
$username = $this->getEnv('SSH_USERNAME');
$password = $this->getEnv('SSH_PASSWORD');
$this->assertTrue(
$ssh->login($username, $password),
'SSH2 login using password failed.'
);
2014-03-06 10:35:54 +00:00
$this->assertTrue(
$ssh->isAuthenticated(),
'Failed asserting that SSH2 is authenticated after good login attempt.'
);
2014-03-06 10:35:54 +00:00
return $ssh;
}
/**
2015-03-29 16:07:17 +00:00
* @depends testPasswordLogin
* @group github280
2022-03-09 00:59:30 +00:00
* @param SSH2 $ssh
2015-03-29 16:07:17 +00:00
*/
2014-03-06 10:35:54 +00:00
public function testExecWithMethodCallback($ssh)
{
2017-12-14 06:31:32 +00:00
$callbackObject = $this->getMockBuilder('stdClass')
2022-01-30 15:34:42 +00:00
->setMethods(['callbackMethod'])
2017-12-14 06:31:32 +00:00
->getMock();
2014-03-06 10:35:54 +00:00
$callbackObject
->expects($this->atLeastOnce())
->method('callbackMethod')
->will($this->returnValue(true));
2017-11-27 08:30:14 +00:00
$ssh->exec('pwd', [$callbackObject, 'callbackMethod']);
2017-05-28 13:58:00 +00:00
return $ssh;
}
public function testGetServerPublicHostKey()
{
2014-12-10 01:31:41 +00:00
$ssh = new SSH2($this->getEnv('SSH_HOSTNAME'));
$this->assertIsString($ssh->getServerPublicHostKey());
}
public function testOpenSocketConnect()
{
$fsock = fsockopen($this->getEnv('SSH_HOSTNAME'), 22);
$ssh = new SSH2($fsock);
$username = $this->getEnv('SSH_USERNAME');
$password = $this->getEnv('SSH_PASSWORD');
$this->assertTrue(
$ssh->login($username, $password),
'SSH2 login using an open socket failed.'
);
}
2017-05-28 13:58:00 +00:00
/**
* @depends testExecWithMethodCallback
* @group github1009
2022-03-09 00:59:30 +00:00
* @param SSH2 $ssh
2017-05-28 13:58:00 +00:00
*/
public function testDisablePTY($ssh)
{
$ssh->enablePTY();
$ssh->exec('ls -latr');
$ssh->disablePTY();
$ssh->exec('pwd');
2017-09-06 05:27:07 +00:00
$this->assertTrue(true);
2017-09-06 05:27:07 +00:00
return $ssh;
}
/**
* @depends testDisablePTY
* @group github1167
2022-03-09 00:59:30 +00:00
* @param SSH2 $ssh
2017-09-06 05:27:07 +00:00
*/
public function testChannelDataAfterOpen($ssh)
{
// Ubuntu's OpenSSH from 5.8 to 6.9 didn't work with multiple channels. see
// https://bugs.launchpad.net/ubuntu/+source/openssh/+bug/1334916 for more info.
// https://lists.ubuntu.com/archives/oneiric-changes/2011-July/005772.html discusses
// when consolekit was incorporated.
// https://marc.info/?l=openssh-unix-dev&m=163409903417589&w=2 discusses some of the
// issues with how Ubuntu incorporated consolekit
2022-03-09 00:59:30 +00:00
$pattern = '#^SSH-2\.0-OpenSSH_([\d.]+)[^ ]* Ubuntu-.*$#';
$match = preg_match($pattern, $ssh->getServerIdentification(), $matches);
$match = $match && version_compare('5.8', $matches[1], '<=');
$match = $match && version_compare('6.9', $matches[1], '>=');
if ($match) {
self::markTestSkipped('Ubuntu\'s OpenSSH >= 5.8 <= 6.9 didn\'t work well with multiple channels');
}
2017-09-06 05:27:07 +00:00
$ssh->write("ping 127.0.0.1\n");
$ssh->enablePTY();
$ssh->exec('bash');
$ssh->write("ls -latr\n");
$ssh->setTimeout(1);
2022-03-09 00:59:30 +00:00
$this->assertIsString($ssh->read());
2017-05-28 13:58:00 +00:00
}
}