From 0a699725711b450aae358d29e8b045d1d0414069 Mon Sep 17 00:00:00 2001 From: Jack Worman Date: Tue, 8 Mar 2022 18:59:30 -0600 Subject: [PATCH] Clean Up Tests --- .github/workflows/ci.yml | 8 +-- .gitignore | 2 - phpseclib/Net/SFTP.php | 6 +-- phpseclib/Net/SSH2.php | 12 ++--- tests/Functional/Net/SFTPUserStoryTest.php | 5 +- tests/Functional/Net/SSH2Test.php | 10 +++- tests/PhpseclibTestCase.php | 50 +++++++++---------- tests/Unit/Crypt/AES/TestCase.php | 18 ++++--- tests/Unit/Crypt/DSA/CreateKeyTest.php | 6 +-- tests/Unit/Crypt/RSA/LoadKeyTest.php | 2 +- tests/bootstrap.php | 22 -------- ...e_compatible_with_new_phpunit_versions.php | 1 + tests/phpunit.xml | 10 +++- 13 files changed, 65 insertions(+), 87 deletions(-) delete mode 100644 tests/bootstrap.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e1038412..5bad4af9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -42,7 +42,8 @@ jobs: tests: name: Tests timeout-minutes: 10 - continue-on-error: ${{ matrix.experimental }} + # Sometimes there is a segfault on PHP 5.6. + continue-on-error: ${{ matrix.php-version == '5.6' }} runs-on: ${{ matrix.os }} steps: - name: Checkout @@ -85,8 +86,3 @@ jobs: matrix: os: [ubuntu-latest, windows-latest, macos-latest] php-version: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1'] - experimental: [false] - include: - - {os: ubuntu-latest, php-version: '8.2', experimental: true} - - {os: windows-latest, php-version: '8.2', experimental: true} - - {os: macos-latest, php-version: '8.2', experimental: true} diff --git a/.gitignore b/.gitignore index bb8002f5..50ee3101 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,5 @@ /.idea/ -/build/vendor/ /build/php-cs-fixer.cache -/build/composer.lock /composer.lock /composer.phar /vendor/ diff --git a/phpseclib/Net/SFTP.php b/phpseclib/Net/SFTP.php index ad7f7632..66cec410 100644 --- a/phpseclib/Net/SFTP.php +++ b/phpseclib/Net/SFTP.php @@ -3266,7 +3266,7 @@ class SFTP extends SSH2 * @param int $request_id * @see self::_get_sftp_packet() * @see self::send_channel_packet() - * @return bool + * @return void * @access private */ private function send_sftp_packet($type, $data, $request_id = 1) @@ -3280,7 +3280,7 @@ class SFTP extends SSH2 pack('NCa*', strlen($data) + 1, $type, $data); $start = microtime(true); - $result = $this->send_channel_packet(self::CHANNEL, $packet); + $this->send_channel_packet(self::CHANNEL, $packet); $stop = microtime(true); if (defined('NET_SFTP_LOGGING')) { @@ -3305,8 +3305,6 @@ class SFTP extends SSH2 } } } - - return $result; } /** diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index f7c4fb5d..6c71f3ac 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -3148,11 +3148,10 @@ class SSH2 /** * Inputs a command into an interactive shell. * - * @see self::read() + * @see SSH2::read() * @param string $cmd - * @return bool + * @return void * @throws \RuntimeException on connection error - * @access public */ public function write($cmd) { @@ -3164,7 +3163,7 @@ class SSH2 throw new \RuntimeException('Unable to initiate an interactive shell session'); } - return $this->send_channel_packet($this->get_interactive_channel(), $cmd); + $this->send_channel_packet($this->get_interactive_channel(), $cmd); } /** @@ -4384,8 +4383,7 @@ class SSH2 * * @param int $client_channel * @param string $data - * @return bool - * @access private + * @return void */ protected function send_channel_packet($client_channel, $data) { @@ -4416,8 +4414,6 @@ class SSH2 $this->window_size_client_to_server[$client_channel] -= strlen($temp); $this->send_binary_packet($packet); } - - return true; } /** diff --git a/tests/Functional/Net/SFTPUserStoryTest.php b/tests/Functional/Net/SFTPUserStoryTest.php index d4e33ac6..a4c71d68 100644 --- a/tests/Functional/Net/SFTPUserStoryTest.php +++ b/tests/Functional/Net/SFTPUserStoryTest.php @@ -771,10 +771,7 @@ class SFTPUserStoryTest extends PhpseclibFunctionalTestCase $this->assertEquals( $list_cache_enabled, $list_cache_disabled, - 'The files should be the same regardless of stat cache', - 0.0, - 10, - true + 'The files should be the same regardless of stat cache' ); return $sftp; diff --git a/tests/Functional/Net/SSH2Test.php b/tests/Functional/Net/SSH2Test.php index 3111c4a5..fe5937e5 100644 --- a/tests/Functional/Net/SSH2Test.php +++ b/tests/Functional/Net/SSH2Test.php @@ -29,6 +29,7 @@ class SSH2Test extends PhpseclibFunctionalTestCase * @depends testConstructor * @group github408 * @group github412 + * @param SSH2 $ssh */ public function testPreLogin($ssh) { @@ -62,6 +63,7 @@ class SSH2Test extends PhpseclibFunctionalTestCase /** * @depends testPreLogin + * @param SSH2 $ssh */ public function testBadPassword($ssh) { @@ -87,6 +89,7 @@ class SSH2Test extends PhpseclibFunctionalTestCase /** * @depends testBadPassword + * @param SSH2 $ssh */ public function testPasswordLogin($ssh) { @@ -108,6 +111,7 @@ class SSH2Test extends PhpseclibFunctionalTestCase /** * @depends testPasswordLogin * @group github280 + * @param SSH2 $ssh */ public function testExecWithMethodCallback($ssh) { @@ -146,6 +150,7 @@ class SSH2Test extends PhpseclibFunctionalTestCase /** * @depends testExecWithMethodCallback * @group github1009 + * @param SSH2 $ssh */ public function testDisablePTY($ssh) { @@ -162,6 +167,7 @@ class SSH2Test extends PhpseclibFunctionalTestCase /** * @depends testDisablePTY * @group github1167 + * @param SSH2 $ssh */ public function testChannelDataAfterOpen($ssh) { @@ -171,7 +177,7 @@ class SSH2Test extends PhpseclibFunctionalTestCase // when consolekit was incorporated. // https://marc.info/?l=openssh-unix-dev&m=163409903417589&w=2 discusses some of the // issues with how Ubuntu incorporated consolekit - $pattern = '#^SSH-2\.0-OpenSSH_([\d\.]+)[^ ]* Ubuntu-.*$#'; + $pattern = '#^SSH-2\.0-OpenSSH_([\d.]+)[^ ]* Ubuntu-.*$#'; $match = preg_match($pattern, $ssh->getServerIdentification(), $matches); $match = $match && version_compare('5.8', $matches[1], '<='); $match = $match && version_compare('6.9', $matches[1], '>='); @@ -188,6 +194,6 @@ class SSH2Test extends PhpseclibFunctionalTestCase $ssh->setTimeout(1); - $ssh->read(); + $this->assertIsString($ssh->read()); } } diff --git a/tests/PhpseclibTestCase.php b/tests/PhpseclibTestCase.php index 57236b3a..f40f395f 100644 --- a/tests/PhpseclibTestCase.php +++ b/tests/PhpseclibTestCase.php @@ -86,27 +86,6 @@ abstract class PhpseclibTestCase extends TestCase } } - /** - * @param string $filename Filename relative to library directory. - * - * @return null - */ - protected static function reRequireFile($filename) - { - if (extension_loaded('runkit')) { - $result = runkit_import( - sprintf('%s/../phpseclib/%s', __DIR__, $filename), - RUNKIT_IMPORT_FUNCTIONS | - RUNKIT_IMPORT_CLASS_METHODS | - RUNKIT_IMPORT_OVERRIDE - ); - - if (!$result) { - self::markTestSkipped("Failed to reimport file $filename"); - } - } - } - protected static function getVar($obj, $var) { $reflection = new \ReflectionClass(get_class($obj)); @@ -126,7 +105,7 @@ abstract class PhpseclibTestCase extends TestCase // assertIsArray was not introduced until PHPUnit 8 public static function assertIsArray($actual, $message = '') { - if (method_exists('\PHPUnit\Framework\TestCase', 'assertIsArray')) { + if (method_exists(parent::class, 'assertIsArray')) { parent::assertIsArray($actual, $message); return; } @@ -137,7 +116,7 @@ abstract class PhpseclibTestCase extends TestCase // assertIsString was not introduced until PHPUnit 8 public static function assertIsString($actual, $message = '') { - if (method_exists('\PHPUnit\Framework\TestCase', 'assertIsString')) { + if (method_exists(parent::class, 'assertIsString')) { parent::assertIsString($actual, $message); return; } @@ -148,7 +127,7 @@ abstract class PhpseclibTestCase extends TestCase // assertIsResource was not introduced until PHPUnit 8 public static function assertIsResource($actual, $message = '') { - if (method_exists('\PHPUnit\Framework\TestCase', 'assertIsResource')) { + if (method_exists(parent::class, 'assertIsResource')) { parent::assertIsResource($actual, $message); return; } @@ -159,7 +138,7 @@ abstract class PhpseclibTestCase extends TestCase // assertIsObject was not introduced until PHPUnit 8 public static function assertIsObject($actual, $message = '') { - if (method_exists('\PHPUnit\Framework\TestCase', 'assertIsObject')) { + if (method_exists(parent::class, 'assertIsObject')) { parent::assertIsObject($actual, $message); return; } @@ -170,7 +149,7 @@ abstract class PhpseclibTestCase extends TestCase // assertContains is deprecated for strings in PHPUnit 8 public static function assertStringContainsString($needle, $haystack, $message = '') { - if (method_exists('\PHPUnit\Framework\TestCase', 'assertStringContainsString')) { + if (method_exists(parent::class, 'assertStringContainsString')) { parent::assertStringContainsString($needle, $haystack, $message); return; } @@ -181,11 +160,28 @@ abstract class PhpseclibTestCase extends TestCase // assertNotContains is deprecated for strings in PHPUnit 8 public static function assertStringNotContainsString($needle, $haystack, $message = '') { - if (method_exists('\PHPUnit\Framework\TestCase', 'assertStringContainsString')) { + if (method_exists(parent::class, 'assertStringContainsString')) { parent::assertStringNotContainsString($needle, $haystack, $message); return; } parent::assertNotContains($needle, $haystack, $message); } + + /** + * assertRegExp() was deprecated in favor of assertMatchesRegularExpression(). + * + * @param string $pattern + * @param string $string + * @param string $message + * @return void + */ + public static function assertMatchesRegularExpression($pattern, $string, $message = '') + { + if (method_exists(parent::class, 'assertMatchesRegularExpression')) { + parent::assertMatchesRegularExpression($pattern, $string, $message); + } else { + parent::assertRegExp($pattern, $string, $message); + } + } } diff --git a/tests/Unit/Crypt/AES/TestCase.php b/tests/Unit/Crypt/AES/TestCase.php index 70c0f76c..e42fbc3f 100644 --- a/tests/Unit/Crypt/AES/TestCase.php +++ b/tests/Unit/Crypt/AES/TestCase.php @@ -131,7 +131,7 @@ abstract class TestCase extends PhpseclibTestCase /** * Produces all combinations of test values. * - * @return array + * @return list */ public function continuousBufferBatteryCombos() { @@ -173,6 +173,16 @@ abstract class TestCase extends PhpseclibTestCase return $result; } + /** + * @return array + */ + public function continuousBufferBatteryCombosWithoutSingleCombos() + { + return array_filter($this->continuousBufferBatteryCombos(), function (array $continuousBufferBatteryCombo) { + return count($continuousBufferBatteryCombo[2]) > 1; + }); + } + /** * @dataProvider continuousBufferBatteryCombos */ @@ -219,14 +229,10 @@ abstract class TestCase extends PhpseclibTestCase /** * Pretty much the same as testContinuousBufferBattery with the caveat that continuous mode is not enabled. * - * @dataProvider continuousBufferBatteryCombos + * @dataProvider continuousBufferBatteryCombosWithoutSingleCombos */ public function testNonContinuousBufferBattery($op, $mode, $test) { - if (count($test) == 1) { - self::markTestSkipped('test is 1'); - } - $iv = str_repeat('x', 16); $key = str_repeat('a', 16); diff --git a/tests/Unit/Crypt/DSA/CreateKeyTest.php b/tests/Unit/Crypt/DSA/CreateKeyTest.php index e7b5b500..9f652956 100644 --- a/tests/Unit/Crypt/DSA/CreateKeyTest.php +++ b/tests/Unit/Crypt/DSA/CreateKeyTest.php @@ -23,17 +23,17 @@ class CreateKeyTest extends PhpseclibTestCase { $dsa = DSA::createParameters(); $this->assertInstanceOf(Parameters::class, $dsa); - $this->assertRegexp('#BEGIN DSA PARAMETERS#', "$dsa"); + $this->assertMatchesRegularExpression('#BEGIN DSA PARAMETERS#', "$dsa"); try { - $dsa = DSA::createParameters(100, 100); + DSA::createParameters(100, 100); } catch (\Exception $e) { $this->assertInstanceOf(\Exception::class, $e); } $dsa = DSA::createParameters(512, 160); $this->assertInstanceOf(Parameters::class, $dsa); - $this->assertRegexp('#BEGIN DSA PARAMETERS#', "$dsa"); + $this->assertMatchesRegularExpression('#BEGIN DSA PARAMETERS#', "$dsa"); return $dsa; } diff --git a/tests/Unit/Crypt/RSA/LoadKeyTest.php b/tests/Unit/Crypt/RSA/LoadKeyTest.php index 4ededb51..639a3b25 100644 --- a/tests/Unit/Crypt/RSA/LoadKeyTest.php +++ b/tests/Unit/Crypt/RSA/LoadKeyTest.php @@ -462,7 +462,7 @@ Private-MAC: 03e2cb74e1d67652fbad063d2ed0478f31bdf256 PKCS1::setEncryptionAlgorithm('AES-256-CBC'); $encryptedKey = $rsa->withPassword('demo')->toString('PKCS1'); - $this->assertRegExp('#AES-256-CBC#', $encryptedKey); + $this->assertMatchesRegularExpression('#AES-256-CBC#', $encryptedKey); $rsa = PublicKeyLoader::load($key, 'demo'); $this->assertInstanceOf(PrivateKey::class, $rsa); diff --git a/tests/bootstrap.php b/tests/bootstrap.php deleted file mode 100644 index 15fbbd9e..00000000 --- a/tests/bootstrap.php +++ /dev/null @@ -1,22 +0,0 @@ - ' function assertIsString($actual, string $message = \'\'): void', '~ function assertStringContainsString\(\$needle, \$haystack, \$message = \'\'\)~' => ' function assertStringContainsString(string $needle, string $haystack, string $message = \'\'): void', '~ function assertStringNotContainsString\(\$needle, \$haystack, \$message = \'\'\)~' => ' function assertStringNotContainsString(string $needle, string $haystack, string $message = \'\'): void', + '~ function assertMatchesRegularExpression\(\$pattern, \$string, \$message = \'\'\)~' => ' function assertMatchesRegularExpression(string $pattern, string $string, string $message = \'\'): void', ]; $updatedFileContents = preg_replace( array_keys($patternToReplacementMap), diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 21b4b453..17a9ff83 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -1,9 +1,12 @@ @@ -18,4 +21,7 @@ ../phpseclib/ + + +