Skip to content

Commit

Permalink
Merge pull request #1 from forkgroup/dev
Browse files Browse the repository at this point in the history
Added PHPUnit of Github Actions
  • Loading branch information
ericyzhu authored Mar 16, 2021
2 parents c7bd13c + 72b2a99 commit d7710b7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 28 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: PHPUnit

on: [ push, pull_request ]

jobs:
ci:
name: Test PHP ${{ matrix.php-version }} on Swoole ${{ matrix.swoole-version }}
runs-on: "${{ matrix.os }}"
env:
SWOOLE_VERSION: ${{ matrix.swoole-version }}
strategy:
matrix:
os: [ ubuntu-latest ]
php-version: [ '7.3', '7.4' ]
swoole-version: [ '4.6.2', '4.5.11' ]
max-parallel: 4
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
tools: phpize
ini-values: opcache.enable_cli=1, swoole.use_shortname='Off'
coverage: none
- name: Setup Swoole
run: |
wget https://github.com/swoole/swoole-src/archive/v${SWOOLE_VERSION}.tar.gz -O swoole.tar.gz
mkdir -p swoole
tar -xf swoole.tar.gz -C swoole --strip-components=1
rm swoole.tar.gz
cd swoole
phpize
./configure --enable-openssl --enable-mysqlnd --enable-http2
make -j$(nproc)
sudo make install
sudo sh -c "echo extension=swoole > /etc/php/${{ matrix.php-version }}/cli/conf.d/swoole.ini"
php --ri swoole
- name: Setup Packages
run: composer update -o --no-scripts
- name: Run Test Cases
run: |
composer analyse
composer test
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.14",
"hyperf/testing": "~2.1.0",
"limingxinleo/happy-join-hyperf": "^1.0",
"phpstan/phpstan": "^0.12",
"swoole/ide-helper": "dev-master"
},
Expand Down
8 changes: 4 additions & 4 deletions src/Driver/AesDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public static function supported(string $key, string $cipher): bool
{
$length = mb_strlen($key, '8bit');

return ($cipher === 'AES-128-CBC' && $length === 16) ||
($cipher === 'AES-256-CBC' && $length === 32);
return ($cipher === 'AES-128-CBC' && $length === 16)
|| ($cipher === 'AES-256-CBC' && $length === 32);
}

/**
Expand Down Expand Up @@ -190,8 +190,8 @@ protected function getJsonPayload(string $payload): array
protected function validPayload($payload): bool
{
try {
return is_array($payload) && isset($payload['iv'], $payload['value'], $payload['mac']) &&
strlen(base64_decode($payload['iv'], true)) === openssl_cipher_iv_length($this->cipher);
return is_array($payload) && isset($payload['iv'], $payload['value'], $payload['mac'])
&& strlen(base64_decode($payload['iv'], true)) === openssl_cipher_iv_length($this->cipher);
} catch (\Throwable $exception) {
}
return false;
Expand Down
37 changes: 13 additions & 24 deletions tests/AesEncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,18 @@ class AesEncryptionTest extends TestCase
public function testEncryption()
{
$e = new AesDriver([
'key' => str_repeat('a', 16),
'key' => base64_encode(str_repeat('a', 16)),
'cipher' => 'AES-128-CBC',
]);
$encrypted = $e->encrypt('foo');
$this->assertNotSame('foo', $encrypted);
$this->assertSame('foo', $e->decrypt($encrypted));
}

public function testRawStringEncryption()
{
$e = new AesDriver([
'key' => str_repeat('a', 16),
'cipher' => 'AES-128-CBC',
]);
$encrypted = $e->encryptString('foo');
$this->assertNotSame('foo', $encrypted);
$this->assertSame('foo', $e->decryptString($encrypted));
}

public function testEncryptionUsingBase64EncodedKey()
{
$e = new AesDriver([
'key' => random_bytes(16),
'key' => base64_encode(random_bytes(16)),
'cipher' => 'AES-128-CBC',
]);
$encrypted = $e->encrypt('foo');
Expand All @@ -57,7 +46,7 @@ public function testEncryptionUsingBase64EncodedKey()
public function testEncryptedLengthIsFixed()
{
$e = new AesDriver([
'key' => str_repeat('a', 16),
'key' => base64_encode(str_repeat('a', 16)),
'cipher' => 'AES-128-CBC',
]);
$lengths = [];
Expand All @@ -70,15 +59,15 @@ public function testEncryptedLengthIsFixed()
public function testWithCustomCipher()
{
$e = new AesDriver([
'key' => str_repeat('b', 32),
'key' => base64_encode(str_repeat('b', 32)),
'cipher' => 'AES-256-CBC',
]);
$encrypted = $e->encrypt('bar');
$this->assertNotSame('bar', $encrypted);
$this->assertSame('bar', $e->decrypt($encrypted));

$e = new AesDriver([
'key' => random_bytes(32),
'key' => base64_encode(random_bytes(32)),
'cipher' => 'AES-256-CBC',
]);
$encrypted = $e->encrypt('foo');
Expand All @@ -92,7 +81,7 @@ public function testDoNoAllowLongerKey()
$this->expectExceptionMessage('The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.');

new AesDriver([
'key' => str_repeat('z', 32),
'key' => base64_encode(str_repeat('z', 32)),
'cipher' => 'AES-128-CBC',
]);
}
Expand All @@ -103,7 +92,7 @@ public function testWithBadKeyLength()
$this->expectExceptionMessage('The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.');

new AesDriver([
'key' => str_repeat('a', 5),
'key' => base64_encode(str_repeat('a', 5)),
'cipher' => 'AES-128-CBC',
]);
}
Expand All @@ -114,7 +103,7 @@ public function testWithBadKeyLengthAlternativeCipher()
$this->expectExceptionMessage('The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.');

new AesDriver([
'key' => str_repeat('a', 16),
'key' => base64_encode(str_repeat('a', 16)),
'cipher' => 'AES-256-CFB8',
]);
}
Expand All @@ -125,7 +114,7 @@ public function testWithUnsupportedCipher()
$this->expectExceptionMessage('The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.');

new AesDriver([
'key' => str_repeat('c', 16),
'key' => base64_encode(str_repeat('c', 16)),
'cipher' => 'AES-256-CFB8',
]);
}
Expand All @@ -136,7 +125,7 @@ public function testExceptionThrownWhenPayloadIsInvalid()
$this->expectExceptionMessage('The payload is invalid.');

$e = new AesDriver([
'key' => str_repeat('a', 16),
'key' => base64_encode(str_repeat('a', 16)),
'cipher' => 'AES-128-CBC',
]);
$payload = $e->encrypt('foo');
Expand All @@ -150,11 +139,11 @@ public function testExceptionThrownWithDifferentKey()
$this->expectExceptionMessage('The MAC is invalid.');

$a = new AesDriver([
'key' => str_repeat('a', 16),
'key' => base64_encode(str_repeat('a', 16)),
'cipher' => 'AES-128-CBC',
]);
$b = new AesDriver([
'key' => str_repeat('b', 16),
'key' => base64_encode(str_repeat('b', 16)),
'cipher' => 'AES-128-CBC',
]);
$b->decrypt($a->encrypt('baz'));
Expand All @@ -166,7 +155,7 @@ public function testExceptionThrownWhenIvIsTooLong()
$this->expectExceptionMessage('The payload is invalid.');

$e = new AesDriver([
'key' => str_repeat('a', 16),
'key' => base64_encode(str_repeat('a', 16)),
'cipher' => 'AES-128-CBC',
]);
$payload = $e->encrypt('foo');
Expand Down

0 comments on commit d7710b7

Please sign in to comment.