PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/HashTest.php

https://bitbucket.org/mgatto/zend_validate_hash
PHP | 609 lines | 458 code | 108 blank | 43 comment | 1 complexity | 31b77e62f447e62b2da9167904c97938 MD5 | raw file
  1. <?php
  2. require_once 'PHPUnit/Framework.php';
  3. require_once 'Zend/Validate/Hash.php';
  4. /*
  5. * @todo test md5 functions
  6. * @todo test new isValid signature of array
  7. */
  8. class HashTest extends PHPUnit_Framework_TestCase
  9. {
  10. protected $_base_string = 'I am a test';
  11. protected $_sha1_of_string = 'c3207bbe306d89116d4058320b086296a43b8964';
  12. protected $_md5_of_string = '3be6306716ab9c505374d7cd72c63abd';
  13. protected $_too_short_md5 = 'be6306716ab9c505374d7cd72c63ab';
  14. protected $_chars_out_of_range_md5 = '3xe6306716ab9c505374d7cd72c63abz';
  15. protected $_sha256_of_string = '5f49192e05c579bb12c6afdd44819a572bf367336a32329c75b960e2ab1ce98e';
  16. protected $_chars_out_of_range_sha1 = 'c3207xbe306d89116d4058320b086296a43z8964';
  17. protected $_too_short_sha1 = '207bbe306d89116d4058320b086296a43b89';
  18. protected $_hasher;
  19. protected function setUp()
  20. {
  21. }
  22. /*
  23. * TEST OPTIONS
  24. */
  25. public function testOptionsAsInstanceOfZendConfig()
  26. {
  27. require_once 'Zend/Config/Ini.php';
  28. $options = new Zend_Config_Ini('.\\Fixtures\test.ini', 'production');
  29. $this->_hasher = new Zend_Validate_Hash($options);
  30. $gotten_options = $this->_hasher->getOptions();
  31. $this->assertArrayHasKey('format', $gotten_options);
  32. $this->assertArrayHasKey('algorithm', $gotten_options);
  33. $this->assertTrue($gotten_options['algorithm'] === $options->hash->algorithm);
  34. }
  35. public function testOptionsAsInstanceOfZendConfigWithoutProperFormat()
  36. {
  37. require_once 'Zend/Config/Ini.php';
  38. $options = new Zend_Config_Ini('.\Fixtures\bad_test.ini', 'production');
  39. try {
  40. $this->_hasher = new Zend_Validate_Hash($options);
  41. }
  42. catch (Zend_Validate_Exception $e) {
  43. $this->assertThat(
  44. $e->getMessage(),
  45. $this->stringContains(
  46. 'The ini options must be within a \'hash.\' grouping'
  47. )
  48. );
  49. return;
  50. }
  51. $this->fail('An expected exception was not raised');
  52. }
  53. public function testOptionsAsArrayWithFormatKey()
  54. {
  55. $options = array(
  56. 'format' => 'binary',
  57. 'algorithm' => 'md5'
  58. );
  59. $this->_hasher = new Zend_Validate_Hash($options);
  60. $gotten_options = $this->_hasher->getOptions();
  61. $this->assertArrayHasKey('format', $gotten_options);
  62. $this->assertTrue($gotten_options['format'] === $options['format']);
  63. }
  64. public function testOptionsAsArrayWithoutFormatKey()
  65. {
  66. $options = array(
  67. 'algorithm' => 'md5'
  68. );
  69. try {
  70. $this->_hasher = new Zend_Validate_Hash($options);
  71. }
  72. catch (Zend_Validate_Exception $e) {
  73. $this->assertThat(
  74. $e->getMessage(),
  75. $this->stringContains(
  76. 'The key \'format\' was not defined in the options array'
  77. )
  78. );
  79. return;
  80. }
  81. $this->fail('An expected exception was not raised');
  82. }
  83. public function testOptionsAsArrayWithABlankFormatKey()
  84. {
  85. $options = array(
  86. 'algorithm' => 'md5',
  87. 'format' => ''
  88. );
  89. try {
  90. $this->_hasher = new Zend_Validate_Hash($options);
  91. }
  92. catch (Zend_Validate_Exception $e) {
  93. $this->assertThat(
  94. $e->getMessage(),
  95. $this->stringContains(
  96. 'The format was not defined'
  97. )
  98. );
  99. return;
  100. }
  101. $this->fail('An expected exception was not raised');
  102. }
  103. public function testOptionsAsArrayWithANullFormatKey()
  104. {
  105. $options = array(
  106. 'algorithm' => 'md5',
  107. 'format' => null
  108. );
  109. try {
  110. $this->_hasher = new Zend_Validate_Hash($options);
  111. }
  112. catch (Zend_Validate_Exception $e) {
  113. $this->assertThat(
  114. $e->getMessage(),
  115. $this->stringContains(
  116. 'The format was not defined'
  117. )
  118. );
  119. return;
  120. }
  121. $this->fail('An expected exception was not raised');
  122. }
  123. public function testOptionsAsArrayWithAlgorithmKey()
  124. {
  125. $options = array(
  126. 'format' => 'binary',
  127. 'algorithm' => 'md5'
  128. );
  129. $this->_hasher = new Zend_Validate_Hash($options);
  130. $this->assertArrayHasKey('algorithm', $this->_hasher->getOptions());
  131. $an_option = $this->_hasher->getOptions();
  132. $this->assertTrue($an_option['algorithm'] == $options['algorithm']);
  133. }
  134. public function testOptionsAsArrayWithoutAlgorithmKey()
  135. {
  136. $options = array(
  137. 'format' => 'binary'
  138. );
  139. try {
  140. $this->_hasher = new Zend_Validate_Hash($options);
  141. }
  142. catch (Zend_Validate_Exception $e) {
  143. $this->assertThat(
  144. $e->getMessage(),
  145. $this->stringContains(
  146. 'The key \'algorithm\' was not defined in the options array'
  147. )
  148. );
  149. return;
  150. }
  151. $this->fail('An expected exception was not raised');
  152. }
  153. public function testOptionsAsArrayWithABlankAlgorithmKey()
  154. {
  155. $options = array(
  156. 'algorithm' => '',
  157. 'format' => 'hexadecimal'
  158. );
  159. try {
  160. $this->_hasher = new Zend_Validate_Hash($options);
  161. }
  162. catch (Zend_Validate_Exception $e) {
  163. $this->assertThat(
  164. $e->getMessage(),
  165. $this->stringContains(
  166. 'The algorithm was not defined'
  167. )
  168. );
  169. return;
  170. }
  171. $this->fail('An expected exception was not raised');
  172. }
  173. public function testOptionsAsArrayWithANullAlgorithmKey()
  174. {
  175. $options = array(
  176. 'algorithm' => null,
  177. 'format' => 'hexadecimal'
  178. );
  179. try {
  180. $this->_hasher = new Zend_Validate_Hash($options);
  181. }
  182. catch (Zend_Validate_Exception $e) {
  183. $this->assertThat(
  184. $e->getMessage(),
  185. $this->stringContains(
  186. 'The algorithm was not defined'
  187. )
  188. );
  189. return;
  190. }
  191. $this->fail('An expected exception was not raised');
  192. }
  193. public function testOptionsAsArray()
  194. {
  195. $options = array(
  196. 'format' => 'binary',
  197. 'algorithm' => 'md5'
  198. );
  199. $this->_hasher = new Zend_Validate_Hash($options);
  200. $this->assertTrue(is_array($this->_hasher->getOptions()));
  201. }
  202. public function testOptionsAsNull()
  203. {
  204. $options = null;
  205. try {
  206. $this->_hasher = new Zend_Validate_Hash($options);
  207. }
  208. catch (Zend_Validate_Exception $e) {
  209. $this->assertThat(
  210. $e->getMessage(),
  211. $this->stringContains(
  212. 'The options must be an array'
  213. )
  214. );
  215. return;
  216. }
  217. $this->fail('An expected exception was not raised');
  218. }
  219. /*
  220. * TEST BLANK ARGUMENTS
  221. */
  222. public function testConstructWithAnUnsupportedAlgorithm()
  223. {
  224. $options = array(
  225. 'algorithm' => 'sha256',
  226. 'format' => 'hexadecimal',
  227. );
  228. try {
  229. $this->_hasher = new Zend_Validate_Hash($options);
  230. }
  231. catch (Zend_Validate_Exception $e) {
  232. $this->assertThat(
  233. $e->getMessage(),
  234. $this->stringContains('This algorithm is unsupported')
  235. );
  236. return;
  237. }
  238. /* should fail if the message is not thus, or no exception */
  239. $this->fail('An expected exception was not raised');
  240. }
  241. public function testConstructWithAnUnsupportedFormat()
  242. {
  243. $options = array(
  244. 'algorithm' => 'md5',
  245. 'format' => 'base64',
  246. );
  247. try {
  248. $this->_hasher = new Zend_Validate_Hash($options);
  249. }
  250. catch (Zend_Validate_Exception $e) {
  251. $this->assertThat(
  252. $e->getMessage(),
  253. $this->stringContains(
  254. 'This format is unsupported'
  255. )
  256. );
  257. return;
  258. }
  259. /* should fail if the message is not thus, or no exception */
  260. $this->fail('An expected exception was not raised');
  261. }
  262. public function testIsValidWithANullHash()
  263. {
  264. $options = array(
  265. 'algorithm' => 'md5',
  266. 'format' => 'hexadecimal',
  267. );
  268. $this->_hasher = new Zend_Validate_Hash($options);
  269. try {
  270. $this->_hasher->isValid(null);
  271. }
  272. catch (Zend_Validate_Exception $e) {
  273. $this->assertThat(
  274. $e->getMessage(),
  275. $this->stringContains('The hash was not defined')
  276. );
  277. return;
  278. }
  279. $this->fail('An expected exception was not raised');
  280. }
  281. public function testIsValidWithABlankHash()
  282. {
  283. $options = array(
  284. 'algorithm' => 'md5',
  285. 'format' => 'hexadecimal',
  286. );
  287. $this->_hasher = new Zend_Validate_Hash($options);
  288. try {
  289. $this->_hasher->isValid('');
  290. }
  291. catch (Zend_Validate_Exception $e) {
  292. $this->assertThat(
  293. $e->getMessage(),
  294. $this->stringContains('The hash was not defined')
  295. );
  296. return;
  297. }
  298. $this->fail('An expected exception was not raised');
  299. }
  300. public function testIsValidWithANonStringValue()
  301. {
  302. $options = array(
  303. 'algorithm' => 'md5',
  304. 'format' => 'hexadecimal',
  305. );
  306. $this->_hasher = new Zend_Validate_Hash($options);
  307. try {
  308. $this->_hasher->isValid(array($this->_md5_of_string));
  309. }
  310. catch (Zend_Validate_Exception $e) {
  311. $this->assertThat(
  312. $e->getMessage(),
  313. $this->stringContains(
  314. 'The hash to validate must be a string'
  315. )
  316. );
  317. return;
  318. }
  319. $this->fail('An expected exception was not raised');
  320. }
  321. public function testSetAlgorithm()
  322. {
  323. $expected_algorithm = 'sha1';
  324. $options = array(
  325. 'algorithm' => $expected_algorithm,
  326. 'format' => 'hexadecimal',
  327. );
  328. $this->_hasher = new Zend_Validate_Hash($options);
  329. $this->assertEquals($expected_algorithm, $this->_hasher->getAlgorithm());
  330. }
  331. /*
  332. * TEST INVALID HASHES
  333. */
  334. public function testSha1HashInHexIsTooShort()
  335. {
  336. $options = array(
  337. 'algorithm' => 'sha1',
  338. 'format' => 'hexadecimal',
  339. );
  340. $this->_hasher = new Zend_Validate_Hash($options);
  341. $this->assertFalse($this->_hasher->isValid($this->_too_short_sha1));
  342. }
  343. public function testSha1HashInHexIsOutOfRange()
  344. {
  345. $options = array(
  346. 'format' => 'hexadecimal',
  347. 'algorithm' => 'sha1'
  348. );
  349. $this->_hasher = new Zend_Validate_Hash($options);
  350. $this->assertFalse(
  351. $this->_hasher->isValid($this->_chars_out_of_range_sha1)
  352. );
  353. }
  354. public function testIsNotAValidSha1HashInBinary()
  355. {
  356. $options = array(
  357. 'format' => 'binary',
  358. 'algorithm' => 'sha1'
  359. );
  360. $this->_hasher = new Zend_Validate_Hash($options);
  361. $binary_sha1 = sha1($this->_base_string, true);
  362. $modified_binary_sha1 = $binary_sha1 . 'a';
  363. $this->assertFalse(
  364. $this->_hasher->isValid($modified_binary_sha1)
  365. );
  366. }
  367. public function testMd5HashInHexIsOutOfRange()
  368. {
  369. $options = array(
  370. 'format' => 'hexadecimal',
  371. 'algorithm' => 'md5'
  372. );
  373. $this->_hasher = new Zend_Validate_Hash($options);
  374. $this->assertFalse(
  375. $this->_hasher->isValid($this->_chars_out_of_range_md5)
  376. );
  377. }
  378. public function testMd5HashInHexIsTooShort()
  379. {
  380. $options = array(
  381. 'format' => 'hexadecimal',
  382. 'algorithm' => 'md5'
  383. );
  384. $this->_hasher = new Zend_Validate_Hash($options);
  385. $this->assertFalse(
  386. $this->_hasher->isValid($this->_too_short_md5)
  387. );
  388. }
  389. public function testIsNotAValidMd5HashInBinary()
  390. {
  391. $options = array(
  392. 'format' => 'binary',
  393. 'algorithm' => 'md5'
  394. );
  395. $this->_hasher = new Zend_Validate_Hash($options);
  396. $binary_md5 = md5($this->_base_string, true);
  397. $modified_binary_md5 = $binary_md5 . 'a';
  398. $this->assertFalse(
  399. $this->_hasher->isValid($modified_binary_md5)
  400. );
  401. }
  402. /*
  403. * TEST VALID HASHES
  404. */
  405. /**
  406. *
  407. */
  408. public function testIsValidSha1HashInHex()
  409. {
  410. $options = array(
  411. 'format' => 'hexadecimal',
  412. 'algorithm' => 'sha1'
  413. );
  414. $this->_hasher = new Zend_Validate_Hash($options);
  415. $this->assertTrue(
  416. $this->_hasher->isValid($this->_sha1_of_string)
  417. );
  418. }
  419. /**
  420. *
  421. */
  422. public function testIsValidSha1HashInBinary()
  423. {
  424. $options = array(
  425. 'format' => 'binary',
  426. 'algorithm' => 'sha1'
  427. );
  428. $this->_hasher = new Zend_Validate_Hash($options);
  429. $this->assertTrue(
  430. $this->_hasher->isValid(sha1($this->_base_string, true))
  431. );
  432. }
  433. /**
  434. *
  435. */
  436. public function testIsValidMd5HashInHex()
  437. {
  438. $options = array(
  439. 'format' => 'hexadecimal',
  440. 'algorithm' => 'md5'
  441. );
  442. $this->_hasher = new Zend_Validate_Hash($options);
  443. $this->assertTrue(
  444. $this->_hasher->isValid($this->_md5_of_string)
  445. );
  446. }
  447. /**
  448. *
  449. */
  450. public function testIsValidMd5HashInBinary()
  451. {
  452. $options = array(
  453. 'format' => 'binary',
  454. 'algorithm' => 'md5'
  455. );
  456. $this->_hasher = new Zend_Validate_Hash($options);
  457. $this->assertTrue(
  458. $this->_hasher->isValid(md5($this->_base_string, true))
  459. );
  460. }
  461. /*
  462. * TESTING getHash()
  463. */
  464. /**
  465. * test to make sure the class has set the hash correctly.
  466. */
  467. public function testGetHashWhenHashIsHex()
  468. {
  469. $options = array(
  470. 'format' => 'hexadecimal',
  471. 'algorithm' => 'sha1'
  472. );
  473. $this->_hasher = new Zend_Validate_Hash($options);
  474. //must run this to get the hash set
  475. $this->_hasher->isValid($this->_sha1_of_string);
  476. /* the actual test */
  477. $this->assertEquals(
  478. $this->_sha1_of_string, $this->_hasher->getHash()
  479. );
  480. }
  481. /**
  482. *
  483. */
  484. public function testGetHashWhenHashIsBinary()
  485. {
  486. $options = array(
  487. 'format' => 'binary',
  488. 'algorithm' => 'sha1'
  489. );
  490. $this->_hasher = new Zend_Validate_Hash($options);
  491. //must run this to get the hash set
  492. $this->_hasher->isValid(sha1($this->_base_string, true));
  493. /* the actual test */
  494. $this->assertEquals(
  495. $this->_sha1_of_string, $this->_hasher->getHash()
  496. );
  497. }
  498. protected function tearDown()
  499. {
  500. }
  501. }