SecurityTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
  2. /**
  3. * Tests Kohana_Security
  4. *
  5. * @group kohana
  6. * @group kohana.core
  7. * @group kohana.core.security
  8. *
  9. * @package Kohana
  10. * @category Tests
  11. */
  12. class Kohana_SecurityTest extends Unittest_TestCase
  13. {
  14. /**
  15. * Provides test data for test_envode_php_tags()
  16. *
  17. * @return array Test data sets
  18. */
  19. public function provider_encode_php_tags()
  20. {
  21. return array(
  22. array("&lt;?php echo 'helloo'; ?&gt;", "<?php echo 'helloo'; ?>"),
  23. );
  24. }
  25. /**
  26. * Tests Security::encode_php_tags()
  27. *
  28. * @test
  29. * @dataProvider provider_encode_php_tags
  30. * @covers Security::encode_php_tags
  31. */
  32. public function test_encode_php_tags($expected, $input)
  33. {
  34. $this->assertSame($expected, Security::encode_php_tags($input));
  35. }
  36. /**
  37. * Provides test data for test_strip_image_tags()
  38. *
  39. * @return array Test data sets
  40. */
  41. public function provider_strip_image_tags()
  42. {
  43. return array(
  44. array('foo', '<img src="foo" />'),
  45. );
  46. }
  47. /**
  48. * Tests Security::strip_image_tags()
  49. *
  50. * @test
  51. * @dataProvider provider_strip_image_tags
  52. * @covers Security::strip_image_tags
  53. */
  54. public function test_strip_image_tags($expected, $input)
  55. {
  56. $this->assertSame($expected, Security::strip_image_tags($input));
  57. }
  58. /**
  59. * Provides test data for Security::token()
  60. *
  61. * @return array Test data sets
  62. */
  63. public function provider_csrf_token()
  64. {
  65. // Unfortunately this data provider has to use the session in order to
  66. // generate its data. If headers have already been sent then this method
  67. // throws an error, even if the test is does not run. If we return an
  68. // empty array then this also causes an error, so the only way to get
  69. // around it is to return an array of misc data and have the test skip
  70. // if headers have been sent. It's annoying this hack has to be
  71. // implemented, but the security code isn't exactly brilliantly
  72. // implemented. Ideally we'd be able to inject a session instance
  73. if (headers_sent())
  74. return array(array('', '', 0));
  75. $array = array();
  76. for ($i = 0; $i <= 4; $i++)
  77. {
  78. Security::$token_name = 'token_'.$i;
  79. $array[] = array(Security::token(TRUE), Security::check(Security::token(FALSE)), $i);
  80. }
  81. return $array;
  82. }
  83. /**
  84. * Tests Security::token()
  85. *
  86. * @test
  87. * @dataProvider provider_csrf_token
  88. * @covers Security::token
  89. */
  90. public function test_csrf_token($expected, $input, $iteration)
  91. {
  92. if (headers_sent()) {
  93. $this->markTestSkipped('Headers have already been sent, session not available');
  94. }
  95. Security::$token_name = 'token_'.$iteration;
  96. $this->assertSame(TRUE, $input);
  97. $this->assertSame($expected, Security::token(FALSE));
  98. Session::instance()->delete(Security::$token_name);
  99. }
  100. }