2
0

HashTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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-2010 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @group Zend_Validate
  43. */
  44. class Zend_Validate_File_HashTest extends PHPUnit_Framework_TestCase
  45. {
  46. /**
  47. * Runs the test methods of this class.
  48. *
  49. * @return void
  50. */
  51. public static function main()
  52. {
  53. $suite = new PHPUnit_Framework_TestSuite("Zend_Validate_File_HashTest");
  54. $result = PHPUnit_TextUI_TestRunner::run($suite);
  55. }
  56. /**
  57. * Ensures that the validator follows expected behavior
  58. *
  59. * @return void
  60. */
  61. public function testBasic()
  62. {
  63. $valuesExpected = array(
  64. array('3f8d07e2', true),
  65. array('9f8d07e2', false),
  66. array(array('9f8d07e2', '3f8d07e2'), true),
  67. array(array('9f8d07e2', '7f8d07e2'), false),
  68. );
  69. foreach ($valuesExpected as $element) {
  70. $validator = new Zend_Validate_File_Hash($element[0]);
  71. $this->assertEquals(
  72. $element[1],
  73. $validator->isValid(dirname(__FILE__) . '/_files/picture.jpg'),
  74. "Tested with " . var_export($element, 1)
  75. );
  76. }
  77. $valuesExpected = array(
  78. array(array('ed74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), true),
  79. array(array('4d74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), false),
  80. array(array('4d74c22109fe9f110579f77b053b8bc3', 'ed74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), true),
  81. array(array('1d74c22109fe9f110579f77b053b8bc3', '4d74c22109fe9f110579f77b053b8bc3', 'algorithm' => 'md5'), false),
  82. );
  83. foreach ($valuesExpected as $element) {
  84. $validator = new Zend_Validate_File_Hash($element[0]);
  85. $this->assertEquals(
  86. $element[1],
  87. $validator->isValid(dirname(__FILE__) . '/_files/picture.jpg'),
  88. "Tested with " . var_export($element, 1)
  89. );
  90. }
  91. $validator = new Zend_Validate_File_Hash('3f8d07e2');
  92. $this->assertFalse($validator->isValid(dirname(__FILE__) . '/_files/nofile.mo'));
  93. $this->assertTrue(array_key_exists('fileHashNotFound', $validator->getMessages()));
  94. $files = array(
  95. 'name' => 'test1',
  96. 'type' => 'text',
  97. 'size' => 200,
  98. 'tmp_name' => 'tmp_test1',
  99. 'error' => 0
  100. );
  101. $validator = new Zend_Validate_File_Hash('3f8d07e2');
  102. $this->assertFalse($validator->isValid(dirname(__FILE__) . '/_files/nofile.mo', $files));
  103. $this->assertTrue(array_key_exists('fileHashNotFound', $validator->getMessages()));
  104. $files = array(
  105. 'name' => 'testsize.mo',
  106. 'type' => 'text',
  107. 'size' => 200,
  108. 'tmp_name' => dirname(__FILE__) . '/_files/testsize.mo',
  109. 'error' => 0
  110. );
  111. $validator = new Zend_Validate_File_Hash('3f8d07e2');
  112. $this->assertTrue($validator->isValid(dirname(__FILE__) . '/_files/picture.jpg', $files));
  113. $files = array(
  114. 'name' => 'testsize.mo',
  115. 'type' => 'text',
  116. 'size' => 200,
  117. 'tmp_name' => dirname(__FILE__) . '/_files/testsize.mo',
  118. 'error' => 0
  119. );
  120. $validator = new Zend_Validate_File_Hash('9f8d07e2');
  121. $this->assertFalse($validator->isValid(dirname(__FILE__) . '/_files/picture.jpg', $files));
  122. $this->assertTrue(array_key_exists('fileHashDoesNotMatch', $validator->getMessages()));
  123. }
  124. /**
  125. * Ensures that getHash() returns expected value
  126. *
  127. * @return void
  128. */
  129. public function testgetHash()
  130. {
  131. $validator = new Zend_Validate_File_Hash('12345');
  132. $this->assertEquals(array('12345' => 'crc32'), $validator->getHash());
  133. $validator = new Zend_Validate_File_Hash(array('12345', '12333', '12344'));
  134. $this->assertEquals(array('12345' => 'crc32', '12333' => 'crc32', '12344' => 'crc32'), $validator->getHash());
  135. }
  136. /**
  137. * Ensures that setHash() returns expected value
  138. *
  139. * @return void
  140. */
  141. public function testSetHash()
  142. {
  143. $validator = new Zend_Validate_File_Hash('12345');
  144. $validator->setHash('12333');
  145. $this->assertEquals(array('12333' => 'crc32'), $validator->getHash());
  146. $validator->setHash(array('12321', '12121'));
  147. $this->assertEquals(array('12321' => 'crc32', '12121' => 'crc32'), $validator->getHash());
  148. }
  149. /**
  150. * Ensures that addHash() returns expected value
  151. *
  152. * @return void
  153. */
  154. public function testAddHash()
  155. {
  156. $validator = new Zend_Validate_File_Hash('12345');
  157. $validator->addHash('12344');
  158. $this->assertEquals(array('12345' => 'crc32', '12344' => 'crc32'), $validator->getHash());
  159. $validator->addHash(array('12321', '12121'));
  160. $this->assertEquals(array('12345' => 'crc32', '12344' => 'crc32', '12321' => 'crc32', '12121' => 'crc32'), $validator->getHash());
  161. }
  162. }
  163. // Call Zend_Validate_File_HashTest::main() if this source file is executed directly.
  164. if (PHPUnit_MAIN_METHOD == "Zend_Validate_File_HashTest::main") {
  165. Zend_Validate_File_HashTest::main();
  166. }