2014-03-03 01:43:58 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Andreas Fischer <bantu@phpbb.com>
|
2014-12-10 00:02:44 +01:00
|
|
|
* @copyright 2014 Andreas Fischer
|
2014-03-03 01:43:58 +01:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
*/
|
|
|
|
|
2019-12-18 14:00:20 +01:00
|
|
|
use tgseclib\Net\SSH2;
|
2014-12-10 02:31:41 +01:00
|
|
|
|
2014-06-01 21:13:20 +02:00
|
|
|
class Functional_Net_SSH2Test extends PhpseclibFunctionalTestCase
|
2014-03-03 01:43:58 +01:00
|
|
|
{
|
|
|
|
public function testConstructor()
|
|
|
|
{
|
2014-12-10 02:31:41 +01:00
|
|
|
$ssh = new SSH2($this->getEnv('SSH_HOSTNAME'));
|
2014-03-03 01:43:58 +01:00
|
|
|
|
2017-12-07 21:08:19 +01:00
|
|
|
$this->assertInternalType(
|
|
|
|
'object',
|
|
|
|
$ssh,
|
2014-03-03 01:43:58 +01:00
|
|
|
'Could not construct NET_SSH2 object.'
|
|
|
|
);
|
|
|
|
|
|
|
|
return $ssh;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-29 18:07:17 +02:00
|
|
|
* @depends testConstructor
|
|
|
|
* @group github408
|
|
|
|
* @group github412
|
|
|
|
*/
|
2014-07-20 22:58:24 +02:00
|
|
|
public function testPreLogin($ssh)
|
|
|
|
{
|
|
|
|
$this->assertFalse(
|
|
|
|
$ssh->isConnected(),
|
|
|
|
'Failed asserting that SSH2 is not connected after construction.'
|
|
|
|
);
|
|
|
|
|
2016-04-10 18:30:59 +02:00
|
|
|
$this->assertFalse(
|
|
|
|
$ssh->isAuthenticated(),
|
|
|
|
'Failed asserting that SSH2 is not authenticated after construction.'
|
|
|
|
);
|
|
|
|
|
2014-07-20 22:58:24 +02: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 18:07:17 +02:00
|
|
|
* @depends testPreLogin
|
|
|
|
*/
|
2016-04-10 18:30:59 +02: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
|
|
|
|
*/
|
2014-03-03 01:43:58 +01:00
|
|
|
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 11:35:54 +01:00
|
|
|
|
2016-04-10 18:30:59 +02:00
|
|
|
$this->assertTrue(
|
|
|
|
$ssh->isAuthenticated(),
|
|
|
|
'Failed asserting that SSH2 is authenticated after good login attempt.'
|
|
|
|
);
|
|
|
|
|
2014-03-06 11:35:54 +01:00
|
|
|
return $ssh;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-29 18:07:17 +02:00
|
|
|
* @depends testPasswordLogin
|
|
|
|
* @group github280
|
|
|
|
*/
|
2014-03-06 11:35:54 +01:00
|
|
|
public function testExecWithMethodCallback($ssh)
|
|
|
|
{
|
2017-12-14 07:31:32 +01:00
|
|
|
$callbackObject = $this->getMockBuilder('stdClass')
|
|
|
|
->setMethods(array('callbackMethod'))
|
|
|
|
->getMock();
|
2014-03-06 11:35:54 +01:00
|
|
|
$callbackObject
|
|
|
|
->expects($this->atLeastOnce())
|
|
|
|
->method('callbackMethod')
|
|
|
|
->will($this->returnValue(true));
|
2017-11-27 09:30:14 +01:00
|
|
|
$ssh->exec('pwd', [$callbackObject, 'callbackMethod']);
|
2017-05-28 15:58:00 +02:00
|
|
|
|
|
|
|
return $ssh;
|
2014-03-03 01:43:58 +01:00
|
|
|
}
|
2014-06-17 01:54:26 +02:00
|
|
|
|
|
|
|
public function testGetServerPublicHostKey()
|
|
|
|
{
|
2014-12-10 02:31:41 +01:00
|
|
|
$ssh = new SSH2($this->getEnv('SSH_HOSTNAME'));
|
2014-06-17 01:54:26 +02:00
|
|
|
|
|
|
|
$this->assertInternalType('string', $ssh->getServerPublicHostKey());
|
|
|
|
}
|
2015-07-17 19:30:44 +02:00
|
|
|
|
|
|
|
public function testOpenSocketConnect()
|
|
|
|
{
|
|
|
|
$fsock = fsockopen($this->getEnv('SSH_HOSTNAME'), 22);
|
2015-07-17 19:43:50 +02:00
|
|
|
$ssh = new SSH2($fsock);
|
2015-07-17 19:30:44 +02:00
|
|
|
|
|
|
|
$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 15:58:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testExecWithMethodCallback
|
|
|
|
* @group github1009
|
|
|
|
*/
|
|
|
|
public function testDisablePTY($ssh)
|
|
|
|
{
|
|
|
|
$ssh->enablePTY();
|
|
|
|
$ssh->exec('ls -latr');
|
|
|
|
$ssh->disablePTY();
|
|
|
|
$ssh->exec('pwd');
|
2017-09-06 07:27:07 +02:00
|
|
|
|
|
|
|
return $ssh;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testDisablePTY
|
|
|
|
* @group github1167
|
|
|
|
*/
|
|
|
|
public function testChannelDataAfterOpen($ssh)
|
|
|
|
{
|
|
|
|
$ssh->write("ping 127.0.0.1\n");
|
|
|
|
|
|
|
|
$ssh->enablePTY();
|
|
|
|
$ssh->exec('bash');
|
|
|
|
|
|
|
|
$ssh->write("ls -latr\n");
|
|
|
|
|
|
|
|
$ssh->setTimeout(1);
|
|
|
|
|
|
|
|
$ssh->read();
|
2017-05-28 15:58:00 +02:00
|
|
|
}
|
2014-03-03 01:43:58 +01:00
|
|
|
}
|