HazardLibTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. App::uses('HazardLib', 'Tools.Lib');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. class HazardLibTest extends MyCakeTestCase {
  5. public function setUp() {
  6. parent::setUp();
  7. if ($this->isDebug()) {
  8. Configure::write('Hazard.debug', true);
  9. }
  10. $this->HazardLib = new TestHazardLib();
  11. }
  12. /**
  13. * @return void
  14. */
  15. public function testParse() {
  16. $is = $this->HazardLib->parseXml(HazardLib::URL);
  17. $this->assertTrue(!empty($is));
  18. $this->assertTrue(count($is) >= 3);
  19. }
  20. /**
  21. * @return void
  22. */
  23. public function testXssStrings() {
  24. $is = $this->HazardLib->xssStrings(false);
  25. $this->assertTrue(!empty($is));
  26. // cached
  27. Cache::delete('security_lib_texts');
  28. $is = $this->HazardLib->xssStrings();
  29. $this->assertTrue(!empty($is));
  30. $is = $this->HazardLib->xssStrings();
  31. $this->assertTrue(!empty($is));
  32. }
  33. /**
  34. * @return void
  35. */
  36. public function testPhp() {
  37. $is = $this->HazardLib->phpStrings();
  38. $this->assertTrue(!empty($is));
  39. }
  40. /**
  41. * @return void
  42. */
  43. public function testSql() {
  44. $is = $this->HazardLib->sqlStrings();
  45. $this->assertTrue(!empty($is));
  46. }
  47. }
  48. class TestHazardLib extends HazardLib {
  49. /**
  50. * Return dummy data as long as no debug mode is given
  51. *
  52. * @return array
  53. */
  54. public function parseXml($file) {
  55. return $this->_parseXml($file);
  56. }
  57. protected static function _parseXml($file) {
  58. if (Configure::read('Hazard.debug')) {
  59. return parent::_parseXml($file);
  60. }
  61. // Simulate the most important ones from the xml file to avoid API requests in CI testing
  62. $array = [
  63. ['code' => '\'\';!--"<XSS>=&{()}'],
  64. ['code' => '<SCRIPT>alert(\'XSS\')</SCRIPT>'],
  65. ['code' => '<STYLE>.XSS{background-image:url("javascript:alert(\'XSS\')");}</STYLE><A CLASS=XSS></A>'],
  66. ];
  67. return $array;
  68. }
  69. }