PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Utility/SecurityTest.php

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