1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
<?php
class Input_test extends CI_TestCase {
public function set_up()
{
// Set server variable to GET as default, since this will leave unset in STDIN env
$_SERVER['REQUEST_METHOD'] = 'GET';
// Set config for Input class
$this->ci_set_config('allow_get_array', TRUE);
$this->ci_set_config('global_xss_filtering', FALSE);
$this->ci_set_config('csrf_protection', FALSE);
$security = new Mock_Core_Security();
$this->ci_set_config('charset', 'UTF-8');
$utf8 = new Mock_Core_Utf8();
$this->input = new Mock_Core_Input($security, $utf8);
}
// --------------------------------------------------------------------
public function tear_down()
{
$_POST = [];
$_GET = [];
}
// --------------------------------------------------------------------
public function test_get_not_exists()
{
$this->assertSame(array(), $this->input->get());
$this->assertNull($this->input->get('foo'));
}
// --------------------------------------------------------------------
public function test_get_exist()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_GET['foo'] = 'bar';
$this->assertArrayHasKey('foo', $this->input->get());
$this->assertEquals('bar', $this->input->get('foo'));
}
// --------------------------------------------------------------------
public function test_get_exist_with_xss_clean()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_GET['harm'] = "Hello, i try to <script>alert('Hack');</script> your site";
$this->assertArrayHasKey('harm', $this->input->get());
$this->assertEquals("Hello, i try to <script>alert('Hack');</script> your site", $this->input->get('harm'));
$this->assertEquals("Hello, i try to [removed]alert('Hack');[removed] your site", $this->input->get('harm', TRUE));
}
// --------------------------------------------------------------------
public function test_post_not_exists()
{
$this->assertSame(array(), $this->input->post());
$this->assertNull($this->input->post('foo'));
}
// --------------------------------------------------------------------
public function test_post_exist()
{
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['foo'] = 'bar';
$this->assertArrayHasKey('foo', $this->input->post());
$this->assertEquals('bar', $this->input->post('foo'));
}
// --------------------------------------------------------------------
public function test_post_exist_with_xss_clean()
{
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['harm'] = "Hello, i try to <script>alert('Hack');</script> your site";
$this->assertArrayHasKey('harm', $this->input->post());
$this->assertEquals("Hello, i try to <script>alert('Hack');</script> your site", $this->input->post('harm'));
$this->assertEquals("Hello, i try to [removed]alert('Hack');[removed] your site", $this->input->post('harm', TRUE));
}
// --------------------------------------------------------------------
public function test_post_get()
{
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['foo'] = 'bar';
$this->assertEquals('bar', $this->input->post_get('foo'));
}
// --------------------------------------------------------------------
public function test_get_post()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_GET['foo'] = 'bar';
$this->assertEquals('bar', $this->input->get_post('foo'));
}
// --------------------------------------------------------------------
public function test_cookie()
{
$_COOKIE['foo'] = 'bar';
$this->assertEquals('bar', $this->input->cookie('foo'));
$this->assertNull($this->input->cookie('bar'));
}
// --------------------------------------------------------------------
public function test_server()
{
$this->assertEquals('GET', $this->input->server('REQUEST_METHOD'));
}
// --------------------------------------------------------------------
public function test_fetch_from_array()
{
$data = array(
'foo' => 'bar',
'harm' => 'Hello, i try to <script>alert(\'Hack\');</script> your site',
);
$foo = $this->input->fetch_from_array($data, 'foo');
$harm = $this->input->fetch_from_array($data, 'harm');
$harmless = $this->input->fetch_from_array($data, 'harm', TRUE);
$this->assertEquals('bar', $foo);
$this->assertEquals("Hello, i try to <script>alert('Hack');</script> your site", $harm);
$this->assertEquals("Hello, i try to [removed]alert('Hack');[removed] your site", $harmless);
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['foo'] = array('bar' => 'baz');
$barArray = array('bar' => 'baz');
$this->assertEquals('baz', $this->input->post('foo[bar]'));
$this->assertEquals($barArray, $this->input->post('foo[]'));
$this->assertNull($this->input->post('foo[baz]'));
}
// --------------------------------------------------------------------
public function test_valid_ip()
{
$this->assertTrue($this->input->valid_ip('192.18.0.1'));
$this->assertTrue($this->input->valid_ip('192.18.0.1', 'ipv4'));
$this->assertFalse($this->input->valid_ip('555.0.0.0'));
$this->assertFalse($this->input->valid_ip('2001:db8:0:85a3::ac1f:8001', 'ipv4'));
// v6 tests
$this->assertFalse($this->input->valid_ip('192.18.0.1', 'ipv6'));
$ip_v6 = array(
'2001:0db8:0000:85a3:0000:0000:ac1f:8001',
'2001:db8:0:85a3:0:0:ac1f:8001',
'2001:db8:0:85a3::ac1f:8001'
);
foreach ($ip_v6 as $ip)
{
$this->assertTrue($this->input->valid_ip($ip));
$this->assertTrue($this->input->valid_ip($ip, 'ipv6'));
}
}
// --------------------------------------------------------------------
public function test_method()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$this->assertEquals('get', $this->input->method());
$this->assertEquals('GET', $this->input->method(TRUE));
$_SERVER['REQUEST_METHOD'] = 'POST';
$this->assertEquals('post', $this->input->method());
$this->assertEquals('POST', $this->input->method(TRUE));
}
// --------------------------------------------------------------------
public function test_is_ajax_request()
{
$this->assertFalse($this->input->is_ajax_request());
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'test';
$this->assertFalse($this->input->is_ajax_request());
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
$this->assertTrue($this->input->is_ajax_request());
}
// --------------------------------------------------------------------
public function test_input_stream()
{
$this->markTestSkipped('TODO: Find a way to test input://');
}
// --------------------------------------------------------------------
public function test_set_cookie()
{
$this->markTestSkipped('TODO: Find a way to test HTTP headers');
}
// --------------------------------------------------------------------
public function test_get_request_header()
{
$this->markTestSkipped('TODO: Find a way to test HTTP headers');
}
// --------------------------------------------------------------------
public function test_ip_address()
{
$this->input->ip_address = '127.0.0.1';
$this->assertEquals('127.0.0.1', $this->input->ip_address());
// 127.0.0.1 is set in our Bootstrap file
$this->input->ip_address = FALSE;
$this->assertEquals('127.0.0.1', $this->input->ip_address());
// Invalid
$_SERVER['REMOTE_ADDR'] = 'invalid_ip_address';
$this->input->ip_address = FALSE; // reset cached value
$this->assertEquals('0.0.0.0', $this->input->ip_address());
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
// Proxy_ips tests
$this->input->ip_address = FALSE;
$this->ci_set_config('proxy_ips', '127.0.0.3, 127.0.0.4, 127.0.0.2');
$_SERVER['HTTP_CLIENT_IP'] = '127.0.0.2';
$this->assertEquals('127.0.0.1', $this->input->ip_address());
// Invalid spoof
$this->input->ip_address = FALSE;
$this->ci_set_config('proxy_ips', 'invalid_ip_address');
$_SERVER['HTTP_CLIENT_IP'] = 'invalid_ip_address';
$this->assertEquals('127.0.0.1', $this->input->ip_address());
$this->input->ip_address = FALSE;
$this->ci_set_config('proxy_ips', 'http://foo/bar/baz, 127.0.0.1/1');
$_SERVER['HTTP_CLIENT_IP'] = '127.0.0.1';
$this->assertEquals('127.0.0.1', $this->input->ip_address());
$this->input->ip_address = FALSE;
$this->ci_set_config('proxy_ips', 'http://foo/bar/baz, 127.0.0.2');
$_SERVER['HTTP_CLIENT_IP'] = '127.0.0.2';
$_SERVER['REMOTE_ADDR'] = '127.0.0.2';
$this->assertEquals('127.0.0.2', $this->input->ip_address());
//IPv6
$this->input->ip_address = FALSE;
$this->ci_set_config('proxy_ips', 'FE80:0000:0000:0000:0202:B3FF:FE1E:8329/1, FE80:0000:0000:0000:0202:B3FF:FE1E:8300/2');
$_SERVER['HTTP_CLIENT_IP'] = 'FE80:0000:0000:0000:0202:B3FF:FE1E:8300';
$_SERVER['REMOTE_ADDR'] = 'FE80:0000:0000:0000:0202:B3FF:FE1E:8329';
$this->assertEquals('FE80:0000:0000:0000:0202:B3FF:FE1E:8300', $this->input->ip_address());
$this->input->ip_address = FALSE;
$this->ci_set_config('proxy_ips', '0::/32');
$_SERVER['HTTP_CLIENT_IP'] = '127.0.0.7';
$_SERVER['REMOTE_ADDR'] = '0000:0000:0000:0000:0000:0000:0000:0001';
$this->assertEquals('127.0.0.7', $this->input->ip_address());
$this->input->ip_address = FALSE;
$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // back to reality
}
// --------------------------------------------------------------------
public function test_user_agent()
{
$_SERVER['HTTP_USER_AGENT'] = 'test';
$this->assertEquals('test', $this->input->user_agent());
}
}
|