From 502a23ea8966b4e02c1b5a1f63c06d2977debcf8 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 11 Dec 2006 02:19:04 +0000 Subject: [PATCH] Lots of testing going on... almost completed the input test. Working towards full code coverage. --- UnitTest/AllTests.php | 1 + UnitTest/DateTest.php | 11 +++ UnitTest/InputTest.php | 157 ++++++++++++++++++++++++++++++++++++++ UnitTest/RegisterTest.php | 21 +++++ 4 files changed, 190 insertions(+) create mode 100644 UnitTest/InputTest.php diff --git a/UnitTest/AllTests.php b/UnitTest/AllTests.php index 9ee24ec..bda69d5 100644 --- a/UnitTest/AllTests.php +++ b/UnitTest/AllTests.php @@ -11,6 +11,7 @@ $test = new GroupTest('All Tests'); $test->addTestFile('RegisterTest.php'); $test->addTestFile('FunctionsTest.php'); $test->addTestFile('DateTest.php'); +$test->addTestFile('InputTest.php'); // run $test->run(new CustomHtmlReporter); diff --git a/UnitTest/DateTest.php b/UnitTest/DateTest.php index 04d1997..c33440e 100644 --- a/UnitTest/DateTest.php +++ b/UnitTest/DateTest.php @@ -32,6 +32,17 @@ class DateTest extends UnitTestCase $this->fixture->setUserTimeZone(-8); $this->assertEqual(date(self::DATE_FORMAT, TIMENOW), $this->fixture->format(self::DATE_FORMAT, TIMENOW)); } + + public function testGmtNoAdjust() + { + $this->fixture->setUserTimeZone(4); + $this->assertEqual(gmdate(self::DATE_FORMAT, TIMENOW), $this->fixture->format(self::DATE_FORMAT, TIMENOW, false)); + } + + public function testTimezoneList() + { + $this->assertEqual(sizeof(BSDate::FetchTimeZoneList()), 30); + } } ?> \ No newline at end of file diff --git a/UnitTest/InputTest.php b/UnitTest/InputTest.php new file mode 100644 index 0000000..1951171 --- /dev/null +++ b/UnitTest/InputTest.php @@ -0,0 +1,157 @@ + 'moo', + '0' => 'foobar', + 'abc' => '-1', + 'ab"c"' => '2.0', + 'ab"c"2' => 'k"lm"', + 'ab\'c\'' => 'nop', + "ab\'c\'2" => "qr\'s\'" + ); + $_COOKIE = array( + 'somecookie' => '"a var"', + 'another"value"' => "isn't it cool" + ); + $_POST = array( + 'nest' => array( + 'foobar' => '"test"', + '"hi"' => 'test\'ing' + ) + ); + + // simulate magic quotes GPC + /*foreach (array($_GET, $_COOKIE) AS $array) + { + foreach ($array AS $var => $value) + { + $array["$var"] = addslashes($value); + } + }*/ + + $this->fixture = BSRegister::LoadModule('Input'); + } + + public function testSanitizeInputData() + { + $this->assertEqual(10, sizeof($this->fixture->in)); + $this->assertEqual(2, sizeof($this->fixture->in['nest'])); + $this->assertEqual('"a var"', $this->fixture->in['somecookie']); + $this->assertEqual('test\'ing', $this->fixture->in['nest']['"hi"']); + } + + public function testEntityEncode() + { + $this->assertEqual('<a href="http://www.something.com/test.php?do=run&moo=foo">', $this->fixture->entityEncode('')); + } + + public function testUnsanitize() + { + $this->assertEqual('', $this->fixture->unsanitize('')); + $this->assertEqual('', $this->fixture->unsanitize('<script type="text/javascript"> alert("XSS is fun!"); </script>')); + $this->assertEqual('', $this->fixture->unsanitize($this->fixture->sanitize(''))); + } + + public function testClean() + { + $this->assertEqual(0, $this->fixture->clean('abc', TYPE_INT)); + $this->assertEqual(-1, $this->fixture->clean('-1', TYPE_INT)); + $this->assertEqual(4, $this->fixture->clean('4def', TYPE_INT)); + + $this->assertEqual(0, $this->fixture->clean('abc', TYPE_UINT)); + $this->assertEqual(0, $this->fixture->clean(-100, TYPE_UINT)); + $this->assertEqual(40, $this->fixture->clean('40.965', TYPE_UINT)); + + $this->assertEqual(0, $this->fixture->clean('0.0', TYPE_FLOAT)); + $this->assertNotEqual(0, $this->fixture->clean('0.032', TYPE_FLOAT)); + + $this->assertEqual(true, $this->fixture->clean('aafsdfa', TYPE_BOOL)); + $this->assertEqual(false, $this->fixture->clean('', TYPE_BOOL)); + $this->assertEqual(false, $this->fixture->clean('0', TYPE_BOOL)); + $this->assertEqual(true, $this->fixture->clean('0.0', TYPE_BOOL)); + + $this->assertEqual('', $this->fixture->clean('', TYPE_STR)); + + $this->assertEqual('', $this->fixture->clean('', TYPE_STRUN)); + $this->assertEqual('', $this->fixture->clean($this->fixture->sanitize(''), TYPE_STRUN)); + + $this->assertEqual('', $this->fixture->clean('', TYPE_NONE)); + + $this->assertEqual('åß∂ƒ©˙∆˚¬…æΩ≈ç√∫≤≥÷œ∑®†¥øπ“‘’”', $this->fixture->clean('åß∂ƒ©˙∆˚¬…æΩ≈ç√∫≤≥÷œ∑®†¥øπ“‘’”', TYPE_BIN)); + + $this->fixture->clean('asdfa', TYPE_THIS_DOES_NOT_EXIST); + $this->assertError(); + } + + public function testCleanArray() + { + $array = array( + 'a' => '1', + 'b' => '2.7', + 'c' => 'adfasdf', + 'd' => '-12' + ); + + $newarray = $this->fixture->clean($array, TYPE_UINT); + + $this->assertEqual(4, sizeof($newarray)); + $this->assertEqual(1, $newarray['a']); + $this->assertEqual(2, $newarray['b']); + $this->assertEqual(0, $newarray['c']); + $this->assertEqual(0, $newarray['d']); + } + + public function testInputClean() + { + $this->assertEqual(-1.0, $this->fixture->inputClean('abc', TYPE_FLOAT)); + $this->assertEqual(-1.0, $this->fixture->in['abc']); + + $this->assertEqual('', $this->fixture->inputClean(':does:not:exist', TYPE_STR)); + } + + public function testInputCleanArray() + { + $this->setUp(); + $this->fixture->inputCleanArray(array( + 'abc' => TYPE_FLOAT, + 'ab"c"' => TYPE_INT + )); + + $this->assertEqual(-1.0, $this->fixture->in['abc']); + $this->assertEqual(2, $this->fixture->in['ab"c"']); + } + + public function testEscape() + { + $this->assertEqual("this isn\'t a test", $this->fixture->escape("this isn't a test", true)); + } + + public function testInputEscape() + { + $this->assertEqual("isn\'t it cool", $this->fixture->inputEscape('another"value"')); + $this->assertEqual('', $this->fixture->inputEscape(':will:never:exist')); + } + + public function testPostCheck() + { + define('ISSO_CHECK_POST_REFERER', true); + $this->setUp(); + } +} + +?> \ No newline at end of file diff --git a/UnitTest/RegisterTest.php b/UnitTest/RegisterTest.php index 3cbf00f..5b4fc5b 100644 --- a/UnitTest/RegisterTest.php +++ b/UnitTest/RegisterTest.php @@ -105,6 +105,27 @@ class RegisterTest extends UnitTestCase BSRegister::Unregister('keyThatWontExist'); $this->assertError(); } + + public function testGetType() + { + $input = BSRegister::LoadModule('Input'); + BSRegister::Register('input', $input); + $this->assertReference($input, BSRegister::GetType('Input')); + + $this->assertEqual(null, BSRegister::GetType('Date')); + } + + public function testRequiredModules() + { + BSRegister::RequiredModules(array('Input')); + $this->assertNoErrors(); + + BSRegister::RequiredModules(array('Input', 'Db')); + $this->assertError(); + + BSRegister::RequiredModules(array('Date')); + $this->assertError(); + } } ?> \ No newline at end of file -- 2.22.5