MDL-14123 cleanremoteaddr improvements and added unit tests
[moodle.git] / lib / simpletest / testmoodlelib.php
CommitLineData
d499142e 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.org //
9// //
56a1a882 10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
d499142e 11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
7c9d46c9 26/**
27 * Unit tests for (some of) ../moodlelib.php.
28 *
29 * @copyright &copy; 2006 The Open University
30 * @author T.J.Hunt@open.ac.uk
d499142e 31 * @author nicolas@moodle.com
7c9d46c9 32 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
33 * @package moodlecore
34 */
35
cdbbd26f 36if (!defined('MOODLE_INTERNAL')) {
37 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
38}
7c9d46c9 39
7c9d46c9 40require_once($CFG->libdir . '/moodlelib.php');
41
b9c639d6 42class moodlelib_test extends MoodleUnitTestCase {
43
a205dcdc 44 var $user_agents = array(
45 'MSIE' => array(
46 '5.5' => array('Windows 2000' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)'),
47 '6.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'),
48 '7.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; YPC 3.0.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)')
b9c639d6 49 ),
a205dcdc 50 'Firefox' => array(
51 '1.0.6' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10) Gecko/20050716 Firefox/1.0.6'),
52 '1.5' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8) Gecko/20051107 Firefox/1.5'),
53 '1.5.0.1' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1'),
54 '2.0' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1',
55 'Ubuntu Linux AMD64' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20060601 Firefox/2.0 (Ubuntu-edgy)')
56 ),
57 'Safari' => array(
58 '312' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312'),
59 '2.0' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412')
60 ),
61 'Opera' => array(
62 '8.51' => array('Windows XP' => 'Opera/8.51 (Windows NT 5.1; U; en)'),
63 '9.0' => array('Windows XP' => 'Opera/9.0 (Windows NT 5.1; U; en)',
64 'Debian Linux' => 'Opera/9.01 (X11; Linux i686; U; en)')
65 )
66 );
b9c639d6 67
7c9d46c9 68 function setUp() {
69 }
70
71 function tearDown() {
72 }
73
65de6864 74 function test_cleanremoteaddr() {
75 //IPv4
76 $this->assertEqual(cleanremoteaddr('1023.121.234.1'), null);
77 $this->assertEqual(cleanremoteaddr('123.121.234.01 '), '123.121.234.1');
78
79 //IPv6
80 $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:0:0'), null);
81 $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:abh'), null);
82 $this->assertEqual(cleanremoteaddr('0:0:0:::0:0:1'), null);
83 $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:0', true), '::');
84 $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:1:1', true), '::1:1');
85 $this->assertEqual(cleanremoteaddr('abcd:00ef:0:0:0:0:0:0', true), 'abcd:ef::');
86 $this->assertEqual(cleanremoteaddr('1:0:0:0:0:0:0:1', true), '1::1');
87 $this->assertEqual(cleanremoteaddr('::10:1', false), '0:0:0:0:0:0:10:1');
88 $this->assertEqual(cleanremoteaddr('01:1::', false), '1:1:0:0:0:0:0:0');
89 $this->assertEqual(cleanremoteaddr('10::10', false), '10:0:0:0:0:0:0:10');
90 $this->assertEqual(cleanremoteaddr('::ffff:192.168.1.1', true), '::ffff:c0a8:11');
91 }
92
7c9d46c9 93 function test_address_in_subnet() {
94 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.1'));
95 $this->assertFalse(address_in_subnet('123.121.234.2', '123.121.234.1'));
96 $this->assertFalse(address_in_subnet('123.121.134.1', '123.121.234.1'));
97 $this->assertFalse(address_in_subnet('113.121.234.1', '123.121.234.1'));
98 $this->assertTrue(address_in_subnet('123.121.234.0', '123.121.234.2/28'));
99 $this->assertTrue(address_in_subnet('123.121.234.15', '123.121.234.2/28'));
100 $this->assertFalse(address_in_subnet('123.121.234.16', '123.121.234.2/28'));
101 $this->assertFalse(address_in_subnet('123.121.234.255', '123.121.234.2/28'));
1cc6b516 102 $this->assertTrue(address_in_subnet('123.121.234.0', '123.121.234.0/')); // / is like /32.
103 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.0/'));
104 $this->assertFalse(address_in_subnet('232.232.232.232', '123.121.234.0/0'));
7c9d46c9 105 $this->assertFalse(address_in_subnet('123.122.234.1', '123.121.'));
106 $this->assertFalse(address_in_subnet('223.121.234.1', '123.121.'));
1cc6b516 107 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121'));
108 $this->assertFalse(address_in_subnet('123.122.234.1', '123.121'));
109 $this->assertFalse(address_in_subnet('223.121.234.1', '123.121'));
110 $this->assertFalse(address_in_subnet('123.121.234.100', '123.121.234.10'));
7c9d46c9 111 $this->assertFalse(address_in_subnet('123.121.234.9', '123.121.234.10-20'));
112 $this->assertTrue(address_in_subnet('123.121.234.10', '123.121.234.10-20'));
113 $this->assertTrue(address_in_subnet('123.121.234.15', '123.121.234.10-20'));
114 $this->assertTrue(address_in_subnet('123.121.234.20', '123.121.234.10-20'));
115 $this->assertFalse(address_in_subnet('123.121.234.21', '123.121.234.10-20'));
116 $this->assertTrue(address_in_subnet(' 123.121.234.1 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
117 $this->assertTrue(address_in_subnet(' 1.1.2.3 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
118 $this->assertTrue(address_in_subnet(' 2.2.234.1 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
119 $this->assertTrue(address_in_subnet(' 3.3.3.4 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
120 $this->assertFalse(address_in_subnet(' 123.121.234.2 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
121 $this->assertFalse(address_in_subnet(' 2.1.2.3 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
122 $this->assertFalse(address_in_subnet(' 2.3.234.1 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
123 $this->assertFalse(address_in_subnet(' 3.3.3.7 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
6ff2be37 124 $this->assertFalse(address_in_subnet('172.16.1.142', '172.16.1.143/148'));
7c9d46c9 125 }
b9c639d6 126
239c1576 127 /**
b9c639d6 128 * Modifies $_SERVER['HTTP_USER_AGENT'] manually to check if check_browser_version
239c1576 129 * works as expected.
130 */
131 function test_check_browser_version()
132 {
a205dcdc 133 global $CFG;
b9c639d6 134
135 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
239c1576 136 $this->assertTrue(check_browser_version('Safari', '312'));
137 $this->assertFalse(check_browser_version('Safari', '500'));
b9c639d6 138
a205dcdc 139 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
239c1576 140 $this->assertTrue(check_browser_version('Opera', '8.0'));
141 $this->assertFalse(check_browser_version('Opera', '10.0'));
b9c639d6 142
a205dcdc 143 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
239c1576 144 $this->assertTrue(check_browser_version('MSIE', '5.0'));
145 $this->assertFalse(check_browser_version('MSIE', '7.0'));
b9c639d6 146
a205dcdc 147 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
239c1576 148 $this->assertTrue(check_browser_version('Firefox', '1.5'));
b9c639d6 149 $this->assertFalse(check_browser_version('Firefox', '3.0'));
d499142e 150 }
b9c639d6 151
facb512c 152 function test_optional_param()
153 {
b9c639d6 154 $_POST['username'] = 'post_user';
facb512c 155 $_GET['username'] = 'get_user';
156 $this->assertEqual(optional_param('username', 'default_user'), 'post_user');
b9c639d6 157
facb512c 158 unset($_POST['username']);
159 $this->assertEqual(optional_param('username', 'default_user'), 'get_user');
b9c639d6 160
facb512c 161 unset($_GET['username']);
162 $this->assertEqual(optional_param('username', 'default_user'), 'default_user');
163 }
b9c639d6 164
facb512c 165 /**
166 * Used by {@link optional_param()} and {@link required_param()} to
167 * clean the variables and/or cast to specific types, based on
168 * an options field.
169 * <code>
170 * $course->format = clean_param($course->format, PARAM_ALPHA);
171 * $selectedgrade_item = clean_param($selectedgrade_item, PARAM_CLEAN);
172 * </code>
173 *
174 * @uses $CFG
175 * @uses PARAM_CLEAN
176 * @uses PARAM_INT
177 * @uses PARAM_INTEGER
178 * @uses PARAM_ALPHA
179 * @uses PARAM_ALPHANUM
180 * @uses PARAM_NOTAGS
181 * @uses PARAM_ALPHAEXT
182 * @uses PARAM_BOOL
183 * @uses PARAM_SAFEDIR
facb512c 184 * @uses PARAM_FILE
185 * @uses PARAM_PATH
186 * @uses PARAM_HOST
187 * @uses PARAM_URL
188 * @uses PARAM_LOCALURL
189 * @uses PARAM_CLEANHTML
190 * @uses PARAM_SEQUENCE
191 * @param mixed $param the variable we are cleaning
192 * @param int $type expected format of param after cleaning.
193 * @return mixed
194 */
195 function test_clean_param()
196 {
c7fd861b 197 global $CFG;
facb512c 198 // Test unknown parameter type
b9c639d6 199
facb512c 200 // Test Raw param
b9c639d6 201 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_RAW),
facb512c 202 '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)');
b9c639d6 203
204 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_CLEAN),
65de6864 205 '#()*#,9789\'".,');
c7fd861b 206
207 // Test PARAM_URL and PARAM_LOCALURL a bit
208 $this->assertEqual(clean_param('http://google.com/', PARAM_URL), 'http://google.com/');
209 $this->assertEqual(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_URL), 'http://some.very.long.and.silly.domain/with/a/path/');
210 $this->assertEqual(clean_param('http://localhost/', PARAM_URL), 'http://localhost/');
211 $this->assertEqual(clean_param('http://0.255.1.1/numericip.php', PARAM_URL), 'http://0.255.1.1/numericip.php');
212 $this->assertEqual(clean_param('/just/a/path', PARAM_URL), '/just/a/path');
213 $this->assertEqual(clean_param('funny:thing', PARAM_URL), '');
214
215 $this->assertEqual(clean_param('http://google.com/', PARAM_LOCALURL), '');
216 $this->assertEqual(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_LOCALURL), '');
217 $this->assertEqual(clean_param($CFG->wwwroot, PARAM_LOCALURL), $CFG->wwwroot);
218 $this->assertEqual(clean_param('/just/a/path', PARAM_LOCALURL), '/just/a/path');
219 $this->assertEqual(clean_param('funny:thing', PARAM_LOCALURL), '');
facb512c 220 }
56a1a882 221
222 function test_make_user_directory() {
223 global $CFG;
224
225 // Test success conditions
226 $this->assertEqual("$CFG->dataroot/user/0/0", make_user_directory(0, true));
227 $this->assertEqual("$CFG->dataroot/user/0/1", make_user_directory(1, true));
228 $this->assertEqual("$CFG->dataroot/user/0/999", make_user_directory(999, true));
229 $this->assertEqual("$CFG->dataroot/user/1000/1000", make_user_directory(1000, true));
230 $this->assertEqual("$CFG->dataroot/user/2147483000/2147483647", make_user_directory(2147483647, true)); // Largest int possible
231
232 // Test fail conditions
233 $this->assertFalse(make_user_directory(2147483648, true)); // outside int boundary
234 $this->assertFalse(make_user_directory(-1, true));
235 $this->assertFalse(make_user_directory('string', true));
236 $this->assertFalse(make_user_directory(false, true));
237 $this->assertFalse(make_user_directory(true, true));
b9c639d6 238
56a1a882 239 }
7c9d46c9 240}
241
242?>