Fix comment blocks that start /* to /**
[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 */
5
6 class IPTest extends MediaWikiTestCase {
7 /**
8 * not sure it should be tested with boolean false. hashar 20100924
9 * @covers IP::isIPAddress
10 */
11 public function testisIPAddress() {
12 $this->assertFalse( IP::isIPAddress( false ), 'Boolean false is not an IP' );
13 $this->assertFalse( IP::isIPAddress( true ), 'Boolean true is not an IP' );
14 $this->assertFalse( IP::isIPAddress( "" ), 'Empty string is not an IP' );
15 $this->assertFalse( IP::isIPAddress( 'abc' ), 'Garbage IP string' );
16 $this->assertFalse( IP::isIPAddress( ':' ), 'Single ":" is not an IP' );
17 $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::1'), 'IPv6 with a double :: occurence' );
18 $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::'), 'IPv6 with a double :: occurence, last at end' );
19 $this->assertFalse( IP::isIPAddress( '::2001:0DB8::5:1'), 'IPv6 with a double :: occurence, firt at beginning' );
20 $this->assertFalse( IP::isIPAddress( '124.24.52' ), 'IPv4 not enough quads' );
21 $this->assertFalse( IP::isIPAddress( '24.324.52.13' ), 'IPv4 out of range' );
22 $this->assertFalse( IP::isIPAddress( '.24.52.13' ), 'IPv4 starts with period' );
23 $this->assertFalse( IP::isIPAddress( 'fc:100:300' ), 'IPv6 with only 3 words' );
24
25 $this->assertTrue( IP::isIPAddress( '::' ), 'RFC 4291 IPv6 Unspecified Address' );
26 $this->assertTrue( IP::isIPAddress( '::1' ), 'RFC 4291 IPv6 Loopback Address' );
27 $this->assertTrue( IP::isIPAddress( '74.24.52.13/20', 'IPv4 range' ) );
28 $this->assertTrue( IP::isIPAddress( 'fc:100:a:d:1:e:ac:0/24' ), 'IPv6 range' );
29 $this->assertTrue( IP::isIPAddress( 'fc::100:a:d:1:e:ac/96' ), 'IPv6 range with "::"' );
30
31 $validIPs = array( 'fc:100::', 'fc:100:a:d:1:e:ac::', 'fc::100', '::fc:100:a:d:1:e:ac',
32 '::fc', 'fc::100:a:d:1:e:ac', 'fc:100:a:d:1:e:ac:0', '124.24.52.13', '1.24.52.13' );
33 foreach ( $validIPs as $ip ) {
34 $this->assertTrue( IP::isIPAddress( $ip ), "$ip is a valid IP address" );
35 }
36 }
37
38 /**
39 * @covers IP::isIPv6
40 */
41 public function testisIPv6() {
42 $this->assertFalse( IP::isIPv6( ':fc:100::' ), 'IPv6 starting with lone ":"' );
43 $this->assertFalse( IP::isIPv6( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
44 $this->assertFalse( IP::isIPv6( 'fc:300' ), 'IPv6 with only 2 words' );
45 $this->assertFalse( IP::isIPv6( 'fc:100:300' ), 'IPv6 with only 3 words' );
46
47 $this->assertTrue( IP::isIPv6( 'fc:100::' ) );
48 $this->assertTrue( IP::isIPv6( 'fc:100:a::' ) );
49 $this->assertTrue( IP::isIPv6( 'fc:100:a:d::' ) );
50 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1::' ) );
51 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e::' ) );
52 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac::' ) );
53
54 $this->assertFalse( IP::isIPv6( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 with 8 words ending with "::"' );
55 $this->assertFalse( IP::isIPv6( 'fc:100:a:d:1:e:ac:0:1::' ), 'IPv6 with 9 words ending with "::"' );
56
57 $this->assertFalse( IP::isIPv6( ':::' ) );
58 $this->assertFalse( IP::isIPv6( '::0:' ), 'IPv6 ending in a lone ":"' );
59
60 $this->assertTrue( IP::isIPv6( '::' ), 'IPv6 zero address' );
61 $this->assertTrue( IP::isIPv6( '::0' ) );
62 $this->assertTrue( IP::isIPv6( '::fc' ) );
63 $this->assertTrue( IP::isIPv6( '::fc:100' ) );
64 $this->assertTrue( IP::isIPv6( '::fc:100:a' ) );
65 $this->assertTrue( IP::isIPv6( '::fc:100:a:d' ) );
66 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1' ) );
67 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e' ) );
68 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e:ac' ) );
69
70 $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
71 $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
72
73 $this->assertFalse( IP::isIPv6( ':fc::100' ), 'IPv6 starting with lone ":"' );
74 $this->assertFalse( IP::isIPv6( 'fc::100:' ), 'IPv6 ending with lone ":"' );
75 $this->assertFalse( IP::isIPv6( 'fc:::100' ), 'IPv6 with ":::" in the middle' );
76
77 $this->assertTrue( IP::isIPv6( 'fc::100' ), 'IPv6 with "::" and 2 words' );
78 $this->assertTrue( IP::isIPv6( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
79 $this->assertTrue( IP::isIPv6( 'fc::100:a:d', 'IPv6 with "::" and 4 words' ) );
80 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
81 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e' ), 'IPv6 with "::" and 6 words' );
82 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
83 $this->assertTrue( IP::isIPv6( '2001::df'), 'IPv6 with "::" and 2 words' );
84 $this->assertTrue( IP::isIPv6( '2001:5c0:1400:a::df'), 'IPv6 with "::" and 5 words' );
85 $this->assertTrue( IP::isIPv6( '2001:5c0:1400:a::df:2'), 'IPv6 with "::" and 6 words' );
86
87 $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
88 $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
89
90 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac:0' ) );
91 }
92
93 /**
94 * @covers IP::isIPv4
95 */
96 public function testisIPv4() {
97 $this->assertFalse( IP::isIPv4( false ), 'Boolean false is not an IP' );
98 $this->assertFalse( IP::isIPv4( true ), 'Boolean true is not an IP' );
99 $this->assertFalse( IP::isIPv4( "" ), 'Empty string is not an IP' );
100 $this->assertFalse( IP::isIPv4( 'abc' ) );
101 $this->assertFalse( IP::isIPv4( ':' ) );
102 $this->assertFalse( IP::isIPv4( '124.24.52' ), 'IPv4 not enough quads' );
103 $this->assertFalse( IP::isIPv4( '24.324.52.13' ), 'IPv4 out of range' );
104 $this->assertFalse( IP::isIPv4( '.24.52.13' ), 'IPv4 starts with period' );
105
106 $this->assertTrue( IP::isIPv4( '124.24.52.13' ) );
107 $this->assertTrue( IP::isIPv4( '1.24.52.13' ) );
108 $this->assertTrue( IP::isIPv4( '74.24.52.13/20', 'IPv4 range' ) );
109 }
110
111 /**
112 * @covers IP::isValid
113 */
114 public function testValidIPs() {
115 foreach ( range( 0, 255 ) as $i ) {
116 $a = sprintf( "%03d", $i );
117 $b = sprintf( "%02d", $i );
118 $c = sprintf( "%01d", $i );
119 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
120 $ip = "$f.$f.$f.$f";
121 $this->assertTrue( IP::isValid( $ip ) , "$ip is a valid IPv4 address" );
122 }
123 }
124 foreach ( range( 0x0, 0xFFFF, 0xF ) as $i ) {
125 $a = sprintf( "%04x", $i );
126 $b = sprintf( "%03x", $i );
127 $c = sprintf( "%02x", $i );
128 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
129 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
130 $this->assertTrue( IP::isValid( $ip ) , "$ip is a valid IPv6 address" );
131 }
132 }
133 // test with some abbreviations
134 $this->assertFalse( IP::isValid( ':fc:100::' ), 'IPv6 starting with lone ":"' );
135 $this->assertFalse( IP::isValid( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
136 $this->assertFalse( IP::isValid( 'fc:300' ), 'IPv6 with only 2 words' );
137 $this->assertFalse( IP::isValid( 'fc:100:300' ), 'IPv6 with only 3 words' );
138
139 $this->assertTrue( IP::isValid( 'fc:100::' ) );
140 $this->assertTrue( IP::isValid( 'fc:100:a:d:1:e::' ) );
141 $this->assertTrue( IP::isValid( 'fc:100:a:d:1:e:ac::' ) );
142
143 $this->assertTrue( IP::isValid( 'fc::100' ), 'IPv6 with "::" and 2 words' );
144 $this->assertTrue( IP::isValid( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
145 $this->assertTrue( IP::isValid( '2001::df'), 'IPv6 with "::" and 2 words' );
146 $this->assertTrue( IP::isValid( '2001:5c0:1400:a::df'), 'IPv6 with "::" and 5 words' );
147 $this->assertTrue( IP::isValid( '2001:5c0:1400:a::df:2'), 'IPv6 with "::" and 6 words' );
148 $this->assertTrue( IP::isValid( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
149 $this->assertTrue( IP::isValid( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
150
151 $this->assertFalse( IP::isValid( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 with 8 words ending with "::"' );
152 $this->assertFalse( IP::isValid( 'fc:100:a:d:1:e:ac:0:1::' ), 'IPv6 with 9 words ending with "::"' );
153 }
154
155 /**
156 * @covers IP::isValid
157 */
158 public function testInvalidIPs() {
159 // Out of range...
160 foreach ( range( 256, 999 ) as $i ) {
161 $a = sprintf( "%03d", $i );
162 $b = sprintf( "%02d", $i );
163 $c = sprintf( "%01d", $i );
164 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
165 $ip = "$f.$f.$f.$f";
166 $this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv4 address" );
167 }
168 }
169 foreach ( range( 'g', 'z' ) as $i ) {
170 $a = sprintf( "%04s", $i );
171 $b = sprintf( "%03s", $i );
172 $c = sprintf( "%02s", $i );
173 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
174 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
175 $this->assertFalse( IP::isValid( $ip ) , "$ip is not a valid IPv6 address" );
176 }
177 }
178 // Have CIDR
179 $ipCIDRs = array(
180 '212.35.31.121/32',
181 '212.35.31.121/18',
182 '212.35.31.121/24',
183 '::ff:d:321:5/96',
184 'ff::d3:321:5/116',
185 'c:ff:12:1:ea:d:321:5/120',
186 );
187 foreach ( $ipCIDRs as $i ) {
188 $this->assertFalse( IP::isValid( $i ),
189 "$i is an invalid IP address because it is a block" );
190 }
191 // Incomplete/garbage
192 $invalid = array(
193 'www.xn--var-xla.net',
194 '216.17.184.G',
195 '216.17.184.1.',
196 '216.17.184',
197 '216.17.184.',
198 '256.17.184.1'
199 );
200 foreach ( $invalid as $i ) {
201 $this->assertFalse( IP::isValid( $i ), "$i is an invalid IP address" );
202 }
203 }
204
205 /**
206 * @covers IP::isValidBlock
207 */
208 public function testValidBlocks() {
209 $valid = array(
210 '116.17.184.5/32',
211 '0.17.184.5/30',
212 '16.17.184.1/24',
213 '30.242.52.14/1',
214 '10.232.52.13/8',
215 '30.242.52.14/0',
216 '::e:f:2001/96',
217 '::c:f:2001/128',
218 '::10:f:2001/70',
219 '::fe:f:2001/1',
220 '::6d:f:2001/8',
221 '::fe:f:2001/0',
222 );
223 foreach ( $valid as $i ) {
224 $this->assertTrue( IP::isValidBlock( $i ), "$i is a valid IP block" );
225 }
226 }
227
228 /**
229 * @covers IP::isValidBlock
230 */
231 public function testInvalidBlocks() {
232 $invalid = array(
233 '116.17.184.5/33',
234 '0.17.184.5/130',
235 '16.17.184.1/-1',
236 '10.232.52.13/*',
237 '7.232.52.13/ab',
238 '11.232.52.13/',
239 '::e:f:2001/129',
240 '::c:f:2001/228',
241 '::10:f:2001/-1',
242 '::6d:f:2001/*',
243 '::86:f:2001/ab',
244 '::23:f:2001/',
245 );
246 foreach ( $invalid as $i ) {
247 $this->assertFalse( IP::isValidBlock( $i ), "$i is not a valid IP block" );
248 }
249 }
250
251 /**
252 * Improve IP::sanitizeIP() code coverage
253 * @todo Most probably incomplete
254 */
255 public function testSanitizeIP() {
256 $this->assertNull( IP::sanitizeIP('') );
257 $this->assertNull( IP::sanitizeIP(' ') );
258 }
259
260 /**
261 * test wrapper around ip2long which might return -1 or false depending on PHP version
262 * @covers IP::toUnsigned
263 */
264 public function testip2longWrapper() {
265 // @todo FIXME: Add more tests ?
266 $this->assertEquals( pow(2,32) - 1, IP::toUnsigned( '255.255.255.255' ));
267 $i = 'IN.VA.LI.D';
268 $this->assertFalse( IP::toUnSigned( $i ) );
269 }
270
271 /**
272 * @covers IP::isPublic
273 */
274 public function testPrivateIPs() {
275 $private = array( 'fc00::3', 'fc00::ff', '::1', '10.0.0.1', '172.16.0.1', '192.168.0.1' );
276 foreach ( $private as $p ) {
277 $this->assertFalse( IP::isPublic( $p ), "$p is not a public IP address" );
278 }
279 $public = array( '2001:5c0:1000:a::133', 'fc::3', '00FC::' );
280 foreach ( $public as $p ) {
281 $this->assertTrue( IP::isPublic( $p ), "$p is a public IP address" );
282 }
283 }
284
285 // Private wrapper used to test CIDR Parsing.
286 private function assertFalseCIDR( $CIDR, $msg='' ) {
287 $ff = array( false, false );
288 $this->assertEquals( $ff, IP::parseCIDR( $CIDR ), $msg );
289 }
290
291 // Private wrapper to test network shifting using only dot notation
292 private function assertNet( $expected, $CIDR ) {
293 $parse = IP::parseCIDR( $CIDR );
294 $this->assertEquals( $expected, long2ip( $parse[0] ), "network shifting $CIDR" );
295 }
296
297 /**
298 * @covers IP::hexToQuad
299 */
300 public function testHexToQuad() {
301 $this->assertEquals( '0.0.0.1' , IP::hexToQuad( '00000001' ) );
302 $this->assertEquals( '255.0.0.0' , IP::hexToQuad( 'FF000000' ) );
303 $this->assertEquals( '255.255.255.255', IP::hexToQuad( 'FFFFFFFF' ) );
304 $this->assertEquals( '10.188.222.255' , IP::hexToQuad( '0ABCDEFF' ) );
305 // hex not left-padded...
306 $this->assertEquals( '0.0.0.0' , IP::hexToQuad( '0' ) );
307 $this->assertEquals( '0.0.0.1' , IP::hexToQuad( '1' ) );
308 $this->assertEquals( '0.0.0.255' , IP::hexToQuad( 'FF' ) );
309 $this->assertEquals( '0.0.255.0' , IP::hexToQuad( 'FF00' ) );
310 }
311
312 /**
313 * @covers IP::hexToOctet
314 */
315 public function testHexToOctet() {
316 $this->assertEquals( '0:0:0:0:0:0:0:1',
317 IP::hexToOctet( '00000000000000000000000000000001' ) );
318 $this->assertEquals( '0:0:0:0:0:0:FF:3',
319 IP::hexToOctet( '00000000000000000000000000FF0003' ) );
320 $this->assertEquals( '0:0:0:0:0:0:FF00:6',
321 IP::hexToOctet( '000000000000000000000000FF000006' ) );
322 $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF',
323 IP::hexToOctet( '000000000000000000000000FCCFFAFF' ) );
324 $this->assertEquals( 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
325 IP::hexToOctet( 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' ) );
326 // hex not left-padded...
327 $this->assertEquals( '0:0:0:0:0:0:0:0' , IP::hexToOctet( '0' ) );
328 $this->assertEquals( '0:0:0:0:0:0:0:1' , IP::hexToOctet( '1' ) );
329 $this->assertEquals( '0:0:0:0:0:0:0:FF' , IP::hexToOctet( 'FF' ) );
330 $this->assertEquals( '0:0:0:0:0:0:0:FFD0' , IP::hexToOctet( 'FFD0' ) );
331 $this->assertEquals( '0:0:0:0:0:0:FA00:0' , IP::hexToOctet( 'FA000000' ) );
332 $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF', IP::hexToOctet( 'FCCFFAFF' ) );
333 }
334
335 /*
336 * IP::parseCIDR() returns an array containing a signed IP address
337 * representing the network mask and the bit mask.
338 * @covers IP::parseCIDR
339 */
340 function testCIDRParsing() {
341 $this->assertFalseCIDR( '192.0.2.0' , "missing mask" );
342 $this->assertFalseCIDR( '192.0.2.0/', "missing bitmask" );
343
344 // Verify if statement
345 $this->assertFalseCIDR( '256.0.0.0/32', "invalid net" );
346 $this->assertFalseCIDR( '192.0.2.0/AA', "mask not numeric" );
347 $this->assertFalseCIDR( '192.0.2.0/-1', "mask < 0" );
348 $this->assertFalseCIDR( '192.0.2.0/33', "mask > 32" );
349
350 // Check internal logic
351 # 0 mask always result in array(0,0)
352 $this->assertEquals( array( 0, 0 ), IP::parseCIDR('192.0.0.2/0') );
353 $this->assertEquals( array( 0, 0 ), IP::parseCIDR('0.0.0.0/0') );
354 $this->assertEquals( array( 0, 0 ), IP::parseCIDR('255.255.255.255/0') );
355
356 // @todo FIXME: Add more tests.
357
358 # This part test network shifting
359 $this->assertNet( '192.0.0.0' , '192.0.0.2/24' );
360 $this->assertNet( '192.168.5.0', '192.168.5.13/24');
361 $this->assertNet( '10.0.0.160' , '10.0.0.161/28' );
362 $this->assertNet( '10.0.0.0' , '10.0.0.3/28' );
363 $this->assertNet( '10.0.0.0' , '10.0.0.3/30' );
364 $this->assertNet( '10.0.0.4' , '10.0.0.4/30' );
365 $this->assertNet( '172.17.32.0', '172.17.35.48/21' );
366 $this->assertNet( '10.128.0.0' , '10.135.0.0/9' );
367 $this->assertNet( '134.0.0.0' , '134.0.5.1/8' );
368 }
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 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 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 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 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 }