SecurityTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  11. * @since CakePHP(tm) v 1.2.0.5432
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. App::uses('Security', 'Utility');
  15. /**
  16. * SecurityTest class
  17. *
  18. * @package Cake.Test.Case.Utility
  19. */
  20. class SecurityTest extends CakeTestCase {
  21. /**
  22. * sut property
  23. *
  24. * @var mixed null
  25. */
  26. public $sut = null;
  27. /**
  28. * testInactiveMins method
  29. *
  30. * @return void
  31. */
  32. public function testInactiveMins() {
  33. Configure::write('Security.level', 'high');
  34. $this->assertEquals(10, Security::inactiveMins());
  35. Configure::write('Security.level', 'medium');
  36. $this->assertEquals(100, Security::inactiveMins());
  37. Configure::write('Security.level', 'low');
  38. $this->assertEquals(300, Security::inactiveMins());
  39. }
  40. /**
  41. * testGenerateAuthkey method
  42. *
  43. * @return void
  44. */
  45. public function testGenerateAuthkey() {
  46. $this->assertEquals(strlen(Security::generateAuthKey()), 40);
  47. }
  48. /**
  49. * testValidateAuthKey method
  50. *
  51. * @return void
  52. */
  53. public function testValidateAuthKey() {
  54. $authKey = Security::generateAuthKey();
  55. $this->assertTrue(Security::validateAuthKey($authKey));
  56. }
  57. /**
  58. * testHashInvalidSalt method
  59. *
  60. * @expectedException PHPUnit_Framework_Error
  61. * @return void
  62. */
  63. public function testHashInvalidSalt() {
  64. Security::hash('someKey', 'blowfish', true);
  65. }
  66. /**
  67. * testHashAnotherInvalidSalt
  68. *
  69. * @expectedException PHPUnit_Framework_Error
  70. * @return void
  71. */
  72. public function testHashAnotherInvalidSalt() {
  73. Security::hash('someKey', 'blowfish', '$1$lksdjoijfaoijs');
  74. }
  75. /**
  76. * testHashYetAnotherInvalidSalt
  77. *
  78. * @expectedException PHPUnit_Framework_Error
  79. * @return void
  80. */
  81. public function testHashYetAnotherInvalidSalt() {
  82. Security::hash('someKey', 'blowfish', '$2a$10$123');
  83. }
  84. /**
  85. * testHashInvalidCost method
  86. *
  87. * @expectedException PHPUnit_Framework_Error
  88. * @return void
  89. */
  90. public function testHashInvalidCost() {
  91. Security::setCost(1000);
  92. }
  93. /**
  94. * testHash method
  95. *
  96. * @return void
  97. */
  98. public function testHash() {
  99. $_hashType = Security::$hashType;
  100. $key = 'someKey';
  101. $hash = 'someHash';
  102. $this->assertSame(strlen(Security::hash($key, null, false)), 40);
  103. $this->assertSame(strlen(Security::hash($key, 'sha1', false)), 40);
  104. $this->assertSame(strlen(Security::hash($key, null, true)), 40);
  105. $this->assertSame(strlen(Security::hash($key, 'sha1', true)), 40);
  106. $result = Security::hash($key, null, $hash);
  107. $this->assertSame($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
  108. $result = Security::hash($key, 'sha1', $hash);
  109. $this->assertSame($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
  110. $hashType = 'sha1';
  111. Security::setHash($hashType);
  112. $this->assertSame(Security::$hashType, $hashType);
  113. $this->assertSame(strlen(Security::hash($key, null, true)), 40);
  114. $this->assertSame(strlen(Security::hash($key, null, false)), 40);
  115. $this->assertSame(strlen(Security::hash($key, 'md5', false)), 32);
  116. $this->assertSame(strlen(Security::hash($key, 'md5', true)), 32);
  117. $hashType = 'md5';
  118. Security::setHash($hashType);
  119. $this->assertSame(Security::$hashType, $hashType);
  120. $this->assertSame(strlen(Security::hash($key, null, false)), 32);
  121. $this->assertSame(strlen(Security::hash($key, null, true)), 32);
  122. if (!function_exists('hash') && !function_exists('mhash')) {
  123. $this->assertSame(strlen(Security::hash($key, 'sha256', false)), 32);
  124. $this->assertSame(strlen(Security::hash($key, 'sha256', true)), 32);
  125. } else {
  126. $this->assertSame(strlen(Security::hash($key, 'sha256', false)), 64);
  127. $this->assertSame(strlen(Security::hash($key, 'sha256', true)), 64);
  128. }
  129. Security::setHash($_hashType);
  130. }
  131. /**
  132. * Test that hash() works with blowfish.
  133. *
  134. * @return void
  135. */
  136. public function testHashBlowfish() {
  137. Security::setCost(10);
  138. $test = Security::hash('password', 'blowfish');
  139. $this->skipIf(strpos($test, '$2a$') === false, 'Blowfish hashes are incorrect.');
  140. $_hashType = Security::$hashType;
  141. $key = 'someKey';
  142. $hashType = 'blowfish';
  143. Security::setHash($hashType);
  144. $this->assertSame(Security::$hashType, $hashType);
  145. $this->assertSame(strlen(Security::hash($key, null, false)), 60);
  146. $password = $submittedPassword = $key;
  147. $storedPassword = Security::hash($password);
  148. $hashedPassword = Security::hash($submittedPassword, null, $storedPassword);
  149. $this->assertSame($storedPassword, $hashedPassword);
  150. $submittedPassword = 'someOtherKey';
  151. $hashedPassword = Security::hash($submittedPassword, null, $storedPassword);
  152. $this->assertNotSame($storedPassword, $hashedPassword);
  153. $expected = sha1('customsaltsomevalue');
  154. $result = Security::hash('somevalue', 'sha1', 'customsalt');
  155. $this->assertSame($expected, $result);
  156. $oldSalt = Configure::read('Security.salt');
  157. Configure::write('Security.salt', 'customsalt');
  158. $expected = sha1('customsaltsomevalue');
  159. $result = Security::hash('somevalue', 'sha1', true);
  160. $this->assertSame($expected, $result);
  161. Configure::write('Security.salt', $oldSalt);
  162. Security::setHash($_hashType);
  163. }
  164. /**
  165. * testCipher method
  166. *
  167. * @return void
  168. */
  169. public function testCipher() {
  170. $length = 10;
  171. $txt = '';
  172. for ($i = 0; $i < $length; $i++) {
  173. $txt .= mt_rand(0, 255);
  174. }
  175. $key = 'my_key';
  176. $result = Security::cipher($txt, $key);
  177. $this->assertEquals($txt, Security::cipher($result, $key));
  178. $txt = '';
  179. $key = 'my_key';
  180. $result = Security::cipher($txt, $key);
  181. $this->assertEquals($txt, Security::cipher($result, $key));
  182. $txt = 123456;
  183. $key = 'my_key';
  184. $result = Security::cipher($txt, $key);
  185. $this->assertEquals($txt, Security::cipher($result, $key));
  186. $txt = '123456';
  187. $key = 'my_key';
  188. $result = Security::cipher($txt, $key);
  189. $this->assertEquals($txt, Security::cipher($result, $key));
  190. }
  191. /**
  192. * testCipherEmptyKey method
  193. *
  194. * @expectedException PHPUnit_Framework_Error
  195. * @return void
  196. */
  197. public function testCipherEmptyKey() {
  198. $txt = 'some_text';
  199. $key = '';
  200. Security::cipher($txt, $key);
  201. }
  202. /**
  203. * testRijndael method
  204. *
  205. * @return void
  206. */
  207. public function testRijndael() {
  208. $this->skipIf(!function_exists('mcrypt_encrypt'));
  209. $txt = 'The quick brown fox jumped over the lazy dog.';
  210. $key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
  211. $result = Security::rijndael($txt, $key, 'encrypt');
  212. $this->assertEquals($txt, Security::rijndael($result, $key, 'decrypt'));
  213. $result = Security::rijndael($key, $txt, 'encrypt');
  214. $this->assertEquals($key, Security::rijndael($result, $txt, 'decrypt'));
  215. $result = Security::rijndael('', $key, 'encrypt');
  216. $this->assertEquals('', Security::rijndael($result, $key, 'decrypt'));
  217. $result = Security::rijndael($txt, $key = 'this is my key of over 32 chars, yes it is', 'encrypt');
  218. $this->assertEquals($txt, Security::rijndael($result, $key, 'decrypt'));
  219. }
  220. /**
  221. * testRijndaelInvalidOperation method
  222. *
  223. * @expectedException PHPUnit_Framework_Error
  224. * @return void
  225. */
  226. public function testRijndaelInvalidOperation() {
  227. $txt = 'The quick brown fox jumped over the lazy dog.';
  228. $key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
  229. Security::rijndael($txt, $key, 'foo');
  230. }
  231. /**
  232. * testRijndaelInvalidKey method
  233. *
  234. * @expectedException PHPUnit_Framework_Error
  235. * @return void
  236. */
  237. public function testRijndaelInvalidKey() {
  238. $txt = 'The quick brown fox jumped over the lazy dog.';
  239. $key = 'too small';
  240. Security::rijndael($txt, $key, 'encrypt');
  241. }
  242. }