Merge "(bug 44219) Avoid fatal errors when a revision doesn't exist in action=info"
[lhc/web/wiklou.git] / tests / phpunit / includes / IPTest.php
1 <?php
2 /**
3 * Tests for IP validity functions. Ported from /t/inc/IP.t by avar.
4 * @group IP
5 */
6
7 class IPTest extends MediaWikiTestCase {
8 /**
9 * not sure it should be tested with boolean false. hashar 20100924
10 * @covers IP::isIPAddress
11 */
12 public function testisIPAddress() {
13 $this->assertFalse( IP::isIPAddress( false ), 'Boolean false is not an IP' );
14 $this->assertFalse( IP::isIPAddress( true ), 'Boolean true is not an IP' );
15 $this->assertFalse( IP::isIPAddress( "" ), 'Empty string is not an IP' );
16 $this->assertFalse( IP::isIPAddress( 'abc' ), 'Garbage IP string' );
17 $this->assertFalse( IP::isIPAddress( ':' ), 'Single ":" is not an IP' );
18 $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::1' ), 'IPv6 with a double :: occurrence' );
19 $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::' ), 'IPv6 with a double :: occurrence, last at end' );
20 $this->assertFalse( IP::isIPAddress( '::2001:0DB8::5:1' ), 'IPv6 with a double :: occurrence, firt at beginning' );
21 $this->assertFalse( IP::isIPAddress( '124.24.52' ), 'IPv4 not enough quads' );
22 $this->assertFalse( IP::isIPAddress( '24.324.52.13' ), 'IPv4 out of range' );
23 $this->assertFalse( IP::isIPAddress( '.24.52.13' ), 'IPv4 starts with period' );
24 $this->assertFalse( IP::isIPAddress( 'fc:100:300' ), 'IPv6 with only 3 words' );
25
26 $this->assertTrue( IP::isIPAddress( '::' ), 'RFC 4291 IPv6 Unspecified Address' );
27 $this->assertTrue( IP::isIPAddress( '::1' ), 'RFC 4291 IPv6 Loopback Address' );
28 $this->assertTrue( IP::isIPAddress( '74.24.52.13/20', 'IPv4 range' ) );
29 $this->assertTrue( IP::isIPAddress( 'fc:100:a:d:1:e:ac:0/24' ), 'IPv6 range' );
30 $this->assertTrue( IP::isIPAddress( 'fc::100:a:d:1:e:ac/96' ), 'IPv6 range with "::"' );
31
32 $validIPs = array( 'fc:100::', 'fc:100:a:d:1:e:ac::', 'fc::100', '::fc:100:a:d:1:e:ac',
33 '::fc', 'fc::100:a:d:1:e:ac', 'fc:100:a:d:1:e:ac:0', '124.24.52.13', '1.24.52.13' );
34 foreach ( $validIPs as $ip ) {
35 $this->assertTrue( IP::isIPAddress( $ip ), "$ip is a valid IP address" );
36 }
37 }
38
39 /**
40 * @covers IP::isIPv6
41 */
42 public function testisIPv6() {
43 $this->assertFalse( IP::isIPv6( ':fc:100::' ), 'IPv6 starting with lone ":"' );
44 $this->assertFalse( IP::isIPv6( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
45 $this->assertFalse( IP::isIPv6( 'fc:300' ), 'IPv6 with only 2 words' );
46 $this->assertFalse( IP::isIPv6( 'fc:100:300' ), 'IPv6 with only 3 words' );
47
48 $this->assertTrue( IP::isIPv6( 'fc:100::' ) );
49 $this->assertTrue( IP::isIPv6( 'fc:100:a::' ) );
50 $this->assertTrue( IP::isIPv6( 'fc:100:a:d::' ) );
51 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1::' ) );
52 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e::' ) );
53 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac::' ) );
54
55 $this->assertFalse( IP::isIPv6( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 with 8 words ending with "::"' );
56 $this->assertFalse( IP::isIPv6( 'fc:100:a:d:1:e:ac:0:1::' ), 'IPv6 with 9 words ending with "::"' );
57
58 $this->assertFalse( IP::isIPv6( ':::' ) );
59 $this->assertFalse( IP::isIPv6( '::0:' ), 'IPv6 ending in a lone ":"' );
60
61 $this->assertTrue( IP::isIPv6( '::' ), 'IPv6 zero address' );
62 $this->assertTrue( IP::isIPv6( '::0' ) );
63 $this->assertTrue( IP::isIPv6( '::fc' ) );
64 $this->assertTrue( IP::isIPv6( '::fc:100' ) );
65 $this->assertTrue( IP::isIPv6( '::fc:100:a' ) );
66 $this->assertTrue( IP::isIPv6( '::fc:100:a:d' ) );
67 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1' ) );
68 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e' ) );
69 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e:ac' ) );
70
71 $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
72 $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
73
74 $this->assertFalse( IP::isIPv6( ':fc::100' ), 'IPv6 starting with lone ":"' );
75 $this->assertFalse( IP::isIPv6( 'fc::100:' ), 'IPv6 ending with lone ":"' );
76 $this->assertFalse( IP::isIPv6( 'fc:::100' ), 'IPv6 with ":::" in the middle' );
77
78 $this->assertTrue( IP::isIPv6( 'fc::100' ), 'IPv6 with "::" and 2 words' );
79 $this->assertTrue( IP::isIPv6( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
80 $this->assertTrue( IP::isIPv6( 'fc::100:a:d', 'IPv6 with "::" and 4 words' ) );
81 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
82 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e' ), 'IPv6 with "::" and 6 words' );
83 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
84 $this->assertTrue( IP::isIPv6( '2001::df' ), 'IPv6 with "::" and 2 words' );
85 $this->assertTrue( IP::isIPv6( '2001:5c0:1400:a::df' ), 'IPv6 with "::" and 5 words' );
86 $this->assertTrue( IP::isIPv6( '2001:5c0:1400:a::df:2' ), 'IPv6 with "::" and 6 words' );
87
88 $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
89 $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
90
91 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac:0' ) );
92 }
93
94 /**
95 * @covers IP::isIPv4
96 */
97 public function testisIPv4() {
98 $this->assertFalse( IP::isIPv4( false ), 'Boolean false is not an IP' );
99 $this->assertFalse( IP::isIPv4( true ), 'Boolean true is not an IP' );
100 $this->assertFalse( IP::isIPv4( "" ), 'Empty string is not an IP' );
101 $this->assertFalse( IP::isIPv4( 'abc' ) );
102 $this->assertFalse( IP::isIPv4( ':' ) );
103 $this->assertFalse( IP::isIPv4( '124.24.52' ), 'IPv4 not enough quads' );
104 $this->assertFalse( IP::isIPv4( '24.324.52.13' ), 'IPv4 out of range' );
105 $this->assertFalse( IP::isIPv4( '.24.52.13' ), 'IPv4 starts with period' );
106
107 $this->assertTrue( IP::isIPv4( '124.24.52.13' ) );
108 $this->assertTrue( IP::isIPv4( '1.24.52.13' ) );
109 $this->assertTrue( IP::isIPv4( '74.24.52.13/20', 'IPv4 range' ) );
110 }
111
112 /**
113 * @covers IP::isValid
114 */
115 public function testValidIPs() {
116 foreach ( range( 0, 255 ) as $i ) {
117 $a = sprintf( "%03d", $i );
118 $b = sprintf( "%02d", $i );
119 $c = sprintf( "%01d", $i );
120 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
121 $ip = "$f.$f.$f.$f";
122 $this->assertTrue( IP::isValid( $ip ), "$ip is a valid IPv4 address" );
123 }
124 }
125 foreach ( range( 0x0, 0xFFFF, 0xF ) as $i ) {
126 $a = sprintf( "%04x", $i );
127 $b = sprintf( "%03x", $i );
128 $c = sprintf( "%02x", $i );
129 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
130 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
131 $this->assertTrue( IP::isValid( $ip ), "$ip is a valid IPv6 address" );
132 }
133 }
134 // test with some abbreviations
135 $this->assertFalse( IP::isValid( ':fc:100::' ), 'IPv6 starting with lone ":"' );
136 $this->assertFalse( IP::isValid( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
137 $this->assertFalse( IP::isValid( 'fc:300' ), 'IPv6 with only 2 words' );
138 $this->assertFalse( IP::isValid( 'fc:100:300' ), 'IPv6 with only 3 words' );
139
140 $this->assertTrue( IP::isValid( 'fc:100::' ) );
141 $this->assertTrue( IP::isValid( 'fc:100:a:d:1:e::' ) );
142 $this->assertTrue( IP::isValid( 'fc:100:a:d:1:e:ac::' ) );
143
144 $this->assertTrue( IP::isValid( 'fc::100' ), 'IPv6 with "::" and 2 words' );
145 $this->assertTrue( IP::isValid( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
146 $this->assertTrue( IP::isValid( '2001::df' ), 'IPv6 with "::" and 2 words' );
147 $this->assertTrue( IP::isValid( '2001:5c0:1400:a::df' ), 'IPv6 with "::" and 5 words' );
148 $this->assertTrue( IP::isValid( '2001:5c0:1400:a::df:2' ), 'IPv6 with "::" and 6 words' );
149 $this->assertTrue( IP::isValid( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
150 $this->assertTrue( IP::isValid( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
151
152 $this->assertFalse( IP::isValid( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 with 8 words ending with "::"' );
153 $this->assertFalse( IP::isValid( 'fc:100:a:d:1:e:ac:0:1::' ), 'IPv6 with 9 words ending with "::"' );
154 }
155
156 /**
157 * @covers IP::isValid
158 */
159 public function testInvalidIPs() {
160 // Out of range...
161 foreach ( range( 256, 999 ) as $i ) {
162 $a = sprintf( "%03d", $i );
163 $b = sprintf( "%02d", $i );
164 $c = sprintf( "%01d", $i );
165 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
166 $ip = "$f.$f.$f.$f";
167 $this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv4 address" );
168 }
169 }
170 foreach ( range( 'g', 'z' ) as $i ) {
171 $a = sprintf( "%04s", $i );
172 $b = sprintf( "%03s", $i );
173 $c = sprintf( "%02s", $i );
174 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
175 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
176 $this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv6 address" );
177 }
178 }
179 // Have CIDR
180 $ipCIDRs = array(
181 '212.35.31.121/32',
182 '212.35.31.121/18',
183 '212.35.31.121/24',
184 '::ff:d:321:5/96',
185 'ff::d3:321:5/116',
186 'c:ff:12:1:ea:d:321:5/120',
187 );
188 foreach ( $ipCIDRs as $i ) {
189 $this->assertFalse( IP::isValid( $i ),
190 "$i is an invalid IP address because it is a block" );
191 }
192 // Incomplete/garbage
193 $invalid = array(
194 'www.xn--var-xla.net',
195 '216.17.184.G',
196 '216.17.184.1.',
197 '216.17.184',
198 '216.17.184.',
199 '256.17.184.1'
200 );
201 foreach ( $invalid as $i ) {
202 $this->assertFalse( IP::isValid( $i ), "$i is an invalid IP address" );
203 }
204 }
205
206 /**
207 * @covers IP::isValidBlock
208 */
209 public function testValidBlocks() {
210 $valid = array(
211 '116.17.184.5/32',
212 '0.17.184.5/30',
213 '16.17.184.1/24',
214 '30.242.52.14/1',
215 '10.232.52.13/8',
216 '30.242.52.14/0',
217 '::e:f:2001/96',
218 '::c:f:2001/128',
219 '::10:f:2001/70',
220 '::fe:f:2001/1',
221 '::6d:f:2001/8',
222 '::fe:f:2001/0',
223 );
224 foreach ( $valid as $i ) {
225 $this->assertTrue( IP::isValidBlock( $i ), "$i is a valid IP block" );
226 }
227 }
228
229 /**
230 * @covers IP::isValidBlock
231 */
232 public function testInvalidBlocks() {
233 $invalid = array(
234 '116.17.184.5/33',
235 '0.17.184.5/130',
236 '16.17.184.1/-1',
237 '10.232.52.13/*',
238 '7.232.52.13/ab',
239 '11.232.52.13/',
240 '::e:f:2001/129',
241 '::c:f:2001/228',
242 '::10:f:2001/-1',
243 '::6d:f:2001/*',
244 '::86:f:2001/ab',
245 '::23:f:2001/',
246 );
247 foreach ( $invalid as $i ) {
248 $this->assertFalse( IP::isValidBlock( $i ), "$i is not a valid IP block" );
249 }
250 }
251
252 /**
253 * Improve IP::sanitizeIP() code coverage
254 * @todo Most probably incomplete
255 */
256 public function testSanitizeIP() {
257 $this->assertNull( IP::sanitizeIP( '' ) );
258 $this->assertNull( IP::sanitizeIP( ' ' ) );
259 }
260
261 /**
262 * test wrapper around ip2long which might return -1 or false depending on PHP version
263 * @covers IP::toUnsigned
264 */
265 public function testip2longWrapper() {
266 // @todo FIXME: Add more tests ?
267 $this->assertEquals( pow( 2, 32 ) - 1, IP::toUnsigned( '255.255.255.255' ) );
268 $i = 'IN.VA.LI.D';
269 $this->assertFalse( IP::toUnSigned( $i ) );
270 }
271
272 /**
273 * @covers IP::isPublic
274 */
275 public function testPrivateIPs() {
276 $private = array( 'fc00::3', 'fc00::ff', '::1', '10.0.0.1', '172.16.0.1', '192.168.0.1' );
277 foreach ( $private as $p ) {
278 $this->assertFalse( IP::isPublic( $p ), "$p is not a public IP address" );
279 }
280 $public = array( '2001:5c0:1000:a::133', 'fc::3', '00FC::' );
281 foreach ( $public as $p ) {
282 $this->assertTrue( IP::isPublic( $p ), "$p is a public IP address" );
283 }
284 }
285
286 // Private wrapper used to test CIDR Parsing.
287 private function assertFalseCIDR( $CIDR, $msg = '' ) {
288 $ff = array( false, false );
289 $this->assertEquals( $ff, IP::parseCIDR( $CIDR ), $msg );
290 }
291
292 // Private wrapper to test network shifting using only dot notation
293 private function assertNet( $expected, $CIDR ) {
294 $parse = IP::parseCIDR( $CIDR );
295 $this->assertEquals( $expected, long2ip( $parse[0] ), "network shifting $CIDR" );
296 }
297
298 /**
299 * @covers IP::hexToQuad
300 */
301 public function testHexToQuad() {
302 $this->assertEquals( '0.0.0.1', IP::hexToQuad( '00000001' ) );
303 $this->assertEquals( '255.0.0.0', IP::hexToQuad( 'FF000000' ) );
304 $this->assertEquals( '255.255.255.255', IP::hexToQuad( 'FFFFFFFF' ) );
305 $this->assertEquals( '10.188.222.255', IP::hexToQuad( '0ABCDEFF' ) );
306 // hex not left-padded...
307 $this->assertEquals( '0.0.0.0', IP::hexToQuad( '0' ) );
308 $this->assertEquals( '0.0.0.1', IP::hexToQuad( '1' ) );
309 $this->assertEquals( '0.0.0.255', IP::hexToQuad( 'FF' ) );
310 $this->assertEquals( '0.0.255.0', IP::hexToQuad( 'FF00' ) );
311 }
312
313 /**
314 * @covers IP::hexToOctet
315 */
316 public function testHexToOctet() {
317 $this->assertEquals( '0:0:0:0:0:0:0:1',
318 IP::hexToOctet( '00000000000000000000000000000001' ) );
319 $this->assertEquals( '0:0:0:0:0:0:FF:3',
320 IP::hexToOctet( '00000000000000000000000000FF0003' ) );
321 $this->assertEquals( '0:0:0:0:0:0:FF00:6',
322 IP::hexToOctet( '000000000000000000000000FF000006' ) );
323 $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF',
324 IP::hexToOctet( '000000000000000000000000FCCFFAFF' ) );
325 $this->assertEquals( 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
326 IP::hexToOctet( 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' ) );
327 // hex not left-padded...
328 $this->assertEquals( '0:0:0:0:0:0:0:0', IP::hexToOctet( '0' ) );
329 $this->assertEquals( '0:0:0:0:0:0:0:1', IP::hexToOctet( '1' ) );
330 $this->assertEquals( '0:0:0:0:0:0:0:FF', IP::hexToOctet( 'FF' ) );
331 $this->assertEquals( '0:0:0:0:0:0:0:FFD0', IP::hexToOctet( 'FFD0' ) );
332 $this->assertEquals( '0:0:0:0:0:0:FA00:0', IP::hexToOctet( 'FA000000' ) );
333 $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF', IP::hexToOctet( 'FCCFFAFF' ) );
334 }
335
336 /**
337 * IP::parseCIDR() returns an array containing a signed IP address
338 * representing the network mask and the bit mask.
339 * @covers IP::parseCIDR
340 */
341 function testCIDRParsing() {
342 $this->assertFalseCIDR( '192.0.2.0', "missing mask" );
343 $this->assertFalseCIDR( '192.0.2.0/', "missing bitmask" );
344
345 // Verify if statement
346 $this->assertFalseCIDR( '256.0.0.0/32', "invalid net" );
347 $this->assertFalseCIDR( '192.0.2.0/AA', "mask not numeric" );
348 $this->assertFalseCIDR( '192.0.2.0/-1', "mask < 0" );
349 $this->assertFalseCIDR( '192.0.2.0/33', "mask > 32" );
350
351 // Check internal logic
352 # 0 mask always result in array(0,0)
353 $this->assertEquals( array( 0, 0 ), IP::parseCIDR( '192.0.0.2/0' ) );
354 $this->assertEquals( array( 0, 0 ), IP::parseCIDR( '0.0.0.0/0' ) );
355 $this->assertEquals( array( 0, 0 ), IP::parseCIDR( '255.255.255.255/0' ) );
356
357 // @todo FIXME: Add more tests.
358
359 # This part test network shifting
360 $this->assertNet( '192.0.0.0', '192.0.0.2/24' );
361 $this->assertNet( '192.168.5.0', '192.168.5.13/24' );
362 $this->assertNet( '10.0.0.160', '10.0.0.161/28' );
363 $this->assertNet( '10.0.0.0', '10.0.0.3/28' );
364 $this->assertNet( '10.0.0.0', '10.0.0.3/30' );
365 $this->assertNet( '10.0.0.4', '10.0.0.4/30' );
366 $this->assertNet( '172.17.32.0', '172.17.35.48/21' );
367 $this->assertNet( '10.128.0.0', '10.135.0.0/9' );
368 $this->assertNet( '134.0.0.0', '134.0.5.1/8' );
369 }
370
371 /**
372 * @covers IP::canonicalize
373 */
374 public function testIPCanonicalizeOnValidIp() {
375 $this->assertEquals( '192.0.2.152', IP::canonicalize( '192.0.2.152' ),
376 'Canonicalization of a valid IP returns it unchanged' );
377 }
378
379 /**
380 * @covers IP::canonicalize
381 */
382 public function testIPCanonicalizeMappedAddress() {
383 $this->assertEquals(
384 '192.0.2.152',
385 IP::canonicalize( '::ffff:192.0.2.152' )
386 );
387 $this->assertEquals(
388 '192.0.2.152',
389 IP::canonicalize( '::192.0.2.152' )
390 );
391 }
392
393 /**
394 * Issues there are most probably from IP::toHex() or IP::parseRange()
395 * @covers IP::isInRange
396 * @dataProvider provideIPsAndRanges
397 */
398 public function testIPIsInRange( $expected, $addr, $range, $message = '' ) {
399 $this->assertEquals(
400 $expected,
401 IP::isInRange( $addr, $range ),
402 $message
403 );
404 }
405
406 /** Provider for testIPIsInRange() */
407 public static function provideIPsAndRanges() {
408 # Format: (expected boolean, address, range, optional message)
409 return array(
410 # IPv4
411 array( true, '192.0.2.0', '192.0.2.0/24', 'Network address' ),
412 array( true, '192.0.2.77', '192.0.2.0/24', 'Simple address' ),
413 array( true, '192.0.2.255', '192.0.2.0/24', 'Broadcast address' ),
414
415 array( false, '0.0.0.0', '192.0.2.0/24' ),
416 array( false, '255.255.255', '192.0.2.0/24' ),
417
418 # IPv6
419 array( false, '::1', '2001:DB8::/32' ),
420 array( false, '::', '2001:DB8::/32' ),
421 array( false, 'FE80::1', '2001:DB8::/32' ),
422
423 array( true, '2001:DB8::', '2001:DB8::/32' ),
424 array( true, '2001:0DB8::', '2001:DB8::/32' ),
425 array( true, '2001:DB8::1', '2001:DB8::/32' ),
426 array( true, '2001:0DB8::1', '2001:DB8::/32' ),
427 array( true, '2001:0DB8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
428 '2001:DB8::/32' ),
429
430 array( false, '2001:0DB8:F::', '2001:DB8::/96' ),
431 );
432 }
433
434 /**
435 * Test for IP::splitHostAndPort().
436 * @dataProvider provideSplitHostAndPort
437 */
438 function testSplitHostAndPort( $expected, $input, $description ) {
439 $this->assertEquals( $expected, IP::splitHostAndPort( $input ), $description );
440 }
441
442 /**
443 * Provider for IP::splitHostAndPort()
444 */
445 public static function provideSplitHostAndPort() {
446 return array(
447 array( false, '[', 'Unclosed square bracket' ),
448 array( false, '[::', 'Unclosed square bracket 2' ),
449 array( array( '::', false ), '::', 'Bare IPv6 0' ),
450 array( array( '::1', false ), '::1', 'Bare IPv6 1' ),
451 array( array( '::', false ), '[::]', 'Bracketed IPv6 0' ),
452 array( array( '::1', false ), '[::1]', 'Bracketed IPv6 1' ),
453 array( array( '::1', 80 ), '[::1]:80', 'Bracketed IPv6 with port' ),
454 array( false, '::x', 'Double colon but no IPv6' ),
455 array( array( 'x', 80 ), 'x:80', 'Hostname and port' ),
456 array( false, 'x:x', 'Hostname and invalid port' ),
457 array( array( 'x', false ), 'x', 'Plain hostname' )
458 );
459 }
460
461 /**
462 * Test for IP::combineHostAndPort()
463 * @dataProvider provideCombineHostAndPort
464 */
465 function testCombineHostAndPort( $expected, $input, $description ) {
466 list( $host, $port, $defaultPort ) = $input;
467 $this->assertEquals(
468 $expected,
469 IP::combineHostAndPort( $host, $port, $defaultPort ),
470 $description );
471 }
472
473 /**
474 * Provider for IP::combineHostAndPort()
475 */
476 public static function provideCombineHostAndPort() {
477 return array(
478 array( '[::1]', array( '::1', 2, 2 ), 'IPv6 default port' ),
479 array( '[::1]:2', array( '::1', 2, 3 ), 'IPv6 non-default port' ),
480 array( 'x', array( 'x', 2, 2 ), 'Normal default port' ),
481 array( 'x:2', array( 'x', 2, 3 ), 'Normal non-default port' ),
482 );
483 }
484
485 /**
486 * Test for IP::sanitizeRange()
487 * @dataProvider provideIPCIDRs
488 */
489 function testSanitizeRange( $input, $expected, $description ) {
490 $this->assertEquals( $expected, IP::sanitizeRange( $input ), $description );
491 }
492
493 /**
494 * Provider for IP::testSanitizeRange()
495 */
496 public static function provideIPCIDRs() {
497 return array(
498 array( '35.56.31.252/16', '35.56.0.0/16', 'IPv4 range' ),
499 array( '135.16.21.252/24', '135.16.21.0/24', 'IPv4 range' ),
500 array( '5.36.71.252/32', '5.36.71.252/32', 'IPv4 silly range' ),
501 array( '5.36.71.252', '5.36.71.252', 'IPv4 non-range' ),
502 array( '0:1:2:3:4:c5:f6:7/96', '0:1:2:3:4:C5:0:0/96', 'IPv6 range' ),
503 array( '0:1:2:3:4:5:6:7/120', '0:1:2:3:4:5:6:0/120', 'IPv6 range' ),
504 array( '0:e1:2:3:4:5:e6:7/128', '0:E1:2:3:4:5:E6:7/128', 'IPv6 silly range' ),
505 array( '0:c1:A2:3:4:5:c6:7', '0:C1:A2:3:4:5:C6:7', 'IPv6 non range' ),
506 );
507 }
508
509 /**
510 * Test for IP::prettifyIP()
511 * @dataProvider provideIPsToPrettify
512 */
513 function testPrettifyIP( $ip, $prettified ) {
514 $this->assertEquals( $prettified, IP::prettifyIP( $ip ), "Prettify of $ip" );
515 }
516
517 /**
518 * Provider for IP::testPrettifyIP()
519 */
520 public static function provideIPsToPrettify() {
521 return array(
522 array( '0:0:0:0:0:0:0:0', '::' ),
523 array( '0:0:0::0:0:0', '::' ),
524 array( '0:0:0:1:0:0:0:0', '0:0:0:1::' ),
525 array( '0:0::f', '::f' ),
526 array( '0::0:0:0:33:fef:b', '::33:fef:b' ),
527 array( '3f:535:0:0:0:0:e:fbb', '3f:535::e:fbb' ),
528 array( '0:0:fef:0:0:0:e:fbb', '0:0:fef::e:fbb' ),
529 array( 'abbc:2004::0:0:0:0', 'abbc:2004::' ),
530 array( 'cebc:2004:f:0:0:0:0:0', 'cebc:2004:f::' ),
531 array( '0:0:0:0:0:0:0:0/16', '::/16' ),
532 array( '0:0:0::0:0:0/64', '::/64' ),
533 array( '0:0::f/52', '::f/52' ),
534 array( '::0:0:33:fef:b/52', '::33:fef:b/52' ),
535 array( '3f:535:0:0:0:0:e:fbb/48', '3f:535::e:fbb/48' ),
536 array( '0:0:fef:0:0:0:e:fbb/96', '0:0:fef::e:fbb/96' ),
537 array( 'abbc:2004:0:0::0:0/40', 'abbc:2004::/40' ),
538 array( 'aebc:2004:f:0:0:0:0:0/80', 'aebc:2004:f::/80' ),
539 );
540 }
541 }