Skip to content

Commit

Permalink
use GitHub Actions instead of Travis CI
Browse files Browse the repository at this point in the history
  • Loading branch information
terrafrost committed Feb 22, 2024
1 parent 86d851b commit 8c338db
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 3 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

name: CI
on: [push, pull_request]

permissions:
contents: read # to fetch code (actions/checkout)

jobs:
tests:
name: Tests
timeout-minutes: 10
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
- name: Composer Install
run: composer install --no-interaction --no-cache
- name: Make Tests Compatiable With PHPUnit 9+
if: contains(fromJSON('["7.3", "7.4", "8.0", "8.1", "8.2", "8.3"]'), matrix.php-version)
run: php tests/make_compatible_with_phpunit9.php
- name: PHPUnit
run: vendor/bin/phpunit
strategy:
fail-fast: false
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', '8.2', '8.3']
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# mcrypt_compat

[![Build Status](https://travis-ci.org/phpseclib/mcrypt_compat.svg?branch=master)](https://app.travis-ci.com/github/phpseclib/mcrypt_compat)
[![CI Status](https://github.com/phpseclib/mcrypt_compat/actions/workflows/ci.yml/badge.svg?branch=1.0&event=push "CI Status")](https://github.com/phpseclib/mcrypt_compat/actions/workflows/ci.yml?query=branch%3A1.0)

PHP 5.x-8.x polyfill for mcrypt extension.

Expand Down
4 changes: 2 additions & 2 deletions lib/mcrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ function phpseclib_mcrypt_ecb($cipher, $key, $data, $mode, $iv = null)
*/
function phpseclib_mcrypt_encrypt($cipher, $key, $data, $mode, $iv = null)
{
return defined('PHPSECLIB_MCRYPT_TARGET_VERSION') && version_compare(PHPSECLIB_MCRYPT_TARGET_VERSION, '5.6.0', '>=') ?
return !defined('PHPSECLIB_MCRYPT_TARGET_VERSION') || version_compare(PHPSECLIB_MCRYPT_TARGET_VERSION, '5.6.0', '>=') ?
phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, 'encrypt') :
phpseclib_mcrypt_helper_old($cipher, $key, $data, $mode, $iv, 'encrypt');
}
Expand All @@ -1198,7 +1198,7 @@ function phpseclib_mcrypt_encrypt($cipher, $key, $data, $mode, $iv = null)
*/
function phpseclib_mcrypt_decrypt($cipher, $key, $data, $mode, $iv = null)
{
return defined('PHPSECLIB_MCRYPT_TARGET_VERSION') && version_compare(PHPSECLIB_MCRYPT_TARGET_VERSION, '5.6.0', '>=') ?
return !defined('PHPSECLIB_MCRYPT_TARGET_VERSION') || version_compare(PHPSECLIB_MCRYPT_TARGET_VERSION, '5.6.0', '>=') ?
phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, 'decrypt') :
phpseclib_mcrypt_helper_old($cipher, $key, $data, $mode, $iv, 'decrypt');
}
Expand Down
47 changes: 47 additions & 0 deletions tests/MCryptCompatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,53 @@ public function testMcryptGenericWithTwoParamsPHPPost71()
phpseclib_mcrypt_generic_init($td, 'xxx');
}

public function testOldMcryptNoIVWarning()
{
$key = 'key';
$data = 'data';
$iv = null;

$this->setExpectedException('PHPUnit_Framework_Error_Warning', 'mcrypt_encrypt(): Attempt to use an empty IV, which is NOT recommended');

phpseclib_mcrypt_helper_old('rijndael-128', $key, $data, 'cbc', $iv, 'encrypt');
}

public function testOldMcryptShortIVWarning()
{
$key = 'key';
$data = 'data';
$iv = 'iv';

$this->setExpectedException('PHPUnit_Framework_Error_Warning', 'mcrypt_encrypt(): The IV parameter must be as long as the blocksize');

phpseclib_mcrypt_helper_old('rijndael-128', $key, $data, 'cbc', $iv, 'encrypt');
}

public function testOldMcryptShortIV()
{
$key = 'key';
$data = 'data';
$iv = 'iv';

$result = @phpseclib_mcrypt_helper_old('rijndael-128', $key, $data, 'cbc', $iv, 'encrypt');

$this->assertEquals('69c48f0bce2c81abd64bbab839080574', bin2hex($result));
}

public function testOldMcryptNoIV()
{
$key = str_pad('key', 16, "\0");
$data = 'data';
$iv = null;

$result = @phpseclib_mcrypt_helper_old('rijndael-128', $key, $data, 'cbc', $iv, 'encrypt');

// this yields the same result that testOldMcryptShortIV() yields. when the IV is invalid it's assumed to be all null bytes,
// whilst a key that's not the right length is either null padded or truncated as appropriate

$this->assertEquals('69c48f0bce2c81abd64bbab839080574', bin2hex($result));
}

public function providerForIVSizeChecks()
{
$tests = [
Expand Down
26 changes: 26 additions & 0 deletions tests/make_compatible_with_phpunit9.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/** @var iterable<SplFileInfo> $files */
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__));
foreach ($files as $file) {
if ($file->getExtension() === 'php' && $file->getPathname() !== __FILE__) {
$fileContents = file_get_contents($file->getPathname());
if ($fileContents === false) {
throw new \RuntimeException('file_get_contents() failed: ' . $file->getPathname());
}
$patternToReplacementMap = array(
'~(n assertIsArray\([^,\)]*,)([^,\)]*\))~' => '$1 string $2: void',
'~(n assertIsArray\([^,\)]*\))~' => '$1: void',
'~(n assertIsString\([^\)]*\))~' => '$1: void',
'~(n assertStringContainsString\([^\)]*\))~' => '$1: void'
);
$updatedFileContents = preg_replace(
array_keys($patternToReplacementMap),
array_values($patternToReplacementMap),
$fileContents
);
if (file_put_contents($file->getPathname(), $updatedFileContents) === false) {
throw new \RuntimeException('file_put_contents() failed: ' . $file->getPathname());
}
}
}

0 comments on commit 8c338db

Please sign in to comment.