From 4817d28a5475662a38c697b3de810acb6a367290 Mon Sep 17 00:00:00 2001 From: Marc Philip Scholten Date: Mon, 16 Dec 2013 20:17:10 +0100 Subject: [PATCH 1/8] 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); + } + + } From 357d4253ee538b514cca82fef06a3eaf2793ac6a Mon Sep 17 00:00:00 2001 From: Marc Philip Scholten Date: Mon, 16 Dec 2013 21:38:04 +0100 Subject: [PATCH 2/8] Fixed typo --- phpseclib/Net/SSH2.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index 6ea8c2ba..87d210c5 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -919,7 +919,7 @@ class Net_SSH2 return false; } - $this->identifiert = $this->_generate_identifier(); + $this->identifier = $this->_generate_identifier(); if (defined('NET_SSH2_LOGGING')) { $this->_append_log('<-', $extra . $temp); From 699ac0b0e9e2db30e8b4af70d6c35b0a5a5d5f08 Mon Sep 17 00:00:00 2001 From: Marc Philip Scholten Date: Tue, 17 Dec 2013 18:43:37 +0100 Subject: [PATCH 3/8] Rewritten test to just check for possible combinations --- tests/Net/SSH2Test.php | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/tests/Net/SSH2Test.php b/tests/Net/SSH2Test.php index cf5a6a6c..23b759aa 100644 --- a/tests/Net/SSH2Test.php +++ b/tests/Net/SSH2Test.php @@ -1,4 +1,5 @@ * @copyright MMXIII Marc Scholten @@ -14,7 +15,7 @@ class Net_SSH2Test extends PhpseclibTestCase { return $this->getMockBuilder('Net_SSH2') ->disableOriginalConstructor() - ->setMethods(['__destruct']) + ->setMethods(array('__destruct')) ->getMock(); } @@ -48,27 +49,36 @@ class Net_SSH2Test extends PhpseclibTestCase 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); + 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')), + ); } - public function testGenerateIdentifierWithMcryptAndBmath() + /** + * @dataProvider generateIdentifierProvider + */ + public function testGenerateIdentifier($expected, array $requiredExtensions) { - if(!extension_loaded('mcrypt') || !extension_loaded('bcmath')) { - $this->markTestSkipped('mcrypt and bcmath are required for this test'); + $notAllowed = array('gmp', 'bcmath', 'mcrypt', 'gmp'); + foreach($notAllowed as $nowAllowedExtension) { + if(in_array($nowAllowedExtension, $requiredExtensions)) { + continue; + } + + if(extension_loaded($nowAllowedExtension)) { + $this->markTestSkipped('Extension ' . $nowAllowedExtension . ' is not allowed for this data-set'); + } } $ssh = $this->createSSHMock(); $identifier = $ssh->_generate_identifier(); - $this->assertEquals('SSH-2.0-phpseclib_0.3 (mcrypt, bcmath)', $identifier); + $this->assertEquals($expected, $identifier); } - } From b0de383f95b1727b61a1df00a1e317773c0a9d5d Mon Sep 17 00:00:00 2001 From: Marc Philip Scholten Date: Tue, 17 Dec 2013 18:44:37 +0100 Subject: [PATCH 4/8] Replaced else { if ()...} with elseif() {} --- phpseclib/Net/SSH2.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index 87d210c5..4a8cdafd 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -974,10 +974,8 @@ class Net_SSH2 if (extension_loaded('gmp')) { $ext[] = 'gmp'; - } else { - if (extension_loaded('bcmath')) { - $ext[] = 'bcmath'; - } + } elseif (extension_loaded('bcmath')) { + $ext[] = 'bcmath'; } if (!empty($ext)) { From ad0c7c52bd0d9fd2b4e8d0b98bff1265c2fb6ac5 Mon Sep 17 00:00:00 2001 From: Marc Philip Scholten Date: Tue, 17 Dec 2013 18:45:17 +0100 Subject: [PATCH 5/8] Fixed cs --- phpseclib/Net/SSH2.php | 1 - 1 file changed, 1 deletion(-) diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index 4a8cdafd..04e24019 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -985,7 +985,6 @@ class Net_SSH2 return $identifier; } - /** * Key Exchange * From 3d5262156d2d28823a5c3859379b07f5e15edffc Mon Sep 17 00:00:00 2001 From: Marc Philip Scholten Date: Thu, 26 Dec 2013 21:03:00 +0100 Subject: [PATCH 6/8] Fixed missing dataProvider in test --- tests/Net/SSH2Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Net/SSH2Test.php b/tests/Net/SSH2Test.php index 23b759aa..f2c2c65d 100644 --- a/tests/Net/SSH2Test.php +++ b/tests/Net/SSH2Test.php @@ -47,7 +47,7 @@ class Net_SSH2Test extends PhpseclibTestCase $this->assertEquals($expected, $result); } - public function testGenerateIdentifierWithMcryptGmpAndBmath() + public function generateIdentifierProvider() { return array( array('SSH-2.0-phpseclib_0.3', array()), From 19be15c4c43f653cb1714894a603de4d06593d19 Mon Sep 17 00:00:00 2001 From: Marc Philip Scholten Date: Sat, 28 Dec 2013 18:16:09 +0100 Subject: [PATCH 7/8] Fixed typos --- phpseclib/Net/SSH2.php | 2 +- tests/Net/SSH2Test.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index 04e24019..1df7450b 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -958,7 +958,7 @@ class Net_SSH2 /** * Generates the SSH identifier - * You should overwrite this method in your own class if you want to use an other identifier + * You should overwrite this method in your own class if you want to use another identifier * * @access protected * @return String diff --git a/tests/Net/SSH2Test.php b/tests/Net/SSH2Test.php index f2c2c65d..3177a323 100644 --- a/tests/Net/SSH2Test.php +++ b/tests/Net/SSH2Test.php @@ -65,13 +65,13 @@ class Net_SSH2Test extends PhpseclibTestCase public function testGenerateIdentifier($expected, array $requiredExtensions) { $notAllowed = array('gmp', 'bcmath', 'mcrypt', 'gmp'); - foreach($notAllowed as $nowAllowedExtension) { - if(in_array($nowAllowedExtension, $requiredExtensions)) { + foreach($notAllowed as $notAllowedExtension) { + if(in_array($notAllowedExtension, $requiredExtensions)) { continue; } - if(extension_loaded($nowAllowedExtension)) { - $this->markTestSkipped('Extension ' . $nowAllowedExtension . ' is not allowed for this data-set'); + if(extension_loaded($notAllowedExtension)) { + $this->markTestSkipped('Extension ' . $notAllowedExtension . ' is not allowed for this data-set'); } } From 0ccac2c998ba56b92a61603588d2d6f5bc198efa Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 28 Dec 2013 13:47:24 -0600 Subject: [PATCH 8/8] SSH2: add new line in docblock comment --- phpseclib/Net/SSH2.php | 1 + 1 file changed, 1 insertion(+) diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index 1df7450b..89ded54c 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -958,6 +958,7 @@ class Net_SSH2 /** * Generates the SSH identifier + * * You should overwrite this method in your own class if you want to use another identifier * * @access protected