Refactored Net_SSH2::$identifier and added unit tests

Added return tag
This commit is contained in:
Marc Philip Scholten 2013-12-16 20:17:10 +01:00
parent b793286561
commit 4817d28a54
2 changed files with 72 additions and 19 deletions

View File

@ -156,7 +156,7 @@ class Net_SSH2
* @var String * @var String
* @access private * @access private
*/ */
var $identifier = 'SSH-2.0-phpseclib_0.3'; var $identifier;
/** /**
* The Socket Object * The Socket Object
@ -919,19 +919,7 @@ class Net_SSH2
return false; return false;
} }
$ext = array(); $this->identifiert = $this->_generate_identifier();
if (extension_loaded('mcrypt')) {
$ext[] = 'mcrypt';
}
if (extension_loaded('gmp')) {
$ext[] = 'gmp';
} else if (extension_loaded('bcmath')) {
$ext[] = 'bcmath';
}
if (!empty($ext)) {
$this->identifier.= ' (' . implode(', ', $ext) . ')';
}
if (defined('NET_SSH2_LOGGING')) { if (defined('NET_SSH2_LOGGING')) {
$this->_append_log('<-', $extra . $temp); $this->_append_log('<-', $extra . $temp);
@ -968,6 +956,38 @@ class Net_SSH2
$this->bitmap = NET_SSH2_MASK_CONSTRUCTOR; $this->bitmap = NET_SSH2_MASK_CONSTRUCTOR;
} }
/**
* Generates the SSH identifier
* You should overwrite this method in your own class if you want to use an other identifier
*
* @access protected
* @return String
*/
function _generate_identifier()
{
$identifier = 'SSH-2.0-phpseclib_0.3';
$ext = array();
if (extension_loaded('mcrypt')) {
$ext[] = 'mcrypt';
}
if (extension_loaded('gmp')) {
$ext[] = 'gmp';
} else {
if (extension_loaded('bcmath')) {
$ext[] = 'bcmath';
}
}
if (!empty($ext)) {
$identifier .= ' (' . implode(', ', $ext) . ')';
}
return $identifier;
}
/** /**
* Key Exchange * Key Exchange
* *

View File

@ -7,6 +7,17 @@
class Net_SSH2Test extends PhpseclibTestCase class Net_SSH2Test extends PhpseclibTestCase
{ {
/**
* @return Net_SSH2
*/
private function createSSHMock()
{
return $this->getMockBuilder('Net_SSH2')
->disableOriginalConstructor()
->setMethods(['__destruct'])
->getMock();
}
public function formatLogDataProvider() public function formatLogDataProvider()
{ {
return array( return array(
@ -29,13 +40,35 @@ class Net_SSH2Test extends PhpseclibTestCase
*/ */
public function testFormatLog(array $message_log, array $message_number_log, $expected) public function testFormatLog(array $message_log, array $message_number_log, $expected)
{ {
$ssh = $this->getMockBuilder('Net_SSH2') $ssh = $this->createSSHMock();
->disableOriginalConstructor()
->setMethods(array('__destruct'))
->getMock();
$result = $ssh->_format_log($message_log, $message_number_log); $result = $ssh->_format_log($message_log, $message_number_log);
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
public function testGenerateIdentifierWithMcryptGmpAndBmath()
{
if(!extension_loaded('mcrypt') || !extension_loaded('gmp') || !extension_loaded('bcmath')) {
$this->markTestSkipped('mcrypt, gmp and bcmath are required for this test');
}
$ssh = $this->createSSHMock();
$identifier = $ssh->_generate_identifier();
$this->assertEquals('SSH-2.0-phpseclib_0.3 (mcrypt, gmp, bcmath)', $identifier);
}
public function testGenerateIdentifierWithMcryptAndBmath()
{
if(!extension_loaded('mcrypt') || !extension_loaded('bcmath')) {
$this->markTestSkipped('mcrypt and bcmath are required for this test');
}
$ssh = $this->createSSHMock();
$identifier = $ssh->_generate_identifier();
$this->assertEquals('SSH-2.0-phpseclib_0.3 (mcrypt, bcmath)', $identifier);
}
} }