From c03bf479ac246fd62203bf71f353b6a2486ec9aa Mon Sep 17 00:00:00 2001 From: Taylor Hornby Date: Thu, 15 Jun 2023 18:23:32 -0600 Subject: [PATCH] Make the tests work on Travis-CI. --- .github/workflows/ci.yml | 107 ---------------------------------- .travis.yml | 21 ++++--- src/Core.php | 5 +- src/File.php | 39 ++++++++++++- test/phpunit-10.xml | 2 +- test/unit/CryptoTest.php | 1 - test/unit/FileTest.php | 6 -- test/unit/RuntimeTestTest.php | 1 - 8 files changed, 56 insertions(+), 126 deletions(-) delete mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml deleted file mode 100644 index 4cb9d7f..0000000 --- a/.github/workflows/ci.yml +++ /dev/null @@ -1,107 +0,0 @@ -name: CI - -on: [push] - -jobs: - old: - name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} - runs-on: ${{ matrix.operating-system }} - strategy: - matrix: - operating-system: ['ubuntu-16.04'] - php-versions: ['5.4', '5.5', '5.6', '7.0'] - phpunit-versions: ['7.5.20'] - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-versions }} - extensions: mbstring, intl - ini-values: post_max_size=256M, max_execution_time=180 - tools: psalm, phpunit:${{ matrix.phpunit-versions }} - - - name: Fix permissions - run: sudo chmod -R 0777 . - - - name: Install dependencies - run: composer self-update --1; composer install - - - name: PHPUnit tests - uses: php-actions/phpunit@v2 - with: - memory_limit: 256M - - moderate: - name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} - runs-on: ${{ matrix.operating-system }} - strategy: - matrix: - operating-system: ['ubuntu-latest'] - php-versions: ['7.1', '7.2', '7.3'] - phpunit-versions: ['latest'] - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-versions }} - extensions: mbstring, intl, sodium - ini-values: post_max_size=256M, max_execution_time=180 - tools: psalm, phpunit:${{ matrix.phpunit-versions }} - - - name: Fix permissions - run: sudo chmod -R 0777 . - - - name: Install dependencies - run: composer install - - - name: PHPUnit tests - uses: php-actions/phpunit@v2 - timeout-minutes: 30 - with: - memory_limit: 256M - - modern: - name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} - runs-on: ${{ matrix.operating-system }} - strategy: - matrix: - operating-system: ['ubuntu-latest'] - php-versions: ['7.4', '8.0'] - phpunit-versions: ['latest'] - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-versions }} - extensions: mbstring, intl, sodium - ini-values: post_max_size=256M, max_execution_time=180 - tools: psalm, phpunit:${{ matrix.phpunit-versions }} - - - name: Fix permissions - run: sudo chmod -R 0777 . - - - name: Install dependencies - run: composer install - - - name: PHPUnit tests - uses: php-actions/phpunit@v2 - timeout-minutes: 30 - with: - memory_limit: 256M - - - name: Install Psalm - if: contains(['7.4', '8.0'], ${{ matrix.php-version }}) - run: composer require --dev vimeo/psalm:^4 - - - name: Static Analysis - if: contains(['7.4', '8.0'], ${{ matrix.php-version }}) - run: vendor/bin/psalm diff --git a/.travis.yml b/.travis.yml index 6050aba..32f1f95 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,11 +9,11 @@ matrix: - php: "7.0" env: USE_PSALM=0 - php: "7.1" - env: USE_PSALM=1 + env: USE_PSALM=0 - php: "7.2" - env: USE_PSALM=1 + env: USE_PSALM=0 - php: "7.3" - env: USE_PSALM=1 + env: USE_PSALM=0 - php: "7.4" env: USE_PSALM=1 - php: "8.0" @@ -29,18 +29,23 @@ matrix: allow_failures: - php: "nightly" - php: "hhvm" + # Travis-CI's 8.2 is currently broken, see: + # https://github.com/defuse/php-encryption/pull/506#issuecomment-1594084107 + - php: "8.2" install: - composer install - - curl -LSs https://box-project.github.io/box2/installer.php | php - - mkdir ~/box - - mv box.phar ~/box/box before_script: - echo "xdebug.mode = coverage" > extra_php_config.ini - phpenv config-add extra_php_config.ini script: - ./test.sh - - PATH=$PATH:~/box/ make -C dist/ build-phar - - ./test.sh dist/defuse-crypto.phar +# - mkdir /tmp/box +# - chmod 755 /tmp/box +# - curl -LSs https://github.com/box-project/box/releases/download/4.3.8/box.phar -o /tmp/box/box +# - chmod 755 /tmp/box/box +# - PATH="$PATH:/tmp/box/" which box +# - PATH="$PATH:/tmp/box/" make -C dist/ build-phar +# - ./test.sh dist/defuse-crypto.phar - if [[ $USE_PSALM -eq 1 ]]; then composer require --with-all-dependencies --dev "vimeo/psalm:dev-master"; fi - if [[ $USE_PSALM -eq 1 ]]; then composer install; fi - if [[ $USE_PSALM -eq 1 ]]; then vendor/bin/psalm; fi diff --git a/src/Core.php b/src/Core.php index 2f3a319..337ad58 100644 --- a/src/Core.php +++ b/src/Core.php @@ -100,7 +100,10 @@ public static function secureRandom($octets) { self::ensureFunctionExists('random_bytes'); try { - return \random_bytes($octets); + if ($octets == 0) { + return ""; + } + return \random_bytes(max(1, $octets)); } catch (\Exception $ex) { throw new Ex\EnvironmentIsBrokenException( 'Your system does not have a secure random number generator.' diff --git a/src/File.php b/src/File.php index 3a64c4a..ad2eac1 100644 --- a/src/File.php +++ b/src/File.php @@ -196,7 +196,9 @@ private static function encryptFileInternal($inputFilename, $outputFilename, Key } /* Open the input file. */ + self::removePHPUnitErrorHandler(); $if = @\fopen($inputFilename, 'rb'); + self::restorePHPUnitErrorHandler(); if ($if === false) { throw new Ex\IOException( 'Cannot open input file for encrypting: ' . @@ -209,7 +211,9 @@ private static function encryptFileInternal($inputFilename, $outputFilename, Key } /* Open the output file. */ + self::removePHPUnitErrorHandler(); $of = @\fopen($outputFilename, 'wb'); + self::restorePHPUnitErrorHandler(); if ($of === false) { \fclose($if); throw new Ex\IOException( @@ -265,7 +269,9 @@ private static function decryptFileInternal($inputFilename, $outputFilename, Key } /* Open the input file. */ + self::removePHPUnitErrorHandler(); $if = @\fopen($inputFilename, 'rb'); + self::restorePHPUnitErrorHandler(); if ($if === false) { throw new Ex\IOException( 'Cannot open input file for decrypting: ' . @@ -279,7 +285,9 @@ private static function decryptFileInternal($inputFilename, $outputFilename, Key } /* Open the output file. */ + self::removePHPUnitErrorHandler(); $of = @\fopen($outputFilename, 'wb'); + self::restorePHPUnitErrorHandler(); if ($of === false) { \fclose($if); throw new Ex\IOException( @@ -770,9 +778,38 @@ private static function getLastErrorMessage() { $error = error_get_last(); if ($error === null) { - return '[no PHP error]'; + return '[no PHP error, or you have a custom error handler set]'; } else { return $error['message']; } } + + /** + * PHPUnit sets an error handler, which prevents getLastErrorMessage() from working, + * because error_get_last does not work when custom handlers are set. + * + * This is a workaround, which should be a no-op in production deployments, to make + * getLastErrorMessage() return the error messages that the PHPUnit tests expect. + * + * If, in a production deployment, a custom error handler is set, the exception + * handling will still work as usual, but the error messages will be confusing. + * + * @return void + */ + private static function removePHPUnitErrorHandler() { + if (defined('PHPUNIT_COMPOSER_INSTALL') || defined('__PHPUNIT_PHAR__')) { + set_error_handler(null); + } + } + + /** + * Undoes what removePHPUnitErrorHandler did. + * + * @return void + */ + private static function restorePHPUnitErrorHandler() { + if (defined('PHPUNIT_COMPOSER_INSTALL') || defined('__PHPUNIT_PHAR__')) { + restore_error_handler(); + } + } } diff --git a/test/phpunit-10.xml b/test/phpunit-10.xml index 712a8cd..c1808f0 100644 --- a/test/phpunit-10.xml +++ b/test/phpunit-10.xml @@ -1,4 +1,4 @@ - + ../src diff --git a/test/unit/CryptoTest.php b/test/unit/CryptoTest.php index 2341d2c..ea81fb8 100644 --- a/test/unit/CryptoTest.php +++ b/test/unit/CryptoTest.php @@ -24,7 +24,6 @@ public function testEmptyString() // We can't runtime-test the password stuff because it runs PBKDF2. public function testEncryptDecryptWithPassword() { - $this->expectNotToPerformAssertions(); $data = "EnCrYpT EvErYThInG\x00\x00"; $password = 'password'; diff --git a/test/unit/FileTest.php b/test/unit/FileTest.php index 59d89ae..98fb17b 100644 --- a/test/unit/FileTest.php +++ b/test/unit/FileTest.php @@ -181,9 +181,6 @@ public static function garbageCiphertextProvider() return $ciphertexts; } - /** - * @expectedException \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException - */ public function testDecryptEmptyFile() { $junk = self::$TEMP_DIR . '/junk'; @@ -357,9 +354,6 @@ public function testNonResourceOutputDecrypt() fclose($resource); } - /** - * @expectedException \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException - */ public function testNonFileResourceDecrypt() { /* This should behave equivalently to an empty file. Calling fstat() on diff --git a/test/unit/RuntimeTestTest.php b/test/unit/RuntimeTestTest.php index 39d9cc1..78325e6 100644 --- a/test/unit/RuntimeTestTest.php +++ b/test/unit/RuntimeTestTest.php @@ -7,7 +7,6 @@ class RuntimeTestTest extends TestCase { public function testRuntimeTest() { - $this->expectNotToPerformAssertions(); RuntimeTests::runtimeTest(); } }