SecurityTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Utility;
  16. use Cake\Core\Configure;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\Utility\Security;
  19. /**
  20. * SecurityTest class
  21. *
  22. */
  23. class SecurityTest extends TestCase {
  24. /**
  25. * testGenerateAuthkey method
  26. *
  27. * @return void
  28. */
  29. public function testGenerateAuthkey() {
  30. $this->assertEquals(strlen(Security::generateAuthKey()), 40);
  31. }
  32. /**
  33. * testHashInvalidSalt method
  34. *
  35. * @expectedException \Cake\Error\Exception
  36. * @return void
  37. */
  38. public function testHashInvalidSalt() {
  39. Security::hash('someKey', 'blowfish', true);
  40. }
  41. /**
  42. * testHashAnotherInvalidSalt
  43. *
  44. * @expectedException \Cake\Error\Exception
  45. * @return void
  46. */
  47. public function testHashAnotherInvalidSalt() {
  48. Security::hash('someKey', 'blowfish', '$1$lksdjoijfaoijs');
  49. }
  50. /**
  51. * testHashYetAnotherInvalidSalt
  52. *
  53. * @expectedException \Cake\Error\Exception
  54. * @return void
  55. */
  56. public function testHashYetAnotherInvalidSalt() {
  57. Security::hash('someKey', 'blowfish', '$2a$10$123');
  58. }
  59. /**
  60. * testHashInvalidCost method
  61. *
  62. * @expectedException \Cake\Error\Exception
  63. * @return void
  64. */
  65. public function testHashInvalidCost() {
  66. Security::setCost(1000);
  67. }
  68. /**
  69. * testHash method
  70. *
  71. * @return void
  72. */
  73. public function testHash() {
  74. $_hashType = Security::$hashType;
  75. $key = 'someKey';
  76. $hash = 'someHash';
  77. $this->assertSame(40, strlen(Security::hash($key, null, false)));
  78. $this->assertSame(40, strlen(Security::hash($key, 'sha1', false)));
  79. $this->assertSame(40, strlen(Security::hash($key, null, true)));
  80. $this->assertSame(40, strlen(Security::hash($key, 'sha1', true)));
  81. $result = Security::hash($key, null, $hash);
  82. $this->assertSame($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
  83. $result = Security::hash($key, 'sha1', $hash);
  84. $this->assertSame($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
  85. $hashType = 'sha1';
  86. Security::setHash($hashType);
  87. $this->assertSame($hashType, Security::$hashType);
  88. $this->assertSame(40, strlen(Security::hash($key, null, true)));
  89. $this->assertSame(40, strlen(Security::hash($key, null, false)));
  90. $this->assertSame(32, strlen(Security::hash($key, 'md5', false)));
  91. $this->assertSame(32, strlen(Security::hash($key, 'md5', true)));
  92. $hashType = 'md5';
  93. Security::setHash($hashType);
  94. $this->assertSame($hashType, Security::$hashType);
  95. $this->assertSame(32, strlen(Security::hash($key, null, false)));
  96. $this->assertSame(32, strlen(Security::hash($key, null, true)));
  97. if (!function_exists('hash') && !function_exists('mhash')) {
  98. $this->assertSame(32, strlen(Security::hash($key, 'sha256', false)));
  99. $this->assertSame(32, strlen(Security::hash($key, 'sha256', true)));
  100. } else {
  101. $this->assertSame(64, strlen(Security::hash($key, 'sha256', false)));
  102. $this->assertSame(64, strlen(Security::hash($key, 'sha256', true)));
  103. }
  104. Security::setHash($_hashType);
  105. }
  106. /**
  107. * Test that hash() works with blowfish.
  108. *
  109. * @return void
  110. */
  111. public function testHashBlowfish() {
  112. Security::setCost(10);
  113. $test = Security::hash('password', 'blowfish');
  114. $_hashType = Security::$hashType;
  115. $key = 'someKey';
  116. $hashType = 'blowfish';
  117. Security::setHash($hashType);
  118. $this->assertSame($hashType, Security::$hashType);
  119. $this->assertSame(60, strlen(Security::hash($key, null, false)));
  120. $password = $submittedPassword = $key;
  121. $storedPassword = Security::hash($password);
  122. $hashedPassword = Security::hash($submittedPassword, null, $storedPassword);
  123. $this->assertSame($storedPassword, $hashedPassword);
  124. $submittedPassword = 'someOtherKey';
  125. $hashedPassword = Security::hash($submittedPassword, null, $storedPassword);
  126. $this->assertNotSame($storedPassword, $hashedPassword);
  127. $expected = sha1('customsaltsomevalue');
  128. $result = Security::hash('somevalue', 'sha1', 'customsalt');
  129. $this->assertSame($expected, $result);
  130. $oldSalt = Configure::read('Security.salt');
  131. Configure::write('Security.salt', 'customsalt');
  132. $expected = sha1('customsaltsomevalue');
  133. $result = Security::hash('somevalue', 'sha1', true);
  134. $this->assertSame($expected, $result);
  135. Configure::write('Security.salt', $oldSalt);
  136. Security::setHash($_hashType);
  137. }
  138. /**
  139. * testRijndael method
  140. *
  141. * @return void
  142. */
  143. public function testRijndael() {
  144. $this->skipIf(!function_exists('mcrypt_encrypt'));
  145. $txt = 'The quick brown fox jumped over the lazy dog.';
  146. $key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
  147. $result = Security::rijndael($txt, $key, 'encrypt');
  148. $this->assertEquals($txt, Security::rijndael($result, $key, 'decrypt'));
  149. $result = Security::rijndael($key, $txt, 'encrypt');
  150. $this->assertEquals($key, Security::rijndael($result, $txt, 'decrypt'));
  151. $result = Security::rijndael('', $key, 'encrypt');
  152. $this->assertEquals('', Security::rijndael($result, $key, 'decrypt'));
  153. $key = 'this is my key of over 32 chars, yes it is';
  154. $result = Security::rijndael($txt, $key, 'encrypt');
  155. $this->assertEquals($txt, Security::rijndael($result, $key, 'decrypt'));
  156. }
  157. /**
  158. * testRijndaelInvalidOperation method
  159. *
  160. * @expectedException \Cake\Error\Exception
  161. * @return void
  162. */
  163. public function testRijndaelInvalidOperation() {
  164. $txt = 'The quick brown fox jumped over the lazy dog.';
  165. $key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
  166. Security::rijndael($txt, $key, 'foo');
  167. }
  168. /**
  169. * testRijndaelInvalidKey method
  170. *
  171. * @expectedException \Cake\Error\Exception
  172. * @return void
  173. */
  174. public function testRijndaelInvalidKey() {
  175. $txt = 'The quick brown fox jumped over the lazy dog.';
  176. $key = 'too small';
  177. Security::rijndael($txt, $key, 'encrypt');
  178. }
  179. /**
  180. * Test encrypt/decrypt.
  181. *
  182. * @return void
  183. */
  184. public function testEncryptDecrypt() {
  185. $txt = 'The quick brown fox';
  186. $key = 'This key is longer than 32 bytes long.';
  187. $result = Security::encrypt($txt, $key);
  188. $this->assertNotEquals($txt, $result, 'Should be encrypted.');
  189. $this->assertNotEquals($result, Security::encrypt($txt, $key), 'Each result is unique.');
  190. $this->assertEquals($txt, Security::decrypt($result, $key));
  191. }
  192. /**
  193. * Test that changing the key causes decryption to fail.
  194. *
  195. * @return void
  196. */
  197. public function testDecryptKeyFailure() {
  198. $txt = 'The quick brown fox';
  199. $key = 'This key is longer than 32 bytes long.';
  200. $result = Security::encrypt($txt, $key);
  201. $key = 'Not the same key. This one will fail';
  202. $this->assertFalse(Security::decrypt($txt, $key), 'Modified key will fail.');
  203. }
  204. /**
  205. * Test that decrypt fails when there is an hmac error.
  206. *
  207. * @return void
  208. */
  209. public function testDecryptHmacFailure() {
  210. $txt = 'The quick brown fox';
  211. $key = 'This key is quite long and works well.';
  212. $salt = 'this is a delicious salt!';
  213. $result = Security::encrypt($txt, $key, $salt);
  214. // Change one of the bytes in the hmac.
  215. $result[10] = 'x';
  216. $this->assertFalse(Security::decrypt($result, $key, $salt), 'Modified hmac causes failure.');
  217. }
  218. /**
  219. * Test that changing the hmac salt will cause failures.
  220. *
  221. * @return void
  222. */
  223. public function testDecryptHmacSaltFailure() {
  224. $txt = 'The quick brown fox';
  225. $key = 'This key is quite long and works well.';
  226. $salt = 'this is a delicious salt!';
  227. $result = Security::encrypt($txt, $key, $salt);
  228. $salt = 'humpty dumpty had a great fall.';
  229. $this->assertFalse(Security::decrypt($result, $key, $salt), 'Modified salt causes failure.');
  230. }
  231. /**
  232. * Test that short keys cause errors
  233. *
  234. * @expectedException \Cake\Error\Exception
  235. * @expectedExceptionMessage Invalid key for encrypt(), key must be at least 256 bits (32 bytes) long.
  236. * @return void
  237. */
  238. public function testEncryptInvalidKey() {
  239. $txt = 'The quick brown fox jumped over the lazy dog.';
  240. $key = 'this is too short';
  241. Security::encrypt($txt, $key);
  242. }
  243. /**
  244. * Test encrypting falsey data
  245. *
  246. * @return void
  247. */
  248. public function testEncryptDecryptFalseyData() {
  249. $key = 'This is a key that is long enough to be ok.';
  250. $result = Security::encrypt('', $key);
  251. $this->assertSame('', Security::decrypt($result, $key));
  252. $result = Security::encrypt(false, $key);
  253. $this->assertSame('', Security::decrypt($result, $key));
  254. $result = Security::encrypt(null, $key);
  255. $this->assertSame('', Security::decrypt($result, $key));
  256. $result = Security::encrypt(0, $key);
  257. $this->assertSame('0', Security::decrypt($result, $key));
  258. $result = Security::encrypt('0', $key);
  259. $this->assertSame('0', Security::decrypt($result, $key));
  260. }
  261. /**
  262. * Test that short keys cause errors
  263. *
  264. * @expectedException \Cake\Error\Exception
  265. * @expectedExceptionMessage Invalid key for decrypt(), key must be at least 256 bits (32 bytes) long.
  266. * @return void
  267. */
  268. public function testDecryptInvalidKey() {
  269. $txt = 'The quick brown fox jumped over the lazy dog.';
  270. $key = 'this is too short';
  271. Security::decrypt($txt, $key);
  272. }
  273. /**
  274. * Test that empty data cause errors
  275. *
  276. * @expectedException \Cake\Error\Exception
  277. * @expectedExceptionMessage The data to decrypt cannot be empty.
  278. * @return void
  279. */
  280. public function testDecryptInvalidData() {
  281. $txt = '';
  282. $key = 'This is a key that is long enough to be ok.';
  283. Security::decrypt($txt, $key);
  284. }
  285. }