Browse Source

Fix tests

Mark Scherer 10 years ago
parent
commit
6adbf26b73
2 changed files with 53 additions and 16 deletions
  1. 3 3
      Lib/HazardLib.php
  2. 50 13
      Test/Case/Lib/HazardLibTest.php

+ 3 - 3
Lib/HazardLib.php

@@ -15,7 +15,7 @@ class HazardLib {
 	const URL = 'http://ha.ckers.org/xssAttacks.xml';
 
 	/**
-	 * Get dangerous sql strings to test with
+	 * Get dangerous SQL strings to test with
 	 *
 	 * @return array
 	 */
@@ -42,7 +42,7 @@ class HazardLib {
 	}
 
 	/**
-	 * Get dangerous php strings to test with
+	 * Get dangerous PHP strings to test with
 	 *
 	 * @return array
 	 */
@@ -55,7 +55,7 @@ class HazardLib {
 	}
 
 	/**
-	 * Get dangerous html strings to test with
+	 * Get dangerous HTML strings to test with
 	 *
 	 * @return array
 	 */

+ 50 - 13
Test/Case/Lib/HazardLibTest.php

@@ -1,51 +1,88 @@
 <?php
 
 App::uses('HazardLib', 'Tools.Lib');
+App::uses('MyCakeTestCase', 'Tools.TestSuite');
 
-class HazardLibTest extends CakeTestCase {
+class HazardLibTest extends MyCakeTestCase {
 
 	public function setUp() {
 		parent::setUp();
 
-		$this->HazardLib = new HazardLib();
+		if ($this->isDebug()) {
+			Configure::write('Hazard.debug', true);
+		}
+
+		$this->HazardLib = new TestHazardLib();
 	}
 
 	/**
+	 * @return void
 	 */
-	public function _testParse() {
-		$is = $this->HazardLib->_parseXml(HazardLib::URL);
-		//pr(h($is));
+	public function testParse() {
+		$is = $this->HazardLib->parseXml(HazardLib::URL);
 		$this->assertTrue(!empty($is));
-		$this->assertEquals(count($is), 113);
+		$this->assertTrue(count($is) >= 3);
 	}
 
 	/**
+	 * @return void
 	 */
 	public function testXssStrings() {
 		$is = $this->HazardLib->xssStrings(false);
-		//pr(h($is));
 		$this->assertTrue(!empty($is));
 
 		// cached
 		Cache::delete('security_lib_texts');
 
 		$is = $this->HazardLib->xssStrings();
-		//pr(h($is));
-		$this->assertTrue(!empty($is) && count($is), 113);
+		$this->assertTrue(!empty($is));
 
 		$is = $this->HazardLib->xssStrings();
-		//pr(h($is));
-		$this->assertTrue(!empty($is) && count($is), 113);
+		$this->assertTrue(!empty($is));
 	}
 
+	/**
+	 * @return void
+	 */
 	public function testPhp() {
 		$is = $this->HazardLib->phpStrings();
-		//pr(h($is));
+		$this->assertTrue(!empty($is));
 	}
 
+	/**
+	 * @return void
+	 */
 	public function testSql() {
 		$is = $this->HazardLib->sqlStrings();
-		//pr(h($is));
+		$this->assertTrue(!empty($is));
+	}
+
+}
+
+class TestHazardLib extends HazardLib {
+
+	/**
+	 * Return dummy data as long as no debug mode is given
+	 *
+	 * @return array
+	 */
+	public function parseXml($file) {
+		return $this->_parseXml($file);
+	}
+
+	protected static function _parseXml($file) {
+		if (Configure::read('Hazard.debug')) {
+			return parent::_parseXml($file);
+		}
+
+		// Simulate the most important ones from the xml file to avoid API requests in CI testing
+		$array = [
+			['code' => '\'\';!--"<XSS>=&{()}'],
+			['code' => '<SCRIPT>alert(\'XSS\')</SCRIPT>'],
+			['code' => '<STYLE>.XSS{background-image:url("javascript:alert(\'XSS\')");}</STYLE><A CLASS=XSS></A>'],
+		];
+
+		return $array;
 	}
 
 }