2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Contains the test class testing the \core\ip_utils static helper class functions.
21 * @copyright 2016 Jake Dallimore <jrhdallimore@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 * This tests the static helper functions contained in the class '\core\ip_utils'.
31 * @copyright 2016 Jake Dallimore <jrhdallimore@gmail.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class core_ip_utils_testcase
extends basic_testcase
{
36 * Test for \core\ip_utils::is_domain_name().
38 * @param string $domainname the domain name to validate.
39 * @param bool $expected the expected result.
40 * @dataProvider domain_name_data_provider
42 public function test_is_domain_name($domainname, $expected) {
43 $this->assertEquals($expected, \core\ip_utils
::is_domain_name($domainname));
47 * Data provider for test_is_domain_name().
51 public function domain_name_data_provider() {
54 ["i.net", true], // Single char, alpha tertiary domain.
55 ["0.org", true], // Single char, non-alpha tertiary domain.
56 ["0.a", true], // Single char, alpha top level domain.
57 ["0.1", false], // Single char, non-alpha top level domain.
58 ["example.com", true],
59 ["sub.example.com", true],
60 ["sub-domain.example-domain.net", true],
63 [str_repeat('sub.', 60) . "1-example.com", true], // Max length without null label is 253 octets = 253 ascii chars.
64 [str_repeat('example', 9) . ".com", true], // Max number of octets per label is 63 = 63 ascii chars.
66 [" example.com", false],
67 ["example.com ", false],
68 ["example.com/", false],
69 ["*.example.com", false],
70 ["*example.com", false],
71 ["example.123", false],
72 ["-example.com", false],
73 ["example-.com", false],
74 [".example.com", false],
76 [str_repeat('sub.', 60) . "11-example.com", false], // Name length is 254 chars, which exceeds the max allowed.
77 [str_repeat('example', 9) . "1.com", false], // Label length is 64 chars, which exceed the max allowed.
78 ["example.com.", true], // Null label explicitly provided - this is valid.
79 [".example.com.", false],
80 ["見.香港", false], // IDNs are invalid.
81 [null, false], // Non-strings are invalid.
86 * Test for \core\ip_utils::is_domain_matching_pattern().
88 * @param string $str the string to evaluate.
89 * @param bool $expected the expected result.
90 * @dataProvider domain_matching_patterns_data_provider
92 public function test_is_domain_matching_pattern($str, $expected) {
93 $this->assertEquals($expected, \core\ip_utils
::is_domain_matching_pattern($str));
97 * Data provider for test_is_domain_matching_pattern().
101 public function domain_matching_patterns_data_provider() {
104 ["*.example.com", true],
105 ["*.example.com", true],
106 ["*.sub.example.com", true],
107 ["*.sub-domain.example-domain.com", true],
108 ["*." . str_repeat('sub.', 60) . "example.com", true], // Max number of domain name chars = 253.
109 ["*." . str_repeat('example', 9) . ".com", true], // Max number of domain name label chars = 63.
111 ["*example.com", false],
112 [" *.example.com", false],
113 ["*.example.com ", false],
114 ["*-example.com", false],
115 ["*.-example.com", false],
116 ["*.example.com/", false],
117 ["sub.*.example.com", false],
118 ["sub.*example.com", false],
119 ["*.*.example.com", false],
120 ["example.com", false],
121 ["*." . str_repeat('sub.', 60) . "1example.com", false], // Name length is 254 chars, which exceeds the max allowed.
122 ["*." . str_repeat('example', 9) . "1.com", false], // Label length is 64 chars, which exceed the max allowed.
123 ["*.example.com.", true], // Null label explicitly provided - this is valid.
124 [".*.example.com.", false],
125 ["*.香港", false], // IDNs are invalid.
126 [null, false], // None-strings are invalid.
131 * Test for \core\ip_utils::is_ip_address().
133 * @param string $address the address to validate.
134 * @param bool $expected the expected result.
135 * @dataProvider ip_address_data_provider
137 public function test_is_ip_address($address, $expected) {
138 $this->assertEquals($expected, \core\ip_utils
::is_ip_address($address));
142 * Data provider for test_is_ip_address().
146 public function ip_address_data_provider() {
151 ["255.255.255.255", true],
152 ["256.0.0.1", false],
153 ["256.0.0.1", false],
154 ["127.0.0.0/24", false],
155 ["127.0.0.0-255", false],
160 ["fe80:fe80:fe80:fe80:fe80:fe80:fe80:fe80", true],
161 ["fe80::ffff", true],
165 ["127.0.0.0/24", false],
166 ["fe80::fe80/128", false],
167 ["fe80:fe80:fe80:fe80:fe80:fe80:fe80:fe80/128", false],
171 ["fe80::ddddd", false],
172 ["fe80::gggg", false],
173 ["fe80:fe80:fe80:fe80:fe80:fe80:fe80:fe80:fe80", false],
178 * Test for \core\ip_utils::is_ipv4_address().
180 * @param string $address the address to validate.
181 * @param bool $expected the expected result.
182 * @dataProvider ipv4_address_data_provider
184 public function test_is_ipv4_address($address, $expected) {
185 $this->assertEquals($expected, \core\ip_utils
::is_ipv4_address($address));
189 * Data provider for test_is_ipv4_address().
193 public function ipv4_address_data_provider() {
197 ["255.255.255.255", true],
198 [" 127.0.0.1", false],
199 ["127.0.0.1 ", false],
200 ["-127.0.0.1", false],
202 ["127.0.0.0.1", false],
204 ["localhost", false],
206 ["256.0.0.1", false],
207 ["256.0.0.1", false],
208 ["127.0.0.0/24", false],
209 ["127.0.0.0-255", false],
214 * Test for \core\ip_utils::is_ipv4_range().
216 * @param string $addressrange the address range to validate.
217 * @param bool $expected the expected result.
218 * @dataProvider ipv4_range_data_provider
220 public function test_is_ipv4_range($addressrange, $expected) {
221 $this->assertEquals($expected, \core\ip_utils
::is_ipv4_range($addressrange));
225 * Data provider for test_is_ipv4_range().
229 public function ipv4_range_data_provider() {
231 ["127.0.0.1/24", true],
232 ["127.0.0.20-20", true],
233 ["127.0.0.20-50", true],
234 ["127.0.0.0-255", true],
235 ["127.0.0.1-1", true],
236 ["255.255.255.0-255", true],
237 ["127.0.0.1", false],
239 [" 127.0.0.0/24", false],
240 ["127.0.0.0/24 ", false],
241 ["a.b.c.d/24", false],
242 ["256.0.0.0-80", false],
243 ["127.0.0.0/a", false],
244 ["256.0.0.0/24", false],
245 ["127.0.0.0/-1", false],
246 ["127.0.0.0/33", false],
247 ["127.0.0.0-127.0.0.10", false],
248 ["127.0.0.30-20", false],
249 ["127.0.0.0-256", false],
250 ["fe80::fe80/64", false],
255 * Test for \core\ip_utils::is_ipv6_address().
257 * @param string $address the address to validate.
258 * @param bool $expected the expected result.
259 * @dataProvider ipv6_address_data_provider
261 public function test_is_ipv6_address($address, $expected) {
262 $this->assertEquals($expected, \core\ip_utils
::is_ipv6_address($address));
266 * Data provider for test_is_ipv6_address().
270 public function ipv6_address_data_provider() {
276 ["fe80:fe80:fe80:fe80:fe80:fe80:fe80:fe80", true],
277 ["fe80::ffff", true],
281 ["127.0.0.0", false],
282 ["127.0.0.0/24", false],
283 ["fe80::fe80/128", false],
284 ["fe80:fe80:fe80:fe80:fe80:fe80:fe80:fe80/128", false],
288 ["fe80::ddddd", false],
289 ["fe80::gggg", false],
290 ["fe80:fe80:fe80:fe80:fe80:fe80:fe80:fe80:fe80", false],
295 * Test for \core\ip_utils::is_ipv6_range().
297 * @param string $addressrange the address range to validate.
298 * @param bool $expected the expected result.
299 * @dataProvider ipv6_range_data_provider
301 public function test_is_ipv6_range($addressrange, $expected) {
302 $this->assertEquals($expected, \core\ip_utils
::is_ipv6_range($addressrange));
306 * Data provider for test_is_ipv6_range().
310 public function ipv6_range_data_provider() {
314 ["fe80:fe80:fe80:fe80:fe80:fe80:fe80:fe80/128", true],
315 ["fe80::dddd/128", true],
317 ["fe80::dddd-ffff", true],
322 ["fe80::fe80", false],
327 ["fe80::fe80/129", false],
328 ["fe80:fe80:fe80:fe80:fe80:fe80:fe80:fe80", false],
329 ["fe80::bbbb-aaaa", false],
330 ["fe80::0-fffg", false],
331 ["fe80::0-fffff", false],
332 ["fe80::0 - ffff", false],
333 [" fe80::0-ffff", false],
334 ["fe80::0-ffff ", false],
335 ["192.0.0.0/24", false],
336 ["fe80:::fe80/128", false],
337 ["fe80:::aaaa-dddd", false],
342 * Test checking domains against a list of allowed domains.
344 * @param bool $expected Expected result
345 * @param string $domain domain address
346 * @dataProvider data_domain_addresses
348 public function test_check_domain_against_allowed_domains($expected, $domain) {
349 $alloweddomains = ['example.com',
351 '*.per.this.penny-arcade.com',
354 $this->assertEquals($expected, \core\ip_utils
::is_domain_in_allowed_list($domain, $alloweddomains));
358 * Data provider for test_check_domain_against_allowed_domains.
362 public function data_domain_addresses() {
364 [true, 'example.com'],
365 [false, 'sub.example.com'],
366 [false, 'example.com.au'],
367 [false, ' example.com'], // A space at the front of the domain is invalid.
368 [false, 'example.123'], // Numbers at the end is invalid.
369 [false, 'test.example.com'],
370 [false, 'moodle.com'],
371 [true, 'test.moodle.com'],
372 [false, 'test.moodle.com.au'],
373 [true, 'nice.address.per.this.penny-arcade.com'],
374 [false, 'normal.per.this.penny-arcade.com.au'],
375 [false, 'bad.thing.url.com'], // The allowed domain (above) has a bad wildcard and so this address will return false.
376 [false, 'trouble.com.au'] // The allowed domain (above) has a space at the front and so will return false.
381 * Data provider for test_is_ip_in_subnet_list.
385 public function data_is_ip_in_subnet_list() {
387 [true, '1.1.1.1', '1.1.1.1', "\n"],
388 [false, '1.1.1.1', '2.2.2.2', "\n"],
389 [true, '1.1.1.1', "1.1.1.5\n1.1.1.1", "\n"],
390 [true, '1.1.1.1', "1.1.1.5,1.1.1.1", ","],
395 * Test checking ips against a list of allowed domains.
397 * @param bool $expected Expected result
398 * @param string $ip IP address
399 * @param string $list list of IP subnets
400 * @param string $delim delimiter of list
401 * @dataProvider data_is_ip_in_subnet_list
403 public function test_is_ip_in_subnet_list($expected, $ip, $list, $delim) {
404 $this->assertEquals($expected, \core\ip_utils
::is_ip_in_subnet_list($ip, $list, $delim));