From 103c7742367f431c3e678e10da761c42028235f6 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 26 Dec 2019 19:17:31 +0100 Subject: [PATCH] Improve tests and conversion --- .gitignore | 1 + composer.json | 17 ++-- src/BigIntegor.php | 2 +- src/danog/MadelineProto/Exception.php | 6 +- .../Stream/Transport/PremadeStream.php | 2 +- src/polyfill.php | 7 ++ tests/makephar.sh | 30 +++++-- tests/makephardebug.sh | 40 --------- tests/random.php | 34 ++++---- tests/test-conversion.sh | 81 +++++++++++++++++++ tests/testing.php | 16 +++- tools/makephar.php | 2 +- tools/phar.php | 2 +- 13 files changed, 164 insertions(+), 76 deletions(-) delete mode 100755 tests/makephardebug.sh create mode 100755 tests/test-conversion.sh diff --git a/.gitignore b/.gitignore index 206cba1d..b71ad487 100644 --- a/.gitignore +++ b/.gitignore @@ -121,3 +121,4 @@ phpunit.xml vendor .php_cs.cache coverage +tempConv diff --git a/composer.json b/composer.json index 8e0e4b99..238c47bb 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,7 @@ "phpunit/phpunit": "^8", "amphp/php-cs-fixer-config": "dev-master", "haydenpierce/class-finder": "^0.4", - "ext-ctype":"*", + "ext-ctype": "*", "danog/7to70": "^1", "danog/7to5": "^1" }, @@ -63,12 +63,10 @@ "src/polyfill.php" ] }, - "repositories": [ - { - "type": "git", - "url": "https://github.com/danog/dns" - } - ], + "repositories": [{ + "type": "git", + "url": "https://github.com/danog/dns" + }], "scripts": { "build": [ "@docs", @@ -78,9 +76,12 @@ "@cs", "@test" ], + "prepare-convert": "rm -rf tempConv && mkdir tempConv", + "test-php7": "tests/test-conversion.sh 70", + "test-php56": "tests/test-conversion.sh 5", "cs": "PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer fix -v --diff --dry-run", "cs-fix": "PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer fix -v --diff", "docs": "php tools/build_docs.php", "test": "@php -dzend.assertions=1 -dassert.exception=1 ./vendor/bin/phpunit --coverage-text" } -} +} \ No newline at end of file diff --git a/src/BigIntegor.php b/src/BigIntegor.php index 59fa649a..0711c6d1 100644 --- a/src/BigIntegor.php +++ b/src/BigIntegor.php @@ -19,7 +19,7 @@ namespace phpseclib\Math; -if (PHP_MAJOR_VERSION < 7 && !(\class_exists(\Phar::class) && \Phar::running())) { +if (PHP_MAJOR_VERSION < 7 && !((\class_exists(\Phar::class) && \Phar::running()) || defined('TESTING_VERSIONS'))) { throw new \Exception('MadelineProto requires php 7 to run natively, use phar.madelineproto.xyz to run on PHP 5.6'); } if (\defined('HHVM_VERSION')) { diff --git a/src/danog/MadelineProto/Exception.php b/src/danog/MadelineProto/Exception.php index 98861ebd..695852b5 100644 --- a/src/danog/MadelineProto/Exception.php +++ b/src/danog/MadelineProto/Exception.php @@ -78,7 +78,11 @@ class Exception extends \Exception public static function exceptionErrorHandler($errno = 0, $errstr = null, $errfile = null, $errline = null) { // If error is suppressed with @, don't throw an exception - if (\error_reporting() === 0 || \strpos($errstr, 'headers already sent') || ($errfile && \strpos($errfile, 'vendor/amphp') !== false)) { + if (\error_reporting() === 0 + || \strpos($errstr, 'headers already sent') + || ($errfile + && (\strpos($errfile, 'vendor/amphp') !== false || \strpos($errfile, 'vendor/league') !== false)) + ) { return false; } diff --git a/src/danog/MadelineProto/Stream/Transport/PremadeStream.php b/src/danog/MadelineProto/Stream/Transport/PremadeStream.php index 40747091..d1f8ab2a 100644 --- a/src/danog/MadelineProto/Stream/Transport/PremadeStream.php +++ b/src/danog/MadelineProto/Stream/Transport/PremadeStream.php @@ -34,7 +34,7 @@ use danog\MadelineProto\Stream\RawStreamInterface; * * @author Daniil Gentili */ -class PremadeStream extends Socket implements RawStreamInterface, ProxyStreamInterface +class PremadeStream implements RawStreamInterface, ProxyStreamInterface { use RawStream; private $stream; diff --git a/src/polyfill.php b/src/polyfill.php index 7ffd1c80..ceef0d30 100644 --- a/src/polyfill.php +++ b/src/polyfill.php @@ -38,3 +38,10 @@ if (!\function_exists('error_clear_last')) { if (!\defined('MADELINEPROTO_TEST')) { \define('MADELINEPROTO_TEST', 'NOT PONY'); } +trait MyCallableMaker +{ + use \Amp\CallableMaker { + callableFromInstanceMethod as public; + callableFromStaticMethod as public; + } +} diff --git a/tests/makephar.sh b/tests/makephar.sh index ac222b56..f694f223 100755 --- a/tests/makephar.sh +++ b/tests/makephar.sh @@ -43,6 +43,7 @@ composer config platform.php "7.4" [ $PHP_MAJOR_VERSION -eq 5 ] && composer require dstuecken/php7ify composer clearcache composer update +composer dumpautoload --optimize cp -a $madelinePath/src vendor/danog/madelineproto/ cd .. @@ -51,13 +52,17 @@ cd .. sudo apt-get update -q sudo apt-get install php7.3-cli php7.3-json php7.3-mbstring php7.3-curl php7.3-xml php7.3-json -y - composer global require spatie/7to5 dev-master#0420ad3 + composer global require danog/7to5 [ -f $HOME/.composer/vendor/bin/php7to5 ] && php7to5=$HOME/.composer/vendor/bin/php7to5 [ -f $HOME/.config/composer/vendor/bin/php7to5 ] && php7to5=$HOME/.config/composer/vendor/bin/php7to5 - find phar7/vendor/league/uri -type f -name '*.php' -exec sed 's/withScheme[(]\$scheme[)]: UriInterface/withScheme(?string $scheme): UriInterface/g' -i {} + - - + sed -re 's/\?(\w*) (\$\w+)/\2 = NULL/g;s/= null = null/=null/ig;s/: self//g' -i vendor/league/uri-interfaces/src/Contracts/UriInterface.php + while IFS= read -r line; do l=$(echo "$line" | sed 's/[(].*//g'); sed -i "s/$l.*/$line/g" vendor/league/uri/src/Uri.php; done <<< $(grep 'public function' vendor/league/uri-interfaces/src/Contracts/UriInterface.php | sed 's/;//g;s/: self//g') + + sed -i 's/handleConnectionWindowIncrement[(]\$windowSize[)]/handleConnectionWindowIncrement(int $windowSize)/g' vendor/amphp/http-client/src/Connection/Internal/Http2ConnectionProcessor.php + + sed -ri 's/^\{/{ use \\MyCallableMaker;/g;s/\\Closure::fromCallable[(]\[(.+), (.+)\][)]/\1->callableFromInstanceMethod(\2)/g' vendor/amphp/http-client/src/Connection/Internal/Http2ConnectionProcessor.php vendor/amphp/http-client/src/Connection/Http{2,1}Connection.php + php7.3 $php7to5 convert --copy-all phar7 phar5 >/dev/null sed 's/^Loop::set.*;//g' -i phar5/vendor/amphp/amp/lib/Loop.php @@ -71,6 +76,16 @@ cd .. find phar5/vendor/danog/madelineproto -type f -name '*.php' -exec sed 's/: EncryptableSocket/: \\Amp\\Socket\\Socket/g' -i {} + + sed 's/use Kelunik\\Certificate\\Certificate/use Kelunik\Certificate\Certificate as _Certificate/g;s/new Certificate/new _Certificate/g' -i vendor/amphp/socket/src/TlsInfo.php + + echo " vendor/league/uri-interfaces/src/Contracts/UriException.php + php -v php=5 @@ -81,7 +96,12 @@ cd .. [ -f $HOME/.composer/vendor/bin/php7to70 ] && php7to70=$HOME/.composer/vendor/bin/php7to70 [ -f $HOME/.config/composer/vendor/bin/php7to70 ] && php7to70=$HOME/.config/composer/vendor/bin/php7to70 - find phar7/vendor/league/uri -type f -name '*.php' -exec sed 's/withScheme[(]\$scheme[)]: UriInterface/withScheme(?string $scheme): UriInterface/g' -i {} + + sed -re 's/\?(\w*) (\$\w+)/\2 = NULL/g;s/= null = null/=null/ig;s/: self//g' -i vendor/league/uri-interfaces/src/Contracts/UriInterface.php + while IFS= read -r line; do l=$(echo "$line" | sed 's/[(].*//g'); sed -i "s/$l.*/$line/g" vendor/league/uri/src/Uri.php; done <<< $(grep 'public function' vendor/league/uri-interfaces/src/Contracts/UriInterface.php | sed 's/;//g;s/: self//g') + + sed -i 's/handleConnectionWindowIncrement[(]\$windowSize[)]/handleConnectionWindowIncrement(int $windowSize)/g' vendor/amphp/http-client/src/Connection/Internal/Http2ConnectionProcessor.php + + sed -ri 's/^\{/{ use \\MyCallableMaker;/g;s/\\Closure::fromCallable[(]\[(.+), (.+)\][)]/\1->callableFromInstanceMethod(\2)/g' vendor/amphp/http-client/src/Connection/Internal/Http2ConnectionProcessor.php vendor/amphp/http-client/src/Connection/Http{2,1}Connection.php $php7to70 convert --copy-all phar7 phar5 >/dev/null diff --git a/tests/makephardebug.sh b/tests/makephardebug.sh deleted file mode 100755 index 9935addc..00000000 --- a/tests/makephardebug.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/bash -e - -composer global require spatie/7to5 dev-master#b20463417bcd2c9d141a5733c6e649b6c468beee - -[ -f $HOME/.composer/vendor/bin/php7to5 ] && php7to5=$HOME/.composer/vendor/bin/php7to5 -[ -f $HOME/.config/composer/vendor/bin/php7to5 ] && php7to5=$HOME/.config/composer/vendor/bin/php7to5 - -rm -rf phar7 phar5 MadelineProtoPhar - -mkdir phar7 -cd phar7 -echo '{ - "name": "danog/madelineprototests", - "minimum-stability":"dev", - "require": { - "danog/madelineproto": "dev-master", - "amphp/dns": "dev-master#ecbeca2ae0e93c08e8150a92810a3961fad8ecbe as v1" - }, - "authors": [ - { - "name": "Daniil Gentili", - "email": "daniil@daniil.it" - } - ] -}' > composer.json -composer update -cp -a ../src vendor/danog/madelineproto -cd .. - -#cp -a phar7 phar5 -$php7to5 convert --copy-all phar7 phar5 >/dev/null -find phar5 -type f -exec sed 's/\w* \.\.\./.../' -i {} + -sed 's/^Loop::set.*;//g' -i phar5/vendor/amphp/amp/lib/Loop.php -echo 'Loop::set((new DriverFactory())->create());' >> phar5/vendor/amphp/amp/lib/Loop.php -cp phar5/vendor/danog/madelineproto/src/danog/MadelineProto/Coroutine.php phar5/vendor/amphp/amp/lib/ -sed -i 's/namespace danog\\MadelineProto;//g' phar5/vendor/amphp/amp/lib/Coroutine.php - - -php tools/makephar.php phar5 madeline.phar $(cat .git/refs/heads/master) - diff --git a/tests/random.php b/tests/random.php index 3ba4c0f6..dbb4e96f 100644 --- a/tests/random.php +++ b/tests/random.php @@ -162,18 +162,18 @@ if (!\is_callable('random_bytes')) { return \serialize($arr); } // prevent circular array recursion - if (isset($arr['__phpseclib_marker'])) { + if (isset($arr['__tgseclib_marker'])) { return ''; } $safearr = []; - $arr['__phpseclib_marker'] = true; + $arr['__tgseclib_marker'] = true; foreach (\array_keys($arr) as $key) { - // do not recurse on the '__phpseclib_marker' key itself, for smaller memory usage - if ($key !== '__phpseclib_marker') { + // do not recurse on the '__tgseclib_marker' key itself, for smaller memory usage + if ($key !== '__tgseclib_marker') { $safearr[$key] = safe_serialize($arr[$key]); } } - unset($arr['__phpseclib_marker']); + unset($arr['__tgseclib_marker']); return \serialize($safearr); } /** @@ -236,23 +236,23 @@ if (!\is_callable('random_bytes')) { // // http://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator#Designs_based_on_cryptographic_primitives switch (true) { - case \class_exists('\\phpseclib\\Crypt\\AES'): - $crypto = new \phpseclib\Crypt\AES('ctr'); + case \class_exists('\\tgseclib\\Crypt\\AES'): + $crypto = new \tgseclib\Crypt\AES('ctr'); break; - case \class_exists('\\phpseclib\\Crypt\\Twofish'): - $crypto = new \phpseclib\Crypt\Twofish('ctr'); + case \class_exists('\\tgseclib\\Crypt\\Twofish'): + $crypto = new \tgseclib\Crypt\Twofish('ctr'); break; - case \class_exists('\\phpseclib\\Crypt\\Blowfish'): - $crypto = new \phpseclib\Crypt\Blowfish('ctr'); + case \class_exists('\\tgseclib\\Crypt\\Blowfish'): + $crypto = new \tgseclib\Crypt\Blowfish('ctr'); break; - case \class_exists('\\phpseclib\\Crypt\\TripleDES'): - $crypto = new \phpseclib\Crypt\TripleDES('ctr'); + case \class_exists('\\tgseclib\\Crypt\\TripleDES'): + $crypto = new \tgseclib\Crypt\TripleDES('ctr'); break; - case \class_exists('\\phpseclib\\Crypt\\DES'): - $crypto = new \phpseclib\Crypt\DES('ctr'); + case \class_exists('\\tgseclib\\Crypt\\DES'): + $crypto = new \tgseclib\Crypt\DES('ctr'); break; - case \class_exists('\\phpseclib\\Crypt\\RC4'): - $crypto = new \phpseclib\Crypt\RC4(); + case \class_exists('\\tgseclib\\Crypt\\RC4'): + $crypto = new \tgseclib\Crypt\RC4(); break; default: throw new \RuntimeException(__CLASS__ . ' requires at least one symmetric cipher be loaded'); diff --git a/tests/test-conversion.sh b/tests/test-conversion.sh new file mode 100755 index 00000000..60cb8d5a --- /dev/null +++ b/tests/test-conversion.sh @@ -0,0 +1,81 @@ +#!/bin/bash -e + +madelinePath=$PWD + +rm -rf /tmp/tempConv +mkdir /tmp/tempConv +cd /tmp/tempConv + +echo '{ + "name": "danog/madelineprototests", + "minimum-stability":"dev", + "require": { + "danog/madelineproto": "dev-master", + "amphp/dns": "dev-master#ecbeca2ae0e93c08e8150a92810a3961fad8ecbe as v1" + }, + "require-dev": { + "vlucas/phpdotenv": "^3" + }, + "repositories": [ + { + "type": "path", + "url": "'$madelinePath'", + "options": { + "symlink": false + } + } + ], + "authors": [ + { + "name": "Daniil Gentili", + "email": "daniil@daniil.it" + } + ] +}' > composer.json +composer update + + +rm -rf vendor/danog/madelineproto/docs + +sed -re 's/\?(\w*) (\$\w+)/\2 = NULL/g;s/= null = null/=null/ig;s/: self//g' -i vendor/league/uri-interfaces/src/Contracts/UriInterface.php +while IFS= read -r line; do l=$(echo "$line" | sed 's/[(].*//g'); sed -i "s/$l.*/$line/g" vendor/league/uri/src/Uri.php; done <<< $(grep 'public function' vendor/league/uri-interfaces/src/Contracts/UriInterface.php | sed 's/;//g;s/: self//g') + +sed -i 's/handleConnectionWindowIncrement[(]\$windowSize[)]/handleConnectionWindowIncrement(int $windowSize)/g' vendor/amphp/http-client/src/Connection/Internal/Http2ConnectionProcessor.php + +sed -ri 's/^\{/{ use \\MyCallableMaker;/g;s/\\Closure::fromCallable[(]\[(.+), (.+)\][)]/\1->callableFromInstanceMethod(\2)/g' vendor/amphp/http-client/src/Connection/Internal/Http2ConnectionProcessor.php vendor/amphp/http-client/src/Connection/Http{2,1}Connection.php + +$madelinePath/vendor/bin/php7to$1 convert --copy-all vendor newVendor +rm -rf vendor +mv newVendor vendor + +[ $1 -eq 5 ] && { + sed 's/^Loop::set.*;//g' -i vendor/amphp/amp/lib/Loop.php + echo 'Loop::set((new DriverFactory())->create());' >> vendor/amphp/amp/lib/Loop.php + cp $madelinePath/tests/random.php vendor/paragonie/random_compat/lib/random.php + cp vendor/danog/madelineproto/src/danog/MadelineProto/Coroutine.php vendor/amphp/amp/lib/Coroutine.php + sed 's/namespace danog\\MadelineProto;/namespace Amp;/g' -i vendor/amphp/amp/lib/Coroutine.php + sed 's/public static function echo/public static function echo_/g' -i vendor/danog/madelineproto/src/danog/MadelineProto/Tools.php + + sed 's/use Kelunik\\Certificate\\Certificate/use Kelunik\Certificate\Certificate as _Certificate/g;s/new Certificate/new _Certificate/g;s/empty[(]\$metadata[)] [?] null : //g' -i vendor/amphp/socket/src/TlsInfo.php + + echo " vendor/league/uri-interfaces/src/Contracts/UriException.php + +} +find vendor/amphp -type f -name '*.php' -exec sed "s/extension_loaded[(]'zlib'[)]/false/g;s/new[(]/new_(/g;s/clone[(]/clone_(/g" -i {} + + +find vendor/danog/madelineproto -type f -name '*.php' -exec sed 's/: EncryptableSocket/: \\Amp\\Socket\\Socket/g' -i {} + + +composer dumpautoload --optimize + +cd $madelinePath +if [ $1 -eq 5 ];then + php5.6 tests/testing.php +else + php7.0 tests/testing.php +fi \ No newline at end of file diff --git a/tests/testing.php b/tests/testing.php index 8984cb1c..fb4063ea 100755 --- a/tests/testing.php +++ b/tests/testing.php @@ -16,20 +16,34 @@ If not, see . */ +$loader = false; if (!\file_exists(__DIR__.'/../vendor/autoload.php')) { echo 'You did not run composer update, using madeline.php'.PHP_EOL; if ($phar = \getenv('TRAVIS_PHAR')) { - include $phar; + $loader = include $phar; } else { if (!\file_exists('madeline.php')) { \copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php'); } include 'madeline.php'; } +} elseif (\file_exists('/tmp/tempConv/vendor/autoload.php')) { + \define('TESTING_VERSIONS', 1); + $loader = require_once('/tmp/tempConv/vendor/autoload.php'); } else { require_once 'vendor/autoload.php'; } +if ($loader) { + foreach ($loader->getClassMap() as $class => $file) { + if (\in_array($class, ['Amp\\Sync\\Internal\\MutexStorage', 'Amp\\Sync\\Internal\\SemaphoreStorage', 'Amp\\Parallel\\Sync\\Internal\\ParcelStorage', 'Amp\\Parallel\\Context\\Internal\\Thread'])) { + continue; + } + echo "Requiring $class => $file\n"; + require_once($file); + } +} + /* * Load .env for settings */ diff --git a/tools/makephar.php b/tools/makephar.php index 03485537..f0de4f93 100755 --- a/tools/makephar.php +++ b/tools/makephar.php @@ -38,6 +38,6 @@ if ($contents = file_get_contents("https://phar.madelineproto.xyz/phar.php?v=new Phar::interceptFileFuncs(); Phar::mapPhar("'.$argv[2].'"); -require_once "phar://'.$argv[2].'/vendor/autoload.php"; +return require_once "phar://'.$argv[2].'/vendor/autoload.php"; __HALT_COMPILER(); ?>'); diff --git a/tools/phar.php b/tools/phar.php index c9ef32c5..640e4dde 100644 --- a/tools/phar.php +++ b/tools/phar.php @@ -90,4 +90,4 @@ function ___install_madeline() ___install_madeline(); -require 'madeline.phar'; +return require_once 'madeline.phar';