]> src.bluestatic.org Git - isso.git/blob - UnitTest/InputTest.php
Changing the error reporting system in the API to use exceptions instead of the old...
[isso.git] / UnitTest / InputTest.php
1 <?php
2
3 require_once 'PHPUnit/Framework.php';
4
5 /**
6 * InputTest
7 *
8 * @author Blue Static
9 * @copyright Copyright (c)2002 - 2007, Blue Static
10 * @package ISSO Tests
11 *
12 */
13 class InputTest extends PHPUnit_Framework_TestCase
14 {
15 private $fixture;
16
17 public function setUp()
18 {
19 $_GET = array(
20 '1' => 'moo',
21 '0' => 'foobar',
22 'abc' => '-1',
23 'ab"c"' => '2.0',
24 'ab"c"2' => 'k"lm"',
25 'ab\'c\'' => 'nop',
26 "ab\'c\'2" => "qr\'s\'"
27 );
28 $_COOKIE = array(
29 'somecookie' => '"a var"',
30 'another"value"' => "isn't it cool"
31 );
32 $_POST = array(
33 'nest' => array(
34 'foobar' => '"test"',
35 '"hi"' => 'test\'ing'
36 )
37 );
38
39 // simulate magic quotes GPC
40 /*foreach (array($_GET, $_COOKIE) AS $array)
41 {
42 foreach ($array AS $var => $value)
43 {
44 $array["$var"] = addslashes($value);
45 }
46 }*/
47
48 require_once ISSO . '/App.php';
49 $this->fixture = BSApp::LoadModule('Input');
50 }
51
52 public function testSanitizeInputData()
53 {
54 $this->assertEquals(10, sizeof($this->fixture->in));
55 $this->assertEquals(2, sizeof($this->fixture->in['nest']));
56 $this->assertEquals('&quot;a var&quot;', $this->fixture->in['somecookie']);
57 $this->assertEquals('test\'ing', $this->fixture->in['nest']['"hi"']);
58 }
59
60 public function testEntityEncode()
61 {
62 $this->assertEquals('&lt;a href=&quot;http://www.something.com/test.php?do=run&amp;moo=foo&quot;&gt;', $this->fixture->entityEncode('<a href="http://www.something.com/test.php?do=run&moo=foo">'));
63 }
64
65 public function testUnsanitize()
66 {
67 $this->assertEquals('<script type="text/javascript"> alert("XSS is fun!"); </script>', $this->fixture->unsanitize('<script type="text/javascript"> alert("XSS is fun!"); </script>'));
68 $this->assertEquals('<script type="text/javascript"> alert("XSS is fun!"); </script>', $this->fixture->unsanitize('&lt;script type=&quot;text/javascript&quot;&gt; alert(&quot;XSS is fun!&quot;); &lt;/script&gt;'));
69 $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>')));
70 }
71
72 public function testClean()
73 {
74 $this->assertEquals(0, $this->fixture->clean('abc', TYPE_INT));
75 $this->assertEquals(-1, $this->fixture->clean('-1', TYPE_INT));
76 $this->assertEquals(4, $this->fixture->clean('4def', TYPE_INT));
77
78 $this->assertEquals(0, $this->fixture->clean('abc', TYPE_UINT));
79 $this->assertEquals(0, $this->fixture->clean(-100, TYPE_UINT));
80 $this->assertEquals(40, $this->fixture->clean('40.965', TYPE_UINT));
81
82 $this->assertEquals(0, $this->fixture->clean('0.0', TYPE_FLOAT));
83 $this->assertNotEquals(0, $this->fixture->clean('0.032', TYPE_FLOAT));
84
85 $this->assertEquals(true, $this->fixture->clean('aafsdfa', TYPE_BOOL));
86 $this->assertEquals(false, $this->fixture->clean('', TYPE_BOOL));
87 $this->assertEquals(false, $this->fixture->clean('0', TYPE_BOOL));
88 $this->assertEquals(true, $this->fixture->clean('0.0', TYPE_BOOL));
89
90 $this->assertEquals('<abc "def" gih>', $this->fixture->clean('<abc "def" gih>', TYPE_STR));
91
92 $this->assertEquals('<abc "def" gih>', $this->fixture->clean('<abc "def" gih>', TYPE_STRUN));
93 $this->assertEquals('<abc "def" gih>', $this->fixture->clean($this->fixture->sanitize('<abc "def" gih>'), TYPE_STRUN));
94
95 $this->assertEquals('<abc "def" gih>', $this->fixture->clean('<abc "def" gih>', TYPE_NONE));
96
97 $this->assertEquals('åß∂ƒ(c)˙∆˚¬…æΩ≈ç√∫≤≥÷œ∑®†¥øπ“‘’”', $this->fixture->clean('åß∂ƒ(c)˙∆˚¬…æΩ≈ç√∫≤≥÷œ∑®†¥øπ“‘’”', TYPE_BIN));
98
99 try
100 {
101 $this->fixture->clean('asdfa', TYPE_THIS_DOES_NOT_EXIST);
102 $this->fail('exception expected');
103 }
104 catch (Exception $e)
105 {}
106 }
107
108 public function testCleanArray()
109 {
110 $array = array(
111 'a' => '1',
112 'b' => '2.7',
113 'c' => 'adfasdf',
114 'd' => '-12'
115 );
116
117 $newarray = $this->fixture->clean($array, TYPE_UINT);
118
119 $this->assertEquals(4, sizeof($newarray));
120 $this->assertEquals(1, $newarray['a']);
121 $this->assertEquals(2, $newarray['b']);
122 $this->assertEquals(0, $newarray['c']);
123 $this->assertEquals(0, $newarray['d']);
124 }
125
126 public function testInputClean()
127 {
128 $this->assertEquals(-1.0, $this->fixture->inputClean('abc', TYPE_FLOAT));
129 $this->assertEquals(-1.0, $this->fixture->in['abc']);
130
131 $this->assertEquals('', $this->fixture->inputClean(':does:not:exist', TYPE_STR));
132 }
133
134 public function testInputCleanArray()
135 {
136 $this->fixture->inputCleanArray(array(
137 'abc' => TYPE_FLOAT,
138 'ab"c"' => TYPE_INT
139 ));
140
141 $this->assertEquals(-1.0, $this->fixture->in['abc']);
142 $this->assertEquals(2, $this->fixture->in['ab"c"']);
143 }
144
145 public function testEscape()
146 {
147 $this->assertEquals("this isn\'t a test", $this->fixture->escape("this isn't a test", true));
148 }
149
150 public function testInputEscape()
151 {
152 $this->assertEquals("isn\'t it cool", $this->fixture->inputEscape('another"value"'));
153 $this->assertEquals('', $this->fixture->inputEscape(':will:never:exist'));
154 }
155
156 public function testPostCheck()
157 {
158 }
159 }
160
161 ?>