/cake/tests/cases/libs/security.test.php

https://github.com/GunioRobot/findTwitter · PHP · 163 lines · 77 code · 13 blank · 73 comment · 4 complexity · 007a7ee3949845df4330a8568cf3a37a MD5 · raw file

  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * SecurityTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.tests.cases.libs
  21. * @since CakePHP(tm) v 1.2.0.5432
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. App::import('Core', 'Security');
  28. /**
  29. * SecurityTest class
  30. *
  31. * @package cake
  32. * @subpackage cake.tests.cases.libs
  33. */
  34. class SecurityTest extends CakeTestCase {
  35. /**
  36. * sut property
  37. *
  38. * @var mixed null
  39. * @access public
  40. */
  41. var $sut = null;
  42. /**
  43. * setUp method
  44. *
  45. * @access public
  46. * @return void
  47. */
  48. function setUp() {
  49. $this->sut =& Security::getInstance();
  50. }
  51. /**
  52. * testInactiveMins method
  53. *
  54. * @access public
  55. * @return void
  56. */
  57. function testInactiveMins() {
  58. Configure::write('Security.level', 'high');
  59. $this->assertEqual(10, Security::inactiveMins());
  60. Configure::write('Security.level', 'medium');
  61. $this->assertEqual(100, Security::inactiveMins());
  62. Configure::write('Security.level', 'low');
  63. $this->assertEqual(300, Security::inactiveMins());
  64. }
  65. /**
  66. * testGenerateAuthkey method
  67. *
  68. * @access public
  69. * @return void
  70. */
  71. function testGenerateAuthkey() {
  72. $this->assertEqual(strlen(Security::generateAuthKey()), 40);
  73. }
  74. /**
  75. * testValidateAuthKey method
  76. *
  77. * @access public
  78. * @return void
  79. */
  80. function testValidateAuthKey() {
  81. $authKey = Security::generateAuthKey();
  82. $this->assertTrue(Security::validateAuthKey($authKey));
  83. }
  84. /**
  85. * testHash method
  86. *
  87. * @access public
  88. * @return void
  89. */
  90. function testHash() {
  91. $Security = Security::getInstance();
  92. $_hashType = $Security->hashType;
  93. $key = 'someKey';
  94. $hash = 'someHash';
  95. $this->assertIdentical(strlen(Security::hash($key, null, false)), 40);
  96. $this->assertIdentical(strlen(Security::hash($key, 'sha1', false)), 40);
  97. $this->assertIdentical(strlen(Security::hash($key, null, true)), 40);
  98. $this->assertIdentical(strlen(Security::hash($key, 'sha1', true)), 40);
  99. $result = Security::hash($key, null, $hash);
  100. $this->assertIdentical($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
  101. $result = Security::hash($key, 'sha1', $hash);
  102. $this->assertIdentical($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
  103. $hashType = 'sha1';
  104. Security::setHash($hashType);
  105. $this->assertIdentical($this->sut->hashType, $hashType);
  106. $this->assertIdentical(strlen(Security::hash($key, null, true)), 40);
  107. $this->assertIdentical(strlen(Security::hash($key, null, false)), 40);
  108. $this->assertIdentical(strlen(Security::hash($key, 'md5', false)), 32);
  109. $this->assertIdentical(strlen(Security::hash($key, 'md5', true)), 32);
  110. $hashType = 'md5';
  111. Security::setHash($hashType);
  112. $this->assertIdentical($this->sut->hashType, $hashType);
  113. $this->assertIdentical(strlen(Security::hash($key, null, false)), 32);
  114. $this->assertIdentical(strlen(Security::hash($key, null, true)), 32);
  115. if (!function_exists('hash') && !function_exists('mhash')) {
  116. $this->assertIdentical(strlen(Security::hash($key, 'sha256', false)), 32);
  117. $this->assertIdentical(strlen(Security::hash($key, 'sha256', true)), 32);
  118. } else {
  119. $this->assertIdentical(strlen(Security::hash($key, 'sha256', false)), 64);
  120. $this->assertIdentical(strlen(Security::hash($key, 'sha256', true)), 64);
  121. }
  122. Security::setHash($_hashType);
  123. }
  124. /**
  125. * testCipher method
  126. *
  127. * @access public
  128. * @return void
  129. */
  130. function testCipher() {
  131. $length = 10;
  132. $txt = '';
  133. for ($i = 0; $i < $length; $i++) {
  134. $txt .= mt_rand(0, 255);
  135. }
  136. $key = 'my_key';
  137. $result = Security::cipher($txt, $key);
  138. $this->assertEqual(Security::cipher($result, $key), $txt);
  139. $txt = '';
  140. $key = 'my_key';
  141. $result = Security::cipher($txt, $key);
  142. $this->assertEqual(Security::cipher($result, $key), $txt);
  143. $txt = 'some_text';
  144. $key = '';
  145. $result = Security::cipher($txt, $key);
  146. $this->assertError();
  147. $this->assertIdentical($result, '');
  148. }
  149. }
  150. ?>