Skip to content

Commit

Permalink
Merge pull request #506 from defuse/phpunit-compat2
Browse files Browse the repository at this point in the history
Make PHPUnit tests pass in PHP 8.0, PHP 8.1 and PHP 8.2 thanks to @boenrobot
  • Loading branch information
defuse authored Jun 17, 2023
2 parents 9006087 + 90c263c commit 1da53bd
Show file tree
Hide file tree
Showing 20 changed files with 224 additions and 334 deletions.
107 changes: 0 additions & 107 deletions .github/workflows/ci.yml

This file was deleted.

27 changes: 18 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,44 @@ 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"
env: USE_PSALM=1
- php: "8.1"
env: USE_PSALM=1
- php: "8.2"
env: USE_PSALM=1
dist: focal
- php: "nightly"
env: USE_PSALM=1
- php: "hhvm"
env: USE_PSALM=1
allow_failures:
- php: "nightly"
- php: "hhvm"
- php: "8.0"
# 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
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"php": ">=5.6.0"
},
"require-dev": {
"phpunit/phpunit": "^4|^5|^6|^7|^8|^9"
"phpunit/phpunit": "^5|^6|^7|^8|^9|^10",
"yoast/phpunit-polyfills": "^2.0.0"
},
"bin": [
"bin/generate-defuse-key"
Expand Down
7 changes: 6 additions & 1 deletion src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,14 @@ public static function incrementCounter($ctr, $inc)
*/
public static function secureRandom($octets)
{
if ($octets <= 0) {
throw new Ex\CryptoException(
'A zero or negative amount of random bytes was requested.'
);
}
self::ensureFunctionExists('random_bytes');
try {
return \random_bytes($octets);
return \random_bytes(max(1, $octets));
} catch (\Exception $ex) {
throw new Ex\EnvironmentIsBrokenException(
'Your system does not have a secure random number generator.'
Expand Down
39 changes: 38 additions & 1 deletion src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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: ' .
Expand All @@ -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(
Expand Down Expand Up @@ -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: ' .
Expand All @@ -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(
Expand Down Expand Up @@ -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();
}
}
}
9 changes: 9 additions & 0 deletions test/phpunit-10.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<phpunit beStrictAboutTestsThatDoNotTestAnything="false">
<source>
<include>
<directory suffix=".php">../src</directory>
</include>
</source>
<coverage includeUncoveredFiles="true">
</coverage>
</phpunit>
7 changes: 7 additions & 0 deletions test/phpunit-5.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<phpunit>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">../src</directory>
</whitelist>
</filter>
</phpunit>
7 changes: 7 additions & 0 deletions test/phpunit-8.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<phpunit>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">../src</directory>
</whitelist>
</filter>
</phpunit>
21 changes: 7 additions & 14 deletions test/phpunit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,12 @@ if [ "$clean" -eq 1 ]; then
fi

# Let's grab the latest release and its signature
phpunitversion=$([ $PHP_VERSION -ge 80100 ] && echo "10" || ([ $PHP_VERSION -ge 70200 ] && echo "8" || echo "5"))
if [ ! -f phpunit.phar ]; then
if [[ $PHP_VERSION -ge 50600 ]]; then
wget -O phpunit.phar https://phar.phpunit.de/phpunit-5.7.phar
else
wget -O phpunit.phar https://phar.phpunit.de/phpunit-4.8.phar
fi
wget -O phpunit.phar https://phar.phpunit.de/phpunit-$phpunitversion.phar
fi
if [ ! -f phpunit.phar.asc ]; then
if [[ $PHP_VERSION -ge 50600 ]]; then
wget -O phpunit.phar.asc https://phar.phpunit.de/phpunit-5.7.phar.asc
else
wget -O phpunit.phar.asc https://phar.phpunit.de/phpunit-4.8.phar.asc
fi
wget -O phpunit.phar.asc https://phar.phpunit.de/phpunit-$phpunitversion.phar.asc
fi

# What are the major/minor versions?
Expand All @@ -56,19 +49,19 @@ gpg --verify phpunit.phar.asc phpunit.phar
if [ $? -eq 0 ]; then
echo
if [ "$2" -eq "1" ]; then
COVERAGE1_ARGS="--coverage-clover=$parentdir/coverage1.xml -c $parentdir/test/phpunit.xml"
COVERAGE2_ARGS="--coverage-clover=$parentdir/coverage2.xml -c $parentdir/test/phpunit.xml"
COVERAGE1_ARGS="--coverage-clover=$parentdir/coverage1.xml"
COVERAGE2_ARGS="--coverage-clover=$parentdir/coverage2.xml"
else
COVERAGE1_ARGS=""
COVERAGE2_ARGS=""
fi
echo -e "\033[33mBegin Unit Testing\033[0m"
# Run the test suite with normal func_overload.
php -d mbstring.func_overload=0 phpunit.phar $COVERAGE1_ARGS --bootstrap "$parentdir/$1" "$parentdir/test/unit" && \
php -d mbstring.func_overload=0 phpunit.phar -c "$parentdir/test/phpunit-$phpunitversion.xml" $COVERAGE1_ARGS --bootstrap "$parentdir/$1" "$parentdir/test/unit" && \
# Run the test suite again with funky func_overload.
# This is deprecated in PHP 7 and PHPUnit is no longer compatible with the options.
if [[ $PHP_VERSION -le 50600 ]]; then
php -d mbstring.func_overload=7 phpunit.phar $COVERAGE2_ARGS --bootstrap "$parentdir/$1" "$parentdir/test/unit"
php -d mbstring.func_overload=7 phpunit.phar -c "$parentdir/test/phpunit-$phpunitversion.xml" $COVERAGE2_ARGS --bootstrap "$parentdir/$1" "$parentdir/test/unit"
fi
EXITCODE=$?
# Cleanup
Expand Down
7 changes: 0 additions & 7 deletions test/phpunit.xml

This file was deleted.

Loading

0 comments on commit 1da53bd

Please sign in to comment.