2
0

HashTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate_File
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: $
  21. */
  22. // Call Zend_Validate_File_HashTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Validate_File_HashTest::main");
  25. }
  26. /**
  27. * Test helper
  28. */
  29. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  30. /**
  31. * @see Zend_Validate_File_Hash
  32. */
  33. require_once 'Zend/Validate/File/Hash.php';
  34. /**
  35. * Hash testbed
  36. *
  37. * @category Zend
  38. * @package Zend_Validate_File
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. class Zend_Validate_File_HashTest extends PHPUnit_Framework_TestCase
  44. {
  45. /**
  46. * Runs the test methods of this class.
  47. *
  48. * @return void
  49. */
  50. public static function main()
  51. {
  52. $suite = new PHPUnit_Framework_TestSuite("Zend_Validate_File_HashTest");
  53. $result = PHPUnit_TextUI_TestRunner::run($suite);
  54. }
  55. /**
  56. * Ensures that the validator follows expected behavior
  57. *
  58. * @return void
  59. */
  60. public function testBasic()
  61. {
  62. $valuesExpected = array(
  63. array('3f8d07e2', true),
  64. array('9f8d07e2', false),
  65. array(array('9f8d07e2', '3f8d07e2'), true),
  66. array(array('9f8d07e2', '7f8d07e2'), false),
  67. );
  68. foreach ($valuesExpected as $element) {
  69. $validator = new Zend_Validate_File_Hash($element[0]);
  70. $this->assertEquals(
  71. $element[1],
  72. $validator->isValid(dirname(__FILE__) . '/_files/picture.jpg'),
  73. "Tested with " . var_export($element, 1)
  74. );
  75. }
  76. $valuesExpected = array(
  77. array(array('ed74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), true),
  78. array(array('4d74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), false),
  79. array(array('4d74c22109fe9f110579f77b053b8bc3', 'ed74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), true),
  80. array(array('1d74c22109fe9f110579f77b053b8bc3', '4d74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), false),
  81. );
  82. foreach ($valuesExpected as $element) {
  83. $validator = new Zend_Validate_File_Hash($element[0]);
  84. $this->assertEquals(
  85. $element[1],
  86. $validator->isValid(dirname(__FILE__) . '/_files/picture.jpg'),
  87. "Tested with " . var_export($element, 1)
  88. );
  89. }
  90. $validator = new Zend_Validate_File_Hash('3f8d07e2');
  91. $this->assertFalse($validator->isValid(dirname(__FILE__) . '/_files/nofile.mo'));
  92. $this->assertTrue(array_key_exists('fileHashNotFound', $validator->getMessages()));
  93. $files = array(
  94. 'name' => 'test1',
  95. 'type' => 'text',
  96. 'size' => 200,
  97. 'tmp_name' => 'tmp_test1',
  98. 'error' => 0
  99. );
  100. $validator = new Zend_Validate_File_Hash('3f8d07e2');
  101. $this->assertFalse($validator->isValid(dirname(__FILE__) . '/_files/nofile.mo', $files));
  102. $this->assertTrue(array_key_exists('fileHashNotFound', $validator->getMessages()));
  103. $files = array(
  104. 'name' => 'testsize.mo',
  105. 'type' => 'text',
  106. 'size' => 200,
  107. 'tmp_name' => dirname(__FILE__) . '/_files/testsize.mo',
  108. 'error' => 0
  109. );
  110. $validator = new Zend_Validate_File_Hash('3f8d07e2');
  111. $this->assertTrue($validator->isValid(dirname(__FILE__) . '/_files/picture.jpg', $files));
  112. $files = array(
  113. 'name' => 'testsize.mo',
  114. 'type' => 'text',
  115. 'size' => 200,
  116. 'tmp_name' => dirname(__FILE__) . '/_files/testsize.mo',
  117. 'error' => 0
  118. );
  119. $validator = new Zend_Validate_File_Hash('9f8d07e2');
  120. $this->assertFalse($validator->isValid(dirname(__FILE__) . '/_files/picture.jpg', $files));
  121. $this->assertTrue(array_key_exists('fileHashDoesNotMatch', $validator->getMessages()));
  122. }
  123. /**
  124. * Ensures that getHash() returns expected value
  125. *
  126. * @return void
  127. */
  128. public function testgetHash()
  129. {
  130. $validator = new Zend_Validate_File_Hash('12345');
  131. $this->assertEquals(array('12345' => 'crc32'), $validator->getHash());
  132. $validator = new Zend_Validate_File_Hash(array('12345', '12333', '12344'));
  133. $this->assertEquals(array('12345' => 'crc32', '12333' => 'crc32', '12344' => 'crc32'), $validator->getHash());
  134. }
  135. /**
  136. * Ensures that setHash() returns expected value
  137. *
  138. * @return void
  139. */
  140. public function testSetHash()
  141. {
  142. $validator = new Zend_Validate_File_Hash('12345');
  143. $validator->setHash('12333');
  144. $this->assertEquals(array('12333' => 'crc32'), $validator->getHash());
  145. $validator->setHash(array('12321', '12121'));
  146. $this->assertEquals(array('12321' => 'crc32', '12121' => 'crc32'), $validator->getHash());
  147. }
  148. /**
  149. * Ensures that addHash() returns expected value
  150. *
  151. * @return void
  152. */
  153. public function testAddHash()
  154. {
  155. $validator = new Zend_Validate_File_Hash('12345');
  156. $validator->addHash('12344');
  157. $this->assertEquals(array('12345' => 'crc32', '12344' => 'crc32'), $validator->getHash());
  158. $validator->addHash(array('12321', '12121'));
  159. $this->assertEquals(array('12345' => 'crc32', '12344' => 'crc32', '12321' => 'crc32', '12121' => 'crc32'), $validator->getHash());
  160. }
  161. }
  162. // Call Zend_Validate_File_HashTest::main() if this source file is executed directly.
  163. if (PHPUnit_MAIN_METHOD == "Zend_Validate_File_HashTest::main") {
  164. Zend_Validate_File_HashTest::main();
  165. }