Merge "Revert "Log the reason why revision->getContent() returns null""
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / IPTest.php
1 <?php
2 /**
3 * Tests for IP validity functions.
4 *
5 * Ported from /t/inc/IP.t by avar.
6 *
7 * @group IP
8 * @todo Test methods in this call should be split into a method and a
9 * dataprovider.
10 */
11 class IPTest extends PHPUnit\Framework\TestCase {
12
13 use MediaWikiCoversValidator;
14
15 /**
16 * @covers IP::isIPAddress
17 * @dataProvider provideInvalidIPs
18 */
19 public function isNotIPAddress( $val, $desc ) {
20 $this->assertFalse( IP::isIPAddress( $val ), $desc );
21 }
22
23 /**
24 * Provide a list of things that aren't IP addresses
25 */
26 public function provideInvalidIPs() {
27 return [
28 [ false, 'Boolean false is not an IP' ],
29 [ true, 'Boolean true is not an IP' ],
30 [ '', 'Empty string is not an IP' ],
31 [ 'abc', 'Garbage IP string' ],
32 [ ':', 'Single ":" is not an IP' ],
33 [ '2001:0DB8::A:1::1', 'IPv6 with a double :: occurrence' ],
34 [ '2001:0DB8::A:1::', 'IPv6 with a double :: occurrence, last at end' ],
35 [ '::2001:0DB8::5:1', 'IPv6 with a double :: occurrence, firt at beginning' ],
36 [ '124.24.52', 'IPv4 not enough quads' ],
37 [ '24.324.52.13', 'IPv4 out of range' ],
38 [ '.24.52.13', 'IPv4 starts with period' ],
39 [ 'fc:100:300', 'IPv6 with only 3 words' ],
40 ];
41 }
42
43 /**
44 * @covers IP::isIPAddress
45 */
46 public function testisIPAddress() {
47 $this->assertTrue( IP::isIPAddress( '::' ), 'RFC 4291 IPv6 Unspecified Address' );
48 $this->assertTrue( IP::isIPAddress( '::1' ), 'RFC 4291 IPv6 Loopback Address' );
49 $this->assertTrue( IP::isIPAddress( '74.24.52.13/20' ), 'IPv4 range' );
50 $this->assertTrue( IP::isIPAddress( 'fc:100:a:d:1:e:ac:0/24' ), 'IPv6 range' );
51 $this->assertTrue( IP::isIPAddress( 'fc::100:a:d:1:e:ac/96' ), 'IPv6 range with "::"' );
52
53 $validIPs = [ 'fc:100::', 'fc:100:a:d:1:e:ac::', 'fc::100', '::fc:100:a:d:1:e:ac',
54 '::fc', 'fc::100:a:d:1:e:ac', 'fc:100:a:d:1:e:ac:0', '124.24.52.13', '1.24.52.13' ];
55 foreach ( $validIPs as $ip ) {
56 $this->assertTrue( IP::isIPAddress( $ip ), "$ip is a valid IP address" );
57 }
58 }
59
60 /**
61 * @covers IP::isIPv6
62 */
63 public function testisIPv6() {
64 $this->assertFalse( IP::isIPv6( ':fc:100::' ), 'IPv6 starting with lone ":"' );
65 $this->assertFalse( IP::isIPv6( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
66 $this->assertFalse( IP::isIPv6( 'fc:300' ), 'IPv6 with only 2 words' );
67 $this->assertFalse( IP::isIPv6( 'fc:100:300' ), 'IPv6 with only 3 words' );
68
69 $this->assertTrue( IP::isIPv6( 'fc:100::' ) );
70 $this->assertTrue( IP::isIPv6( 'fc:100:a::' ) );
71 $this->assertTrue( IP::isIPv6( 'fc:100:a:d::' ) );
72 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1::' ) );
73 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e::' ) );
74 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac::' ) );
75
76 $this->assertFalse( IP::isIPv6( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 with 8 words ending with "::"' );
77 $this->assertFalse(
78 IP::isIPv6( 'fc:100:a:d:1:e:ac:0:1::' ),
79 'IPv6 with 9 words ending with "::"'
80 );
81
82 $this->assertFalse( IP::isIPv6( ':::' ) );
83 $this->assertFalse( IP::isIPv6( '::0:' ), 'IPv6 ending in a lone ":"' );
84
85 $this->assertTrue( IP::isIPv6( '::' ), 'IPv6 zero address' );
86 $this->assertTrue( IP::isIPv6( '::0' ) );
87 $this->assertTrue( IP::isIPv6( '::fc' ) );
88 $this->assertTrue( IP::isIPv6( '::fc:100' ) );
89 $this->assertTrue( IP::isIPv6( '::fc:100:a' ) );
90 $this->assertTrue( IP::isIPv6( '::fc:100:a:d' ) );
91 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1' ) );
92 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e' ) );
93 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e:ac' ) );
94
95 $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
96 $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
97
98 $this->assertFalse( IP::isIPv6( ':fc::100' ), 'IPv6 starting with lone ":"' );
99 $this->assertFalse( IP::isIPv6( 'fc::100:' ), 'IPv6 ending with lone ":"' );
100 $this->assertFalse( IP::isIPv6( 'fc:::100' ), 'IPv6 with ":::" in the middle' );
101
102 $this->assertTrue( IP::isIPv6( 'fc::100' ), 'IPv6 with "::" and 2 words' );
103 $this->assertTrue( IP::isIPv6( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
104 $this->assertTrue( IP::isIPv6( 'fc::100:a:d' ), 'IPv6 with "::" and 4 words' );
105 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
106 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e' ), 'IPv6 with "::" and 6 words' );
107 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
108 $this->assertTrue( IP::isIPv6( '2001::df' ), 'IPv6 with "::" and 2 words' );
109 $this->assertTrue( IP::isIPv6( '2001:5c0:1400:a::df' ), 'IPv6 with "::" and 5 words' );
110 $this->assertTrue( IP::isIPv6( '2001:5c0:1400:a::df:2' ), 'IPv6 with "::" and 6 words' );
111
112 $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
113 $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
114
115 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac:0' ) );
116 }
117
118 /**
119 * @covers IP::isIPv4
120 * @dataProvider provideInvalidIPv4Addresses
121 */
122 public function testisNotIPv4( $bogusIP, $desc ) {
123 $this->assertFalse( IP::isIPv4( $bogusIP ), $desc );
124 }
125
126 public function provideInvalidIPv4Addresses() {
127 return [
128 [ false, 'Boolean false is not an IP' ],
129 [ true, 'Boolean true is not an IP' ],
130 [ '', 'Empty string is not an IP' ],
131 [ 'abc', 'Letters are not an IP' ],
132 [ ':', 'A colon is not an IP' ],
133 [ '124.24.52', 'IPv4 not enough quads' ],
134 [ '24.324.52.13', 'IPv4 out of range' ],
135 [ '.24.52.13', 'IPv4 starts with period' ],
136 ];
137 }
138
139 /**
140 * @covers IP::isIPv4
141 * @dataProvider provideValidIPv4Address
142 */
143 public function testIsIPv4( $ip, $desc ) {
144 $this->assertTrue( IP::isIPv4( $ip ), $desc );
145 }
146
147 /**
148 * Provide some IPv4 addresses and ranges
149 */
150 public function provideValidIPv4Address() {
151 return [
152 [ '124.24.52.13', 'Valid IPv4 address' ],
153 [ '1.24.52.13', 'Another valid IPv4 address' ],
154 [ '74.24.52.13/20', 'An IPv4 range' ],
155 ];
156 }
157
158 /**
159 * @covers IP::isValid
160 */
161 public function testValidIPs() {
162 foreach ( range( 0, 255 ) as $i ) {
163 $a = sprintf( "%03d", $i );
164 $b = sprintf( "%02d", $i );
165 $c = sprintf( "%01d", $i );
166 foreach ( array_unique( [ $a, $b, $c ] ) as $f ) {
167 $ip = "$f.$f.$f.$f";
168 $this->assertTrue( IP::isValid( $ip ), "$ip is a valid IPv4 address" );
169 }
170 }
171 foreach ( range( 0x0, 0xFFFF, 0xF ) as $i ) {
172 $a = sprintf( "%04x", $i );
173 $b = sprintf( "%03x", $i );
174 $c = sprintf( "%02x", $i );
175 foreach ( array_unique( [ $a, $b, $c ] ) as $f ) {
176 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
177 $this->assertTrue( IP::isValid( $ip ), "$ip is a valid IPv6 address" );
178 }
179 }
180 // test with some abbreviations
181 $this->assertFalse( IP::isValid( ':fc:100::' ), 'IPv6 starting with lone ":"' );
182 $this->assertFalse( IP::isValid( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
183 $this->assertFalse( IP::isValid( 'fc:300' ), 'IPv6 with only 2 words' );
184 $this->assertFalse( IP::isValid( 'fc:100:300' ), 'IPv6 with only 3 words' );
185
186 $this->assertTrue( IP::isValid( 'fc:100::' ) );
187 $this->assertTrue( IP::isValid( 'fc:100:a:d:1:e::' ) );
188 $this->assertTrue( IP::isValid( 'fc:100:a:d:1:e:ac::' ) );
189
190 $this->assertTrue( IP::isValid( 'fc::100' ), 'IPv6 with "::" and 2 words' );
191 $this->assertTrue( IP::isValid( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
192 $this->assertTrue( IP::isValid( '2001::df' ), 'IPv6 with "::" and 2 words' );
193 $this->assertTrue( IP::isValid( '2001:5c0:1400:a::df' ), 'IPv6 with "::" and 5 words' );
194 $this->assertTrue( IP::isValid( '2001:5c0:1400:a::df:2' ), 'IPv6 with "::" and 6 words' );
195 $this->assertTrue( IP::isValid( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
196 $this->assertTrue( IP::isValid( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
197
198 $this->assertFalse(
199 IP::isValid( 'fc:100:a:d:1:e:ac:0::' ),
200 'IPv6 with 8 words ending with "::"'
201 );
202 $this->assertFalse(
203 IP::isValid( 'fc:100:a:d:1:e:ac:0:1::' ),
204 'IPv6 with 9 words ending with "::"'
205 );
206 }
207
208 /**
209 * @covers IP::isValid
210 */
211 public function testInvalidIPs() {
212 // Out of range...
213 foreach ( range( 256, 999 ) as $i ) {
214 $a = sprintf( "%03d", $i );
215 $b = sprintf( "%02d", $i );
216 $c = sprintf( "%01d", $i );
217 foreach ( array_unique( [ $a, $b, $c ] ) as $f ) {
218 $ip = "$f.$f.$f.$f";
219 $this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv4 address" );
220 }
221 }
222 foreach ( range( 'g', 'z' ) as $i ) {
223 $a = sprintf( "%04s", $i );
224 $b = sprintf( "%03s", $i );
225 $c = sprintf( "%02s", $i );
226 foreach ( array_unique( [ $a, $b, $c ] ) as $f ) {
227 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
228 $this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv6 address" );
229 }
230 }
231 // Have CIDR
232 $ipCIDRs = [
233 '212.35.31.121/32',
234 '212.35.31.121/18',
235 '212.35.31.121/24',
236 '::ff:d:321:5/96',
237 'ff::d3:321:5/116',
238 'c:ff:12:1:ea:d:321:5/120',
239 ];
240 foreach ( $ipCIDRs as $i ) {
241 $this->assertFalse( IP::isValid( $i ),
242 "$i is an invalid IP address because it is a range" );
243 }
244 // Incomplete/garbage
245 $invalid = [
246 'www.xn--var-xla.net',
247 '216.17.184.G',
248 '216.17.184.1.',
249 '216.17.184',
250 '216.17.184.',
251 '256.17.184.1'
252 ];
253 foreach ( $invalid as $i ) {
254 $this->assertFalse( IP::isValid( $i ), "$i is an invalid IP address" );
255 }
256 }
257
258 /**
259 * Provide some valid IP ranges
260 */
261 public function provideValidRanges() {
262 return [
263 [ '116.17.184.5/32' ],
264 [ '0.17.184.5/30' ],
265 [ '16.17.184.1/24' ],
266 [ '30.242.52.14/1' ],
267 [ '10.232.52.13/8' ],
268 [ '30.242.52.14/0' ],
269 [ '::e:f:2001/96' ],
270 [ '::c:f:2001/128' ],
271 [ '::10:f:2001/70' ],
272 [ '::fe:f:2001/1' ],
273 [ '::6d:f:2001/8' ],
274 [ '::fe:f:2001/0' ],
275 ];
276 }
277
278 /**
279 * @covers IP::isValidRange
280 * @dataProvider provideValidRanges
281 */
282 public function testValidRanges( $range ) {
283 $this->assertTrue( IP::isValidRange( $range ), "$range is a valid IP range" );
284 }
285
286 /**
287 * @covers IP::isValidRange
288 * @dataProvider provideInvalidRanges
289 */
290 public function testInvalidRanges( $invalid ) {
291 $this->assertFalse( IP::isValidRange( $invalid ), "$invalid is not a valid IP range" );
292 }
293
294 public function provideInvalidRanges() {
295 return [
296 [ '116.17.184.5/33' ],
297 [ '0.17.184.5/130' ],
298 [ '16.17.184.1/-1' ],
299 [ '10.232.52.13/*' ],
300 [ '7.232.52.13/ab' ],
301 [ '11.232.52.13/' ],
302 [ '::e:f:2001/129' ],
303 [ '::c:f:2001/228' ],
304 [ '::10:f:2001/-1' ],
305 [ '::6d:f:2001/*' ],
306 [ '::86:f:2001/ab' ],
307 [ '::23:f:2001/' ],
308 ];
309 }
310
311 /**
312 * @covers IP::sanitizeIP
313 * @dataProvider provideSanitizeIP
314 */
315 public function testSanitizeIP( $expected, $input ) {
316 $result = IP::sanitizeIP( $input );
317 $this->assertEquals( $expected, $result );
318 }
319
320 /**
321 * Provider for IP::testSanitizeIP()
322 */
323 public static function provideSanitizeIP() {
324 return [
325 [ '0.0.0.0', '0.0.0.0' ],
326 [ '0.0.0.0', '00.00.00.00' ],
327 [ '0.0.0.0', '000.000.000.000' ],
328 [ '141.0.11.253', '141.000.011.253' ],
329 [ '1.2.4.5', '1.2.4.5' ],
330 [ '1.2.4.5', '01.02.04.05' ],
331 [ '1.2.4.5', '001.002.004.005' ],
332 [ '10.0.0.1', '010.0.000.1' ],
333 [ '80.72.250.4', '080.072.250.04' ],
334 [ 'Foo.1000.00', 'Foo.1000.00' ],
335 [ 'Bar.01', 'Bar.01' ],
336 [ 'Bar.010', 'Bar.010' ],
337 [ null, '' ],
338 [ null, ' ' ]
339 ];
340 }
341
342 /**
343 * @covers IP::toHex
344 * @dataProvider provideToHex
345 */
346 public function testToHex( $expected, $input ) {
347 $result = IP::toHex( $input );
348 $this->assertTrue( $result === false || is_string( $result ) );
349 $this->assertEquals( $expected, $result );
350 }
351
352 /**
353 * Provider for IP::testToHex()
354 */
355 public static function provideToHex() {
356 return [
357 [ '00000001', '0.0.0.1' ],
358 [ '01020304', '1.2.3.4' ],
359 [ '7F000001', '127.0.0.1' ],
360 [ '80000000', '128.0.0.0' ],
361 [ 'DEADCAFE', '222.173.202.254' ],
362 [ 'FFFFFFFF', '255.255.255.255' ],
363 [ '8D000BFD', '141.000.11.253' ],
364 [ false, 'IN.VA.LI.D' ],
365 [ 'v6-00000000000000000000000000000001', '::1' ],
366 [ 'v6-20010DB885A3000000008A2E03707334', '2001:0db8:85a3:0000:0000:8a2e:0370:7334' ],
367 [ 'v6-20010DB885A3000000008A2E03707334', '2001:db8:85a3::8a2e:0370:7334' ],
368 [ false, 'IN:VA::LI:D' ],
369 [ false, ':::1' ]
370 ];
371 }
372
373 /**
374 * @covers IP::isPublic
375 * @dataProvider provideIsPublic
376 */
377 public function testIsPublic( $expected, $input ) {
378 $result = IP::isPublic( $input );
379 $this->assertEquals( $expected, $result );
380 }
381
382 /**
383 * Provider for IP::testIsPublic()
384 */
385 public static function provideIsPublic() {
386 return [
387 [ false, 'fc00::3' ], # RFC 4193 (local)
388 [ false, 'fc00::ff' ], # RFC 4193 (local)
389 [ false, '127.1.2.3' ], # loopback
390 [ false, '::1' ], # loopback
391 [ false, 'fe80::1' ], # link-local
392 [ false, '169.254.1.1' ], # link-local
393 [ false, '10.0.0.1' ], # RFC 1918 (private)
394 [ false, '172.16.0.1' ], # RFC 1918 (private)
395 [ false, '192.168.0.1' ], # RFC 1918 (private)
396 [ true, '2001:5c0:1000:a::133' ], # public
397 [ true, 'fc::3' ], # public
398 [ true, '00FC::' ] # public
399 ];
400 }
401
402 // Private wrapper used to test CIDR Parsing.
403 private function assertFalseCIDR( $CIDR, $msg = '' ) {
404 $ff = [ false, false ];
405 $this->assertEquals( $ff, IP::parseCIDR( $CIDR ), $msg );
406 }
407
408 // Private wrapper to test network shifting using only dot notation
409 private function assertNet( $expected, $CIDR ) {
410 $parse = IP::parseCIDR( $CIDR );
411 $this->assertEquals( $expected, long2ip( $parse[0] ), "network shifting $CIDR" );
412 }
413
414 /**
415 * @covers IP::hexToQuad
416 * @dataProvider provideIPsAndHexes
417 */
418 public function testHexToQuad( $ip, $hex ) {
419 $this->assertEquals( $ip, IP::hexToQuad( $hex ) );
420 }
421
422 /**
423 * Provide some IP addresses and their equivalent hex representations
424 */
425 public function provideIPsandHexes() {
426 return [
427 [ '0.0.0.1', '00000001' ],
428 [ '255.0.0.0', 'FF000000' ],
429 [ '255.255.255.255', 'FFFFFFFF' ],
430 [ '10.188.222.255', '0ABCDEFF' ],
431 // hex not left-padded...
432 [ '0.0.0.0', '0' ],
433 [ '0.0.0.1', '1' ],
434 [ '0.0.0.255', 'FF' ],
435 [ '0.0.255.0', 'FF00' ],
436 ];
437 }
438
439 /**
440 * @covers IP::hexToOctet
441 * @dataProvider provideOctetsAndHexes
442 */
443 public function testHexToOctet( $octet, $hex ) {
444 $this->assertEquals( $octet, IP::hexToOctet( $hex ) );
445 }
446
447 /**
448 * Provide some hex and octet representations of the same IPs
449 */
450 public function provideOctetsAndHexes() {
451 return [
452 [ '0:0:0:0:0:0:0:1', '00000000000000000000000000000001' ],
453 [ '0:0:0:0:0:0:FF:3', '00000000000000000000000000FF0003' ],
454 [ '0:0:0:0:0:0:FF00:6', '000000000000000000000000FF000006' ],
455 [ '0:0:0:0:0:0:FCCF:FAFF', '000000000000000000000000FCCFFAFF' ],
456 [ 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF', 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' ],
457 // hex not left-padded...
458 [ '0:0:0:0:0:0:0:0', '0' ],
459 [ '0:0:0:0:0:0:0:1', '1' ],
460 [ '0:0:0:0:0:0:0:FF', 'FF' ],
461 [ '0:0:0:0:0:0:0:FFD0', 'FFD0' ],
462 [ '0:0:0:0:0:0:FA00:0', 'FA000000' ],
463 [ '0:0:0:0:0:0:FCCF:FAFF', 'FCCFFAFF' ],
464 ];
465 }
466
467 /**
468 * IP::parseCIDR() returns an array containing a signed IP address
469 * representing the network mask and the bit mask.
470 * @covers IP::parseCIDR
471 */
472 public function testCIDRParsing() {
473 $this->assertFalseCIDR( '192.0.2.0', "missing mask" );
474 $this->assertFalseCIDR( '192.0.2.0/', "missing bitmask" );
475
476 // Verify if statement
477 $this->assertFalseCIDR( '256.0.0.0/32', "invalid net" );
478 $this->assertFalseCIDR( '192.0.2.0/AA', "mask not numeric" );
479 $this->assertFalseCIDR( '192.0.2.0/-1', "mask < 0" );
480 $this->assertFalseCIDR( '192.0.2.0/33', "mask > 32" );
481
482 // Check internal logic
483 # 0 mask always result in array(0,0)
484 $this->assertEquals( [ 0, 0 ], IP::parseCIDR( '192.0.0.2/0' ) );
485 $this->assertEquals( [ 0, 0 ], IP::parseCIDR( '0.0.0.0/0' ) );
486 $this->assertEquals( [ 0, 0 ], IP::parseCIDR( '255.255.255.255/0' ) );
487
488 // @todo FIXME: Add more tests.
489
490 # This part test network shifting
491 $this->assertNet( '192.0.0.0', '192.0.0.2/24' );
492 $this->assertNet( '192.168.5.0', '192.168.5.13/24' );
493 $this->assertNet( '10.0.0.160', '10.0.0.161/28' );
494 $this->assertNet( '10.0.0.0', '10.0.0.3/28' );
495 $this->assertNet( '10.0.0.0', '10.0.0.3/30' );
496 $this->assertNet( '10.0.0.4', '10.0.0.4/30' );
497 $this->assertNet( '172.17.32.0', '172.17.35.48/21' );
498 $this->assertNet( '10.128.0.0', '10.135.0.0/9' );
499 $this->assertNet( '134.0.0.0', '134.0.5.1/8' );
500 }
501
502 /**
503 * @covers IP::canonicalize
504 */
505 public function testIPCanonicalizeOnValidIp() {
506 $this->assertEquals( '192.0.2.152', IP::canonicalize( '192.0.2.152' ),
507 'Canonicalization of a valid IP returns it unchanged' );
508 }
509
510 /**
511 * @covers IP::canonicalize
512 */
513 public function testIPCanonicalizeMappedAddress() {
514 $this->assertEquals(
515 '192.0.2.152',
516 IP::canonicalize( '::ffff:192.0.2.152' )
517 );
518 $this->assertEquals(
519 '192.0.2.152',
520 IP::canonicalize( '::192.0.2.152' )
521 );
522 }
523
524 /**
525 * Issues there are most probably from IP::toHex() or IP::parseRange()
526 * @covers IP::isInRange
527 * @dataProvider provideIPsAndRanges
528 */
529 public function testIPIsInRange( $expected, $addr, $range, $message = '' ) {
530 $this->assertEquals(
531 $expected,
532 IP::isInRange( $addr, $range ),
533 $message
534 );
535 }
536
537 /** Provider for testIPIsInRange() */
538 public static function provideIPsAndRanges() {
539 # Format: (expected boolean, address, range, optional message)
540 return [
541 # IPv4
542 [ true, '192.0.2.0', '192.0.2.0/24', 'Network address' ],
543 [ true, '192.0.2.77', '192.0.2.0/24', 'Simple address' ],
544 [ true, '192.0.2.255', '192.0.2.0/24', 'Broadcast address' ],
545
546 [ false, '0.0.0.0', '192.0.2.0/24' ],
547 [ false, '255.255.255', '192.0.2.0/24' ],
548
549 # IPv6
550 [ false, '::1', '2001:DB8::/32' ],
551 [ false, '::', '2001:DB8::/32' ],
552 [ false, 'FE80::1', '2001:DB8::/32' ],
553
554 [ true, '2001:DB8::', '2001:DB8::/32' ],
555 [ true, '2001:0DB8::', '2001:DB8::/32' ],
556 [ true, '2001:DB8::1', '2001:DB8::/32' ],
557 [ true, '2001:0DB8::1', '2001:DB8::/32' ],
558 [ true, '2001:0DB8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
559 '2001:DB8::/32' ],
560
561 [ false, '2001:0DB8:F::', '2001:DB8::/96' ],
562 ];
563 }
564
565 /**
566 * @covers IP::splitHostAndPort()
567 * @dataProvider provideSplitHostAndPort
568 */
569 public function testSplitHostAndPort( $expected, $input, $description ) {
570 $this->assertEquals( $expected, IP::splitHostAndPort( $input ), $description );
571 }
572
573 /**
574 * Provider for IP::splitHostAndPort()
575 */
576 public static function provideSplitHostAndPort() {
577 return [
578 [ false, '[', 'Unclosed square bracket' ],
579 [ false, '[::', 'Unclosed square bracket 2' ],
580 [ [ '::', false ], '::', 'Bare IPv6 0' ],
581 [ [ '::1', false ], '::1', 'Bare IPv6 1' ],
582 [ [ '::', false ], '[::]', 'Bracketed IPv6 0' ],
583 [ [ '::1', false ], '[::1]', 'Bracketed IPv6 1' ],
584 [ [ '::1', 80 ], '[::1]:80', 'Bracketed IPv6 with port' ],
585 [ false, '::x', 'Double colon but no IPv6' ],
586 [ [ 'x', 80 ], 'x:80', 'Hostname and port' ],
587 [ false, 'x:x', 'Hostname and invalid port' ],
588 [ [ 'x', false ], 'x', 'Plain hostname' ]
589 ];
590 }
591
592 /**
593 * @covers IP::combineHostAndPort()
594 * @dataProvider provideCombineHostAndPort
595 */
596 public function testCombineHostAndPort( $expected, $input, $description ) {
597 list( $host, $port, $defaultPort ) = $input;
598 $this->assertEquals(
599 $expected,
600 IP::combineHostAndPort( $host, $port, $defaultPort ),
601 $description );
602 }
603
604 /**
605 * Provider for IP::combineHostAndPort()
606 */
607 public static function provideCombineHostAndPort() {
608 return [
609 [ '[::1]', [ '::1', 2, 2 ], 'IPv6 default port' ],
610 [ '[::1]:2', [ '::1', 2, 3 ], 'IPv6 non-default port' ],
611 [ 'x', [ 'x', 2, 2 ], 'Normal default port' ],
612 [ 'x:2', [ 'x', 2, 3 ], 'Normal non-default port' ],
613 ];
614 }
615
616 /**
617 * @covers IP::sanitizeRange()
618 * @dataProvider provideIPCIDRs
619 */
620 public function testSanitizeRange( $input, $expected, $description ) {
621 $this->assertEquals( $expected, IP::sanitizeRange( $input ), $description );
622 }
623
624 /**
625 * Provider for IP::testSanitizeRange()
626 */
627 public static function provideIPCIDRs() {
628 return [
629 [ '35.56.31.252/16', '35.56.0.0/16', 'IPv4 range' ],
630 [ '135.16.21.252/24', '135.16.21.0/24', 'IPv4 range' ],
631 [ '5.36.71.252/32', '5.36.71.252/32', 'IPv4 silly range' ],
632 [ '5.36.71.252', '5.36.71.252', 'IPv4 non-range' ],
633 [ '0:1:2:3:4:c5:f6:7/96', '0:1:2:3:4:C5:0:0/96', 'IPv6 range' ],
634 [ '0:1:2:3:4:5:6:7/120', '0:1:2:3:4:5:6:0/120', 'IPv6 range' ],
635 [ '0:e1:2:3:4:5:e6:7/128', '0:E1:2:3:4:5:E6:7/128', 'IPv6 silly range' ],
636 [ '0:c1:A2:3:4:5:c6:7', '0:C1:A2:3:4:5:C6:7', 'IPv6 non range' ],
637 ];
638 }
639
640 /**
641 * @covers IP::prettifyIP()
642 * @dataProvider provideIPsToPrettify
643 */
644 public function testPrettifyIP( $ip, $prettified ) {
645 $this->assertEquals( $prettified, IP::prettifyIP( $ip ), "Prettify of $ip" );
646 }
647
648 /**
649 * Provider for IP::testPrettifyIP()
650 */
651 public static function provideIPsToPrettify() {
652 return [
653 [ '0:0:0:0:0:0:0:0', '::' ],
654 [ '0:0:0::0:0:0', '::' ],
655 [ '0:0:0:1:0:0:0:0', '0:0:0:1::' ],
656 [ '0:0::f', '::f' ],
657 [ '0::0:0:0:33:fef:b', '::33:fef:b' ],
658 [ '3f:535:0:0:0:0:e:fbb', '3f:535::e:fbb' ],
659 [ '0:0:fef:0:0:0:e:fbb', '0:0:fef::e:fbb' ],
660 [ 'abbc:2004::0:0:0:0', 'abbc:2004::' ],
661 [ 'cebc:2004:f:0:0:0:0:0', 'cebc:2004:f::' ],
662 [ '0:0:0:0:0:0:0:0/16', '::/16' ],
663 [ '0:0:0::0:0:0/64', '::/64' ],
664 [ '0:0::f/52', '::f/52' ],
665 [ '::0:0:33:fef:b/52', '::33:fef:b/52' ],
666 [ '3f:535:0:0:0:0:e:fbb/48', '3f:535::e:fbb/48' ],
667 [ '0:0:fef:0:0:0:e:fbb/96', '0:0:fef::e:fbb/96' ],
668 [ 'abbc:2004:0:0::0:0/40', 'abbc:2004::/40' ],
669 [ 'aebc:2004:f:0:0:0:0:0/80', 'aebc:2004:f::/80' ],
670 ];
671 }
672 }