From 4817d28a5475662a38c697b3de810acb6a367290 Mon Sep 17 00:00:00 2001 From: Marc Philip Scholten Date: Mon, 16 Dec 2013 20:17:10 +0100 Subject: [PATCH] Refactored Net_SSH2::$identifier and added unit tests Added return tag --- phpseclib/Net/SSH2.php | 48 ++++++++++++++++++++++++++++++------------ tests/Net/SSH2Test.php | 43 ++++++++++++++++++++++++++++++++----- 2 files changed, 72 insertions(+), 19 deletions(-) diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index 3b43d006..6ea8c2ba 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -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->identifiert = $this->_generate_identifier(); if (defined('NET_SSH2_LOGGING')) { $this->_append_log('<-', $extra . $temp); @@ -968,6 +956,38 @@ 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 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 * diff --git a/tests/Net/SSH2Test.php b/tests/Net/SSH2Test.php index 954fb8b2..cf5a6a6c 100644 --- a/tests/Net/SSH2Test.php +++ b/tests/Net/SSH2Test.php @@ -7,6 +7,17 @@ class Net_SSH2Test extends PhpseclibTestCase { + /** + * @return Net_SSH2 + */ + private function createSSHMock() + { + return $this->getMockBuilder('Net_SSH2') + ->disableOriginalConstructor() + ->setMethods(['__destruct']) + ->getMock(); + } + public function formatLogDataProvider() { return array( @@ -29,13 +40,35 @@ 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 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); + } + + }