summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/helpers/security_helper_test.php
blob: effd3ec0289ecfcfd3df42d542e123f7d060415e (plain)
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
<?php

class Security_helper_tests extends CI_TestCase {

	function setUp()
	{
		$this->helper('security');
		$obj = new stdClass;
		$obj->security = new Mock_Core_Security();
		$this->ci_instance($obj);
	}

	function test_xss_clean()
	{
		$this->assertEquals('foo', xss_clean('foo'));

		$this->assertEquals("Hello, i try to [removed]alert&#40;'Hack'&#41;;[removed] your site", xss_clean("Hello, i try to <script>alert('Hack');</script> your site"));
	}

	function test_sanitize_filename()
	{
		$this->assertEquals('hello.doc', sanitize_filename('hello.doc'));

		$filename = './<!--foo-->';
		$this->assertEquals('foo', sanitize_filename($filename));
	}

	function test_do_hash()
	{
		$md5 = md5('foo');
		$sha1 = sha1('foo');

		$algos = hash_algos();
		$algo_results = array();
		foreach ($algos as $k => $v)
		{
			$algo_results[$v] = hash($v, 'foo');
		}

		$this->assertEquals($sha1, do_hash('foo'));
		$this->assertEquals($sha1, do_hash('foo', 'sha1'));
		$this->assertEquals($md5, do_hash('foo', 'md5'));
		$this->assertEquals($md5, do_hash('foo', 'foobar'));

		// Test each algorithm available to PHP
		foreach ($algo_results as $algo => $result)
		{
			$this->assertEquals($result, do_hash('foo', $algo));
		}
	}

	function test_strip_image_tags()
	{
		$this->assertEquals('http://example.com/spacer.gif', strip_image_tags('http://example.com/spacer.gif'));

		$this->assertEquals('http://example.com/spacer.gif', strip_image_tags('<img src="http://example.com/spacer.gif" alt="Who needs CSS when you have a spacer.gif?" />'));
	}

	function test_encode_php_tags()
	{
		$this->assertEquals('&lt;? echo $foo; ?&gt;', encode_php_tags('<? echo $foo; ?>'));
	}

}