SecurityTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. * testHash method
  34. *
  35. * @return void
  36. */
  37. public function testHash() {
  38. $_hashType = Security::$hashType;
  39. $key = 'someKey';
  40. $hash = 'someHash';
  41. $this->assertSame(40, strlen(Security::hash($key, null, false)));
  42. $this->assertSame(40, strlen(Security::hash($key, 'sha1', false)));
  43. $this->assertSame(40, strlen(Security::hash($key, null, true)));
  44. $this->assertSame(40, strlen(Security::hash($key, 'sha1', true)));
  45. $result = Security::hash($key, null, $hash);
  46. $this->assertSame($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
  47. $result = Security::hash($key, 'sha1', $hash);
  48. $this->assertSame($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
  49. $hashType = 'sha1';
  50. Security::setHash($hashType);
  51. $this->assertSame($hashType, Security::$hashType);
  52. $this->assertSame(40, strlen(Security::hash($key, null, true)));
  53. $this->assertSame(40, strlen(Security::hash($key, null, false)));
  54. $this->assertSame(32, strlen(Security::hash($key, 'md5', false)));
  55. $this->assertSame(32, strlen(Security::hash($key, 'md5', true)));
  56. $hashType = 'md5';
  57. Security::setHash($hashType);
  58. $this->assertSame($hashType, Security::$hashType);
  59. $this->assertSame(32, strlen(Security::hash($key, null, false)));
  60. $this->assertSame(32, strlen(Security::hash($key, null, true)));
  61. $this->assertSame(64, strlen(Security::hash($key, 'sha256', false)));
  62. $this->assertSame(64, strlen(Security::hash($key, 'sha256', true)));
  63. Security::setHash($_hashType);
  64. }
  65. /**
  66. * testRijndael method
  67. *
  68. * @return void
  69. */
  70. public function testRijndael() {
  71. $this->skipIf(!function_exists('mcrypt_encrypt'));
  72. $txt = 'The quick brown fox jumped over the lazy dog.';
  73. $key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
  74. $result = Security::rijndael($txt, $key, 'encrypt');
  75. $this->assertEquals($txt, Security::rijndael($result, $key, 'decrypt'));
  76. $result = Security::rijndael($key, $txt, 'encrypt');
  77. $this->assertEquals($key, Security::rijndael($result, $txt, 'decrypt'));
  78. $result = Security::rijndael('', $key, 'encrypt');
  79. $this->assertEquals('', Security::rijndael($result, $key, 'decrypt'));
  80. $key = 'this is my key of over 32 chars, yes it is';
  81. $result = Security::rijndael($txt, $key, 'encrypt');
  82. $this->assertEquals($txt, Security::rijndael($result, $key, 'decrypt'));
  83. }
  84. /**
  85. * testRijndaelInvalidOperation method
  86. *
  87. * @expectedException \Cake\Error\Exception
  88. * @return void
  89. */
  90. public function testRijndaelInvalidOperation() {
  91. $txt = 'The quick brown fox jumped over the lazy dog.';
  92. $key = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
  93. Security::rijndael($txt, $key, 'foo');
  94. }
  95. /**
  96. * testRijndaelInvalidKey method
  97. *
  98. * @expectedException \Cake\Error\Exception
  99. * @return void
  100. */
  101. public function testRijndaelInvalidKey() {
  102. $txt = 'The quick brown fox jumped over the lazy dog.';
  103. $key = 'too small';
  104. Security::rijndael($txt, $key, 'encrypt');
  105. }
  106. /**
  107. * Test encrypt/decrypt.
  108. *
  109. * @return void
  110. */
  111. public function testEncryptDecrypt() {
  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. $txt = 'The quick brown fox';
  126. $key = 'This key is longer than 32 bytes long.';
  127. $result = Security::encrypt($txt, $key);
  128. $key = 'Not the same key. This one will fail';
  129. $this->assertFalse(Security::decrypt($txt, $key), 'Modified key will fail.');
  130. }
  131. /**
  132. * Test that decrypt fails when there is an hmac error.
  133. *
  134. * @return void
  135. */
  136. public function testDecryptHmacFailure() {
  137. $txt = 'The quick brown fox';
  138. $key = 'This key is quite long and works well.';
  139. $salt = 'this is a delicious salt!';
  140. $result = Security::encrypt($txt, $key, $salt);
  141. // Change one of the bytes in the hmac.
  142. $result[10] = 'x';
  143. $this->assertFalse(Security::decrypt($result, $key, $salt), 'Modified hmac causes failure.');
  144. }
  145. /**
  146. * Test that changing the hmac salt will cause failures.
  147. *
  148. * @return void
  149. */
  150. public function testDecryptHmacSaltFailure() {
  151. $txt = 'The quick brown fox';
  152. $key = 'This key is quite long and works well.';
  153. $salt = 'this is a delicious salt!';
  154. $result = Security::encrypt($txt, $key, $salt);
  155. $salt = 'humpty dumpty had a great fall.';
  156. $this->assertFalse(Security::decrypt($result, $key, $salt), 'Modified salt causes failure.');
  157. }
  158. /**
  159. * Test that short keys cause errors
  160. *
  161. * @expectedException \Cake\Error\Exception
  162. * @expectedExceptionMessage Invalid key for encrypt(), key must be at least 256 bits (32 bytes) long.
  163. * @return void
  164. */
  165. public function testEncryptInvalidKey() {
  166. $txt = 'The quick brown fox jumped over the lazy dog.';
  167. $key = 'this is too short';
  168. Security::encrypt($txt, $key);
  169. }
  170. /**
  171. * Test encrypting falsey data
  172. *
  173. * @return void
  174. */
  175. public function testEncryptDecryptFalseyData() {
  176. $key = 'This is a key that is long enough to be ok.';
  177. $result = Security::encrypt('', $key);
  178. $this->assertSame('', Security::decrypt($result, $key));
  179. $result = Security::encrypt(false, $key);
  180. $this->assertSame('', Security::decrypt($result, $key));
  181. $result = Security::encrypt(null, $key);
  182. $this->assertSame('', Security::decrypt($result, $key));
  183. $result = Security::encrypt(0, $key);
  184. $this->assertSame('0', Security::decrypt($result, $key));
  185. $result = Security::encrypt('0', $key);
  186. $this->assertSame('0', Security::decrypt($result, $key));
  187. }
  188. /**
  189. * Test that short keys cause errors
  190. *
  191. * @expectedException \Cake\Error\Exception
  192. * @expectedExceptionMessage Invalid key for decrypt(), key must be at least 256 bits (32 bytes) long.
  193. * @return void
  194. */
  195. public function testDecryptInvalidKey() {
  196. $txt = 'The quick brown fox jumped over the lazy dog.';
  197. $key = 'this is too short';
  198. Security::decrypt($txt, $key);
  199. }
  200. /**
  201. * Test that empty data cause errors
  202. *
  203. * @expectedException \Cake\Error\Exception
  204. * @expectedExceptionMessage The data to decrypt cannot be empty.
  205. * @return void
  206. */
  207. public function testDecryptInvalidData() {
  208. $txt = '';
  209. $key = 'This is a key that is long enough to be ok.';
  210. Security::decrypt($txt, $key);
  211. }
  212. }