Skip to content

Commit

Permalink
Apply coding guidelines (with exceptions) to the tests directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
bantu committed Feb 15, 2014
1 parent 9cb25c3 commit ef528ea
Show file tree
Hide file tree
Showing 16 changed files with 636 additions and 626 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ before_script:

script:
- sh -c "if [ '$TRAVIS_PHP_VERSION' = '5.5' ]; then vendor/bin/phpcs -s --extensions=php --standard=build/code-sniffer-ruleset.xml phpseclib/; fi"
- sh -c "if [ '$TRAVIS_PHP_VERSION' = '5.5' ]; then vendor/bin/phpcs -s --extensions=php --standard=build/code-sniffer-ruleset-tests.xml tests/; fi"
- phpunit --verbose --coverage-text --coverage-html code_coverage/

after_success:
Expand Down
21 changes: 21 additions & 0 deletions build/code-sniffer-ruleset-tests.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<ruleset name="phpseclib Test Standard">

<description>phpseclib coding standard for tests</description>

<!-- In general rules that apply to library code also apply to tests. -->
<rule ref="./code-sniffer-ruleset.xml">
<!-- Exceptions to the library coding standard follow. -->

<!-- We do not care too much about method, class and file documentation,
but having @author, @copyright and @license tags would be nice.
The following configuration does not check for these tags, but
complains if the file doc block is missing completely. -->
<exclude name="PEAR.Commenting.ClassComment" />
<exclude name="PEAR.Commenting.FunctionComment" />
<exclude name="PEAR.Commenting.FileComment.MissingTag" />
<exclude name="PEAR.Commenting.FileComment.MissingVersion" />
<exclude name="PEAR.Commenting.FileComment.SpacingBeforeTags" />
</rule>

</ruleset>
111 changes: 56 additions & 55 deletions tests/Crypt/AES/ContinuousBufferTest.php
Original file line number Diff line number Diff line change
@@ -1,71 +1,72 @@
<?php
/**
* @author Andreas Fischer <[email protected]>
* @copyright MMXIII Andreas Fischer
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @author Andreas Fischer <[email protected]>
* @copyright MMXIII Andreas Fischer
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/

class Crypt_AES_ContinuousBufferTest extends Crypt_AES_TestCase
{
// String intented
protected $modes = array(
'CRYPT_AES_MODE_CTR',
'CRYPT_AES_MODE_OFB',
'CRYPT_AES_MODE_CFB',
);
// 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 $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 $ivs = array(
'',
'test123',
);

protected $keys = array(
'',
':-8', // https://github.com/phpseclib/phpseclib/pull/43
'FOOBARZ',
);
protected $keys = array(
'',
':-8', // https://github.com/phpseclib/phpseclib/pull/43
'FOOBARZ',
);

/**
* Produces all combinations of test values.
*
* @return array
*/
public function allCombinations()
{
$result = array();
/**
* Produces all combinations of test values.
*
* @return array
*/
public function allCombinations()
{
$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);
// @codingStandardsIgnoreStart
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);
// @codingStandardsIgnoreEnd

return $result;
}
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);
/**
* @dataProvider allCombinations
*/
public function testEncryptDecrypt($mode, $plaintext, $iv, $key)
{
$aes = new Crypt_AES(constant($mode));
$aes->enableContinuousBuffer();
$aes->setIV($iv);
$aes->setKey($key);

$actual = '';
for ($i = 0, $strlen = strlen($plaintext); $i < $strlen; ++$i)
{
$actual .= $aes->decrypt($aes->encrypt($plaintext[$i]));
}
$actual = '';
for ($i = 0, $strlen = strlen($plaintext); $i < $strlen; ++$i) {
$actual .= $aes->decrypt($aes->encrypt($plaintext[$i]));
}

$this->assertEquals($plaintext, $actual);
}
$this->assertEquals($plaintext, $actual);
}
}
38 changes: 19 additions & 19 deletions tests/Crypt/AES/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<?php
/**
* @author Andreas Fischer <[email protected]>
* @copyright MMXIII Andreas Fischer
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @author Andreas Fischer <[email protected]>
* @copyright MMXIII Andreas Fischer
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/

require_once 'Crypt/AES.php';

abstract class Crypt_AES_TestCase extends PhpseclibTestCase
{
static public function setUpBeforeClass()
{
require_once('Crypt/AES.php');

if (!defined('CRYPT_AES_MODE'))
{
define('CRYPT_AES_MODE', CRYPT_AES_MODE_INTERNAL);
}
}
static public function setUpBeforeClass()
{
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.');
}
}
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.'
);
}
}
}
72 changes: 36 additions & 36 deletions tests/Crypt/Hash/MD5Test.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
<?php
/**
* @author Andreas Fischer <[email protected]>
* @copyright MMXII Andreas Fischer
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @author Andreas Fischer <[email protected]>
* @copyright MMXII Andreas Fischer
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/

class Crypt_Hash_MD5Test extends Crypt_Hash_TestCase
{
public function getInstance()
{
return new Crypt_Hash('md5');
}
public function getInstance()
{
return new Crypt_Hash('md5');
}

/**
* @dataProvider hashData()
*/
public function testHash($message, $result)
{
$this->assertHashesTo($this->getInstance(), $message, $result);
}
/**
* @dataProvider hashData()
*/
public function testHash($message, $result)
{
$this->assertHashesTo($this->getInstance(), $message, $result);
}

