RequestTokenTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\security\validation;
  9. use lithium\action\Request;
  10. use lithium\security\Password;
  11. use lithium\security\validation\RequestToken;
  12. class RequestTokenTest extends \lithium\test\Unit {
  13. protected static $_storage = array();
  14. public function setUp() {
  15. self::$_storage = array();
  16. RequestToken::config(array('classes' => array('session' => __CLASS__)));
  17. }
  18. public function tearDown() {
  19. RequestToken::config(array('classes' => array('session' => 'lithium\storage\Session')));
  20. }
  21. public static function read($key) {
  22. return isset(static::$_storage[$key]) ? static::$_storage[$key] : null;
  23. }
  24. public static function write($key, $val) {
  25. return static::$_storage[$key] = $val;
  26. }
  27. /**
  28. * Tests that class dependencies can be reconfigured.
  29. */
  30. public function testConfiguration() {
  31. $expected = array('classes' => array('session' => __CLASS__));
  32. $this->assertEqual($expected, RequestToken::config());
  33. $new = array('classes' => array('session' => 'lithium\storage\Session'));
  34. RequestToken::config($new);
  35. $this->assertEqual($new, RequestToken::config());
  36. }
  37. /**
  38. * Tests proper generation of secure tokens.
  39. */
  40. public function testTokenGeneration() {
  41. $token = RequestToken::get();
  42. $this->assertPattern('/^[a-f0-9]{128}$/', $token);
  43. $this->assertEqual(array('security.token' => $token), self::$_storage);
  44. $newToken = RequestToken::get();
  45. $this->assertEqual($token, $newToken);
  46. $reallyNewToken = RequestToken::get(array('regenerate' => true));
  47. $this->assertPattern('/^[a-f0-9]{128}$/', $reallyNewToken);
  48. $this->assertNotEqual($token, $reallyNewToken);
  49. $this->assertEqual(array('security.token' => $reallyNewToken), self::$_storage);
  50. }
  51. /**
  52. * Tests that a random sequence of keys and tokens properly match one another.
  53. */
  54. public function testKeyMatching() {
  55. for ($i = 0; $i < 4; $i++) {
  56. $token = RequestToken::get(array('regenerate' => true));
  57. for ($j = 0; $j < 4; $j++) {
  58. $key = Password::hash($token);
  59. $this->assertTrue(RequestToken::check($key));
  60. }
  61. }
  62. }
  63. /**
  64. * Tests extracting a key from a `Request` object and matching it against a token.
  65. */
  66. public function testTokenFromRequestObject() {
  67. $request = new Request(array('data' => array(
  68. 'security' => array('token' => RequestToken::key())
  69. )));
  70. $this->assertTrue(RequestToken::check($request));
  71. }
  72. }
  73. ?>