Merge branch 'ssh2-identifier' into php5

This commit is contained in:
terrafrost 2013-12-28 13:50:05 -06:00
commit 05077cf0f3
2 changed files with 80 additions and 19 deletions

View File

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

View File

@ -1,4 +1,5 @@
<?php
/**
* @author Marc Scholten <marc@pedigital.de>
* @copyright MMXIII Marc Scholten
@ -7,6 +8,17 @@
class Net_SSH2Test extends PhpseclibTestCase
{
/**
* @return Net_SSH2
*/
private function createSSHMock()
{
return $this->getMockBuilder('Net_SSH2')
->disableOriginalConstructor()
->setMethods(array('__destruct'))
->getMock();
}
public function formatLogDataProvider()
{
return array(
@ -29,13 +41,44 @@ class Net_SSH2Test extends PhpseclibTestCase
*/
public function testFormatLog(array $message_log, array $message_number_log, $expected)
{
$ssh = $this->getMockBuilder('Net_SSH2')
->disableOriginalConstructor()
->setMethods(array('__destruct'))
->getMock();
$ssh = $this->createSSHMock();
$result = $ssh->_format_log($message_log, $message_number_log);
$this->assertEquals($expected, $result);
}
public function generateIdentifierProvider()
{
return array(
array('SSH-2.0-phpseclib_0.3', array()),
array('SSH-2.0-phpseclib_0.3 (gmp)', array('gmp')),
array('SSH-2.0-phpseclib_0.3 (bcmath)', array('bcmath')),
array('SSH-2.0-phpseclib_0.3 (mcrypt)', array('mcrypt')),
array('SSH-2.0-phpseclib_0.3 (mcrypt, gmp)', array('mcrypt', 'gmp')),
array('SSH-2.0-phpseclib_0.3 (mcrypt, bcmath)', array('mcrypt', 'bcmath')),
);
}
/**
* @dataProvider generateIdentifierProvider
*/
public function testGenerateIdentifier($expected, array $requiredExtensions)
{
$notAllowed = array('gmp', 'bcmath', 'mcrypt', 'gmp');
foreach($notAllowed as $notAllowedExtension) {
if(in_array($notAllowedExtension, $requiredExtensions)) {
continue;
}
if(extension_loaded($notAllowedExtension)) {
$this->markTestSkipped('Extension ' . $notAllowedExtension . ' is not allowed for this data-set');
}
}
$ssh = $this->createSSHMock();
$identifier = $ssh->_generate_identifier();
$this->assertEquals($expected, $identifier);
}
}