3 require_once 'PHPUnit/Framework.php';
9 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
14 class InputTest
extends PHPUnit_Framework_TestCase
18 public function setUp()
27 "ab\'c\'2" => "qr\'s\'"
30 'somecookie' => '"a var"',
31 'another"value"' => "isn't it cool"
40 // simulate magic quotes GPC
41 /*foreach (array($_GET, $_COOKIE) AS $array)
43 foreach ($array AS $var => $value)
45 $array["$var"] = addslashes($value);
49 require_once 'ISSO/Register.php';
50 $this->fixture
= BSRegister
::LoadModule('Input');
53 public function testSanitizeInputData()
55 $this->assertEquals(10, sizeof($this->fixture
->in
));
56 $this->assertEquals(2, sizeof($this->fixture
->in
['nest']));
57 $this->assertEquals('"a var"', $this->fixture
->in
['somecookie']);
58 $this->assertEquals('test\'ing', $this->fixture
->in
['nest']['"hi"']);
61 public function testEntityEncode()
63 $this->assertEquals('<a href="http://www.something.com/test.php?do=run&moo=foo">', $this->fixture
->entityEncode('<a href="http://www.something.com/test.php?do=run&moo=foo">'));
66 public function testUnsanitize()
68 $this->assertEquals('<script type="text/javascript"> alert("XSS is fun!"); </script>', $this->fixture
->unsanitize('<script type="text/javascript"> alert("XSS is fun!"); </script>'));
69 $this->assertEquals('<script type="text/javascript"> alert("XSS is fun!"); </script>', $this->fixture
->unsanitize('<script type="text/javascript"> alert("XSS is fun!"); </script>'));
70 $this->assertEquals('<script type="text/javascript"> alert("XSS is fun!"); </script>', $this->fixture
->unsanitize($this->fixture
->sanitize('<script type="text/javascript"> alert("XSS is fun!"); </script>')));
73 public function testClean()
75 $this->assertEquals(0, $this->fixture
->clean('abc', TYPE_INT
));
76 $this->assertEquals(-1, $this->fixture
->clean('-1', TYPE_INT
));
77 $this->assertEquals(4, $this->fixture
->clean('4def', TYPE_INT
));
79 $this->assertEquals(0, $this->fixture
->clean('abc', TYPE_UINT
));
80 $this->assertEquals(0, $this->fixture
->clean(-100, TYPE_UINT
));
81 $this->assertEquals(40, $this->fixture
->clean('40.965', TYPE_UINT
));
83 $this->assertEquals(0, $this->fixture
->clean('0.0', TYPE_FLOAT
));
84 $this->assertNotEquals(0, $this->fixture
->clean('0.032', TYPE_FLOAT
));
86 $this->assertEquals(true, $this->fixture
->clean('aafsdfa', TYPE_BOOL
));
87 $this->assertEquals(false, $this->fixture
->clean('', TYPE_BOOL
));
88 $this->assertEquals(false, $this->fixture
->clean('0', TYPE_BOOL
));
89 $this->assertEquals(true, $this->fixture
->clean('0.0', TYPE_BOOL
));
91 $this->assertEquals('<abc "def" gih>', $this->fixture
->clean('<abc "def" gih>', TYPE_STR
));
93 $this->assertEquals('<abc "def" gih>', $this->fixture
->clean('<abc "def" gih>', TYPE_STRUN
));
94 $this->assertEquals('<abc "def" gih>', $this->fixture
->clean($this->fixture
->sanitize('<abc "def" gih>'), TYPE_STRUN
));
96 $this->assertEquals('<abc "def" gih>', $this->fixture
->clean('<abc "def" gih>', TYPE_NONE
));
98 $this->assertEquals('åß∂ƒ©˙∆˚¬…æΩ≈ç√∫≤≥÷œ∑®†¥øπ“‘’”', $this->fixture
->clean('åß∂ƒ©˙∆˚¬…æΩ≈ç√∫≤≥÷œ∑®†¥øπ“‘’”', TYPE_BIN
));
100 $this->fixture
->clean('asdfa', TYPE_THIS_DOES_NOT_EXIST
);
101 // TODO - use exceptions
102 // $this->assertError();
105 public function testCleanArray()
114 $newarray = $this->fixture
->clean($array, TYPE_UINT
);
116 $this->assertEquals(4, sizeof($newarray));
117 $this->assertEquals(1, $newarray['a']);
118 $this->assertEquals(2, $newarray['b']);
119 $this->assertEquals(0, $newarray['c']);
120 $this->assertEquals(0, $newarray['d']);
123 public function testInputClean()
125 $this->assertEquals(-1.0, $this->fixture
->inputClean('abc', TYPE_FLOAT
));
126 $this->assertEquals(-1.0, $this->fixture
->in
['abc']);
128 $this->assertEquals('', $this->fixture
->inputClean(':does:not:exist', TYPE_STR
));
131 public function testInputCleanArray()
134 $this->fixture
->inputCleanArray(array(
139 $this->assertEquals(-1.0, $this->fixture
->in
['abc']);
140 $this->assertEquals(2, $this->fixture
->in
['ab"c"']);
143 public function testEscape()
145 $this->assertEquals("this isn\'t a test", $this->fixture
->escape("this isn't a test", true));
148 public function testInputEscape()
150 $this->assertEquals("isn\'t it cool", $this->fixture
->inputEscape('another"value"'));
151 $this->assertEquals('', $this->fixture
->inputEscape(':will:never:exist'));
154 public function testPostCheck()
156 define('ISSO_CHECK_POST_REFERER', true);