/Tests/HashTest.php
PHP | 609 lines | 458 code | 108 blank | 43 comment | 1 complexity | 31b77e62f447e62b2da9167904c97938 MD5 | raw file
- <?php
-
- require_once 'PHPUnit/Framework.php';
- require_once 'Zend/Validate/Hash.php';
-
- /*
- * @todo test md5 functions
- * @todo test new isValid signature of array
- */
- class HashTest extends PHPUnit_Framework_TestCase
- {
- protected $_base_string = 'I am a test';
- protected $_sha1_of_string = 'c3207bbe306d89116d4058320b086296a43b8964';
- protected $_md5_of_string = '3be6306716ab9c505374d7cd72c63abd';
- protected $_too_short_md5 = 'be6306716ab9c505374d7cd72c63ab';
- protected $_chars_out_of_range_md5 = '3xe6306716ab9c505374d7cd72c63abz';
- protected $_sha256_of_string = '5f49192e05c579bb12c6afdd44819a572bf367336a32329c75b960e2ab1ce98e';
- protected $_chars_out_of_range_sha1 = 'c3207xbe306d89116d4058320b086296a43z8964';
- protected $_too_short_sha1 = '207bbe306d89116d4058320b086296a43b89';
- protected $_hasher;
-
- protected function setUp()
- {
-
- }
-
- /*
- * TEST OPTIONS
- */
-
- public function testOptionsAsInstanceOfZendConfig()
- {
- require_once 'Zend/Config/Ini.php';
-
- $options = new Zend_Config_Ini('.\\Fixtures\test.ini', 'production');
-
- $this->_hasher = new Zend_Validate_Hash($options);
- $gotten_options = $this->_hasher->getOptions();
-
- $this->assertArrayHasKey('format', $gotten_options);
- $this->assertArrayHasKey('algorithm', $gotten_options);
-
- $this->assertTrue($gotten_options['algorithm'] === $options->hash->algorithm);
- }
-
- public function testOptionsAsInstanceOfZendConfigWithoutProperFormat()
- {
- require_once 'Zend/Config/Ini.php';
-
- $options = new Zend_Config_Ini('.\Fixtures\bad_test.ini', 'production');
-
- try {
- $this->_hasher = new Zend_Validate_Hash($options);
- }
- catch (Zend_Validate_Exception $e) {
- $this->assertThat(
- $e->getMessage(),
- $this->stringContains(
- 'The ini options must be within a \'hash.\' grouping'
- )
- );
-
- return;
- }
-
- $this->fail('An expected exception was not raised');
-
- }
-
- public function testOptionsAsArrayWithFormatKey()
- {
- $options = array(
- 'format' => 'binary',
- 'algorithm' => 'md5'
- );
-
- $this->_hasher = new Zend_Validate_Hash($options);
- $gotten_options = $this->_hasher->getOptions();
-
- $this->assertArrayHasKey('format', $gotten_options);
- $this->assertTrue($gotten_options['format'] === $options['format']);
- }
-
- public function testOptionsAsArrayWithoutFormatKey()
- {
- $options = array(
- 'algorithm' => 'md5'
- );
-
- try {
- $this->_hasher = new Zend_Validate_Hash($options);
- }
- catch (Zend_Validate_Exception $e) {
- $this->assertThat(
- $e->getMessage(),
- $this->stringContains(
- 'The key \'format\' was not defined in the options array'
- )
- );
-
- return;
- }
-
- $this->fail('An expected exception was not raised');
- }
-
- public function testOptionsAsArrayWithABlankFormatKey()
- {
- $options = array(
- 'algorithm' => 'md5',
- 'format' => ''
- );
-
- try {
- $this->_hasher = new Zend_Validate_Hash($options);
- }
- catch (Zend_Validate_Exception $e) {
- $this->assertThat(
- $e->getMessage(),
- $this->stringContains(
- 'The format was not defined'
- )
- );
-
- return;
- }
-
- $this->fail('An expected exception was not raised');
- }
-
- public function testOptionsAsArrayWithANullFormatKey()
- {
- $options = array(
- 'algorithm' => 'md5',
- 'format' => null
- );
-
- try {
- $this->_hasher = new Zend_Validate_Hash($options);
- }
- catch (Zend_Validate_Exception $e) {
- $this->assertThat(
- $e->getMessage(),
- $this->stringContains(
- 'The format was not defined'
- )
- );
-
- return;
- }
-
- $this->fail('An expected exception was not raised');
- }
-
- public function testOptionsAsArrayWithAlgorithmKey()
- {
- $options = array(
- 'format' => 'binary',
- 'algorithm' => 'md5'
- );
- $this->_hasher = new Zend_Validate_Hash($options);
-
- $this->assertArrayHasKey('algorithm', $this->_hasher->getOptions());
- $an_option = $this->_hasher->getOptions();
- $this->assertTrue($an_option['algorithm'] == $options['algorithm']);
- }
-
- public function testOptionsAsArrayWithoutAlgorithmKey()
- {
- $options = array(
- 'format' => 'binary'
- );
-
- try {
- $this->_hasher = new Zend_Validate_Hash($options);
- }
- catch (Zend_Validate_Exception $e) {
- $this->assertThat(
- $e->getMessage(),
- $this->stringContains(
- 'The key \'algorithm\' was not defined in the options array'
- )
- );
-
- return;
- }
-
- $this->fail('An expected exception was not raised');
- }
-
- public function testOptionsAsArrayWithABlankAlgorithmKey()
- {
- $options = array(
- 'algorithm' => '',
- 'format' => 'hexadecimal'
- );
-
- try {
- $this->_hasher = new Zend_Validate_Hash($options);
- }
- catch (Zend_Validate_Exception $e) {
- $this->assertThat(
- $e->getMessage(),
- $this->stringContains(
- 'The algorithm was not defined'
- )
- );
-
- return;
- }
-
- $this->fail('An expected exception was not raised');
- }
-
- public function testOptionsAsArrayWithANullAlgorithmKey()
- {
- $options = array(
- 'algorithm' => null,
- 'format' => 'hexadecimal'
- );
-
- try {
- $this->_hasher = new Zend_Validate_Hash($options);
- }
- catch (Zend_Validate_Exception $e) {
- $this->assertThat(
- $e->getMessage(),
- $this->stringContains(
- 'The algorithm was not defined'
- )
- );
-
- return;
- }
-
- $this->fail('An expected exception was not raised');
- }
- public function testOptionsAsArray()
- {
- $options = array(
- 'format' => 'binary',
- 'algorithm' => 'md5'
- );
-
- $this->_hasher = new Zend_Validate_Hash($options);
-
- $this->assertTrue(is_array($this->_hasher->getOptions()));
- }
-
- public function testOptionsAsNull()
- {
- $options = null;
-
- try {
- $this->_hasher = new Zend_Validate_Hash($options);
- }
- catch (Zend_Validate_Exception $e) {
- $this->assertThat(
- $e->getMessage(),
- $this->stringContains(
- 'The options must be an array'
- )
- );
-
- return;
- }
-
- $this->fail('An expected exception was not raised');
- }
-
- /*
- * TEST BLANK ARGUMENTS
- */
-
-
- public function testConstructWithAnUnsupportedAlgorithm()
- {
- $options = array(
- 'algorithm' => 'sha256',
- 'format' => 'hexadecimal',
- );
-
- try {
- $this->_hasher = new Zend_Validate_Hash($options);
- }
- catch (Zend_Validate_Exception $e) {
- $this->assertThat(
- $e->getMessage(),
- $this->stringContains('This algorithm is unsupported')
- );
-
- return;
- }
- /* should fail if the message is not thus, or no exception */
- $this->fail('An expected exception was not raised');
- }
-
- public function testConstructWithAnUnsupportedFormat()
- {
- $options = array(
- 'algorithm' => 'md5',
- 'format' => 'base64',
- );
-
- try {
- $this->_hasher = new Zend_Validate_Hash($options);
- }
- catch (Zend_Validate_Exception $e) {
- $this->assertThat(
- $e->getMessage(),
- $this->stringContains(
- 'This format is unsupported'
- )
- );
-
- return;
- }
- /* should fail if the message is not thus, or no exception */
- $this->fail('An expected exception was not raised');
- }
-
- public function testIsValidWithANullHash()
- {
- $options = array(
- 'algorithm' => 'md5',
- 'format' => 'hexadecimal',
- );
- $this->_hasher = new Zend_Validate_Hash($options);
-
- try {
- $this->_hasher->isValid(null);
- }
- catch (Zend_Validate_Exception $e) {
- $this->assertThat(
- $e->getMessage(),
- $this->stringContains('The hash was not defined')
- );
-
- return;
- }
-
- $this->fail('An expected exception was not raised');
- }
-
- public function testIsValidWithABlankHash()
- {
- $options = array(
- 'algorithm' => 'md5',
- 'format' => 'hexadecimal',
- );
- $this->_hasher = new Zend_Validate_Hash($options);
-
- try {
- $this->_hasher->isValid('');
- }
- catch (Zend_Validate_Exception $e) {
- $this->assertThat(
- $e->getMessage(),
- $this->stringContains('The hash was not defined')
- );
-
- return;
- }
-
- $this->fail('An expected exception was not raised');
- }
-
- public function testIsValidWithANonStringValue()
- {
- $options = array(
- 'algorithm' => 'md5',
- 'format' => 'hexadecimal',
- );
- $this->_hasher = new Zend_Validate_Hash($options);
-
- try {
- $this->_hasher->isValid(array($this->_md5_of_string));
- }
- catch (Zend_Validate_Exception $e) {
- $this->assertThat(
- $e->getMessage(),
- $this->stringContains(
- 'The hash to validate must be a string'
- )
- );
-
- return;
- }
-
- $this->fail('An expected exception was not raised');
- }
-
- public function testSetAlgorithm()
- {
- $expected_algorithm = 'sha1';
-
- $options = array(
- 'algorithm' => $expected_algorithm,
- 'format' => 'hexadecimal',
- );
- $this->_hasher = new Zend_Validate_Hash($options);
-
- $this->assertEquals($expected_algorithm, $this->_hasher->getAlgorithm());
- }
-
-
- /*
- * TEST INVALID HASHES
- */
-
- public function testSha1HashInHexIsTooShort()
- {
- $options = array(
- 'algorithm' => 'sha1',
- 'format' => 'hexadecimal',
- );
- $this->_hasher = new Zend_Validate_Hash($options);
-
- $this->assertFalse($this->_hasher->isValid($this->_too_short_sha1));
- }
-
- public function testSha1HashInHexIsOutOfRange()
- {
- $options = array(
- 'format' => 'hexadecimal',
- 'algorithm' => 'sha1'
- );
- $this->_hasher = new Zend_Validate_Hash($options);
-
- $this->assertFalse(
- $this->_hasher->isValid($this->_chars_out_of_range_sha1)
- );
- }
-
- public function testIsNotAValidSha1HashInBinary()
- {
- $options = array(
- 'format' => 'binary',
- 'algorithm' => 'sha1'
- );
- $this->_hasher = new Zend_Validate_Hash($options);
-
- $binary_sha1 = sha1($this->_base_string, true);
- $modified_binary_sha1 = $binary_sha1 . 'a';
-
- $this->assertFalse(
- $this->_hasher->isValid($modified_binary_sha1)
- );
- }
-
- public function testMd5HashInHexIsOutOfRange()
- {
- $options = array(
- 'format' => 'hexadecimal',
- 'algorithm' => 'md5'
- );
- $this->_hasher = new Zend_Validate_Hash($options);
-
- $this->assertFalse(
- $this->_hasher->isValid($this->_chars_out_of_range_md5)
- );
-
- }
-
- public function testMd5HashInHexIsTooShort()
- {
- $options = array(
- 'format' => 'hexadecimal',
- 'algorithm' => 'md5'
- );
- $this->_hasher = new Zend_Validate_Hash($options);
-
- $this->assertFalse(
- $this->_hasher->isValid($this->_too_short_md5)
- );
- }
-
- public function testIsNotAValidMd5HashInBinary()
- {
- $options = array(
- 'format' => 'binary',
- 'algorithm' => 'md5'
- );
- $this->_hasher = new Zend_Validate_Hash($options);
-
- $binary_md5 = md5($this->_base_string, true);
- $modified_binary_md5 = $binary_md5 . 'a';
-
- $this->assertFalse(
- $this->_hasher->isValid($modified_binary_md5)
- );
- }
-
- /*
- * TEST VALID HASHES
- */
-
- /**
- *
- */
- public function testIsValidSha1HashInHex()
- {
- $options = array(
- 'format' => 'hexadecimal',
- 'algorithm' => 'sha1'
- );
- $this->_hasher = new Zend_Validate_Hash($options);
-
- $this->assertTrue(
- $this->_hasher->isValid($this->_sha1_of_string)
- );
- }
-
- /**
- *
- */
- public function testIsValidSha1HashInBinary()
- {
- $options = array(
- 'format' => 'binary',
- 'algorithm' => 'sha1'
- );
- $this->_hasher = new Zend_Validate_Hash($options);
-
- $this->assertTrue(
- $this->_hasher->isValid(sha1($this->_base_string, true))
- );
- }
-
- /**
- *
- */
- public function testIsValidMd5HashInHex()
- {
- $options = array(
- 'format' => 'hexadecimal',
- 'algorithm' => 'md5'
- );
- $this->_hasher = new Zend_Validate_Hash($options);
-
- $this->assertTrue(
- $this->_hasher->isValid($this->_md5_of_string)
- );
- }
-
- /**
- *
- */
- public function testIsValidMd5HashInBinary()
- {
- $options = array(
- 'format' => 'binary',
- 'algorithm' => 'md5'
- );
- $this->_hasher = new Zend_Validate_Hash($options);
-
- $this->assertTrue(
- $this->_hasher->isValid(md5($this->_base_string, true))
- );
- }
-
- /*
- * TESTING getHash()
- */
-
- /**
- * test to make sure the class has set the hash correctly.
- */
- public function testGetHashWhenHashIsHex()
- {
- $options = array(
- 'format' => 'hexadecimal',
- 'algorithm' => 'sha1'
- );
- $this->_hasher = new Zend_Validate_Hash($options);
-
- //must run this to get the hash set
- $this->_hasher->isValid($this->_sha1_of_string);
-
- /* the actual test */
- $this->assertEquals(
- $this->_sha1_of_string, $this->_hasher->getHash()
- );
- }
-
- /**
- *
- */
- public function testGetHashWhenHashIsBinary()
- {
- $options = array(
- 'format' => 'binary',
- 'algorithm' => 'sha1'
- );
- $this->_hasher = new Zend_Validate_Hash($options);
-
- //must run this to get the hash set
- $this->_hasher->isValid(sha1($this->_base_string, true));
-
- /* the actual test */
- $this->assertEquals(
- $this->_sha1_of_string, $this->_hasher->getHash()
- );
- }
-
- protected function tearDown()
- {
- }
- }