From 318ac0bd8681591507980697730aaf1972df7492 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Thu, 3 Jan 2013 17:47:40 +0100 Subject: [PATCH 1/3] [issue/39] Abstract test case for Crypt_AES. --- tests/Crypt/AES/TestCase.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/Crypt/AES/TestCase.php diff --git a/tests/Crypt/AES/TestCase.php b/tests/Crypt/AES/TestCase.php new file mode 100644 index 00000000..d5884129 --- /dev/null +++ b/tests/Crypt/AES/TestCase.php @@ -0,0 +1,27 @@ + + * @copyright MMXIII Andreas Fischer + * @license http://www.opensource.org/licenses/mit-license.html MIT License + */ + +abstract class Crypt_AES_TestCase extends PHPUnit_Framework_TestCase +{ + static public function setUpBeforeClass() + { + require_once('Crypt/AES.php'); + + if (!defined('CRYPT_AES_MODE')) + { + define('CRYPT_AES_MODE', CRYPT_AES_MODE_INTERNAL); + } + } + + public function setUp() + { + if (defined('CRYPT_AES_MODE') && CRYPT_AES_MODE !== CRYPT_AES_MODE_INTERNAL) + { + $this->markTestSkipped('Skipping test because CRYPT_AES_MODE is not defined as CRYPT_AES_MODE_INTERNAL.'); + } + } +} From f15bc38520000d384ef282e11744fc64a490cfb0 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Thu, 3 Jan 2013 17:48:14 +0100 Subject: [PATCH 2/3] [issue/39] Test case for Github ContinuousBuffer issue 39. --- tests/Crypt/AES/ContinuousBufferTest.php | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/Crypt/AES/ContinuousBufferTest.php diff --git a/tests/Crypt/AES/ContinuousBufferTest.php b/tests/Crypt/AES/ContinuousBufferTest.php new file mode 100644 index 00000000..3eef02f2 --- /dev/null +++ b/tests/Crypt/AES/ContinuousBufferTest.php @@ -0,0 +1,26 @@ + + * @copyright MMXIII Andreas Fischer + * @license http://www.opensource.org/licenses/mit-license.html MIT License + */ + +class Crypt_AES_ContinuousBufferTest extends Crypt_AES_TestCase +{ + // https://github.com/phpseclib/phpseclib/issues/39 + public function testGithubIssue39EncryptDecrypt() + { + $aes = new Crypt_AES(CRYPT_AES_MODE_CFB); + $aes->enableContinuousBuffer(); + + $expected = '12345678901234567'; + $actual = ''; + + for ($i = 0, $strlen = strlen($expected); $i < $strlen; ++$i) + { + $actual .= $aes->decrypt($aes->encrypt($expected[$i])); + } + + $this->assertEquals($expected, $actual); + } +} From 6e597bfe2580e1de28d628de6244237360a5c13a Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Fri, 4 Jan 2013 14:08:41 +0100 Subject: [PATCH 3/3] [issue/39] Extend tests to cover other modes. Also IVs, keys, etc. --- tests/Crypt/AES/ContinuousBufferTest.php | 61 ++++++++++++++++++++---- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/tests/Crypt/AES/ContinuousBufferTest.php b/tests/Crypt/AES/ContinuousBufferTest.php index 3eef02f2..2d7185b3 100644 --- a/tests/Crypt/AES/ContinuousBufferTest.php +++ b/tests/Crypt/AES/ContinuousBufferTest.php @@ -7,20 +7,65 @@ class Crypt_AES_ContinuousBufferTest extends Crypt_AES_TestCase { - // https://github.com/phpseclib/phpseclib/issues/39 - public function testGithubIssue39EncryptDecrypt() + // String intented + protected $modes = array( + 'CRYPT_AES_MODE_CTR', + 'CRYPT_AES_MODE_OFB', + 'CRYPT_AES_MODE_CFB', + ); + + protected $plaintexts = array( + '', + '12345678901234567', // https://github.com/phpseclib/phpseclib/issues/39 + "\xDE\xAD\xBE\xAF", + ':-):-):-):-):-):-)', // https://github.com/phpseclib/phpseclib/pull/43 + ); + + protected $ivs = array( + '', + 'test123', + ); + + protected $keys = array( + '', + ':-8', // https://github.com/phpseclib/phpseclib/pull/43 + 'FOOBARZ', + ); + + /** + * Produces all combinations of test values. + * + * @return array + */ + public function allCombinations() { - $aes = new Crypt_AES(CRYPT_AES_MODE_CFB); + $result = array(); + + foreach ($this->modes as $mode) + foreach ($this->plaintexts as $plaintext) + foreach ($this->ivs as $iv) + foreach ($this->keys as $key) + $result[] = array($mode, $plaintext, $iv, $key); + + return $result; + } + + /** + * @dataProvider allCombinations + */ + public function testEncryptDecrypt($mode, $plaintext, $iv, $key) + { + $aes = new Crypt_AES(constant($mode)); $aes->enableContinuousBuffer(); + $aes->setIV($iv); + $aes->setKey($key); - $expected = '12345678901234567'; $actual = ''; - - for ($i = 0, $strlen = strlen($expected); $i < $strlen; ++$i) + for ($i = 0, $strlen = strlen($plaintext); $i < $strlen; ++$i) { - $actual .= $aes->decrypt($aes->encrypt($expected[$i])); + $actual .= $aes->decrypt($aes->encrypt($plaintext[$i])); } - $this->assertEquals($expected, $actual); + $this->assertEquals($plaintext, $actual); } }