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