included JavaScript escape test into PHPUnit test suite

This commit is contained in:
Sebastian Mendel
2007-03-16 11:59:40 +00:00
parent 5272b1be7d
commit ed52421872
3 changed files with 37 additions and 81 deletions

View File

@@ -24,6 +24,7 @@ require_once './test/PMA_get_real_size_test.php';
require_once './test/PMA_sanitize_test.php'; require_once './test/PMA_sanitize_test.php';
require_once './test/PMA_pow_test.php'; require_once './test/PMA_pow_test.php';
require_once './test/Environment_test.php'; require_once './test/Environment_test.php';
require_once './test/escape_js_string.php';
class AllTests class AllTests
{ {
@@ -45,6 +46,7 @@ class AllTests
$suite->addTestSuite('PMA_get_real_size_test'); $suite->addTestSuite('PMA_get_real_size_test');
$suite->addTestSuite('PMA_sanitize_test'); $suite->addTestSuite('PMA_sanitize_test');
$suite->addTestSuite('PMA_pow_test'); $suite->addTestSuite('PMA_pow_test');
$suite->addTestSuite('PMA_escapeJsString_test');
return $suite; return $suite;
} }
} }

View File

@@ -1,66 +0,0 @@
<?php
/* vim: expandtab sw=4 ts=4 sts=4: */
/**
* Core testing library to wrap phpMyAdmin and add some useful functions.
*
* @author Michal Čihař <michal@cihar.com>
* @package phpMyAdmin-test
* @version $Id: common.lib.php 9832 2007-01-09 09:50:49Z nijel $
*/
/**
* Go to root directory.
*/
chdir('..');
/**
* Report failed test.
*
* @param string function to test
* @param string test description
* @param string failure description
*/
function PMA_test_fail($function, $test, $message) {
$function = htmlspecialchars($function);
$test = htmlspecialchars($test);
$message = htmlspecialchars($message);
echo <<<EOT
<dt>$function ($test)</dt>
<dd><strong>Failed:</strong> $message</dd>
EOT;
}
/**
* Report ok test.
*
* @param string function to test
* @param string test description
*/
function PMA_test_okay($function, $test) {
$function = htmlspecialchars($function);
$test = htmlspecialchars($test);
echo <<<EOT
<dt>$function ($test)</dt>
<dd><strong>OK</strong></dd>
EOT;
}
/**
* Function for testing strings.
*
* @uses PMA_test_okay()
* @uses PMA_test_fail()
* @param string function to test
* @param string test description
* @param string actual result
* @param string expected result
*/
function PMA_test_string($function, $test, $received, $expected) {
if ($received != $expected) {
PMA_test_fail($function, $test, "Strings >$received< and >$expected< do not match");
} else {
PMA_test_okay($function, $test);
}
}
?>

View File

@@ -11,28 +11,48 @@
/** /**
* Tests core. * Tests core.
*/ */
include('./core.lib.php'); require_once 'PHPUnit/Framework.php';
/** /**
* Include to test. * Include to test.
*/ */
include('./libraries/js_escape.lib.php'); require_once './libraries/js_escape.lib.php';
/** /**
* Test java script escaping. * Test java script escaping.
* *
* @uses PMA_escapeJsString()
* @uses PMA_test_string()
* @param string string to escape
* @param string expected result
*/ */
function PMA_test_escape($test, $expected) { class PMA_escapeJsString_test extends PHPUnit_Framework_TestCase
PMA_test_string('PMA_escapeJsString', $test, PMA_escapeJsString($test), $expected); {
public function testEscape_1()
{
$this->assertEquals('\\\';', PMA_escapeJsString('\';'));
} }
PMA_test_escape('\';', '\\\';'); public function testEscape_2()
PMA_test_escape("\r\n'<scrIpt></sCRIPT>", '\r\n\\\'<scrIpt></\' + \'script>'); {
PMA_test_escape('\';[XSS]', '\\\';[XSS]'); $this->assertEquals('\r\n\\\'<scrIpt></\' + \'script>', PMA_escapeJsString("\r\n'<scrIpt></sCRIPT>"));
PMA_test_escape('</SCRIPT></head><body>[HTML]', '</\' + \'script></head><body>[HTML]'); }
PMA_test_escape('"\'\\\'"', '"\\\'\\\\\\\'"');
PMA_test_escape("\\''''''''''''\\", "\\\\\'\'\'\'\'\'\'\'\'\'\'\'\\\\"); public function testEscape_3()
{
$this->assertEquals('\\\';[XSS]', PMA_escapeJsString('\';[XSS]'));
}
public function testEscape_4()
{
$this->assertEquals('</\' + \'script></head><body>[HTML]', PMA_escapeJsString('</SCRIPT></head><body>[HTML]'));
}
public function testEscape_5()
{
$this->assertEquals('"\\\'\\\\\\\'"', PMA_escapeJsString('"\'\\\'"'));
}
public function testEscape_6()
{
$this->assertEquals("\\\\\'\'\'\'\'\'\'\'\'\'\'\'\\\\", PMA_escapeJsString("\\''''''''''''\\"));
}
}
?> ?>