SecurityTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  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://cakephp.org CakePHP(tm) Project
  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\TestSuite\TestCase;
  17. use Cake\Utility\Crypto\Mcrypt;
  18. use Cake\Utility\Crypto\OpenSsl;
  19. use Cake\Utility\Security;
  20. /**
  21. * SecurityTest class
  22. *
  23. */
  24. class SecurityTest extends TestCase
  25. {
  26. /**
  27. * testHash method
  28. *
  29. * @return void
  30. */
  31. public function testHash()
  32. {
  33. $_hashType = Security::$hashType;
  34. $key = 'someKey';
  35. $hash = 'someHash';
  36. $this->assertSame(40, strlen(Security::hash($key, null, false)));
  37. $this->assertSame(40, strlen(Security::hash($key, 'sha1', false)));
  38. $this->assertSame(40, strlen(Security::hash($key, null, true)));
  39. $this->assertSame(40, strlen(Security::hash($key, 'sha1', true)));
  40. $result = Security::hash($key, null, $hash);
  41. $this->assertSame($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
  42. $result = Security::hash($key, 'sha1', $hash);
  43. $this->assertSame($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
  44. $hashType = 'sha1';
  45. Security::setHash($hashType);
  46. $this->assertSame($hashType, Security::$hashType);
  47. $this->assertSame(40, strlen(Security::hash($key, null, true)));
  48. $this->assertSame(40, strlen(Security::hash($key, null, false)));
  49. $this->assertSame(32, strlen(Security::hash($key, 'md5', false)));
  50. $this->assertSame(32, strlen(Security::hash($key, 'md5', true)));
  51. $hashType = 'md5';
  52. Security::setHash($hashType);
  53. $this->assertSame($hashType, Security::$hashType);
  54. $this->assertSame(32, strlen(Security::hash($key, null, false)));
  55. $this->assertSame(32, strlen(Security::hash($key, null, true)));
  56. $this->assertSame(64, strlen(Security::hash($key, 'sha256', false)));
  57. $this->assertSame(64, strlen(Security::hash($key, 'sha256', true)));
  58. Security::setHash($_hashType);
  59. }
  60. /**
  61. * testRijndael method
  62. *
  63. * @return void
  64. */
  65. public function testRijndael()
  66. {
  67. $this->skipIf(!function_exists('mcrypt_encrypt'));
  68. $engine = Security::engine();
  69. Security::engine(new Mcrypt());
  70. $txt = 'The quick brown fox jumped over the lazy dog.';
  71. $key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
  72. $result = Security::rijndael($txt, $key, 'encrypt');
  73. $this->assertEquals($txt, Security::rijndael($result, $key, 'decrypt'));
  74. $result = Security::rijndael('', $key, 'encrypt');
  75. $this->assertEquals('', Security::rijndael($result, $key, 'decrypt'));
  76. $key = 'this is my key of over 32 chars, yes it is';
  77. $result = Security::rijndael($txt, $key, 'encrypt');
  78. $this->assertEquals($txt, Security::rijndael($result, $key, 'decrypt'));
  79. Security::engine($engine);
  80. }
  81. /**
  82. * testRijndaelInvalidOperation method
  83. *
  84. * @expectedException \InvalidArgumentException
  85. * @return void
  86. */
  87. public function testRijndaelInvalidOperation()
  88. {
  89. $txt = 'The quick brown fox jumped over the lazy dog.';
  90. $key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
  91. Security::rijndael($txt, $key, 'foo');
  92. }
  93. /**
  94. * testRijndaelInvalidKey method
  95. *
  96. * @expectedException \InvalidArgumentException
  97. * @return void
  98. */
  99. public function testRijndaelInvalidKey()
  100. {
  101. $txt = 'The quick brown fox jumped over the lazy dog.';
  102. $key = 'too small';
  103. Security::rijndael($txt, $key, 'encrypt');
  104. }
  105. /**
  106. * Test encrypt/decrypt.
  107. *
  108. * @return void
  109. */
  110. public function testEncryptDecrypt()
  111. {
  112. $txt = 'The quick brown fox';
  113. $key = 'This key is longer than 32 bytes long.';
  114. $result = Security::encrypt($txt, $key);
  115. $this->assertNotEquals($txt, $result, 'Should be encrypted.');
  116. $this->assertNotEquals($result, Security::encrypt($txt, $key), 'Each result is unique.');
  117. $this->assertEquals($txt, Security::decrypt($result, $key));
  118. }
  119. /**
  120. * Test that changing the key causes decryption to fail.
  121. *
  122. * @return void
  123. */
  124. public function testDecryptKeyFailure()
  125. {
  126. $txt = 'The quick brown fox';
  127. $key = 'This key is longer than 32 bytes long.';
  128. $result = Security::encrypt($txt, $key);
  129. $key = 'Not the same key. This one will fail';
  130. $this->assertFalse(Security::decrypt($txt, $key), 'Modified key will fail.');
  131. }
  132. /**
  133. * Test that decrypt fails when there is an hmac error.
  134. *
  135. * @return void
  136. */
  137. public function testDecryptHmacFailure()
  138. {
  139. $txt = 'The quick brown fox';
  140. $key = 'This key is quite long and works well.';
  141. $salt = 'this is a delicious salt!';
  142. $result = Security::encrypt($txt, $key, $salt);
  143. // Change one of the bytes in the hmac.
  144. $result[10] = 'x';
  145. $this->assertFalse(Security::decrypt($result, $key, $salt), 'Modified hmac causes failure.');
  146. }
  147. /**
  148. * Test that changing the hmac salt will cause failures.
  149. *
  150. * @return void
  151. */
  152. public function testDecryptHmacSaltFailure()
  153. {
  154. $txt = 'The quick brown fox';
  155. $key = 'This key is quite long and works well.';
  156. $salt = 'this is a delicious salt!';
  157. $result = Security::encrypt($txt, $key, $salt);
  158. $salt = 'humpty dumpty had a great fall.';
  159. $this->assertFalse(Security::decrypt($result, $key, $salt), 'Modified salt causes failure.');
  160. }
  161. /**
  162. * Test that short keys cause errors
  163. *
  164. * @expectedException \InvalidArgumentException
  165. * @expectedExceptionMessage Invalid key for encrypt(), key must be at least 256 bits (32 bytes) long.
  166. * @return void
  167. */
  168. public function testEncryptInvalidKey()
  169. {
  170. $txt = 'The quick brown fox jumped over the lazy dog.';
  171. $key = 'this is too short';
  172. Security::encrypt($txt, $key);
  173. }
  174. /**
  175. * Test encrypting falsey data
  176. *
  177. * @return void
  178. */
  179. public function testEncryptDecryptFalseyData()
  180. {
  181. $key = 'This is a key that is long enough to be ok.';
  182. $result = Security::encrypt('', $key);
  183. $this->assertSame('', Security::decrypt($result, $key));
  184. $result = Security::encrypt(false, $key);
  185. $this->assertSame('', Security::decrypt($result, $key));
  186. $result = Security::encrypt(null, $key);
  187. $this->assertSame('', Security::decrypt($result, $key));
  188. $result = Security::encrypt(0, $key);
  189. $this->assertSame('0', Security::decrypt($result, $key));
  190. $result = Security::encrypt('0', $key);
  191. $this->assertSame('0', Security::decrypt($result, $key));
  192. }
  193. /**
  194. * Test that short keys cause errors
  195. *
  196. * @expectedException \InvalidArgumentException
  197. * @expectedExceptionMessage Invalid key for decrypt(), key must be at least 256 bits (32 bytes) long.
  198. * @return void
  199. */
  200. public function testDecryptInvalidKey()
  201. {
  202. $txt = 'The quick brown fox jumped over the lazy dog.';
  203. $key = 'this is too short';
  204. Security::decrypt($txt, $key);
  205. }
  206. /**
  207. * Test that empty data cause errors
  208. *
  209. * @expectedException \InvalidArgumentException
  210. * @expectedExceptionMessage The data to decrypt cannot be empty.
  211. * @return void
  212. */
  213. public function testDecryptInvalidData()
  214. {
  215. $txt = '';
  216. $key = 'This is a key that is long enough to be ok.';
  217. Security::decrypt($txt, $key);
  218. }
  219. /**
  220. * Test that values encrypted with open ssl can be decrypted with mcrypt and the reverse.
  221. *
  222. * @return void
  223. */
  224. public function testEngineEquivalence()
  225. {
  226. $restore = Security::engine();
  227. $txt = "Obi-wan you're our only hope";
  228. $key = 'This is my secret key phrase it is quite long.';
  229. $salt = 'A tasty salt that is delicious';
  230. Security::engine(new Mcrypt());
  231. $cipher = Security::encrypt($txt, $key, $salt);
  232. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  233. Security::engine(new OpenSsl());
  234. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  235. Security::engine(new OpenSsl());
  236. $cipher = Security::encrypt($txt, $key, $salt);
  237. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  238. Security::engine(new Mcrypt());
  239. $this->assertEquals($txt, Security::decrypt($cipher, $key, $salt));
  240. }
  241. /**
  242. * Tests that the salt can be set and retrieved
  243. *
  244. * @return void
  245. */
  246. public function testSalt()
  247. {
  248. Security::salt('foobarbaz');
  249. $this->assertEquals('foobarbaz', Security::salt());
  250. }
  251. }