Sha1Test.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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_Sha1Test::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Validate_File_Sha1Test::main");
  25. }
  26. /**
  27. * Test helper
  28. */
  29. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  30. /**
  31. * @see Zend_Validate_File_Sha1
  32. */
  33. require_once 'Zend/Validate/File/Sha1.php';
  34. /**
  35. * Sha1 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_Sha1Test 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_Sha1Test");
  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('b2a5334847b4328e7d19d9b41fd874dffa911c98', true),
  64. array('52a5334847b4328e7d19d9b41fd874dffa911c98', false),
  65. array(array('42a5334847b4328e7d19d9b41fd874dffa911c98', 'b2a5334847b4328e7d19d9b41fd874dffa911c98'), true),
  66. array(array('42a5334847b4328e7d19d9b41fd874dffa911c98', '72a5334847b4328e7d19d9b41fd874dffa911c98'), false),
  67. );
  68. foreach ($valuesExpected as $element) {
  69. $validator = new Zend_Validate_File_Sha1($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. $validator = new Zend_Validate_File_Sha1('b2a5334847b4328e7d19d9b41fd874dffa911c98');
  77. $this->assertFalse($validator->isValid(dirname(__FILE__) . '/_files/nofile.mo'));
  78. $this->assertTrue(array_key_exists('fileSha1NotFound', $validator->getMessages()));
  79. $files = array(
  80. 'name' => 'test1',
  81. 'type' => 'text',
  82. 'size' => 200,
  83. 'tmp_name' => 'tmp_test1',
  84. 'error' => 0
  85. );
  86. $validator = new Zend_Validate_File_Sha1('b2a5334847b4328e7d19d9b41fd874dffa911c98');
  87. $this->assertFalse($validator->isValid(dirname(__FILE__) . '/_files/nofile.mo', $files));
  88. $this->assertTrue(array_key_exists('fileSha1NotFound', $validator->getMessages()));
  89. $files = array(
  90. 'name' => 'testsize.mo',
  91. 'type' => 'text',
  92. 'size' => 200,
  93. 'tmp_name' => dirname(__FILE__) . '/_files/testsize.mo',
  94. 'error' => 0
  95. );
  96. $validator = new Zend_Validate_File_Sha1('b2a5334847b4328e7d19d9b41fd874dffa911c98');
  97. $this->assertTrue($validator->isValid(dirname(__FILE__) . '/_files/picture.jpg', $files));
  98. $files = array(
  99. 'name' => 'testsize.mo',
  100. 'type' => 'text',
  101. 'size' => 200,
  102. 'tmp_name' => dirname(__FILE__) . '/_files/testsize.mo',
  103. 'error' => 0
  104. );
  105. $validator = new Zend_Validate_File_Sha1('42a5334847b4328e7d19d9b41fd874dffa911c98');
  106. $this->assertFalse($validator->isValid(dirname(__FILE__) . '/_files/picture.jpg', $files));
  107. $this->assertTrue(array_key_exists('fileSha1DoesNotMatch', $validator->getMessages()));
  108. }
  109. /**
  110. * Ensures that getSha1() returns expected value
  111. *
  112. * @return void
  113. */
  114. public function testgetSha1()
  115. {
  116. $validator = new Zend_Validate_File_Sha1('12345');
  117. $this->assertEquals(array('12345' => 'sha1'), $validator->getSha1());
  118. $validator = new Zend_Validate_File_Sha1(array('12345', '12333', '12344'));
  119. $this->assertEquals(array('12345' => 'sha1', '12333' => 'sha1', '12344' => 'sha1'), $validator->getSha1());
  120. }
  121. /**
  122. * Ensures that getHash() returns expected value
  123. *
  124. * @return void
  125. */
  126. public function testgetHash()
  127. {
  128. $validator = new Zend_Validate_File_Sha1('12345');
  129. $this->assertEquals(array('12345' => 'sha1'), $validator->getHash());
  130. $validator = new Zend_Validate_File_Sha1(array('12345', '12333', '12344'));
  131. $this->assertEquals(array('12345' => 'sha1', '12333' => 'sha1', '12344' => 'sha1'), $validator->getHash());
  132. }
  133. /**
  134. * Ensures that setSha1() returns expected value
  135. *
  136. * @return void
  137. */
  138. public function testSetSha1()
  139. {
  140. $validator = new Zend_Validate_File_Sha1('12345');
  141. $validator->setSha1('12333');
  142. $this->assertEquals(array('12333' => 'sha1'), $validator->getSha1());
  143. $validator->setSha1(array('12321', '12121'));
  144. $this->assertEquals(array('12321' => 'sha1', '12121' => 'sha1'), $validator->getSha1());
  145. }
  146. /**
  147. * Ensures that setHash() returns expected value
  148. *
  149. * @return void
  150. */
  151. public function testSetHash()
  152. {
  153. $validator = new Zend_Validate_File_Sha1('12345');
  154. $validator->setHash('12333');
  155. $this->assertEquals(array('12333' => 'sha1'), $validator->getSha1());
  156. $validator->setHash(array('12321', '12121'));
  157. $this->assertEquals(array('12321' => 'sha1', '12121' => 'sha1'), $validator->getSha1());
  158. }
  159. /**
  160. * Ensures that addSha1() returns expected value
  161. *
  162. * @return void
  163. */
  164. public function testAddSha1()
  165. {
  166. $validator = new Zend_Validate_File_Sha1('12345');
  167. $validator->addSha1('12344');
  168. $this->assertEquals(array('12345' => 'sha1', '12344' => 'sha1'), $validator->getSha1());
  169. $validator->addSha1(array('12321', '12121'));
  170. $this->assertEquals(array('12345' => 'sha1', '12344' => 'sha1', '12321' => 'sha1', '12121' => 'sha1'), $validator->getSha1());
  171. }
  172. /**
  173. * Ensures that addHash() returns expected value
  174. *
  175. * @return void
  176. */
  177. public function testAddHash()
  178. {
  179. $validator = new Zend_Validate_File_Sha1('12345');
  180. $validator->addHash('12344');
  181. $this->assertEquals(array('12345' => 'sha1', '12344' => 'sha1'), $validator->getSha1());
  182. $validator->addHash(array('12321', '12121'));
  183. $this->assertEquals(array('12345' => 'sha1', '12344' => 'sha1', '12321' => 'sha1', '12121' => 'sha1'), $validator->getSha1());
  184. }
  185. }
  186. // Call Zend_Validate_File_Sha1Test::main() if this source file is executed directly.
  187. if (PHPUnit_MAIN_METHOD == "Zend_Validate_File_Sha1Test::main") {
  188. Zend_Validate_File_Sha1Test::main();
  189. }