SecurityTest.php 9.4 KB

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