static public function hashData()
{
return array(
array('', 'd41d8cd98f00b204e9800998ecf8427e'),
array('The quick brown fox jumps over the lazy dog', '9e107d9d372bb6826bd81d3542a419d6'),
array('The quick brown fox jumps over the lazy dog.', 'e4d909c290d0fb1ca068ffaddf22cbd0'),
);
}
static public function hashData()
{
return array(
array('', 'd41d8cd98f00b204e9800998ecf8427e'),
array('The quick brown fox jumps over the lazy dog', '9e107d9d372bb6826bd81d3542a419d6'),
array('The quick brown fox jumps over the lazy dog.', 'e4d909c290d0fb1ca068ffaddf22cbd0'),
);
}

/**
* @dataProvider hmacData()
*/
public function testHMAC($key, $message, $result)
{
$this->assertHMACsTo($this->getInstance(), $key, $message, $result);
}
/**
* @dataProvider hmacData()
*/
public function testHMAC($key, $message, $result)
{
$this->assertHMACsTo($this->getInstance(), $key, $message, $result);
}

static public function hmacData()
{
return array(
array('', '', '74e6f7298a9c2d168935f58c001bad88'),
array('key', 'The quick brown fox jumps over the lazy dog', '80070713463e7749b90c2dc24911e275'),
);
}
static public function hmacData()
{
return array(
array('', '', '74e6f7298a9c2d168935f58c001bad88'),
array('key', 'The quick brown fox jumps over the lazy dog', '80070713463e7749b90c2dc24911e275'),
);
}
}
77 changes: 41 additions & 36 deletions tests/Crypt/Hash/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
<?php
/**
* @author Andreas Fischer <[email protected]>
* @copyright MMXII Andreas Fischer
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @author Andreas Fischer <[email protected]>
* @copyright MMXII Andreas Fischer
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/

require_once 'Crypt/Hash.php';

abstract class Crypt_Hash_TestCase extends PhpseclibTestCase
{
static public function setUpBeforeClass()
{
require_once('Crypt/Hash.php');

if (!defined('CRYPT_HASH_MODE'))
{
define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_INTERNAL);
}
}
static public function setUpBeforeClass()
{
if (!defined('CRYPT_HASH_MODE')) {
define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_INTERNAL);
}
}

public function setUp()
{
if (defined('CRYPT_HASH_MODE') && CRYPT_HASH_MODE !== CRYPT_HASH_MODE_INTERNAL)
{
$this->markTestSkipped('Skipping test because CRYPT_HASH_MODE is not defined as CRYPT_HASH_MODE_INTERNAL.');
}
}
public function setUp()
{
if (defined('CRYPT_HASH_MODE') && CRYPT_HASH_MODE !== CRYPT_HASH_MODE_INTERNAL) {
$this->markTestSkipped(
'Skipping test because CRYPT_HASH_MODE is not defined as CRYPT_HASH_MODE_INTERNAL.'
);
}
}

protected function assertHashesTo(Crypt_Hash $hash, $message, $expected)
{
$this->assertEquals(
strtolower($expected),
bin2hex($hash->hash($message)),
sprintf("Failed asserting that '%s' hashes to '%s'.", $message, $expected)
);
}
protected function assertHashesTo(Crypt_Hash $hash, $message, $expected)
{
$this->assertEquals(
strtolower($expected),
bin2hex($hash->hash($message)),
sprintf("Failed asserting that '%s' hashes to '%s'.", $message, $expected)
);
}

protected function assertHMACsTo(Crypt_Hash $hash, $key, $message, $expected)
{
$hash->setKey($key);
protected function assertHMACsTo(Crypt_Hash $hash, $key, $message, $expected)
{
$hash->setKey($key);

$this->assertEquals(
strtolower($expected),
bin2hex($hash->hash($message)),
sprintf("Failed asserting that '%s' HMACs to '%s' with key '%s'.", $message, $expected, $key)
);
}
$this->assertEquals(
strtolower($expected),
bin2hex($hash->hash($message)),
sprintf(
"Failed asserting that '%s' HMACs to '%s' with key '%s'.",
$message,
$expected,
$key
)
);
}
}
Loading

0 comments on commit ef528ea

Please sign in to comment.