From c5f8bab06549eff28516088b450931174109a126 Mon Sep 17 00:00:00 2001 From: Marc Philip Scholten Date: Sun, 15 Dec 2013 21:05:31 +0100 Subject: [PATCH 1/4] Added Net_SSH1::_format_log test Conflicts: tests/Net/SSH1Test.php --- tests/Net/SSH1Test.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tests/Net/SSH1Test.php diff --git a/tests/Net/SSH1Test.php b/tests/Net/SSH1Test.php new file mode 100644 index 00000000..fca6ebf0 --- /dev/null +++ b/tests/Net/SSH1Test.php @@ -0,0 +1,41 @@ + + * @copyright MMXIII Marc Scholten + * @license http://www.opensource.org/licenses/mit-license.html MIT License + */ + +class SSH1Test extends PhpseclibTestCase +{ + public function formatLogDataProvider() + { + return array( + array( + array('hello world'), + array('<--'), + "<--\r\n00000000 68:65:6c:6c:6f:20:77:6f:72:6c:64 hello world\r\n\r\n" + ), + array( + array('hello', 'world'), + array('<--', '<--'), + "<--\r\n00000000 68:65:6c:6c:6f hello\r\n\r\n" . + "<--\r\n00000000 77:6f:72:6c:64 world\r\n\r\n" + ), + ); + } + + /** + * @dataProvider formatLogDataProvider + */ + public function testFormatLog(array $message_log, array $message_number_log, $expected) + { + $ssh = $this->getMockBuilder('Net_SSH1') + ->disableOriginalConstructor() + ->setMethods(null) + ->getMock(); + + $result = $ssh->_format_log($message_log, $message_number_log); + + $this->assertEquals($expected, $result); + } +} From ea8cadddadf1f2a4fff9a16c9111f5d48c01e36f Mon Sep 17 00:00:00 2001 From: Marc Philip Scholten Date: Wed, 18 Dec 2013 16:36:21 +0100 Subject: [PATCH 2/4] Fixed /e preg_replace modifier exactly like in 0dc8b27a6a47c22a20efd7172fd71d88234869ca --- phpseclib/Net/SSH1.php | 52 +++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/phpseclib/Net/SSH1.php b/phpseclib/Net/SSH1.php index 80fbbdc6..2ee34e2b 100644 --- a/phpseclib/Net/SSH1.php +++ b/phpseclib/Net/SSH1.php @@ -434,6 +434,30 @@ class Net_SSH1 */ var $curTimeout; + /** + * Log Boundary + * + * @see Net_SSH2::_format_log + * @access private + */ + var $log_boundary = ':'; + + /** + * Log Long Width + * + * @see Net_SSH2::_format_log + * @access private + */ + var $log_long_width = 65; + + /** + * Log Short Width + * + * @see Net_SSH2::_format_log + * @access private + */ + var $log_short_width = 16; + /** * Default Constructor. * @@ -1351,8 +1375,6 @@ class Net_SSH1 */ function _format_log($message_log, $message_number_log) { - static $boundary = ':', $long_width = 65, $short_width = 16; - $output = ''; for ($i = 0; $i < count($message_log); $i++) { $output.= $message_number_log[$i] . "\r\n"; @@ -1362,19 +1384,13 @@ class Net_SSH1 if (strlen($current_log)) { $output.= str_pad(dechex($j), 7, '0', STR_PAD_LEFT) . '0 '; } - $fragment = $this->_string_shift($current_log, $short_width); - $hex = substr( - preg_replace( - '#(.)#es', - '"' . $boundary . '" . str_pad(dechex(ord(substr("\\1", -1))), 2, "0", STR_PAD_LEFT)', - $fragment), - strlen($boundary) - ); + $fragment = $this->_string_shift($current_log, $this->log_short_width); + $hex = substr(preg_replace_callback('#.#s', array($this, '_format_log_helper'), $fragment), strlen($this->log_boundary)); // replace non ASCII printable characters with dots // http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters // also replace < with a . since < messes up the output on web browsers $raw = preg_replace('#[^\x20-\x7E]|<#', '.', $fragment); - $output.= str_pad($hex, $long_width - $short_width, ' ') . $raw . "\r\n"; + $output.= str_pad($hex, $this->log_long_width - $this->log_short_width, ' ') . $raw . "\r\n"; $j++; } while (strlen($current_log)); $output.= "\r\n"; @@ -1383,6 +1399,20 @@ class Net_SSH1 return $output; } + /** + * Helper function for _format_log + * + * For use with preg_replace_callback() + * + * @param Array $matches + * @access private + * @return String + */ + function _format_log_helper($matches) + { + return $this->log_boundary . str_pad(dechex(ord($matches[0])), 2, '0', STR_PAD_LEFT); + } + /** * Return the server key public exponent * From 6d8c02c432ebddf4914f3c99938fa3c117f42760 Mon Sep 17 00:00:00 2001 From: Marc Philip Scholten Date: Thu, 26 Dec 2013 12:09:40 +0100 Subject: [PATCH 3/4] Fixed some wrong @see annotations --- phpseclib/Net/SSH1.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpseclib/Net/SSH1.php b/phpseclib/Net/SSH1.php index 2ee34e2b..cdec0b68 100644 --- a/phpseclib/Net/SSH1.php +++ b/phpseclib/Net/SSH1.php @@ -437,7 +437,7 @@ class Net_SSH1 /** * Log Boundary * - * @see Net_SSH2::_format_log + * @see Net_SSH1::_format_log * @access private */ var $log_boundary = ':'; @@ -445,7 +445,7 @@ class Net_SSH1 /** * Log Long Width * - * @see Net_SSH2::_format_log + * @see Net_SSH1::_format_log * @access private */ var $log_long_width = 65; @@ -453,7 +453,7 @@ class Net_SSH1 /** * Log Short Width * - * @see Net_SSH2::_format_log + * @see Net_SSH1::_format_log * @access private */ var $log_short_width = 16; From 2a7b6e39fb5da13837a24487c4f14e3b93d1871d Mon Sep 17 00:00:00 2001 From: Marc Philip Scholten Date: Thu, 26 Dec 2013 12:16:08 +0100 Subject: [PATCH 4/4] Fixed classname of test --- tests/Net/SSH1Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Net/SSH1Test.php b/tests/Net/SSH1Test.php index fca6ebf0..dc02c36d 100644 --- a/tests/Net/SSH1Test.php +++ b/tests/Net/SSH1Test.php @@ -5,7 +5,7 @@ * @license http://www.opensource.org/licenses/mit-license.html MIT License */ -class SSH1Test extends PhpseclibTestCase +class Net_SSH1Test extends PhpseclibTestCase { public function formatLogDataProvider() {