PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/hardsshah/bookmarks
PHP | 158 lines | 74 code | 11 blank | 73 comment | 4 complexity | 2efeec74cd6b2c879ab50587cf953ace 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. $key = 'someKey';
  92. $hash = 'someHash';
  93. $this->assertIdentical(strlen(Security::hash($key, null, false)), 40);
  94. $this->assertIdentical(strlen(Security::hash($key, 'sha1', false)), 40);
  95. $this->assertIdentical(strlen(Security::hash($key, null, true)), 40);
  96. $this->assertIdentical(strlen(Security::hash($key, 'sha1', true)), 40);
  97. $result = Security::hash($key, null, $hash);
  98. $this->assertIdentical($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
  99. $result = Security::hash($key, 'sha1', $hash);
  100. $this->assertIdentical($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
  101. $hashType = 'sha1';
  102. Security::setHash($hashType);
  103. $this->assertIdentical($this->sut->hashType, $hashType);
  104. $this->assertIdentical(strlen(Security::hash($key, null, true)), 40);
  105. $this->assertIdentical(strlen(Security::hash($key, null, false)), 40);
  106. $this->assertIdentical(strlen(Security::hash($key, 'md5', false)), 32);
  107. $this->assertIdentical(strlen(Security::hash($key, 'md5', true)), 32);
  108. $hashType = 'md5';
  109. Security::setHash($hashType);
  110. $this->assertIdentical($this->sut->hashType, $hashType);
  111. $this->assertIdentical(strlen(Security::hash($key, null, false)), 32);
  112. $this->assertIdentical(strlen(Security::hash($key, null, true)), 32);
  113. if (!function_exists('hash') && !function_exists('mhash')) {
  114. $this->assertIdentical(strlen(Security::hash($key, 'sha256', false)), 32);
  115. $this->assertIdentical(strlen(Security::hash($key, 'sha256', true)), 32);
  116. } else {
  117. $this->assertIdentical(strlen(Security::hash($key, 'sha256', false)), 64);
  118. $this->assertIdentical(strlen(Security::hash($key, 'sha256', true)), 64);
  119. }
  120. }
  121. /**
  122. * testCipher method
  123. *
  124. * @access public
  125. * @return void
  126. */
  127. function testCipher() {
  128. $length = 10;
  129. $txt = '';
  130. for ($i = 0; $i < $length; $i++) {
  131. $txt .= mt_rand(0, 255);
  132. }
  133. $key = 'my_key';
  134. $result = Security::cipher($txt, $key);
  135. $this->assertEqual(Security::cipher($result, $key), $txt);
  136. $txt = '';
  137. $key = 'my_key';
  138. $result = Security::cipher($txt, $key);
  139. $this->assertEqual(Security::cipher($result, $key), $txt);
  140. $txt = 'some_text';
  141. $key = '';
  142. $result = Security::cipher($txt, $key);
  143. $this->assertError();
  144. $this->assertIdentical($result, '');
  145. }
  146. }
  147. ?>